I don't actually see initialization of TPp variable in your code. Change it to the following and try again. You will need to initialize TPp one If you put it ouside the if blocks.
if(OrderType()==OP_BUY) {SLp = iLow(NULL, 0, 1); TPp = OrderTakeProfit(); //<-----HERE OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(SLp,Digits),TPp,0,Blue); Comment("\nStop Loss will move to BE at ", Low[1]); } if(OrderType()==OP_SELL) {SLp = iHigh(NULL, 0, 1); TPp = OrderTakeProfit(); //<-----HERE OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(SLp,Digits),TPp,0,Red); Comment("\nStop Loss will move to BE at ", High[1]); } //}
Thanks for the hint. I just forgot to set it.
But it still doesn't work probably. When I apply it on a M1 Chart it only set the S/L every 5th-7th candle. Are there any restrictions on changing the S/L? Maybe it is only possible to change it by a certain amount of pips or only every five minutes...
> Maybe it is only possible to change it by a certain amount of pips
You can move a stop by 1 sub-pip if you want but you have to be aware of being too close to market price when placing or modifying an order - see
MarketInfo(Symbol(), MODE_STOPLEVEL)
MarketInfo(Symbol(), MODE_FREEZELEVEL)-BB-
if(OrderMagicNumber() != 456976 || OrderMagicNumber() != 659970...
I don't know why you want to use multiple magic numbers, one is sufficient. But, you are excluding all orders with those numbers.Total=OrdersTotal(); if(Total>0) { for(i=Total-1; i>=0; i--)
The IF is unnecessary as the FOR will do nothing if there aren't any open orders.- Simplicy the code (no multiple IFs):
for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if ( OrderSelect(pos, SELECT_BY_POS) // Only my orders w/ &&( OrderMagicNumber() == 456976 // my magic number ||OrderMagicNumber() == 659970 ||... ) && OrderSymbol() == Symbol() ){ // and my pair. if(OrderType()==OP_BUY){ ...
SLp = iHigh(NULL, 0, 1); // Equivalent SLp = High[1];
Why use function calls, simplify.
-
I don't know why you want to use multiple magic numbers, one is sufficient. But, you are excluding all orders with those numbers.
-
The IF is unnecessary as the FOR will do nothing if there aren't any open orders.
- Simplicy the code (no multiple IFs):
-
Why use function calls, simplify.
> Maybe it is only possible to change it by a certain amount of pips
You can move a stop by 1 sub-pip if you want but you have to be aware of being too close to market price when placing or modifying an order - see
-BB-Is the Freezelevel a given thing or is it possible to set it?
If you didn't solve it already, you can try something like this:
if(OrderType()==OP_BUY) {SLp = iLow(NULL, 0, 1); TPp = OrderTakeProfit(); SLp = MathMin( iLow(Symbol(),Period(),1), Bid-MarketInfo(Symbol(),MODE_STOPLEVEL)*Point ); //<--HERE OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(SLp,Digits),TPp,0,Blue); Comment("\nStop Loss will move to BE at ", Low[1]); } if(OrderType()==OP_SELL) {SLp = iHigh(NULL, 0, 1); TPp = OrderTakeProfit(); SLp = MathMax( iHigh(Symbol(),Period(),1), Ask+MarketInfo(Symbol(),MODE_STOPLEVEL)*Point); //<--HERE OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(SLp,Digits),TPp,0,Red); Comment("\nStop Loss will move to BE at ", High[1]); } //}
As MODE_STOPLEVEL has usually got a small value (about 5 to 10 points), consider changing the code to add more points. Try it before going to live account (I didn't check it).
Regards,
AM.
// STOP LOSSE 4 SELL ORDERS if((OrderMagicNumber()==MagicNumber1)) if(OrderType()==OP_SELL) { StopLoss=iHigh(NULL,0,1); TakeProfit=OrderTakeProfit(); StopLoss=MathMax(iHigh(Symbol(),Period(),1),Ask+MarketInfo(Symbol(),MODE_STOPLEVEL)*Point); //<--HERE int sellclose=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(StopLoss,Digits),TakeProfit,0,Green); Comment("\nStop Loss will move to BE at ",High[1]); } //-----------------------------------------------------------------+ // STOP LOSSE 4 BUY ORDERS if((OrderMagicNumber()==MagicNumber2)) if(OrderType()==OP_BUY) { StopLoss=iLow(NULL,0,1); TakeProfit=OrderTakeProfit(); StopLoss=MathMin(iLow(Symbol(),Period(),1),Bid-MarketInfo(Symbol(),MODE_STOPLEVEL)*Point); //<--HERE int buyclose=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(StopLoss,Digits),TakeProfit,0,Green); Comment("\nStop Loss will move to BE at ",Low[1]); } //+------------------------------------------------------------------+Joao Rosas:
If you didn't solve it already, you can try something like this:
As MODE_STOPLEVEL has usually got a small value (about 5 to 10 points), consider changing the code to add more points. Try it before going to live account (I didn't check it).
Regards,
AM.
Hi Joao, Could you help me please, How to edit this code to be: +5 points UP the High & -5 points Under the Low? Thanx..

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi Guys,
i tried to code a stoploss EA, which sets the S/L on the low/high of the previous candle (see pic below).
So now I wrote down some code and it does sometimes work. But actually not at every candle.
Maybe it is just a simple mistake I did. Could someone please help?