Trying to code Stop Loss behind last three bars bars need help (I am new to coding as well).

 

I'm trying to get the stop loss in my EA to auto set behind last three bars can someone show me how this is done, when complied I get error, not all control paths return a value how do I fix this?

I have some idea that I have to use: return StopLossBuy;

Files:
 
mt4trader12345:

I'm trying to get the stop loss in my EA to auto set behind last three bars can someone show me how this is done, when complied I get error, not all control paths return a value how do I fix this?

I have some idea that I have to use: return StopLossBuy;


If you would like people to help you with your code, then  you need to actually provide the code and not just a screenshot.

 
mt4trader12345:

I'm trying to get the stop loss in my EA to auto set behind last three bars can someone show me how this is done, when complied I get error, not all control paths return a value how do I fix this?

I have some idea that I have to use: return StopLossBuy;

Say you are going LONG, and you want to set the SL at the lowest price of the last three bars.

#define MIN3(A,B,C) MathMin(A, MathMin(B,C))

double sl = MIN3(Low[1],Low[2],Low[3]) - 10;

SHORT is similar. Just change the Low's to High's, the Mins to Maxs.

 

When I add the #define code, it seems to not put the stops in the right places.

extern int MagicNumber     = 12345;
extern double LotSize      = 0.01;


bool IsNewCandle()
{
static int BarsOnChart=0;
if(Bars==BarsOnChart)
return(false);
BarsOnChart=Bars;
return(true);
}


#define MIN3(A,B,C) MathMin(A, MathMin(B,C))

double slb = MIN3(Low[1],Low[2],Low[3]);

#define MAX3(X,Y,Z) MathMax(X, MathMax(Y,Z))

double sls = MAX3(High[1],High[2],High[3]);


double SetLotSize()
{
static double LastAccountBalance;
if(LastAccountBalance>AccountBalance())
{
if (LotSize==0.01) 
LotSize=0.02;

else if (LotSize==0.02)
LotSize=0.03;

else if (LotSize==0.03)
LotSize=0.05;

else if (LotSize==0.05)
LotSize=0.08;

else if (LotSize==0.08)
LotSize=0.13;

else if (LotSize==0.13)
LotSize=0.19;

else if (LotSize==0.19)
LotSize=0.27;

else if (LotSize==0.27)
LotSize=0.50;
}
if(LastAccountBalance < AccountBalance())
LotSize=0.01;

LastAccountBalance=AccountBalance();
return LotSize;
}


void TakeTrade()
{
double MA1a = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,1);
double MA1b = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,2);
double MA2a = iMA(NULL,0,75,0,MODE_EMA,PRICE_CLOSE,1);
double MA2b = iMA(NULL,0,75,0,MODE_EMA,PRICE_CLOSE,2);
double StochSig  = iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_SIGNAL,1);
double StochMain = iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_MAIN,1);

if(OrdersTotal()==0)
if((MA1a>MA2a)&&(MA1b>MA2b)&&(StochSig>StochMain)&&(StochSig<20))
int buy=OrderSend(Symbol(),OP_BUY,SetLotSize(),Ask,3,slb,Ask+300*_Point,NULL,MagicNumber,0,Green);

if(OrdersTotal()==0)
if((MA1a<MA2a)&&(MA1b<MA2b)&&(StochSig<StochMain)&&(StochSig>80))
int sell=OrderSend(Symbol(),OP_SELL,SetLotSize(),Bid,3,sls,Bid-300*_Point,NULL,MagicNumber,0,Red);
}


void OnTick()
{
if(IsNewCandle())TakeTrade();
}

I have added my code above. Could someone show and explain how I can get my EA to put the stops above the high of last 3 bars and the low of the last 3 bars.

 
Note when testing I get: OrderSend error 130
 
  1. mt4trader12345:
    #define MIN3(A,B,C) MathMin(A, MathMin(B,C))

    Don't use macros, when you can use overloaded functions.

    template <typename T>
    T        MathMax(T a, T b, T c){ return MathMax(a, MathMax(b, c) );  }
    

  2. extern double LotSize      = 0.01;
    
    Risk depends on your initial stop loss, lot size, and the value of the pair. In code (MT4):
    1. You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    2. Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
    3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers.
    4. You must normalize lots properly and check against min and max.
    5. You must also check FreeMargin to avoid stop out
    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5=0.1 Lots maximum.

 
whroeder1:
  1. Don't use macros, when you can use overloaded functions.

Now that I see it, I agree this is the better way.
 

I agree that risking percentage per trade is better, but my question on how to put my stops behind the last three candles low or high.

Could someone show me how to do this?

Reason: