What is your suggestion?

 
1. My min brooker stoplevel is 2 pips, but I want to set sl/tp to 1 pip. What's your idea? Create an array to store SLs/TPs? or store them in a file(.csv)? Once the price reached those levels, then close the relevant positions.

2. I wonder to declare a few variables as global or inside OnTick()? Those variables will be only used inside OnTick(), and they should be calculated every tick
 
pavelion: 1. My min brooker stoplevel is 2 pips, but I want to set sl/tp to 1 pip. What's your idea? Create an array to store SLs/TPs? or store them in a file(.csv)? Once the price reached those levels, then close the relevant positions. 2. I wonder to declare a few variables as global or inside OnTick()? Those variables will be only used inside OnTick(), and they should be calculated every tick

Just use a "virtual" offset.

Lets say that your original Stop is 1.3 pips (13 points). Just define your "virtual" offset to for example, 5 pips (50 points) and add it to your expected stop.

That would make your real broker side stop at 13 + 50 = 63 points (which the broker will accept because it is more than the stops level). In your code you then simply read your stop (from your broker data), subtract 50 points, getting 13 points, and if the price has moved more than that, then you close your position.

This allows you to easily implement trailing stops and not have to store extra variables in your EA. It also allows you to recover if the EA, Terminal or PC crash, and give you a safety net so that the broker will still close the trade even if you lose the connection. It will have more loss than usual but if you use a reasonable virtual stop size, it will still be within an acceptable "critical" loss level.

 
Fernando Carreiro #:

Just use a "virtual" offset.

Lets say that your original Stop is 1.3 pips (13 points). Just define your "virtual" offset to for example, 5 pips (50 points) and add it to your expected stop.

That would make your real broker side stop at 13 + 50 = 63 points (which the broker will accept because it is more than the stops level). In your code you then simply read your stop (from your broker data), subtract 50 points, getting 13 points, and if the price has moved more than that, then you close your position.

This allows you to easily implement trailing stops and not have to store extra variables in your EA. It also allows you to recover if the EA, Terminal or PC crash, and give you a safety net so that the broker will still close the trade even if you lose the connection. It will have more loss than usual but if you use a reasonable virtual stop size, it will still be within an acceptable "critical" loss level.

Thank you. Storing values in broker's server improves a lot, but it's not what I'm looking for.

Do you have any idea for #2?
 
pavelion #: Do you have any idea for #2?

You can use static variables inside functions. That way you won't need to update them on every tick. Only when needed. They will work in a similar way as globally scoped variables, but their scope will be local, only within that function.

Reason: