StopLoss Min Distance

 

Ocassionally I need to move the stoploss as close as possible to a trade (open, limit or stop). I cannot seem to find the correct procedure to do this. For example with the pair GBPUSD I have tried using MarketInfo(Symbol(), (MODE_STOPLEVEL)) gives 3, MarketInfo(Symbol(), (MODE_SPREAD)) gives 4, MarketInfo(Symbol(), (MODE_FREEZE)) (notexactly sure what this does)etc for example with code as below for a Buy Trade:

if (TradeBusy() < 0)
{
Print(Symbol(), " Trade is still busy when attempting to open BuyStop trade");
return(0);
}
Result = OrderModify(MyTicket, OrderOpenPrice(), MarketInfo(Symbol(), MODE_xxxxxx * Point, 0, 0, Blue);
if (Result < 1)
{
Print("Error ", GetLastError(), " While Modifying Buy SL On ", Symbol());
}
TradeNotBusy();

All result in an error 130.

So I then tried using:

if (TradeBusy() < 0)
{
Print(Symbol(), " Trade is still busy when attempting to open BuyStop trade");
return(0);
}
Result = OrderModify(MyTicket, OrderOpenPrice(), OrderOpenPrice() - xx * Point, 0, 0, Blue);
if (Result < 1)
{
Print("Error ", GetLastError(), " While Modifying Buy SL On ", Symbol());
}
TradeNotBusy();

With the value of xx below 19 I still get the error 130 on GBPUSD but for 19 and above the OrderModify works fine (Seems a bit high a value for a 4 pip spread).

HOW DO YOU CODE TO GET THE MINIMUM DEVIATION FOR A STOPLOSS ON ANY GIVEN CURRENCY PAIR?

 

below will answer ALL questions... get that pencil + paper out :O)

.

REF.1: "Order Characteristics and Rules for Making Trades" https://book.mql4.com/trading/orders
REF.2: "Requirements and Limitations in Making Trades" https://book.mql4.com/appendix/limits

 

BigAl wrote >>

Result = OrderModify(MyTicket, OrderOpenPrice(), MarketInfo(Symbol(), MODE_xxxxxx * Point, 0, 0, Blue);

Isn't this going to calculate the stop-loss price as something like 3 * 0.0001 = 0.0003?

Result = OrderModify(MyTicket, OrderOpenPrice(), OrderOpenPrice() - xx * Point, 0, 0, Blue);

With the value of xx below 19 I still get the error 130

I can't explain the whole of the 19, but isn't some of it the fact that you're calculating the stop-loss based on the entry price? If you go long, you've bought at the ask. Your stop-loss is effectively a sell order, and gets taken out at the bid. It ought to be the case that the closest stop you can immediately place is therefore the bid minus MODE_STOPLEVEL, not the ask/open-price minus MODE_STOPLEVEL. But that ought to be something like xx = 7 given the numbers you're quoting, not xx = 19.

 

Hello Friends,

Answer: compare double, floating-point number, rather with zero than with another number.

AIS2 Trading Robot code sample:

...

//< 7.6.2. Trailing Logic 18 >                                                                                //< 464>
         //<  Buy Orders Trailing Rules >                                                                     //< 465>
         if ( OrderType       () == OP_BUY                                                                  ) //< 466>
         if ( OrderProfit     ()  > 0                                                                       ) //< 467>
         if ( NormalizeDouble ( avd.QuoteTrail     - avd.QuoteStops                      , avi.Digits ) > 0 ) //< 468>
         if ( NormalizeDouble ( avd.QuoteTrail     - avd.QuoteFreeze                     , avi.Digits ) > 0 ) //< 469>
         if ( NormalizeDouble ( OrderTakeProfit () - avd.QuoteBid       - avd.QuoteStops                      //< 470>
                                                                                         , avi.Digits ) > 0 ) //< 471>
         if ( NormalizeDouble ( avd.QuoteBid       - OrderStopLoss ()   - avd.TrailStep  - avd.QuoteTrail     //< 472>
                                                                                         , avi.Digits ) > 0 ) //< 473>
              avd.Stop        = NormalizeDouble (    avd.QuoteBid       - avd.QuoteTrail , avi.Digits     ) ; //< 474>
         //</ Buy Orders Trailing Rules >                                                                     //< 475>
                                                                                                              //< 476>
         //<  Sell Orders Trailing Rules >                                                                    //< 477>
         if ( OrderType       () == OP_SELL                                                                 ) //< 478>
         if ( OrderProfit     ()  > 0                                                                       ) //< 479>
         if ( NormalizeDouble ( avd.QuoteTrail     - avd.QuoteStops                      , avi.Digits ) > 0 ) //< 480>
         if ( NormalizeDouble ( avd.QuoteTrail     - avd.QuoteFreeze                     , avi.Digits ) > 0 ) //< 481>
         if ( NormalizeDouble ( avd.QuoteAsk       - OrderTakeProfit () - avd.QuoteStops                      //< 482>
                                                                                         , avi.Digits ) > 0 ) //< 483>
         if ( NormalizeDouble ( OrderStopLoss   () - avd.QuoteAsk       - avd.TrailStep  - avd.QuoteTrail     //< 484>
                                                                                         , avi.Digits ) > 0 ) //< 485>
              avd.Stop        = NormalizeDouble (    avd.QuoteAsk       + avd.QuoteTrail , avi.Digits     ) ; //< 486>
         //</ Sell Orders Trailing Rules >                                                                    //< 487>
//</7.6.2. Trailing Logic 18 >                                                                                //< 488>

... 

Best regards,

Ais

 

OK - Thanks for the suggestions but I probably need to explain exactly what my problem is:

My code has placed a buy order that did not reach the TP level and has now moved into negative profit but my calculations tell me that the trade to will start moving into profit very quickly. However, the calculations are not a 100% guarantee so I do not want the trade to go much more negative as the calculated alternative would be a rapid move in the wrong direction. I therefore want to move my stoploss immediately as close as possible to the trade just in case it goes more negative (without closing it). Thats why I am trying to obtain the correct stoploss level. I have read the limitations etc but the marketinfo stuff does not appear to provide the solution as it only gives 3/4 pips (or is this the correct figure based on the Ask/Bid). Using a demo account seems only to allow a stoploss of around 8 pips on gbpusd with a manual trade. I can see that the stoploss is not related to the orderopenprice which is an error on my part as it obviously (now I see it) must be related to the current Ask/Bid price. BUT HOW DO I OBTAIN THE CORRECT STOPLOSS figure to do what I need for any currency pair.

 

Think I need to try MODE_STOPLOSS LEVEL as per suggestion from jjc.

Sounds correct for what I require

thanks

 
BigAl wrote >>

Think I need to try MODE_STOPLOSS LEVEL as per suggestion from jjc.

Sounds correct for what I require

thanks

That works fine, thanks again to all who contributed