EA open position above the chart

 
Hi guys, I build a simple EA that open a position according to EMA crossover. But when I backtest it I found an odd open position above the candle like on the image. This kind open position happened more than once and I don't know why it behave like that. Here's my EMA crossover code.
int EMACrossing(int shift = 0)
  {
   int result = 0;
   double EMAFastCurr = iMA(Symbol(),PERIOD_CURRENT,9,0,MODE_EMA,PRICE_CLOSE,shift);
   double EMASlowCurr = iMA(Symbol(),PERIOD_CURRENT,21,0,MODE_EMA,PRICE_CLOSE,shift);
   double EMAFastPrev = iMA(Symbol(),PERIOD_CURRENT,9,0,MODE_EMA,PRICE_CLOSE,shift+1);
   double EMASlowPrev = iMA(Symbol(),PERIOD_CURRENT,21,0,MODE_EMA,PRICE_CLOSE,shift+1);
   if(EMAFastPrev<EMASlowPrev && EMAFastCurr>EMASlowPrev)
      result=1;
   if(EMAFastPrev>EMASlowPrev && EMAFastCurr<EMASlowCurr)
      result=2;
   return(result);
  }

and here's my open position code.

if(EMACrossing() == 1 && Volume[0]==1)
   {
   double stopLossPrice = Ask - trailingStop*Point();
   double lotSize = NormalizeDouble(OptimalLotSize(riskPerTrade,Ask,stopLossPrice)/lotType,2);
   orderId = OrderSend(Symbol(),OP_BUY,lotSize,Ask,0,stopLossPrice,0,NULL,magicNumber1);
   Print("Order ticket: ",OrderTicket());
   }
if(EMACrossing() == 2 && Volume[0]==1)
   {
   double stopLossPrice = Bid + trailingStop*Point();
   double lotSize = NormalizeDouble(OptimalLotSize(riskPerTrade,Bid,stopLossPrice)/lotType,2);
   orderId = OrderSend(Symbol(),OP_SELL,lotSize,Bid,0,stopLossPrice,0,NULL,magicNumber1);
   Print("Order ticket: ",OrderTicket());
   }

anyone knows how this is happened?

Testing trading strategies on real ticks
Testing trading strategies on real ticks
  • www.mql5.com
The article provides the results of testing a simple trading strategy in three modes: " 1 minute OHLC " using only Open, High, Low and Close prices of minute bars; detailed modeling in " Every tick " mode, as well as the most accurate " Every tick based on real ticks " mode applying actual historical data. Comparing the results allows us to...
Files:
Capture.JPG  55 kb
 
Luandre Ezra I found an odd open position above the candle like on the image.

You buy at the Ask and sell at the Bid.

  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using the Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

  2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger at a specific Bid price, add the average spread.
              MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

  3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
    Most brokers with variable spread widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134.

 
William Roeder:
  1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using the Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

Hi William, what do you mean by the specified amount? I used the Ask as the base line and add the stop loss which is 5 pips to trail it, is this not specified?

 
  1. Luandre Ezra  what do you mean by the specified amount? I used the Ask as the base line and add the stop loss which is 5 pips to trail it, is this not specified?

    You added 5 pips. Is that a specific amount?

  2. double stopLossPrice = Bid + trailingStop*Point();
    You did not add that to the Ask. Your effective SL is 5 pips minus the spread.

  3. if(EMACrossing() == 2 && Volume[0]==1)

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 2014.04.04

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.

 
You added 5 pips. Is that a specific amount?

I don't understand your meaning by specific amount? Is it specific with the meaning of is it enough amount of pips or specific like it should be like this specific price?


You did not add that to the Ask. Your effective SL is 5 pips minus the spread.

    Do you mean I should change the Bid into Ask? it's for the short position and this is the code before I realize I made that mistake. I already change the bid with the ask and minus it with the spread


    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 2014.04.04

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.

      Thank you for this info I don't know about this. If you can elaborate more about the Bars and Volume why both of it is unreliable? from my understanding MT4 would see the current bars is bars no.0, even if the MT4 is refresh or reconnect the current bars would still be the no.0 bars. And for the Volume I think this could be happened if the MT4 is refresh or reconnect due to MT doesn't realize how many ticks has happened since the first bars. 

       
      Luandre Ezra: I don't understand your meaning by specific amount?
      Luandre Ezra: Do you mean I should change 
      Luandre Ezra: can elaborate more about the Bars and Volume why both of it is unreliable?
      Him sad, Brain Broken - YouTube
      Reason: