1
votes

Trying to add a StopLoss to my open market positions which also takes into account my brokers stoplevel. I have set this to add a breakeven stop loss when my trade gets to 100 points in profit.

This is the code - but its completely ignoring any order modification during my back testing.

OK this is now what I have so far, one for loop for the buy, one for the sell. I;ve also declared the "BuyMod" & "SellMod" to true as was suggested in pervious answers, as well as Normalzing prices in the OrderModify signature.

 /*Breakeven Order Modification*/
bool BuyMod               =  true;
bool SellMod              =  true;
                    for(int b = OrdersTotal()-1;b>=0;b--)
                    {
                    if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
                       {
                       double   aBidPrice   =  MarketInfo(Symbol(),MODE_BID);
                       double   anOpenPrice =  OrderOpenPrice();
                       double   aNewTpPrice =  OrderTakeProfit();
                       double   aCurrentSL  =  OrderStopLoss();
                       double   aNewSLPrice =  anOpenPrice;
                       double   pnlPoints   =  (aBidPrice - anOpenPrice)/_Point;
                       double   stopPoints  =  (aBidPrice - aNewSLPrice)/_Point;
                       int      stopLevel   =  int(MarketInfo(Symbol(),MODE_STOPLEVEL));
                       int      aTicket     =  OrderTicket();
                       if(OrderType() == OP_BUY)
                       if(stopPoints >= stopLevel)
                       if(aTicket > 0)
                       if(pnlPoints >= breakeven)
                       if(aNewSLPrice != aCurrentSL)
                          {
                          BuyMod = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(aNewSLPrice,Digits),NormalizeDouble(aNewTpPrice,Digits),0,buycolor);
                          SendMail("Notification of Order Modification for Ticket#"+IntegerToString(OrderTicket(),10),"Good news! Order Ticket#"+IntegerToString(OrderTicket(),10)+"has been changed to breakeven");
                          }
                       }
                    }
                    for(int s = OrdersTotal()-1; s>=0; s--)
                    {
                    if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
                       {
                       double   anAskPrice  =  MarketInfo(Symbol(),MODE_ASK);
                       double   anOpenPrice =  OrderOpenPrice();
                       double   aNewTpPrice =  OrderTakeProfit();
                       double   aCurrentSL  =  OrderStopLoss();
                       double   aNewSLPrice =  anOpenPrice;
                       double   pnlPoints   =  (anOpenPrice - anAskPrice)/_Point;
                       double   stopPoints  =  (aNewSLPrice - anAskPrice)/_Point;
                       int      stopLevel   =  int(MarketInfo(Symbol(),MODE_STOPLEVEL));
                       int      aTicket     =  OrderTicket();
                       if(OrderType()== OP_SELL)
                       if(stopPoints >= stopLevel)
                       if(pnlPoints >= breakeven)
                       if(aNewSLPrice != aCurrentSL)
                       if(aTicket > 0)
                          {
                          SellMod = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(aNewSLPrice,Digits),NormalizeDouble(aNewTpPrice,Digits),0,sellcolor);
                          SendMail("Notification of Order Modification for Ticket#"+IntegerToString(OrderTicket(),10),"Good news! Order Ticket#"+IntegerToString(OrderTicket(),10)+"has been changed to breakeven");
                          }
                       }
                    }
1
You might want to know, StackOverflow does not promote duplicate questions. ( see the original >>> stackoverflow.com/q/36677588/3666197 )user3666197
Well I'm new to this website, I'm still learning my way around it & if it allowed me to delete the original question, then there wouldn't be a duplicate question. I wanted to better rephrase the question but it wouldn't let me delete the original one. Then I realised I could update the original al question, so I did.user6216142

1 Answers

0
votes

This code will not work due to several reasons:

First:
The function-call signature of OrderModify() is wrong.

You might want to know, that OrderModify() call-signature requires as per MQL4 documentation to include also an OrderExpiration value in the call, even though the Order is currently not a pending order any more.

Check the documentation and the IDE tooltip, which helps one remind the order of the function call parameters.

Second:
The other, less-visible reasons could be reported by GetLastError() after the OrderModify() function call returns a False as an indication of failure.