Thread: How to code a moving Stop? (not a T/S).

1. How to code a moving Stop? (not a T/S).

Hi,

I have been trying to duplicate a Breakeven code in my EA to act like a stepping stop but my code wont allow the next step to activate.

If someone could help/point me in the direction of how to code a stepping stop where the steps can be set at individual levels.

For example: If price moves 20pips move stop to breakeven, then if price moves another 30pips move stop to 25pips profit and so on.

I hope I have explained clearly enough, I don't what to confuse my question with Trailing Stops or a Trailing Stop Steps.

Thanks.

#Edit --->

In my search for an answer I found and added below from another forum a similar request but it never got resolved (I have modified parts of the original text by stu). It might be more specific on the lines of what I would like to do:

1) My EA Enters based on my entry criteria
2) EA sets initial Stop 30 pips behind entry
3) Once trade moves 20 pips in my favor, automatically move Stop to Breakeven.
4) Once trade moves 30 pips in my favor, automatically move Stop to +10 pips after entry point.
5) Once trade moves 40 pips in my favor, automatically move Stop to +20 pips after entry point
6) Once trade moves 50 pips in my favor, automatically move Stop to +35 pips after entry point
7) Once trade moves 55 pips in my favor, automatically move Stop to +45 pips after entry point
8) Take profit at +60 pips.

.......even if someone could show or point me to some code or an EA that deals with at least 2 individual stepping stops (not the usual trailing stop step) I should be able to code the rest.

"maybe logic starting like this,

extern int MoveStopWhen_a = 20; //Breakeven activation 20pips after entry.
extern int MoveStopTo_a = 2; //S/L placement at breakeven or higher for small profit.
extern int MoveStopWhen_b = 50; //50pips after entry.
extern int MoveStopTo_b= 35; //S/L moved to 35pips after entry to lockin profit.

etc,etc,etc

Thanks again,

Dono

2. Here's the code I use. It is with 1 level (after profit reaches a certain level, stop loss is moved to BreakEven + X pips):

Code:
```extern      int       Lock.Profit.After          = -1;
extern      int       Pips.To.Lock               = -1;

/** Handle the profit lockage.
*/
void handleProfitLock()
{
double newSL = 0;

if(Lock.Profit.After == -1 || Pips.To.Lock == -1)
return(0);

for(int i = 0; i < OrdersTotal(); i++)
{
OrderSelect(i, SELECT_BY_POS);

if(OrderMagicNumber() == Magic.Number &&
OrderSymbol() == Symbol() &&
(OrderType() == OP_BUY || OrderType() == OP_SELL) &&
calcPipsProfit() >= Lock.Profit.After)
{
newSL = OrderOpenPrice() + Pips.To.Lock * Point*10;
else
newSL = OrderOpenPrice() - Pips.To.Lock * Point*10;

modifyStopLoss(newSL, "Modified stop loss by profit lock, new stop: " + newSL);
}
}
}

/** This method modifies the stop loss (if the order has been preselected).
@param dontRaiseRisk if true, the stop will not move down in long trades, and up in short ones.
*/
void modifyStopLoss(double NewSL, string strComment = "", bool dontRaiseRisk = true)
{
(OrderStopLoss() < NewSL || !dontRaiseRisk))
{
OrderModify(OrderTicket(), OrderOpenPrice(), NewSL, OrderTakeProfit(), OrderExpiration(), Red);

if(strComment != "")
Print(strComment);
}

if(OrderType() == OP_SELL &&
(OrderStopLoss() > NewSL || !dontRaiseRisk))
{
OrderModify(OrderTicket(), OrderOpenPrice(), NewSL, OrderTakeProfit(), OrderExpiration(), Red);

if(strComment != "")
Print(strComment);
}
}

/** Returns the profit in pips (if the order has been preselected).
*/
int calcPipsProfit()
{
int PipsProfit;

PipsProfit = (Ask - OrderOpenPrice()) / (Point*10);
else
PipsProfit = (OrderOpenPrice() - Bid) / (Point*10);

return(PipsProfit);
}```
Put this function in your code, and call HandleProfitLock() every tick. Pay attention, it is for 5-digit brokers only.

3. Thanks FXCode for taking the time to answer but I have a similar code that I use as a breakeven that I have working.
My problem is trying to add extra steps to it or preferably finding a code that has individual stepping stop levels. At the moment my S/L is set at 35pips and my Breakeven is activated at 20pips so my S/L is moved to 5pips profit minus spread (so I make about 3pips if my S/L is hit). I would like to add another S/L move once the price moves another 30pips to 20pips profit and so on until my T/P is hit.

I have edited my original post to better explain my request.
Thanks again FXCode,

Dono

4. Originally Posted by Dono
Thanks FXCode for taking the time to answer but I have a similar code that I use as a breakeven that I have working.
My problem is trying to add extra steps to it or preferably finding a code that has individual stepping stop levels. At the moment my S/L is set at 35pips and my Breakeven is activated at 20pips so my S/L is moved to 5pips profit minus spread (so I make about 3pips if my S/L is hit). I would like to add another S/L move once the price moves another 30pips to 20pips profit and so on until my T/P is hit.

I have edited my original post to better explain my request.
Thanks again FXCode,

Dono
Within the void() function after you have selected your order, you need to insert additional rules for recalculating the value of the newSL double.

For instance, you might say (in the case of Open Buy trades):

Code:
```newSL=OrderOpenPrice()-30*Point;//this sets your initial SL

if (Bid-OrderOpenPrice()>=50*Point) newSL=OrderOpenPrice()+20*Point+Bid-Ask;//this locks in some more profit (again less the spread)```
You then need to normalise the newSL double (to prevent errors in the OrderModify() function) before creating a final rule which modifies the SL only if the newSL value is higher than the existing stop-loss.

You would do the reverse of this for Open Sell orders.

5. Thanks jezzer1961,

I will try to have a play with your example, I think your example also explains why my first attempt to recode a breakeven code didn't work. I thought it may have been because my second step was coded to look for where the stop was prior to the original breakeven placement. Pretty as much as you mentioned.

Thanks again, and any other options/examples from others would be appreciated as once I see a few examples I usually can put something together.

6. Originally Posted by Dono
Thanks jezzer1961,

I will try to have a play with your example, I think your example also explains why my first attempt to recode a breakeven code didn't work. I thought it may have been because my second step was coded to look for where the stop was prior to the original breakeven placement. Pretty as much as you mentioned.

Thanks again, and any other options/examples from others would be appreciated as once I see a few examples I usually can put something together.
The important thing here is to understand the significance of putting the different rules to calculate newSL into the correct order.

For instance, I've used 3 examples to calculate a different value for newSL. If you were to put line 1 at the end of the list, it would over-ride the other two lines and they would then never be applied. It is important that the largest value which will be calculated for newSL goes at the bottom of the list of rules.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•