remember original stoploss value?

 

Hi,

For my EA, how can I make it remember the stoploss value of the FIRST of a series of open positions? (This first position is opened by manual, the latter positions are by EA.) With market moves, SL of this first position and other positions have to be changed(by my EA), but I need the original value remembered.

I'm thinking of static variable and globalvariableset. But if this EA runs simaltaneously for multiple currency pairs, there would be conflict. I may do this with help of "case", but is there any smarter way around? since if this EA encounters abnormal finish, there would be more complicated things to deal with relics of this static/global....

 

You can just declare a double variable outside of the special functions. It will remember its value until you close the EA. And no conflicts with other instances of the EA.

More safely, you can write the value to file (using the symbol in the file name to distinguish it).

 
alladir:

You can just declare a double variable outside of the special functions. It will remember its value until you close the EA. And no conflicts with other instances of the EA.

More safely, you can write the value to file (using the symbol in the file name to distinguish it).


Thanks for the help.

"variable outside function...", but I need this EA keep working even if MT4 restarts, recognize positions opened by itself before.

I decide to use Globalvariableset()...,a simple look, I find that write/read file is so complicated for me.

~Cheers.

 
joshatt:

Hi,

For my EA, how can I make it remember the stoploss value of the FIRST of a series of open positions? (This first position is opened by manual, the latter positions are by EA.) With market moves, SL of this first position and other positions have to be changed(by my EA), but I need the original value remembered.

I'm thinking of static variable and globalvariableset. But if this EA runs simaltaneously for multiple currency pairs, there would be conflict. I may do this with help of "case", but is there any smarter way around? since if this EA encounters abnormal finish, there would be more complicated things to deal with relics of this static/global....


You could resolve conflicts by naming the variable by combining the Magic number and chart symbol

string name= "Stop_"+Symbol()+MagicNumber;
GlobalVariableSet(name,StopLossValue );

Of course you will have to make sure that your code doesn't re-assign a new value to the variable, should the SL level change.

 

Global variable is fine if you only need to record double values (prices etc.).

I like to read/write as it keeps excellent logs to analyse what went right or wrong afterwards... it might be worth your time looking at a few examples to get the hang of it in the future.

 
alladir:

Global variable is fine if you only need to record double values (prices etc.).

I like to read/write as it keeps excellent logs to analyse what went right or wrong afterwards... it might be worth your time looking at a few examples to get the hang of it in the future.

Thanks.
Reason: