Hello i made a simple EA,
which is supposed to buy when: 22EMA is above 44SMA, 2nd and 1st previous candles were both above 22EMA, no positions are open -
and sell when: 22EMA is below 44SMA, 2nd and 1st previous candles were both below 22EMA, no positions are opened yet.
It is a basic EA for trading price bounce of moving average, sadly when i use a strategy tester no positions are opened not for a buy, not for a sell.
Can anybody help me with this problem? Code below. Thank you very much!
Hello,
First, need to put indicators and prices (High, Low) variables in OnTick() function.
Outside of OnTick function this variables get once time the value.
Inside in OnTick function, gets every tick new value.
Second, to get correct price for Stop loss and take profit, need to use this way ...
Ask - STOPAK * Point //for buy stop loss Bid + STOPAK * Point //for sell stop lossAsk + PROFIT * Point //for buy take profit Bid - PROFIT * Point //for sell take profit
double MAkratky = iMA(_Symbol,PERIOD_M5, 22, 0, MODE_EMA, PRICE_CLOSE, 0); double MAdlhy = iMA(_Symbol,PERIOD_M5, 44, 0, MODE_SMA, PRICE_CLOSE, 0);Global and static variables 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
Ahoj obchodniku123,
podle 2 řádky kódu předpokládám, že jsi slovák, takže snad Tě neurazí když Ti odpovím česky.
Některé připomínky napsali již kolegové.
Místo :
if(MAkratky > MAdlhy)
bys měl asi spíš mít:
if ( iMA(Symbol(),0, 22, 0, MODE_EMA, PRICE_CLOSE, 0) > iMA(Symbol(),0, 44, 0, MODE_SMA, PRICE_CLOSE, 0) && (další podmínka) && (další podmínka) ... ) { OrderSend.... }
To, že to je rozepsané do několika řádek je pouze kvůli přehlednosti, na věci to nic nemění. Jde o podmínku v závorkách.
Nahradil jsem PERIODU_M5 nulou "0". Protože je to univerzální a bude to fungovat na jakémkoliv TF.
Další příkazy "if" nejsou potřeba, stačí přiřadit pouze podmínky.
Důležité je, aby to bylo odkaz na funkci iMA byl v mezi závorkami funkce OnTick (přepočítává pří každém ticku).
Ale úplně nejlepší by asi bylo, kdybys nejprve v programu měl jenom příkaz OrderSend a ten aby Ti fungoval.
Teprve potom doporučuju přidávat podmínky.
Zdraví.
JO.
Thank you very much all for your answers, as you pointed out i have more mistakes in my EA than i thought so im gonna post updates tomorrow. I will try to work on all of the points you stared here.
Once again thank you.
To: JAN OPOCENSKY
Ahoj, áno trafil si to som Slovák. Veľmi ďakujem za odpoveď. Vyskúšal som to zaradenie variables pod "OnTick" a stratégia mi otvára pozície len na sell. Zajtra vyskúšam postavenie EA odznova podľa toho čo píšeš: Najskor funkčný OrderSend a potom pridávať podmienky, a vyskúšam aj AND namiesto tých IF.
Výsledky napíšem. Ešte raz ďakujem :)
- www.mql5.com
- 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 i made a simple EA,
which is supposed to buy when: 22EMA is above 44SMA, 2nd and 1st previous candles were both above 22EMA, no positions are open -
and sell when: 22EMA is below 44SMA, 2nd and 1st previous candles were both below 22EMA, no positions are opened yet.
It is a basic EA for trading price bounce of moving average, sadly when i use a strategy tester no positions are opened not for a buy, not for a sell.
Can anybody help me with this problem? Code below. Thank you very much!