ea buys and sellls at price levels where price never was

 

Hi everyone!

I wasted the last couple of hours to find an error in my code. See the pic below:

Suddenly, in the strategy tester, it buys and sells where it shouldnt. I mean its the right bar, but at a price level, price never saw.  I took a look into the logs for some errors, but it says that the price level it opened a position at was the actual ask/bid price. Even when price never got there, like in the pic. THere is just one error in the log, when it fails to set proper sl/tp because of this behavior. At this point there is just an open position, which will be closed before the end of test. Take a look on the pics margin area in the end and you know what i mean.  

I cant get behind it, any ideas?


 
Pipser:

Hi everyone!

I wasted the last couple of hours to find an error in my code. See the pic below:

Suddenly, in the strategy tester, it buys and sells where it shouldnt. I mean its the right bar, but at a price level, price never saw.  I took a look into the logs for some errors, but it says that the price level it opened a position at was the actual ask/bid price. Even when price never got there, like in the pic. THere is just one error in the log, when it fails to set proper sl/tp because of this behavior. At this point there is just an open position, which will be closed before the end of test. Take a look on the pics margin area in the end and you know what i mean.  

I cant get behind it, any ideas?


Chart shows you Bid price. When you buy something, it's at Ask Price, which is always greater than bid price. Sell is at bid price as in chart.
 

Thanks for the reply angevoyageur!

So you say he is buying at bid, where he should buy at ask. But if buy conditions in this ea are met he buys at ask i think.


 if(Cexpert.checkBuy()==true)
     {
      //--- do we already have an opened buy position
      if(Buy_opened || Sell_opened)
        {
      //   Print("We already have a Buy Position!!!");

         return;    // Don't open a new Buy Position
        }
      double aprice = NormalizeDouble(last_tick.ask,_Digits);              // current Ask price
      double stl    = NormalizeDouble(last_tick.ask - STP*_Point,_Digits); // Stop Loss
      double tkp    = NormalizeDouble(last_tick.ask + TKP*_Point,_Digits); // Take profit
      int    mdev   = 100;                                                    // Maximum deviation
                         Print("buy   ",last_tick.ask);                                                     // place order
      Cexpert.openBuy(ORDER_TYPE_BUY,aprice,stl,tkp,mdev);
     }
     

He only checks for buy conditions at open of current bar and buys at last_tick.ask

Sometimes he buys/sells 4.5 pips from price away and sometimes even 7 pips, because of this i thought i would be no spread or ask/bid problem. Or something went terribly wrong :)

 
Pipser:

Thanks for the reply angevoyageur!

So you say he is buying at bid, where he should buy at ask. But if buy conditions in this ea are met he buys at ask i think.


He only checks for buy conditions at open of current bar and buys at last_tick.ask

Sometimes he buys/sells 4.5 pips from price away and sometimes even 7 pips, because of this i thought i would be no spread or ask/bid problem. Or something went terribly wrong :)

No, a buy is always at ask price. Can you show the code of Cexpert.openBuy ?

You can print ask/bid price to check.

 

sure. here you go


void ExpertA::openBuy(ENUM_ORDER_TYPE otype,double askprice,double SL, double TP, int dev,string comment="")
  {


   if(Chk_Margin==1)
     {
      if(MarginOK()==false)
        {
         Errormsg= "You do not have enough money to open this Position!!!";
         Errcode =GetLastError();
         showError(Errormsg,Errcode);
        }
      else
        {
         trequest.action=TRADE_ACTION_DEAL;
         trequest.type=otype;
         trequest.volume=LOTS;
         trequest.price=askprice;
         trequest.sl=0;
         trequest.tp=0;
         trequest.deviation=dev;
         trequest.magic=Magic_No;
         trequest.symbol=symbol;
         trequest.type_filling=ORDER_FILLING_FOK;
         //--- send
         OrderSend(trequest,tresult);
         //--- check result
         if(tresult.retcode==10009 || tresult.retcode==10008) //Request successfully completed 
           {
            Alert("A Buy order has been successfully placed with Ticket#:",tresult.order,"!!");
           }
         else
           {
            Errormsg= "The Buy order request could not be completed";
            Errcode =GetLastError();
            showError(Errormsg,Errcode);
           }
        }
     }
   else
     {
  //     Alert("test");
      trequest.action=TRADE_ACTION_DEAL;
      trequest.type=otype;
      trequest.volume=LOTS;

//Alert("volume",trequest.volume);
      trequest.price=askprice;
      trequest.sl=0;
      trequest.tp=0;
      trequest.deviation=dev;
      trequest.magic=Magic_No;
      trequest.symbol=symbol;
      trequest.type_filling=ORDER_FILLING_FOK;
      //--- send
      OrderSend(trequest,tresult);
      //--- check result
      if(tresult.retcode==10009 || tresult.retcode==10008) //Request successfully completed 
        {
         Alert("A Buy order has been successfully placed with Ticket#:",tresult.order,"!!");
        }
      else
        {
         Errormsg= "The Buy order request could not be completed";
         Errcode =GetLastError();
         showError(Errormsg,Errcode);
        }

  Sleep(10);     

  trequest.action = TRADE_ACTION_SLTP;
  trequest.symbol = _Symbol;

    trequest.sl = SL; //NormalizeDouble( trequest.price + Stop*_Point,_Digits); // Stop Loss
    trequest.tp = TP; //NormalizeDouble( trequest.price + Take*_Point,_Digits); // Stop Loss
  trequest.deviation = Slippage;
               
  OrderSend(trequest, tresult);          
        if(tresult.retcode==10009 || tresult.retcode==10008) //Request successfully completed 
        {
         Alert("A Buy order has been successfully placed with Ticket#:",tresult.order,"!!");
        }
      else
        {
         Errormsg= "The Buy order request could not be completed";
         Errcode =GetLastError();
         showError(Errormsg,Errcode);
        }      
  // }   
        
     }
  }
 
Pipser:

sure. here you go


All seems correct, although a little weird (why are repeating the same code twice).

Anyway why are you thinking the difference isn't from ask/bid ? You can check the spread on chart by displaying Data Window (Ctrl+D) and moving your mouse on the chart at the date of interest. Obviously you can also check it by code.

 

maybe i got it all wrong. But i show you another pic



lets take a look on the first signal. the big vertical arrow shows the, when the position is opened. At the beginning of the bar at the price level of the small horizontal bar. As you said, he buys at ask which is slightly higher then the signal. Everything ok here.

Problem ist the secon signal he buys at the beginning of the big vertical arrow bar, but not with a little bit higher ask level. he opens 13 pips above signal. that cant be the ask. Or?

edit: if i print ask, it tells me this is the ask price. is that possible?

 
Pipser:

lets take a look on the first signal. the big vertical arrow shows the, when the position is opened. At the beginning of the bar at the price level of the small horizontal bar. As you said, he buys at ask which is slightly higher then the signal. Everything ok here.

Problem ist the secon signal he buys at the beginning of the big vertical arrow bar, but not with a little bit higher ask level. he opens 13 pips above signal. that cant be the ask. Or?

You have been given good advice,  take it and you will know the answer instead of asking other people to guess . . . .

angevoyageur:


You can print ask/bid price to check.

 When your EA places a trade Print(),  Bid, Ask, OpenPrice of the trade and anything else that will help you,  make sure to use DoubleToStr(value, _Digits)   to get the correct number of decimals for the symbol you are using.

 
You are right, Ctrl+D was new for me. Seems there was such a high spread, although i thought my broker would limit it. Seems i need to find a way to handle my sl/tp different, if spread exceeds them and i got a 4756 for invalid stops. Thx for the help, sometimes everything seems to be hard, when you dont know how.
 
Pipser:

maybe i got it all wrong. But i show you another pic



lets take a look on the first signal. the big vertical arrow shows the, when the position is opened. At the beginning of the bar at the price level of the small horizontal bar. As you said, he buys at ask which is slightly higher then the signal. Everything ok here.

Problem ist the secon signal he buys at the beginning of the big vertical arrow bar, but not with a little bit higher ask level. he opens 13 pips above signal. that cant be the ask. Or?

edit: if i print ask, it tells me this is the ask price. is that possible?

This is the ask, a buy trade CAN'T be opened at other price. But are you sure it opens a trade at the BEGINNING of the bar ?
Reason: