Download MetaTrader 5

Spread current causes a ordermodify 138 error in strategy tester (build509)

To add comments, please log in or register
The entire MQL5.community history on a single page!
Subgenius
582
Subgenius 2013.12.31 00:09 

I've tested my EA on multiple currencies with spread current and all I can see is that I cannot use Spread Current in tester with the EA that I wrote. No errors occur if using a spread of 2 or 5, etc. I haven't seen this kind of problem come up on the web (or on google) so I'm worried my coding isn't somehow normal unless its just MT4 data.

What I'm hoping for is that anyone may offer Any suggestions for making it work with spread current, or in general work better.

Here's where I'm getting the error...

//+------------------------------------------------------------------+
//OpenOrder(BUY);
void OpenOrder(int CMD)
   {
   //point and spread
   _digits=MarketInfo(_symbol,MODE_DIGITS);
   if(_digits==3){SymPt=0.01; SymSprd=(Ask-Bid)*100;}
   if(_digits==5){SymPt=0.0001; SymSprd=(Ask-Bid)*10000;}
   //place orders with error check
   bool OpenOrderLoop=False;
   int OpenTryCount=0;
   for(int OpenCNT=0; OpenCNT<OrderQuantity; OpenCNT++)
      {
      while(!OpenOrderLoop)
         {
         //trading busy or spread too high
         while(IsTradeContextBusy() || SymSprd>MaximumSpread)
            {
            if(SymSprd>MaximumSpread)
               {
               if(_debug){Print("OpenOrder: Spread is "+SymSprd+", and the Max-Spread allowed is "+MaximumSpread+". Trying Again..");}
               }
            if(_debug){Print("OpenOrder: Trade Context Busy, Trying Again..");}
            Sleep(10);
            RefreshRates();
            }
         //place stp order
         int _ticket=-1;
         double newSL,newTP,_price;
         if(CMD==BUY)
            {
            newSL=Bid-StopLoss*SymPt; newTP=Bid+TakeProfit*SymPt; _price=Bid;
            if(_ecn){_ticket=OrderSend(_symbol,Cmd(CMD),Lot(),_price,_slippage,0.0,0.0,_comment,newMagic,0,_buyarrow);}
            else{_ticket=OrderSend(_symbol,Cmd(CMD),Lot(),_price,_slippage,newSL,newTP,_comment,newMagic,0,_buyarrow);}
            }
         if(CMD==SELL)
            {
            newSL=Ask+StopLoss*SymPt; newTP=Ask-TakeProfit*SymPt; _price=Ask;
            if(_ecn){_ticket=OrderSend(_symbol,Cmd(CMD),Lot(),_price,_slippage,0,0,_comment,newMagic,0,_sellarrow);}
            else{_ticket=OrderSend(_symbol,Cmd(CMD),Lot(),_price,_slippage,newSL,newTP,_comment,newMagic,0,_sellarrow);}
            }
         //error check and place ecn order
         _err=GetLastError(); if(!_ecn && _err==0){if(AudibleAlerts){PlaySound("alert.wav");} OpenOrderLoop=true;}       
         //--ecn modify
         if(_ecn && TakeProfit>0 && StopLoss>0 && _err==0)
            {
            if(OrderSelect(_ticket,SELECT_BY_TICKET))
               {
               if(CMD==BUY){OrderModify(_ticket,OrderOpenPrice(),newSL,newTP,0,_buyarrow);}
               if(CMD==SELL){OrderModify(_ticket,OrderOpenPrice(),newSL,newTP,0,_sellarrow);}
               if(AudibleAlerts){PlaySound("alert.wav");}
               OpenOrderLoop=true;
               }
            }
         //--no more retries, abort.
         if(_err>0)
            {
            Print("Open Error: " + ErrorDescription(_err) + ", Trying Again..");
            if(OpenTryCount>=_retries)
               {
               if(_debug){Print("Open Error: " + ErrorDescription(_err) + ". Retries exceeded, Could Not Open Order.");}
               OpenOrderLoop=true;
               }
            OpenTryCount++;          
            }
         }
      }
   }

Attached

Files:
Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2013.12.31 00:26  
Subgenius:

I've tested my EA on multiple currencies with spread current and all I can see is that I cannot use Spread Current in tester with the EA that I wrote. No errors occur if using a spread of 2 or 5, etc. I haven't seen this kind of problem come up on the web (or on google) so I'm worried my coding isn't somehow normal unless its just MT4 data.

What I'm hoping for is that anyone may offer Any suggestions for making it work with spread current, or in general work better.

Here's where I'm getting the error...

Attached

I'm pretty sure it has nothing to do with Spread . . . you Buy at Ask and Sell at Bid . . . not this . . .

if(CMD==BUY)
            {
            newSL=Bid-StopLoss*SymPt; newTP=Bid+TakeProfit*SymPt;   _price = Bid;
            if(_ecn){_ticket=OrderSend(_symbol,Cmd(CMD),Lot(),_price,_slippage,0.0,0.0,_comment,newMagic,0,_buyarrow);}
            else{_ticket=OrderSend(_symbol,Cmd(CMD),Lot(),_price,_slippage,newSL,newTP,_comment,newMagic,0,_buyarrow);}
            }
if(CMD==SELL)
            {
            newSL=Ask+StopLoss*SymPt; newTP=Ask-TakeProfit*SymPt;   _price = Ask;
            if(_ecn){_ticket=OrderSend(_symbol,Cmd(CMD),Lot(),_price,_slippage,0,0,_comment,newMagic,0,_sellarrow);}
            else{_ticket=OrderSend(_symbol,Cmd(CMD),Lot(),_price,_slippage,newSL,newTP,_comment,newMagic,0,_sellarrow);}

. . . also bear in mind that Sleep() does nothing in the Strategy tester, neither does RefreshRates()

You are getting error 138 because . . . "bid and ask prices have been mixed up."

Subgenius
582
Subgenius 2013.12.31 00:32  
RaptorUK:

I'm pretty sure it has nothing to do with Spread . . . you Buy at Ask and Sell at Bid . . . not this . . .

. . . also bear in mind that Sleep() does nothing in the Strategy tester, neither does RefreshRates()

You are getting error 138 because . . . "bid and ask prices have been mixed up."


So cool!! my mind is just warped

To add comments, please log in or register