Please use the CODE button (Alt-S) when inserting code in your posts.
No, don't ever do that! OnTick() is an event handler that is called by the terminal when a New Tick event occurs.
It should never be called directly by your code. Learn to correctly structure your code logic and handle the terminal's events properly.
Nothing is going to change inside the event handler. You must note your condition, return and wait for a new tick, and then check for the other condition.
Multiple conditions for taking a trade. - Expert Advisors and Automated Trading - MQL5 programming forum (2023)
Nothing is going to change inside the event handler. You must note your condition, return and wait for a new tick, and then check for the other condition.
Multiple conditions for taking a trade. - Expert Advisors and Automated Trading - MQL5 programming forum (2023)
Hi sir, i tried this one but, it did not work. can you help me with this. thanks you and i really appreciate it.
//curent chart, current period, 50 candles,no shift, simple, close price double SlowMovingAverage15 = iMA(NULL, 1,20,0,MODE_EMA,PRICE_CLOSE,0); //curent chart, current period, 50 candles,no shift, simple, close price double LastSlowMovingAverage15 = iMA(NULL, 1,20,0,MODE_EMA,PRICE_CLOSE,1); //curent chart, current period, 10 candles,no shift, simple, close price double FastMovingAverage15 = iMA(NULL, 1,10,0,MODE_EMA,PRICE_CLOSE,0); //curent chart, current period, 10 candles,no shift, simple, close price double LastFastMovingAverage15 = iMA(NULL, 1,10,0,MODE_EMA,PRICE_CLOSE,1); //curent chart, current period, 50 candles,no shift, simple, close price double SlowMovingAverage = iMA(NULL, 1,8,0,MODE_EMA,PRICE_CLOSE,0); //curent chart, current period, 50 candles,no shift, simple, close price double LastSlowMovingAverage = iMA(NULL, 1,8,0,MODE_EMA,PRICE_CLOSE,1); //curent chart, current period, 10 candles,no shift, simple, close price double FastMovingAverage = iMA(NULL, 1,4,0,MODE_EMA,PRICE_CLOSE,0); //curent chart, current period, 10 candles,no shift, simple, close price double LastFastMovingAverage = iMA(NULL, 1,4,0,MODE_EMA,PRICE_CLOSE,1); bool firstpart_buy = LastFastMovingAverage15 < LastSlowMovingAverage15 && FastMovingAverage15 > SlowMovingAverage15 ; bool firstpart_sell = LastFastMovingAverage15 > LastSlowMovingAverage15 && FastMovingAverage15 < SlowMovingAverage15 ; bool secondpart_buy = LastFastMovingAverage > LastSlowMovingAverage && FastMovingAverage < SlowMovingAverage ; bool secondpart_sell = LastFastMovingAverage < LastSlowMovingAverage && FastMovingAverage > SlowMovingAverage ; void OnTick() { enum State {idle, c1b,c1s,c2b,c2s}; static State state = idle; if (firstpart_buy == true) { if(state == idle ) state = c2b; } if (firstpart_sell == true) { if(state == idle) state = c2s; } if (secondpart_buy == true) { if(state == c2b) close_buy(); if(state == idle) return; } if (secondpart_sell == true) { if(state == c2s) close_sell(); if(state == idle) return; } } void close_buy() { for (int i= OrdersTotal(); i>=0; i--) { if (OrderSelect(i, SELECT_BY_POS)== true) //if (OrderSymbol() == Symbol()) OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 0, Green ); } } void close_sell() { for (int c= OrdersTotal(); c>=0; c--) { if (OrderSelect(c, SELECT_BY_POS)== true) //if (OrderSymbol() == Symbol()) OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 0, Red ); } }
Your entire first section should not be in the global scope. It should be inside the OnTick() event handler, or in some function called from within OnTick().
You were doing it correctly before, so why did you now decide to put it in the global scope?
EDIT: I suggest you take some to first learn the basics of programming, for example in a language like C/C++.
void OnTick() { //curent chart, current period, 50 candles,no shift, simple, close price double SlowMovingAverage15 = iMA(NULL, 1,20,0,MODE_EMA,PRICE_CLOSE,0); //curent chart, current period, 50 candles,no shift, simple, close price double LastSlowMovingAverage15 = iMA(NULL, 1,20,0,MODE_EMA,PRICE_CLOSE,1); //curent chart, current period, 10 candles,no shift, simple, close price double FastMovingAverage15 = iMA(NULL, 1,10,0,MODE_EMA,PRICE_CLOSE,0); //curent chart, current period, 10 candles,no shift, simple, close price double LastFastMovingAverage15 = iMA(NULL, 1,10,0,MODE_EMA,PRICE_CLOSE,1); //curent chart, current period, 50 candles,no shift, simple, close price double SlowMovingAverage = iMA(NULL, 1,8,0,MODE_EMA,PRICE_CLOSE,0); //curent chart, current period, 50 candles,no shift, simple, close price double LastSlowMovingAverage = iMA(NULL, 1,8,0,MODE_EMA,PRICE_CLOSE,1); //curent chart, current period, 10 candles,no shift, simple, close price double FastMovingAverage = iMA(NULL, 1,4,0,MODE_EMA,PRICE_CLOSE,0); //curent chart, current period, 10 candles,no shift, simple, close price double LastFastMovingAverage = iMA(NULL, 1,4,0,MODE_EMA,PRICE_CLOSE,1); bool firstpart_buy = LastFastMovingAverage15 < LastSlowMovingAverage15 && FastMovingAverage15 > SlowMovingAverage15 ; bool firstpart_sell = LastFastMovingAverage15 > LastSlowMovingAverage15 && FastMovingAverage15 < SlowMovingAverage15 ; bool secondpart_buy = LastFastMovingAverage > LastSlowMovingAverage && FastMovingAverage < SlowMovingAverage ; bool secondpart_sell = LastFastMovingAverage < LastSlowMovingAverage && FastMovingAverage > SlowMovingAverage ; enum State {idle, c1b,c1s,c2b,c2s}; static State state = idle; if (firstpart_buy == true) { if(state == idle ) state = c2b; } if (firstpart_sell == true) { if(state == idle) state = c2s; } if (secondpart_buy == true) { if(state == c2b) { for (int i= OrdersTotal(); i>=0; i--) { if (OrderSelect(i, SELECT_BY_POS)== true) //if (OrderSymbol() == Symbol()) OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 0, Green ); } } if(state == idle) return; } if (secondpart_sell == true) { if(state == c2s) { for (int c= OrdersTotal(); c>=0; c--) { if (OrderSelect(c, SELECT_BY_POS)== true) //if (OrderSymbol() == Symbol()) OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 0, Green ); } } if(state == idle) return; } }
double FastMovingAverage15 = iMA(NULL, 1,10,0,MODE_EMA,PRICE_CLOSE,0); double LastFastMovingAverage15 = iMA(NULL, 1,10,0,MODE_EMA,PRICE_CLOSE,1); double SlowMovingAverage = iMA(NULL, 1,8,0,MODE_EMA,PRICE_CLOSE,0); double LastSlowMovingAverage = iMA(NULL, 1,8,0,MODE_EMA,PRICE_CLOSE,1); double FastMovingAverage = iMA(NULL, 1,4,0,MODE_EMA,PRICE_CLOSE,0); double LastFastMovingAverage = iMA(NULL, 1,4,0,MODE_EMA,PRICE_CLOSE,1); bool firstpart_buy = LastFastMovingAverage15 < LastSlowMovingAverage15 && FastMovingAverage15 > SlowMovingAverage15 ; bool firstpart_sell = LastFastMovingAverage15 > LastSlowMovingAverage15 && FastMovingAverage15 < SlowMovingAverage15 ; bool secondpart_buy = LastFastMovingAverage > LastSlowMovingAverage && FastMovingAverage < SlowMovingAverage ; bool secondpart_sell = LastFastMovingAverage < LastSlowMovingAverage && FastMovingAverage > SlowMovingAverage ;
Those are not assignments; they are initialization of a common (globally declared), or static variable(s) 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 indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), 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.
- A 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)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello everyone. I'm sorry if i ask questions so basic I'm new to this. But i just want to know it this is possible. I actually want , one ma cross, and then wait for another ma to cross then close the trade. Thank you everyone in advance.