Download MetaTrader 5

How to find highest AccountProfit() value for trailing stop coding

To add comments, please log in or register
Do you know C++? MQL5 has much in common with it. Just try it yourself!
keithaco 2014.03.13 10:25 
Hi everyone! This website has been super helpful as I'm very new to coding.
But there's just one thing I could not figure out myself, I really appreciate if someone can assist me.

I am trying to create a trailing stop system based on the AccountProfit() values within the trading period of a single trade.
I would like to know how I can record/store StopLossLevel value when highest AccountProfit() values > 0, on every tick move.

- double CurrentProfit = AccountProfit()
- double HighestProfit = highest AccountProfit() value within the trading period of a single trade
- double StopLossLevel = HighestProfit - 10;

So for example, when HighestProfit is 30, I want my StopLossLevel to be 20 (30 - 10 = 20).
If the CurrentProfit increases to 45, HighestProfit value also becomes 45, then I want my StopLossLevel to be 35 (45 - 10 = 35).

*No new trade will be created when existing positions are open.
*I will use my code which closes open position(s) when CurrentProfit hits StopLessLevel.
*I would like to use AccountProfit() instead of OrderProfit() as I will have more than 1 position depending on conditions.

I hope my explanation makes sense. Really appreciate any input. Thanks!

qjol 2014.03.13 11:02  
double CurrentProfit = AccountProfit();
double HighestProfit;
double StopLossLevel = HighestProfit - 10;

if (CurrentProfit > HighestProfit)
   HighestProfit = CurrentProfit;
keithaco 2014.03.14 06:09  
qjol, thanks so much! It's working :)
To add comments, please log in or register