EA clashing when using multiple pairs... - page 2

 
DomGilberto:
Sorry - Thats my fault for not explaining it properly. I look at D1, 4Hr and finally 1Hr for the MA's to be fanned apart (amongst a load of other filters) - Once they all lined up, on the H1 it will wait until any bar pulls back to the 21 EMA ONCE, and then from there it will place an order above the highs and a stop beneath the 60 EMA - ATR.

The pullback to the 21 EMA only happens once. From there, it will stop looking for any pullbacks to the 21 EMA. What it should be doing however, is making sure that the current open PENDING order is precisely up to date with the lots, stops and take profits AFTER every hour close. However, I cannot work out, why sometimes it works flawlessly, and updates the pending order after every H1 close, and other times it does not? The reason why the pending order will update itself is because of ""if(OrderStopLoss() < iMA(NULL, 60, MA_Period, 0, 1, 0, 0) - ATR) ".

If that is not true then it won't - HOWEVER, there are countless times when that is true, yet it will not delete the order and place a new one with the new values?

Not sure if you can see something in that code above relative to the video? (First code you corrected). Hope thats clearer?

I think you are going to have to do it the hard and laborious way . . . print the relevant variables, move through time in the Strategy Tester, tick by tick if needed, verifying what the variables are . . . then if you find that they should make something happen and they didn't you can focus in on that code.

It's relatively easy to spot simple syntax issues in someone else's code, it's harder and a lot more time consuming to spot errors in coding their strategy, for one thing, to be able to do that the strategy has to be understood as well, if not better, that the person doing the coding.

 
Yea you're right! I'll do exactly that now :) thanks Raptor!
 
One thing I do need to get clear in my head is; this code here, on the loop, how do I make sure that it is carrying out the OrderDelete() function and the loop is looping on every H1 close? Is that because I have this at the "int start" that depicts whether or not the loop is done on a H1 basis? (The "IsNewCandle()") ?

int start()
 {
   if(IsNewCandle())
      {
      CheckForMaTrade();//signals, deletions and candle trails only need checked on a new candle.
      }

   if(OpenOrdersThisPair(Symbol())>0)
      {
      if(UseMoveToBreakEven)MoveToBreakEven();//Move to b/e, normal trail and MA_trail need to trail on each tick
      if(Use_MA_Trail)MA_Trail();
      }
   
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  
if(OrderSelect(OrderTicket(),SELECT_BY_TICKET, MODE_TRADES)==True)
{  
            
      if(OpenOrdersThisPair(Symbol())>0 && OrderSymbol()==Symbol() && OrderType()==OP_SELL)
         {
         CloseHalfOrder1(); 
         }
   
      if(OpenOrdersThisPair(Symbol())>0 && OrderSymbol()==Symbol() && OrderType()==OP_BUY)
         {
         CloseHalfOrder(); 
         }   
     }
}
 for(int b=OrdersTotal()-1; b>=0; b--)
         {
         if(!OrderSelect(b, SELECT_BY_POS, MODE_TRADES)) continue;
           
         if( OrderType() == OP_BUYSTOP &&
            OrderMagicNumber() == MagicNumber &&
            OrderSymbol() == Symbol() )        
            {
            if(OrderStopLoss() < iMA(NULL, 60, MA_Period, 0, 1, 0, 0) - ATR)  
               {
               Stored_BuyPrice = OrderOpenPrice();   
               DeleteOrder = OrderDelete(OrderTicket()); 
               }

            if(OpenOrdersThisPair(Symbol()) == 0 && DeleteOrder)  // If there are no open orders = place a new order. 
               {
               int NewBuyOrder = OrderSend(Symbol(), OP_BUYSTOP, LotSize, Stored_BuyPrice, 3, BuyStopPrice, btp, NULL, MagicNumber, 0, Green);
               if(NewBuyOrder == -1) Print("New Buy Order Last Error = ", GetLastError());
               }
            }
         }
bool IsNewCandle()
   {
   static datetime  Bar1Time;
   if(Bar1Time == Time[1]) //"Time" used to be "Bars" with no brackets 
      return(false);
      
   Bar1Time = Time[1]; //"Time" used to be "Bars" with no brackets 
   return(true);
   }
 
DomGilberto:
One thing I do need to get clear in my head is; this code here, on the loop, how do I make sure that it is carrying out the OrderDelete() function and the loop is looping on every H1 close? Is that because I have this at the "int start" that depicts whether or not the loop is done on a H1 basis? (The "IsNewCandle()") ?


CheckForMATrade() is called once per new bar, if you are running on a H1 chart that will be once at the start of the new H1 bar . . . the rest is executed every tick.

Are you calculating the correct ATR value, for the start of the new bar ? bearing in mind you just started Bar 0 do you really want to be getting the MA based on Bar 0 ? indicators aren't my thing so don't assume i'm saying you are wrong, I'm just asking

 
Yea I've just done a print to double check the MA-ATR math. It is correct. I am doing it on Bar 1 (The most recently closed H1 bar). The math is correct, I just don't think the calculations are consistently being done on EVERY H1 Bar close...
 
DomGilberto:
Yea I've just done a print to double check the MA-ATR math. It is correct. I am doing it on Bar 1 (The most recently closed H1 bar). The math is correct, I just don't think the calculations are consistently being done on EVERY H1 Bar close...

The MA is not on bar 1 though . . .

iMA(NULL, 60, MA_Period, 0, 1, 0, 0   ) - ATR)  
 
Good spot - thanks :) I think I know why it's not deleting the order... IF the OrderStopLoss is NOT > than the MA-ATR, then it will do nothing... So it makes sense... Just sometimes I look at it, and its as if it doesn't want to delete the order and place a new one when it should?

One last question if you wouldn't mind - I am still getting this poxy Order Modify error 1 ! It turns out it's coming from here when I print it out? Any ideas?

//+----------------------------------------------------------------------------------------------------------------------------------------+  
//Moving Average Trailing Stop Function
//+----------------------------------------------------------------------------------------------------------------------------------------+   
void MA_Trail()

{

   double ATR = iATR(NULL,60,14,1);
   double MA = iMA(NULL,60,MA_Period,0,1,0,1);
   
   double BuyStopPriceMath = MA - ATR;
   double SellStopPriceMath = MA + ATR;
   
   double BuyStopPrice = NormalizeDouble(BuyStopPriceMath,5);
   double SellStopPrice = NormalizeDouble(SellStopPriceMath,5);

   for(int b=OrdersTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==MagicNumber)
            if(OrderSymbol()==Symbol())
               {
               //buy order section - This is where the stop will trail based 
               // upon a fixed point value stated on the EA. It will trail with price.
               if(OrderType()==OP_BUY)
                  {
                  if(OrderStopLoss() > BuyStopPrice) break; 
                  if(OrderStopLoss() < BuyStopPrice)
                     bool BuyModify = OrderModify(OrderTicket(),OrderOpenPrice(),BuyStopPrice,OrderTakeProfit(),0,CLR_NONE);
                   if(!BuyModify)Print(" Buy Trailing Stop Failed: ", GetLastError());
                   }     

               // sell order section - This is where the stop will trail based 
               // upon a fixed point value stated on the EA. It will trail with price.     
               if(OrderType()==OP_SELL)
                  {
                  if(OrderStopLoss() < SellStopPrice) break; 
                  if(OrderStopLoss() > SellStopPrice)
                     bool SellModify = OrderModify(OrderTicket(),OrderOpenPrice(),SellStopPrice,OrderTakeProfit(),0,CLR_NONE);
                  if(!SellModify)Print(" Sell Trailing Stop Failed: ", GetLastError());
                  }
               }   
     }
}
 
DomGilberto:
Good spot - thanks :) I think I know why it's not deleting the order... IF the OrderStopLoss is NOT > than the MA-ATR, then it will do nothing... So it makes sense... Just sometimes I look at it, and its as if it doesn't want to delete the order and place a new one when it should?

One last question if you wouldn't mind - I am still getting this poxy Order Modify error 1 ! It turns out it's coming from here when I print it out? Any ideas?

This is the issue . . . (similar for the OP_SELL)

if( OrderStopLoss() < BuyStopPrice )

. . . and when you get an error 1 it is because OrderStopLoss() == BuyStopPrice so you are modifying the order to have the same StopLoss value, hence error 1 so now you are wondering how on one hand OrderStopLoss() == BuyStopPrice and on the other hand OrderStopLoss() < BuyStopPrice

Did you follow the link in the post and read it till your eyes bled ? I bet you didn't . . . https://www.mql5.com/en/forum/146380 if you keep ignoring this issue it will keep catching you out and you will keep wasting time . . . get to grips with it, understand it, don't suffer from it any longer, be happy

 

Ok I'll read through all that again - going back to the issue with the video attached, how I think I have resolved it is below, HOWEVER, it does not work on the Buy side? For some reason, no trades will be triggered at all. However on the sell side, it is working EXACTLY how I want it to?

Everything is EXACTLY reversed for the Buy side. All those if statements are just moving averages. The specific line I have changed is "triggerBarTime+H1_high >= ema21". On the Buy side it would look like = "triggerBarTime+H1_low <= ema21" - If I take out "triggerBarTime" then it will work on both buy and sell orders. However, if I leave in "triggerBarTime" it will work perfectly on the Sell Side (as in, close the orders on EVERY bar, recalculate it perfectly and place a new order with adjusted parameters!?) BUT not place ANY orders are attempt to do anything on the Buy side? This is a really important tweak I need to make...

Check out this video: http://screencast.com/t/uJu3nlNxAXDF - Hopefully this should really explain it well!! :D


No worries if you cannot help :)

     // Check for Moving Averages Fanned up ON THE DAILY TIME FRAME, creating an UP bias.   
    if(D1_Bias=="None") 
      if(Daily_3<Daily_5)
         if(Daily_5<Daily_8)
            if(Daily_8<Daily_10)
               if(Daily_10<Daily_12)
                  if(Daily_12<Daily_15)
                     if(Daily_15<Daily_30)
                        if(Daily_30<Daily_35)
                           if(Daily_35<Daily_40)
                              if(Daily_40<Daily_45)
                                 if(Daily_45<Daily_50)
                                    if(Daily_50<Daily_60)
                                       {
                                       D1_Bar=Time[1];
                                       D1_Bias="Daily is Down";
                                       Comment("Bias is: "+D1_Bias+" since: "+TimeToStr(D1_Bar,TIME_DATE|TIME_MINUTES));
                                       }
   
   // Check for Moving Averages Fanned up ON THE 4 HOUR TIME FRAME, creating an UP bias.  
    if(H4_Bias=="None") 
      if(Hour4_3<Hour4_5)
         if(Hour4_5<Hour4_8)
            if(Hour4_8<Hour4_10)
               if(Hour4_10<Hour4_12)
                  if(Hour4_12<Hour4_15)
                     if(Hour4_15<Hour4_30)
                        if(Hour4_30<Hour4_35)
                           if(Hour4_35<Hour4_40)
                              if(Hour4_40<Hour4_45)
                                 if(Hour4_45<Hour4_50)
                                    if(Hour4_50<Hour4_60)
                                       {
                                       H4_Bar=Time[1];
                                       H4_Bias="4 Hour is Down";
                                       Comment("Bias is: "+H4_Bias+" since: "+TimeToStr(H4_Bar,TIME_DATE|TIME_MINUTES));
                                       }
   
   // Check for Moving Averages Fanned down creating an DOWN bias.  
   if(D1_Bias=="Daily is Down" && H4_Bias=="4 Hour is Down" && H1_Bias=="None")
      if(CurrentSmallFish1<CurrentSmallFish2)
         if(CurrentSmallFish2<CurrentSmallFish3)
            if(CurrentSmallFish3<CurrentSmallFish4)
               if(CurrentSmallFish4<CurrentSmallFish5)
                  if(CurrentSmallFish5<CurrentSmallFish6)
                     if(CurrentSmallFish6<CurrentBigFish1)
                        if(CurrentBigFish1<CurrentBigFish2)
                           if(CurrentBigFish2<CurrentBigFish3)
                              if(CurrentBigFish3<CurrentBigFish4)
                                 if(CurrentBigFish4<CurrentBigFish5)
                                    if(CurrentBigFish5<CurrentBigFish6)
                                       {
                                       triggerBarTime=Time[1];
                                       H1_Bias="Down";
                                       Comment("Bias is: "+H1_Bias+" since: "+TimeToStr(triggerBarTime,TIME_DATE|TIME_MINUTES));
                                       H4_Bias="4 Hour is Down";
                                       Comment("Bias is: "+H4_Bias+" since: "+TimeToStr(H4_Bar,TIME_DATE|TIME_MINUTES));
                                       D1_Bias="Daily is Down";
                                       Comment("Bias is: "+D1_Bias+" since: "+TimeToStr(D1_Bar,TIME_DATE|TIME_MINUTES));
                                       }
   
   ///////////////////////////////////////////////////////////////////////////////////////
   
   H1_high  = iHigh(NULL, PERIOD_H1, 1);
   H1_close = iClose(NULL, PERIOD_H1, 1);
   if(H1_Bias=="Down" && H4_Bias=="4 Hour is Down" && D1_Bias=="Daily is Down" && triggerBarTime+H1_high >= ema21 && H1_close < CurrentBigFish6)
      {
      OrderEntry(1); // Sell function is called.
      }
 
Any ideas?
Reason: