EA open trade on open price

ezraluandre

Trader
Jun 17, 2020
33
1
24
30
Hi I tired to open trade on the next bar by using the code from earnforex web. After using this code I use this code to open the trade,

MQL4:
slippage = 0;
if(logic == 1)
   {
        OpenCandle = iOpen(Symbol(),PERIOD_CURRENT,0);
        double openPriceBuy    = OpenCandle;
        double stoplossBuy     = OpenCandle - stoploss;
        double takeProfitBuy   = OpenCandle + takeprofit;
        double lotsizeBuy      = 0.01;
        int    ticket          = OrderSend(Symbol(),OP_BUY,lotSizeBuy,openPriceBuy,slippage,stoplossBuy,takeProfitBuy,"BUY",MagicNumber);
   }
 
if(logic == 2)
   {
        OpenCandle = iOpen(Symbol(),PERIOD_CURRENT,0);
        double openPriceSell    = OpenCandle;
        double stoplossSell     = OpenCandle - stoploss;
        double takeProfitSell   = OpenCandle + takeprofit;
        double lotsizeSell      = 0.01;
        int    ticket          = OrderSend(Symbol(),OP_BUY,lotSizeSell,openPriceSell,slippage,stoplossSell,takeProfitSell,"BUY",MagicNumber);
   }

But when I backtested my strategy I found that EA will open trade on next candle but on the close price or near close price (pic attached, logic where dot occur). Anyone can tell why EA open trade near close price instead of open price?
 

Attachments

  • Capture.PNG
    Capture.PNG
    41.9 KB · Views: 6
Last edited by a moderator:

ezraluandre

Trader
Jun 17, 2020
33
1
24
30
I attached the simple EA that I working on. This problem occurred not only using this strategy, even a simple moving average crossover has the same problem.
 

Attachments

  • Test Strategy.mq4
    3.9 KB · Views: 3
Last edited:

Enivid

Administrator
Staff member
Nov 30, 2008
18,532
1,355
144
Odesa
www.earnforex.com
I don't know. I've just run it on Daily and it took entries only at the beginning of a bar:
Screenshot 2022-06-27 155801.png

The only thing I changed is replacing the calls to custom indicators with random signals.
 

Attachments

  • Test Strategy(1).mq4
    4.6 KB · Views: 6

ezraluandre

Trader
Jun 17, 2020
33
1
24
30
It doesn't have a problem with the time of trades open. It has problem with the entry price of the open trades.

When new candle form the price of open, high, close and low would be the same. When we send OrderSend() of short, the bid price should be the open price of that candle, but in my screen capture the entry price of the order is close price or near it.

my question is does using IsNewCandle() function in https://www.earnforex.com/guides/mql4-on-new-bar/ doesn't make entry price of short order a open price of a candle?
 

Enivid

Administrator
Staff member
Nov 30, 2008
18,532
1,355
144
Odesa
www.earnforex.com
As you can see on the attached screenshot, it does enter exactly at the Open prices:
Screenshot 2022-06-28 121735.png

If it doesn't in your case, then you have to check what price Model you use in the backtest and the quality of the ticks that your platform generates to use for trading.