static double longcurrentprice = Close[0] + (Point*40); static double shortcurrentprice = Close[0] - (Point*40); static double longSL = Close[0] - (Point*80); static double shortSL = Close[0] + (Point*80);
That is not an assignment; it's initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.
-
They are initialized once on program load.
-
They don't update unless you assign to them.
-
In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).
MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and
Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:
- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
-
Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
external static variable - MQL4 programming forum #2 2013.02.10
Thank you for that piece of information. How do I get around the ordershistorytotal loop executing an order regardless of whether I am in a trade or not? Ignoring the current variables in the code above, a simple look at the last order to see if I was stopped out and take the trade in SL direction. This was my main concern.
Only if you store the ticks as they come.
static int iTick=0; static double ticks[X]; iTick = (iTick+1) % X; // Circular buffer. double bidXticks = ticks[iTick]; // Save X ticks ago. ticks[iTick] = Bid; // Update buffer.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Happy Thursday earthlings! I place an order inside the checkMN function, outside of that code block I have 2 separate for loops. One for modifying the current open order and one for checking the prev closed orders. the one that checks the prev order is to take a position in the direction of the stoploss for that order.
Problem(prev order for loop): It will execute regardless of whether i am in a trade or not, if i put it inside of the checkMN code block it will still execute since it compares itself to closed orders. How do I get around this?
To clarify: Starting with a simple buy order, my goal is to adjust the stop-loss once, every time the price moves 4 pips in my favor. I have made the currentprice and SL static so it will not update every tick. The stop-loss is to remain at 8 pips away. When I get stopped out, I take a position in that direction and follow the same parameters.
I appreciate the help!