Stop loss for LESS of a loss

 

I have a simple EA that will close orders based on take profit and stop loss.

(TotalProfit<=0-stoploss_dollars || percentProfit<=0-stoploss_percent)

How would the math work if you wanted to have an order close out when it's LESS of a loss, for example, if you have -100 and you want it to close when it's -20.

 

Something like . . .

extern double AcceptableLoss = 20; 

 
OrderSelect( . . .  );

if(OrderProfit() <=  -1 * AcceptableLoss )
   {
   if(!CloseOrder(OrderTicket(), OrderLots(), OrderCloseProiice(), etc, etc))
      Print("Order close failed, ticket: ", OrderTicket());     
   }

 
 
eliteeservices:

How would the math work if you wanted to have an order close out when it's LESS of a loss, for example, if you have -100 and you want it to close when it's -20.

Or maybe you want to keep the Order open until the situation improves ?
 
I want to have a static number, for example -20, that when the loss DECREASES from let's say -100 to -20, positions are closed. There is a particular reason i want to do this.
 
eliteeservices:
I want to have a static number, for example -20, that when the loss DECREASES from let's say -100 to -20, positions are closed. There is a particular reason i want to do this.
The complication you are going to have is that you need to know that the profit has first gone to -100 before you can then close it when it recovers to -20 . . . if you just close at -20 it will also close on the way down from -10 to -20 . . .
 

set a flag to true if OrderProfit() is -100 or lower

if the flag is true and OrderProfit() is -20 or higher, close.

 
eliteeservices:
I want to have a static number, for example -20, that when the loss DECREASES from let's say -100 to -20, positions are closed. There is a particular reason i want to do this.

Static numbers for TP and SL are bad. They may backtest ok under current market conditiions but will fail when an instrument has a huge range. Look at the AUDUSD for example that has had a range of .65 to 1.10 over the last couple of years. 100 points means something totally different when the AUDUSD is at .65 vs 1.10. I have moved all my TP and SL to be factors of average bar lengths over time and do not trade when volatility gets too extreme. BTW, I only look at high timeframes (4h) and am trying to be a trend trader with limited success, so you may not want to take my advice :-)
 

Raptor, thank you. I believe we need to reverse the sign as well:

else if (TotalProfit>=(-1*lessloss_dollars) || percentProfit>=(-1*lessloss_percent))

Because this way, if loss is greater than value, such as -100, it will not trigger until -20. correct?

This assumes that loss is already -100 when EA is loaded. This is acceptable since our strategy is hybrid (manual / auto).

 

you can check each order and calculate the range from openprice to highest, lowest and currentprice.


hint:

bool OrderSelect(       int index, int select, int pool=MODE_TRADES);

OrderOpenTime();

OrderOpenPrice();

OrderClosePrice();

int iBarShift(  string symbol, int timeframe, datetime time, bool exact=false);

int iHighest(   string symbol, int timeframe, int type, int count=WHOLE_ARRAY, int start=0);

int iLowest(    string symbol, int timeframe, int type, int count=WHOLE_ARRAY, int start=0);


in correct way this will give you amount of pips.


the calculation to get it in USD or wathever currency you prefer, is somewere here in the forum. search.


Happy coding!

Reason: