Download MetaTrader 5

EA clashing when using multiple pairs...

To add comments, please log in or register
Dominic Gilbert
1639
Dominic Gilbert  
First off, I know Raptor that you have raised this issue with me before, but I cannot remember where you wrote it, and, more importantly understand where I am going wrong?

As far as I am aware I was writing it correctly so that the EA attached to any corresponding Pair will be able to always work on that pair only. At the moment, I have noticed that GBPCAD and GBPUSD seem to not get on, meaning the GBPCAD stoploss thinks it's calculation is done by the GBPUSD pair... so when a pending order is triggered, the stop loss flickers from GBPCAD values to cable....

I use magicnumber==1234;

Any suggestions or areas I am being an idiot with, please feel free to highlight :(

//+-------------------------------------------------------------------------------------+
//| Order Buy Function                                                                  |
//+-------------------------------------------------------------------------------------+   

//Place a pending buystop if no orders exists.. pending or otherwise.
if(direction==0)
{ 
      double btp=buy_takeprofit_price;
      
      double Min_Lot = MarketInfo(Symbol(),MODE_MINLOT);

      double Lot_Step = MarketInfo(Symbol(),MODE_LOTSTEP);

      double BuyLotSize =(RiskedAmount/(pips_to_bsl/pips))/10;
      double Lots = NormalizeDouble(BuyLotSize,2);
      LotSize = MathFloor(Lots/Lot_Step)*Lot_Step;

  
      static double Stored_BuyPrice;

      if(OpenOrdersThisPair(Symbol())==0)
         {
         int BuyTicketOrder= OrderSend(Symbol(),OP_BUYSTOP,LotSize,buyPrice,3,BuyStopPrice,btp,NULL,MagicNumber,0,Green);  >>>>// I am not sure if its this part? There is no way for this first OrderSend to tell what pair it is?
         if(BuyTicketOrder == -1)Print("First Buy Order Last Error = ",GetLastError());
         } 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



      for(int b=OrdersTotal()-1; b>=0; b--)
         {
         if(!OrderSelect(b,SELECT_BY_POS,MODE_TRADES))continue;
           
         if(OrderType()==OP_BUYSTOP) 
            if(OrderMagicNumber()==MagicNumber)
               if(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==True)// 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());
            }
      }
} 
Simon Gniadkowski
Moderator
18056
Simon Gniadkowski  
DomGilberto:
First off, I know Raptor that you have raised this issue with me before, but I cannot remember where you wrote it, and, more importantly understand where I am going wrong?

As far as I am aware I was writing it correctly so that the EA attached to any corresponding Pair will be able to always work on that pair only. At the moment, I have noticed that GBPCAD and GBPUSD seem to not get on, meaning the GBPCAD stoploss thinks it's calculation is done by the GBPUSD pair... so when a pending order is triggered, the stop loss flickers from GBPCAD values to cable....

I use magicnumber==1234;

Any suggestions or areas I am being an idiot with, please feel free to highlight :(

Your issue is clear, but can be a little hard to see because of the way you use braces indenting and conditions . . . you do this:

      for(int b=OrdersTotal()-1; b>=0; b--)
         {
         if(!OrderSelect(b,SELECT_BY_POS,MODE_TRADES))continue;
           
         if(OrderType()==OP_BUYSTOP) 
            if(OrderMagicNumber()==MagicNumber)
               if(OrderSymbol()==Symbol())                                  // if the symbol matches do . . . 
                   if(OrderStopLoss()<iMA(NULL,60,MA_Period,0,1,0,0)-ATR)   //  . . . .  this
                        {
                        Stored_BuyPrice = OrderOpenPrice();                 // and this
                        DeleteOrder = OrderDelete(OrderTicket());           // and this
                        }

         if(OpenOrdersThisPair(Symbol())==0 && DeleteOrder==True)// If there are no open orders = place a new order.   //  this happens even if the symbol didn't match . . .
            {
            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());
            }
      }

I think what you meant was this . . .

     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());
               }
            }
         }
Dominic Gilbert
1639
Dominic Gilbert  
Ah ok - such a small little thing I missed there. Also, when the first OrderSend() function is called, am I doing something wrong there? I ask this because I am trying to think logically about whether or not it knows at that point what pair the ordersend should be sent to? That might be a stupid question, but I am not doing anything wrong with that part am I? The "BuyTicketOrder" send function part?

Thanks for spotting that bit out for me :)
Simon Gniadkowski
Moderator
18056
Simon Gniadkowski  
DomGilberto:
Ah ok - such a small little thing I missed there. Also, when the first OrderSend() function is called, am I doing something wrong there? I ask this because I am trying to think logically about whether or not it knows at that point what pair the ordersend should be sent to? That might be a stupid question, but I am not doing anything wrong with that part am I? The "BuyTicketOrder" send function part?

Thanks for spotting that bit out for me :)
Symbol() is the symbol of the chart that the EA is running on . . . it can't get it wrong, it can only be one symbol
Dominic Gilbert
1639
Dominic Gilbert  
//+----------------------------------------------------------------------------------------------------------------------------------------+  
//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);

//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.
   for(int b=OrdersTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==MagicNumber)
            if(OrderSymbol()==Symbol())
               if(OrderType()==OP_BUY)
               {
                 if(OrderStopLoss() > BuyStopPrice)break; 
                  if(OrderStopLoss() < BuyStopPrice)
                    bool BuyModify = OrderModify(OrderTicket(),OrderOpenPrice(),BuyStopPrice,OrderTakeProfit(),0,CLR_NONE);
                    if(BuyModify < 0)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.     
   for(int s=OrdersTotal()-1; s>=0; s--)
     {
      if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==MagicNumber)
            if(OrderSymbol()==Symbol())
               if(OrderType()==OP_SELL)
               {
                 if(OrderStopLoss() < SellStopPrice)break; 
                   if(OrderStopLoss() > SellStopPrice)
                    bool SellModify = OrderModify(OrderTicket(),OrderOpenPrice(),SellStopPrice,OrderTakeProfit(),0,CLR_NONE);
                    if(SellModify < 0)Print(" Sell Trailing Stop Failed: ", GetLastError());
               }   
     }

  }
Ok - I think this is what was causing the problem. I have justed updated the code on this part and it seems to have now stopped flicking on every tick from cable to GBPCAD prices... It's now stuck on one stop price until it will update itself on this hourly close... so I will confirm.

Does that seem ok to you? Also, because I have these partial order closes, I assume I probably need to double check that the braces are in the correct position relative to "OrderSymbol()==Symbol())"?
whroeder1
17074
whroeder1  
if(OpenOrdersThisPair(Symbol()) == 0
How many times do you want to call this? For every order (your code)? For every order pending order on the current chart (RaptorUK's code)? Or ONCE, AFTER you store and delete the pending?
Simon Gniadkowski
Moderator
18056
Simon Gniadkowski  
DomGilberto:
Ok - I think this is what was causing the problem. I have justed updated the code on this part and it seems to have now stopped flicking on every tick from cable to GBPCAD prices... It's now stuck on one stop price until it will update itself on this hourly close... so I will confirm.

Does that seem ok to you? Also, because I have these partial order closes, I assume I probably need to double check that the braces are in the correct position relative to "OrderSymbol()==Symbol())"?

It looks OK, yes, always check your braces, and make life easier for yourself wherever you can, if that means adding braces then do so, but try to be consistent with braces and indenting.

You don't need two loops, one will do . . .

//+----------------------------------------------------------------------------------------------------------------------------------------+  
//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());
                  }
               }   
     }

OrderModify() returns a bool, true or false, not an int . . . so SellModify will never be less than 0 . . . I typed the same thing yesterday over at ForexFactory

Dominic Gilbert
1639
Dominic Gilbert  

Every time the order has been deleted - which I am wanting the pending order to be deleted EVERY 1 hour bar close and then a new order opened with either the same parameters, OR if applicable, new stops, targets and lot sizing based upon "if(OrderStopLoss() < iMA(NULL, 60, MA_Period, 0, 1, 0, 0) - ATR) "

The only thing I need storing for the NEW pending order(after the delete), is the initial entry price on that particular set-up. (Stored_BuyPrice = OrderOpenPrice();)


It's funny because that first bit of code above is also in connection to another thread I was about to post. Rather than post a new topic with a similar question I'll ask here, if thats ok? Check out the link below - just a couple minute video explaining the issue I have and am finding it hard to resolve...

I've written so much code, but I am now starting to realise that it's a case of the inefficiencies I'm having to repair...

Video: http://screencast.com/t/4nl8AaH8Sag

If I have made this really ambiguous with only providing limited amount of code, then I'll stick some more up?

Dominic Gilbert
1639
Dominic Gilbert  
Thanks Raptor - Yea I just noticed that. I've spent some time cleaning up my code, as it was a mess. Amateur errors like trying to use Prints like the example you gave above lol.
Simon Gniadkowski
Moderator
18056
Simon Gniadkowski  
DomGilberto:

Every time the order has been deleted - which I am wanting the pending order to be deleted EVERY 1 hour bar close and then a new order opened with either the same parameters, OR if applicable, new stops, targets and lot sizing based upon "if(OrderStopLoss() < iMA(NULL, 60, MA_Period, 0, 1, 0, 0) - ATR) "

The only thing I need storing for the NEW pending order(after the delete), is the initial entry price on that particular set-up. (Stored_BuyPrice = OrderOpenPrice();)


It's funny because that first bit of code above is also in connection to another thread I was about to post. Rather than post a new topic with a similar question I'll ask here, if thats ok? Check out the link below - just a couple minute video explaining the issue I have and am finding it hard to resolve...

I've written so much code, but I am now starting to realise that it's a case of the inefficiencies I'm having to repair...

Video: http://screencast.com/t/4nl8AaH8Sag

If I have made this really ambiguous with only providing limited amount of code, then I'll stick some more up?

Correct me if I misunderstand . . . but for most of the bars in the video you didn't get a pullback to the 21 EMA
Dominic Gilbert
1639
Dominic Gilbert  
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?
To add comments, please log in or register