checkBuy and checkSell dont work half the time...

 

Hello everyone!

I am new to mql5 programmig and after some reading and tutorials i took this article as basis for simple EA. Nothing fancy. I just used price bars to entry. After some ECN and Mqltraderequest related problems i got it work in demo and strategy tester. Now another problem encountered with checkSell and checkBuy. It seems the simple signal i programmed doesnt work always. below is the checkSell func and i attached a pic with the behavior. The white arrows are showing some of the bars, were the EA should have given a signal. Any ideas? I feel like missing something obvious, but i dont find it. There are now errors, checkSell and checkBuy just give no signal sometimes.


bool ExpertA::checkSell()
  {


   MqlRates rates[];
   ArraySetAsSeries(rates,true);

   if(CopyRates(_Symbol,_Period,0,3,rates)<0)
     {
      Alert("Error copying rates/history data - error:",GetLastError(),"!!");
   
     }

   MqlTick last_tick;
   if(!SymbolInfoTick(_Symbol,last_tick))
     {
       Print("SymbolInfoTick() failed, error = ",GetLastError());
     }
  
   double pr1_high= rates[1].high;
   double pr1_low= rates[1].low;  
   double pr2_high= rates[2].high;
   double pr2_low= rates[2].low; 

bool Buy_Condition_1=(pr1_high<pr2_high) && (pr1_low>pr2_low) && (last_tick.bid<pr1_low);
// Sell when bar 1 has higher low and lower high tha bar 2 and last tick is lower than bar 1 low
   if(Buy_Condition_1)
     {
      return(true);
     }
   else
     {
      return(false);
     }


 
Pipser:

Hello everyone!

I am new to mql5 programmig and after some reading and tutorials i took this article as basis for simple EA. Nothing fancy. I just used price bars to entry. After some ECN and Mqltraderequest related problems i got it work in demo and strategy tester. Now another problem encountered with checkSell and checkBuy. It seems the simple signal i programmed doesnt work always. below is the checkSell func and i attached a pic with the behavior. The white arrows are showing some of the bars, were the EA should have given a signal. Any ideas? I feel like missing something obvious, but i dont find it. There are now errors, checkSell and checkBuy just give no signal sometimes.

Do you check you trade conditions on every tick ?
 
void OnTick()
  {


   int Mybars=Bars(_Symbol,_Period);
   if(Mybars<60) 
     {
      Alert("We have less than 60 bars, EA will now exit!!");
      return;
     }


   MqlTick last_tick;
   MqlRates rates[];


   ArraySetAsSeries(rates,true);


   if(!SymbolInfoTick(_Symbol,last_tick))
     {
      Alert("Error getting the latest price quote - error:",GetLastError(),"!!");
      return;
     }


   if(CopyRates(_Symbol,_Period,0,3,rates)<0)
     {
      Alert("Error copying rates/history data - error:",GetLastError(),"!!");
      return;
     }


   static datetime Prev_time;

   datetime Bar_time[1];


   Bar_time[0]=rates[0].time;

   if(Prev_time==Bar_time[0])
     {
      return;
     }

   Prev_time=Bar_time[0];


   bool Buy_opened=false,Sell_opened=false;

   if(PositionSelect(_Symbol)==true)
     {
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
        {
         Buy_opened=true;  
        }
      else if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
        {
         Sell_opened=true; 
        }
     }

  
  Cexpert.setCloseprice(rates[1].close); 
   

   if(Cexpert.checkBuy()==true)
     {
      //--- do we already have an opened buy position
      if(Buy_opened)
        {
         Alert("We already have a Buy Position!!!");
         return;    // Don't open a new Buy Position
        }
      double aprice = NormalizeDouble(last_tick.ask,_Digits);              // current Ask price
    double stl    = NormalizeDouble(last_tick.ask - STP*_Point,_Digits); // Stop Loss
      double tkp    = NormalizeDouble(last_tick.ask + TKP*_Point,_Digits); // Take profit
      int    mdev   = 100;                                                    // Maximum deviation
                                                                              // place order
      Cexpert.openBuy(ORDER_TYPE_BUY,aprice,stl,tkp,mdev);
     }
//--- check for any Sell position
   if(Cexpert.checkSell()==true)
     {
     
      //--- do we already have an opened Sell position
      if(Sell_opened)
        {
         Alert("We already have a Sell position!!!");
         return;    // Don't open a new Sell Position
        }
      double bprice=NormalizeDouble(last_tick.bid,_Digits);                 // Current Bid price
   double bstl    = NormalizeDouble(last_tick.bid + STP*_Point,_Digits); // Stop Loss
      double btkp    = NormalizeDouble(last_tick.bid - TKP*_Point,_Digits); // Take Profit
      int    bdev=100;                                                         // Maximum deviation
      
                                                                               // place order
      Cexpert.openSell(ORDER_TYPE_SELL,bprice,bstl, btkp, bdev);
 //     Cexpert.takeStop(TP1,SL1, bdev);
     }

   return;
  }

I thought so, because checkSell and checkBuy called in the OnTick() function. Like above.
 

No, this is what I thought, your EA trade on new bar, and not every tick :

   static datetime Prev_time;

   datetime Bar_time[1];


   Bar_time[0]=rates[0].time;

   if(Prev_time==Bar_time[0])
     {
      return;
     }
It's better to understand a code when you copy it
 
ahh i should have seen it, but i never even considered to look there. So he just opened a position if a signal occured on a new bar, not some ticks later. You are right about the copying, too. I will write the next EA from scratch. Thanks for the help! :)
Reason: