sl script works buys, not sells. - page 2

 

@Alain Verleyen

could be a bug with 5833 mt5.

the script works on different deposit currency accounts when i change the code to this below. note the change from using std library Trade.mqh to PositionGetXXXX

lines 32, 36, 37, 42, 46, 51.

//+------------------------------------------------------------------+
//|                                               SCR5.rRemoveSL.mq5 |
//|                                  Copyright 2026, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
#include <Trade/Trade.mqh>
//bool finished;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnStart()
  {
   CTrade trade;
   CPositionInfo position;
//   trade.SetAsyncMode(true);
   string sym;
   static int start = PositionsTotal(), pos=0, dig;
   static double ts, tv, step, ltv;//, poi; //pft, ltv, cursl, maxsl;
//---
   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      if(position.SelectByIndex(i))
        {
         if(PositionGetDouble(POSITION_SL)>0)
         continue;
         sym=PositionGetString(POSITION_SYMBOL);
         ts=SymbolInfoDouble(sym,SYMBOL_TRADE_TICK_SIZE);
         ltv=PositionGetDouble(POSITION_VOLUME);
         dig=(int)SymbolInfoInteger(sym,SYMBOL_DIGITS);
         step=PositionGetDouble(POSITION_PRICE_OPEN); //position.PriceOpen();
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
           {
            tv=SymbolInfoDouble(sym,SYMBOL_TRADE_TICK_VALUE_LOSS);
            step-=NormalizeDouble(.35 * 1000 / ts * tv / ltv / pow(10,dig*2),dig);
//            if(PositionGetDouble(POSITION_SL)!=step)
               if(trade.PositionModify(PositionGetInteger(POSITION_TICKET),step,NULL))
                  pos-=1;
           }
         else
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
              {
               tv=SymbolInfoDouble(sym,SYMBOL_TRADE_TICK_VALUE_LOSS);
               step+=NormalizeDouble(.35 * 1000 / ts * tv / ltv / pow(10,dig*2),dig);
//               if(PositionGetDouble(POSITION_SL)!=step)
                  if(trade.PositionModify(PositionGetInteger(POSITION_TICKET),step,NULL))
                     pos-=1;
              }
        }
     }
  }
//+------------------------------------------------------------------+
 
my formulae is clearly wrong on some pairs. i have 4 trades with negative 350 floating losses, and the sl is not even red. how do i prove that ticksize or tickvalue is incorrect? or how do i prove that it is my formulae is wrong?
 
Michael Charles Schefe #:

i changed the step so that it was simple 2000 points and then 800 points. ALL WITH SAME RESULTS. sell trades are not being modified. mayve it IS NOT my math formulae that was the problem! Can someone test the code on their own demo account please.

You are using wrong method 
position.Type()

Which return always 0 (POSITION_TYPE_BUY).

You should use 

position.PositionType()

 
Alain Verleyen #:
You are using wrong method 

Which return always 0 (POSITION_TYPE_BUY).

You should use 

position.PositionType()

oh. 🫣
 
Michael Charles Schefe #:
my formulae is clearly wrong on some pairs. i have 4 trades with negative 350 floating losses, and the sl is not even red. how do i prove that ticksize or tickvalue is incorrect? or how do i prove that it is my formulae is wrong?

Your formula is wrong. Do you want me to give you the correct one directly or do you prefer to fix it yourself ?

step+=NormalizeDouble(.35 * 1000 / ts * tv / ltv / pow(10,dig*2),dig);
 
Alain Verleyen #:
Your formula is wrong. Do you want me to give you the correct one directly or do you prefer to fix it yourself ?
directly or here...please...thanks!
 
Michael Charles Schefe #:
directly or here...please...thanks!
step+=NormalizeDouble(350 / tv * ts / ltv,dig);
 
Alain Verleyen #:

this is the result, shocking but weirdly impressive that the broker server accepts them.


 
Michael Charles Schefe #:

this is the result, shocking but weirdly impressive that the broker server accepts them.



Show your code please.

 
Alain Verleyen #:


Show your code please.

//+------------------------------------------------------------------+
//|                                               SCR5.rRemoveSL.mq5 |
//|                                  Copyright 2026, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
#include <Trade/Trade.mqh>
//bool finished;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnStart()
  {
   CTrade trade;
   CPositionInfo position;
//   trade.SetAsyncMode(true);
   string sym;
   static int start = PositionsTotal(), pos=0, dig;
   static double ts, tv, step, ltv;//, poi; //pft, ltv, cursl, maxsl;
//---
   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      if(position.SelectByIndex(i))
        {
         if(PositionGetDouble(POSITION_SL)>0)
         continue;
         sym=PositionGetString(POSITION_SYMBOL);
         ts=SymbolInfoDouble(sym,SYMBOL_TRADE_TICK_SIZE);
         ltv=PositionGetDouble(POSITION_VOLUME);
         dig=(int)SymbolInfoInteger(sym,SYMBOL_DIGITS);
         step=PositionGetDouble(POSITION_PRICE_OPEN); //position.PriceOpen();
         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
           {
            tv=SymbolInfoDouble(sym,SYMBOL_TRADE_TICK_VALUE_LOSS);
            step-=NormalizeDouble(.35 * 1000 / ts * tv / ltv,dig);  // should be / tv * ts
//            if(PositionGetDouble(POSITION_SL)!=step)
               if(trade.PositionModify(PositionGetInteger(POSITION_TICKET),step,NULL))
                  pos-=1;
           }
         else
            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
              {
               tv=SymbolInfoDouble(sym,SYMBOL_TRADE_TICK_VALUE_LOSS);
               step+=NormalizeDouble(.35 * 1000 / ts * tv / ltv,dig); // should be / tv * ts
//               if(PositionGetDouble(POSITION_SL)!=step)
                  if(trade.PositionModify(PositionGetInteger(POSITION_TICKET),step,NULL))
                     pos-=1;
              }
        }
     }
  }
//+------------------------------------------------------------------+

THANKS ALAIN