Help please to program maximum SL on EA

Hi everybody,

I come to you because I need a clarification. So I programmed an Expert advisor, he uses the ATR as SL. What I would like to be able to do is tell my expert advisor not to take into account the SL of the ATR if it is more than 200 pips for example.
If the ATR wants to place an SL at more than 200 pips, then our Expert Advisor will not take into account the SL of the ATR, but the one that will have defined by default, 200 pips.

This is the part of the code that interests us:

void BUY()
{
  double SL=0, TP=0;
  int  T=0;
  bool OpenBar=true;
//---
      if(EnterOpenBar) if(iVolume(NULL,0,0)>1) OpenBar=false;
//---
      if (OpenBar)
  {
//---
      if(StopLoss>0)
        SL=Ask-StopLoss*point;
    else
      if(StopLoss<=0) 
        SL=Bid-ATR_Multiplicator*iATR(Symbol(),0,ATR_Level,0);
//---
      if(TakeProfit>0)
        TP=Ask+TakeProfit*point;
    else
      if(TakeProfit<=0) 
        TP=Bid+ATR_Multiplicator*iATR(Symbol(),0,ATR_Level,0);
   {
//---
      if (TotalOrders()<1
//---
  && (MarketInfo(Symbol(),MODE_ASK) > iBands(Symbol(),Band_Time_Frames,P1,P2,Band_Shift,PRICE_CLOSE,MODE_UPPER,1))
  && (iMA(Symbol(),MA_Time_Frames,P3,0,MA_MODE,MA_Applied_Price,1) > iMA(Symbol(),MA_Time_Frames,P4,0,MA_MODE,MA_Applied_Price,1)))
//---  
     {
        if (Alert_Buy)
       {
        Alert(EA_Comment+" "+Symbol()+" BUY");
       }
//---
      T=OrderSend(Symbol(),OP_BUY,Lots_B,Ask,Slippage,SL,TP,EA_Comment,MagicNumber,0,Blue);
       {
        (Print(EA_Comment+" "+Symbol()+" BUY"));
       }
//---
      }  
    }  
  }  
}  

As can be seen, one has an SL that can be programmed with the number of pips we want, otherwise if the SL is at 0 in the parameters then the ATR calculates the SL.

But how do you explain to EA that you do not want to have a SL of more than 200 pips ?!

If SL of ATR > 200 pips them SL = 200 pips.

How do you code that?

If anyone could help me please. thank you.

//---
      if(StopLoss_R>0)
   {
        SL=Ask-StopLoss_R*point;
   }
    else if(StopLoss_R<=0) 
   {
        SL=Bid-ATR_Multiplicator_R*iATR(Symbol(),0,ATR_Level_R,0);
   }
    else if(SL>=201) 
   {
        SL=Ask-200*point;
   }
//---

I try this, but not work…

You could use a simple turnary operator which has a syntax of (condition) ? (if_true) : (if_false)

So in your case. SL = ATRstop > maxPips ? maxPips : ATRstop;

You could also use the fmax function

//±-----------------------------------------------------------------+
//| ut_Kanada |
//| [email protected] |
//±-----------------------------------------------------------------+
#property copyright “ut_Kanada”
#property link “[email protected]
#property version “1.00”
#property strict

//==================================================================+
// ЗАДАНЫЕ ЗНАЧЕНИЯ |
//==================================================================+

extern int max_SL = 200;
extern int ATR_Level_R = 14;
extern double ATR_Multiplicator_R = 1000;

//************************************************************************************************************
//==================================================================+
// Переменные для программы |
//==================================================================+

double sl;
int delta_SL;
double SL;

//==================================================================+

int start()
{

SL = NormalizeDouble((Ask - iATR(Symbol(),0,ATR_Level_R,0)),Digits);

delta_SL = (int)((Ask - SL)/Point);

if(delta_SL > max_SL)
sl = Ask - max_SL*Point;
else
if(delta_SL <= max_SL)
sl = SL;

Comment("SL = “,SL,” delta_SL = “,delta_SL,” sl = ",sl);

//--------------
return(0);
}
It is difficult to read the torn part from the full text. I don’t understand why atr_multiplicator_r is here. You are missing a variable to determine where to stop. I added: double sl; This will be the place of StopLoss in the OrderSend () function. Please contact me, I’m good at writing programs.