Help with simple EA

To add comments, please log in or register
obchodnik123
16
obchodnik123  

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!

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);
input double PIPS = 5.00;
input double PROFIT = 30;
input double  STOPAK = 30;

double High1 = iHigh(NULL,PERIOD_M5,1);
double High2 = iHigh(NULL,PERIOD_M5,2);
double Low1 = iLow(NULL,PERIOD_M5,1);
double Low2 = iLow(NULL,PERIOD_M5,2);


void OnTick()

{
 
  if(MAkratky > MAdlhy)
   if((Low2 && High2) > MAkratky)
    if((Low1 && High1) > MAkratky)
     if(Ask < (MAkratky + PIPS))
      if(OrdersTotal() == 0)      
     
  {   
  int buyticket = OrderSend (Symbol (),OP_BUY, 1.00, Ask, 5,(Ask - STOPAK),(Ask + PROFIT), NULL,0,0,Green);
 } 

  
   if(MAkratky < MAdlhy)
     if((Low2 && High2) < MAkratky)
       if((Low1 && High1) < MAkratky)
        if((MAkratky + PIPS) < Bid)
         if(OrdersTotal() ==0)  
         
               
    {
       int sellticket = OrderSend (Symbol (),OP_SELL, 1.00, Bid, 5,(Bid + STOPAK),(Bid - PROFIT),NULL,0,0,Red);  
     }

 }
  
  
 
 
   
Nikolaos Pantzos
39642
Nikolaos Pantzos  
obchodnik123:

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 loss

Ask + PROFIT * Point //for buy take profit
Bid - PROFIT * Point //for sell take profit


William Roeder
22645
William Roeder  
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++.
  1. They are initialized once on program load.
  2. They don't update unless you assign to them.
  3. 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:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
  4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
              external static variable - MQL4 programming forum
JAN OPOCENSKY
4700
JAN OPOCENSKY  

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.

obchodnik123
16
obchodnik123  

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 :)

JAN OPOCENSKY
JAN OPOCENSKY
  • www.mql5.com
Published code Power of USD Power of USD indicator shows actual strength of currency USD calculated of 7 pairs that contain USD. Indicator can be used for strategies based on mean reversion and/or correlation. Included pairs: EUR USD, AUD USD, NZD USD, GBP USD, USD JPY, USD CAD, USD CHF. Published code Power of NZD Power of NZD indicator...
William Roeder
22645
William Roeder  
Please post only in English on this forum. Use the automatic translation tool if needed. Use simple language structure when using mechanical translation.
To add comments, please log in or register