candle Moving Average

To add comments, please log in or register
Abubakar Saidu
1026
Abubakar Saidu  

Hello everyone

 coding for candle MA..

but the problem is the EA will not place trade at next candle open.. it places the trades lately

my code

double m2=iMA(Symbol(),0,MA_Period,0,MA_Method,MA_Price,2); 
double m1=iMA(Symbol(),0,MA_Period,0,MA_Method,MA_Price,1); 
double m0=iMA(Symbol(),0,MA_Period,0,MA_Method,MA_Price,0); 
bool buy_entry=Low[1]<Low[2]&&Low[2]>m2&&Low[1]<=m1&&Low[0]>Low[1]&&High[0]>High[1]&&Low[0]>m0;
bool sell_entry=High[1]>High[2]&&High[2]<m2&&High[1]>=m1&&Low[0]<Low[1]&&High[0]<High[1]&&High[0]<m0;

Mehmet Bastem
1780
Mehmet Bastem  
Abubakar Saidu:

Hello everyone

i succeeded in coding for if candle rejects MA..

but the problem is the EA will not place trade at next candle open.. it places the trades lately

my code

the problem


bool NewBar()
  {
   static datetime lastbar;
   datetime curbar=Time[0];
   if(lastbar!=curbar)
     {
      lastbar=curbar;
      return (true);
     }
   else
     {
      return(false);
     }
  }
double m2=iMA(Symbol(),0,MA_Period,0,MA_Method,MA_Price,2); 
double m1=iMA(Symbol(),0,MA_Period,0,MA_Method,MA_Price,1); 
double m0=iMA(Symbol(),0,MA_Period,0,MA_Method,MA_Price,0); 

bool buy_entry=Low[1]<Low[2]&&Low[2]>m2&&Low[1]<=m1&&Low[0]>Low[1]&&High[0]>High[1]&&Low[0]>m0;
bool sell_entry=High[1]>High[2]&&High[2]<m2&&High[1]>=m1&&Low[0]<Low[1]&&High[0]<High[1]&&High[0]<m0;

if(NewBar() &&  buy_entry) // Ordersend Buy 
if(NewBar() &&  sell_entry) // Ordersend Sell

code 

William Roeder
20249
William Roeder  
You can not call NewBar() more than once per tick.
Abubakar Saidu
1026
Abubakar Saidu  

Hi, thanks to you all forreplying.

i tried the above code.. sometimes work and sometimes not

William Roeder
20249
William Roeder  
  1. "Doesn't work" is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
  2. You haven't posted code that does anything. Do you really expect an answer? We can't see your broken code. There are no mind readers here and our crystal balls are cracked.
tomm73
1404
tomm73  
Mehmet Bastem:

code 


Try this:

...

bool new_bar = NewBar();

if(new_bar &&  buy_entry) // Ordersend Buy 
if(new_bar &&  sell_entry) // Ordersend Sell
tomm73
1404
tomm73  
Abubakar Saidu:

hahahaa....

my mistake.. forgot to paste the code

Yes - it works if you buffer the result of NewBar. You can call it only once per bar, the second time it will go to "false". But you call it twice, so you need to buffer the first result.

Abubakar Saidu
1026
Abubakar Saidu  

yes it works..

back-test for 5 months only a single trade opened at candle open

without new_bar many of trades opened late

if(com_buy==true){buy=1;}
if(com_sell==true){sell=1;}}

only a single trade, at candle open

bool com_buy=false, com_sell=false;
bool new_bar = NewBar();
buy=0
//entry conditions
if(adx_sell1 && adx_sell2)adxsell=true; 
if(adx_buy1 && adx_buy2)adxbuy=true;
if(buy_entry && adxbuy=true)com_buy=true;
if(sell_entry && adxsell=true)com_sell=true;

//----
if(com_buy==true  && new_bar){buy=1;}
if(com_sell==true && new_bar){sell=1;}}
William Roeder
20249
William Roeder  
bool com_buy=false, com_sell=false;

if(buy_entry)com_buy=true;
if(sell_entry)com_sell=true;
if(adx_sell1 && adx_sell2)adxsell=true; //entry conditions
if(adx_buy1 && adx_buy2)adxbuy=true;
//----
if(com_buy==true && adxbuy==true && NewBar()){buy=1;break;}
if(com_sell==true && adxsell==true && NewBar()){sell=1;break;}}
  1. Simplify your code,
    bool com_buy=buy_entry;
    bool com_sell=sell_entry;
    remove the redundant variables.
  2. adx_sell1, adx_sell2, adx_buy1, adx_buy2 are doubles not booleans. Since they will never be exactly zero, the if statements will always be true, and adxsell, adxbuy will be also.
  3. I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 programming forum

    Your sell=1 will never be called.
  4. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
Abubakar Saidu
1026
Abubakar Saidu  
William Roeder:
  1. Simplify your code, remove the redundant variables.
  2. adx_sell1, adx_sell2, adx_buy1, adx_buy2 are doubles not booleans. Since they will never be exactly zero, the if statements will always be true, and adxsell, adxbuy will be also.
  3. I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 programming forum

    Your sell=1 will never be called.
  4. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

Thats it so great.. Thanks to you all for your feedback..

:)

To add comments, please log in or register