EA works only sometimes

 

Dear Everyone!

 I am asking for your help since i wrote a code which does the following:

BUY: 4h candle falls below RSI,14=30 then on next 4 h candle on lower timeframe for example 5m everytime when 5-12 EMA crossover to up i buy TP 3 pips, SL 300 pips!

Sell: the opposite of buy (RSI,14=70)

here becomes a bit funny since i use 14 charts and 3 computer (first comp M1 timeframe all charts,2nd M5,3rd M15) so 3*14=42 all same EA i wrote and same account only one!

i check triple time everything but just cant seem to find the problem! which is that on M5 and M15 the signals sometimes missed (no opened order, and no printed anything only "new candle found" (check code))! yes only sometimes not always! 

Pls help me!

input double Pips=30;
input double StopLossPips=3000;
input double DigitsNum=5;
input ENUM_TIMEFRAMES TimeFrame=PERIOD_M1;

double LastBarData;
double SLPips;
double PricePips;

int OnInit()
{
   PricePips=Pips/pow(10,DigitsNum);
   SLPips=StopLossPips/pow(10,DigitsNum);
   LastBarData=iMA(NULL,TimeFrame,5,0,MODE_EMA,PRICE_CLOSE,1);
   return(INIT_SUCCEEDED);
}

void start()
{
   RefreshRates();
   if(LastBarData!=iMA(NULL,TimeFrame,5,0,MODE_EMA,PRICE_CLOSE,1))
   {
      Print("New Candle!");
      if(iMA(NULL,TimeFrame,5,0,MODE_EMA,PRICE_CLOSE,1)>iMA(NULL,TimeFrame,12,0,MODE_EMA,PRICE_CLOSE,1) &&
         iMA(NULL,TimeFrame,5,0,MODE_EMA,PRICE_CLOSE,2)<iMA(NULL,TimeFrame,12,0,MODE_EMA,PRICE_CLOSE,2) &&
         iRSI(NULL,PERIOD_H4,14,PRICE_CLOSE,1)<30)
      {
         Print("Buy Order");
         if(OrderSend(Symbol(),OP_BUY,szorzo()*0.01,Ask,5,Bid-SLPips,Ask+PricePips,NULL,0,0,clrNONE))
         {
            Print("Buy Order Sent 4h=",iTime(NULL,PERIOD_H4,1));
         }
         else
         {
            Print("Buy Error: ",GetLastError());
         }
      }
      if(iMA(NULL,TimeFrame,5,0,MODE_EMA,PRICE_CLOSE,1)<iMA(NULL,TimeFrame,12,0,MODE_EMA,PRICE_CLOSE,1) &&
         iMA(NULL,TimeFrame,5,0,MODE_EMA,PRICE_CLOSE,2)>iMA(NULL,TimeFrame,12,0,MODE_EMA,PRICE_CLOSE,2) &&
         iRSI(NULL,PERIOD_H4,14,PRICE_CLOSE,1)>70)
      {
         Print("Sell Order");
         if(OrderSend(Symbol(),OP_SELL,szorzo()*0.01,Bid,5,Ask+SLPips,Bid-PricePips,NULL,0,0,clrNONE))
         {
            Print("Sell Order Sent 4h=",iTime(NULL,PERIOD_H4,1));
         }
         else
         {
            Print("Sell Error: ",GetLastError());
         }
      }
      LastBarData=iMA(NULL,TimeFrame,5,0,MODE_EMA,PRICE_CLOSE,1);
   }
}

int szorzo() //Multiplier
{
   RefreshRates();
   double balance=AccountBalance();
   int num=0;
   while(balance>200)
   {
      balance-=200;
      num++;
   }
   if(num==0)
   {
      num++;
   }
   return num;
}
 
i check triple time everything but just cant seem to find the problem! which is that on M5 and M15 the signals sometimes missed (no opened order, and no printed anything only "new candle found" (check code))! yes only sometimes not always! 

I can't spot anything.

I assume that you are resetting the input


input ENUM_TIMEFRAMES TimeFrame=PERIOD_M1;


for M5 or M15?
 
Keith Watford:

I can't spot anything.

I assume that you are resetting the input


input ENUM_TIMEFRAMES TimeFrame=PERIOD_M1;


for M5 or M15?

Yes, actually when i put on different computer which is going with M5 or M15 i rewrite the default  input so i make sure not to give bad input! Of course i leave everything else as it is.

the funny fact is that this happening only on M5 and M15 and only sometimes....... 

 

Anyway thank you for trying i hope someone can find what both of us missed :( 

 
Since i put a few Print function in the code, i saw when the code should have place order it dont get past the IF! so when i see signal (via eyes :D) the code just print NEW CANDLE but no BUY/SELL ORDER or ERROR, which leads to the conclusion that SOMEHOW SOMETIMES the IF is not good even though i have signal....
 
Onarius Mandel:
Since i put a few Print function in the code, i saw when the code should have place order it dont get past the IF! so when i see signal (via eyes :D) the code just print NEW CANDLE but no BUY/SELL ORDER or ERROR, which leads to the conclusion that SOMEHOW SOMETIMES the IF is not good even though i have signal....
Print your values (MA, RSI...) and check why the conditions are not met.
 
Alain Verleyen:
Print your values (MA, RSI...) and check why the conditions are not met.
Thank you i wrote in the code, i'm waiting now for a signal that didnt happen so far nothing (i mean all signal caught normally) hopefully it will give some answers but still what gives me headache that why just sometimes, i mean if something would be wrong with the code than all signals would be lost isnt it? or im missing some knowledge here?
 
Onarius Mandel:
Thank you i wrote in the code, i'm waiting now for a signal that didnt happen so far nothing (i mean all signal caught normally) hopefully it will give some answers but still what gives me headache that why just sometimes, i mean if something would be wrong with the code than all signals would be lost isnt it? or im missing some knowledge here?
You are missing something ;-)
 
Alain Verleyen:
You are missing something ;-)
Then pls enlighten me since i dont know what im missing! i would like to learn to be better!
 

- Try with less charts to make sure this is not because the PC is lagging. EAs don't always open when the terminal is frozen.

- Check deviation in Tools/Options/Trade.

- Is it sure that MT4 indicators always return values without error? I doubt and would change code for any EMPTY_VALUE. Anyway if you print indicator values you will find it.
 
Does he need to explicitly call RefreshRates() in start ? Aren't they already refreshed by the time start is called ? Maybe with the multiple charts this is causing race conditions.
 
Frederic Metraux:

- Try with less charts to make sure this is not because the PC is lagging. EAs don't always open when the terminal is frozen.

- Check deviation in Tools/Options/Trade.

- Is it sure that MT4 indicators always return values without error? I doubt and would change code for any EMPTY_VALUE. Anyway if you print indicator values you will find it.

Thank you for help, In the options i have default which is LAST USED VALUE (i translated myself i dont know if in egnlish they use this phrase since i have different language mt4), maybe this has to do with pc lagg since everyday  since problems i restart pc-s and since that i dont have any problems (even i wrote in the code that shows me indicators value but so far everything okay)!

Actually i find a "bug" on M1 chart after weekend should have placed order (few min after weekend) but i just check in morning and after many value the EA diary (still dont know if in english thats how its called) gets deleted so i just see back to around 1 hour of M1 data (14*60 row of Print it gets deleted :( )

Reason: