sl script works buys, not sells.

 

gidday. i want my script to work on forex pairs, all trades, from all forec symbols. My script below is working on both usd and aud deposit accounts, but only on BUY TRADES. help!

I want all my trades have an sl that is equivalent of $350. 

//+------------------------------------------------------------------+
//|                                               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))
        {
         sym=position.Symbol();
         ts=SymbolInfoDouble(sym,SYMBOL_TRADE_TICK_SIZE);
         ltv=PositionGetDouble(POSITION_VOLUME);
         dig=(int)SymbolInfoInteger(sym,SYMBOL_DIGITS);
         step=position.PriceOpen();
         if(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(position.Ticket(),step,NULL))
                  pos-=1;
           }
         else
            if(position.Type()==POSITION_TYPE_SELL)
              {
               tv=SymbolInfoDouble(sym,SYMBOL_TRADE_TICK_VALUE_PROFIT);
               step+=NormalizeDouble(.35 * 1000 / ts * tv / ltv / pow(10,dig*2),dig);
               if(PositionGetDouble(POSITION_SL)!=step)
                  if(trade.PositionModify(position.Ticket(),step,NULL))
                     pos-=1;
              }
        }
     }
  }
//+------------------------------------------------------------------+
 

i wrote a different script that prints out much of the data for usdjpy on my aud account.

The 0.35 in the code is my common tradesize, albeit i want other trades with different sizes -- to have sl that is equivalent of $350 ie if my trade is 0.14, then my sl target will be roughly 2.5X the distance from open price as on the larger 0.35 trade.

Here is the output of that script. supporting my calc result is 875 points for the 0.35 trade. i have also supported this by using myfxbook calculator and got same result of 87.5 pips.


NOTE by "Not working" i mean i get Invalid stops errors. These sell trades do not have a sl on them.
 
Why not ask AI tools? For experienced developers, AI can be helpful. I sometimes put my hand written code into AI and get it to do a code review for me.
 
Conor Mcnamara #:
Why not ask AI tools? For experienced developers, AI can be helpful. I sometimes put my hand written code into AI and get it to do a code review for me.
i just havent made the time to explore that much. and like they say bout "old dogs" and "new tricks".
 
Michael Charles Schefe #NOTE by "Not working" i mean i get Invalid stops errors. These sell trades do not have a sl on them.

Always, before sending an order to modify a position, you need to check the stop levels. Here's an example:

//+--------------------------------------------------------------------------------------------------------------------+
//| Check stop levels
//+--------------------------------------------------------------------------------------------------------------------+
bool CheckStopsLevel(string symbol, ENUM_ORDER_TYPE type, double sl, double tp)
  {
   if(g_cache.stopsLevel == 0)
      return true;

   double price = (type == ORDER_TYPE_BUY) ? g_cache.bid : g_cache.ask;
   double minDistance = g_cache.stopsLevel * g_cache.point;

   if(type == ORDER_TYPE_BUY)
     {
      if(price - sl <= minDistance)
         return false;
      if(tp - price <= minDistance)
         return false;
     }
   else
     {
      if(sl - price <= minDistance)
         return false;
      if(price - tp <= minDistance)
         return false;
     }

   return true;
  }