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.
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.
- 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) ); }
extern double LotSize = 0.01;
Risk depends on your initial stop loss, lot size, and the value of the pair. In code (MT4):- 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.
- 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.)
-
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. - You must normalize lots properly and check against min and max.
- You must also check FreeMargin to avoid stop out
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?
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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;