EA open order with wrong logic at week opening at 00:00 Time

 

Here's my story, I trade with specific broker (Alpari UK) only

At opening trading time this week (monday at 00:00), my EA open position with very wrong logic..

 here's the screenshot..

Wrong logic at opening trading time 

my EA logic is to open BUY, if price open below the lower band of PREVIOUS day Bollinger Band (BLUE BAND), open SELL, if price open above the upper band of PREVIOUS day Bollinger Band (Blue Band)..

 as u can see the price open at the middle of the band, and my EA initiate SELL.. Anybody know why this happen??

This is my EA code

//+------------------------------------------------------------------+

//|                                       Experts Experiment 1.0.mq4 |

//|                                      Pandapika (Christian Young) |

//|                                              Pandapika@gmail.com |

//+------------------------------------------------------------------+

#property copyright "Pandapika (Christian Young)"

#property link      "Pandapika@gmail.com"

#property version   "1.00"

#property strict

//order input

input double   LotSize=0.02;

input  int     TP =345;

input  int     Spread = 25;

//Open Band

double PreviousOpenUpperBand;

double PreviousOpenMovingAverage;

double PreviousOpenLowerBand;

extern int    OpenBandsPeriod=4;

extern int    OpenBandsShift=0;

extern double OpenBandsDeviations=1.33;

extern int    MA_Method=3;

//Close Band

double PreviousCloseUpperBand;

double PreviousCloseMovingAverage;

double PreviousCloseLowerBand;

extern int    CloseBandsPeriod=3;

extern int    CloseBandsShift=0;

extern double CloseBandsDeviations=3.58;

extern int    CloseBandsAppliedTo=0;

//General properties

int TicketNumber;

bool IsNewBar;

datetime LastBarOpenAt;

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

  {

string  MessageBox=("Terima Kasih Pandapika EURUSD 1.1 (^.^)");

   return(INIT_SUCCEEDED);

  }

  

//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {   

  }

  

//+------------------------------------------------------------------+

//| OnTick function                                                  |

//+------------------------------------------------------------------+

void OnTick()

{

   if(LastBarOpenAt == Time[0]) 

    {                            

     LastBarOpenAt = Time[0];

     IsNewBar=false;              

    }

  else

    {

      LastBarOpenAt = Time[0];

      IsNewBar = true;

      Alert ("Candle baru telah terbentuk \njangan lupa matikan AutoTrading sebelum pindah \ntimeframe atau ketika mematikan metatrader");    

    } 

if (IsNewBar) CheckForBBTrade(); // jika isnewbar true maka check for BB trade

}

   

void CheckForBBTrade ()  

   {

   PreviousOpenUpperBand      =NormalizeDouble(iCustom(NULL,0,"Bands with changes 2",OpenBandsPeriod,OpenBandsShift,OpenBandsDeviations,MA_Method,1,1),5);

   PreviousOpenMovingAverage  =NormalizeDouble(iCustom(NULL,0,"Bands with changes 2",OpenBandsPeriod,OpenBandsShift,OpenBandsDeviations,MA_Method,0,1),5);

   PreviousOpenLowerBand      =NormalizeDouble(iCustom(NULL,0,"Bands with changes 2",OpenBandsPeriod,OpenBandsShift,OpenBandsDeviations,MA_Method,2,1),5);

   

   if (iOpen(NULL,0,0)<PreviousOpenLowerBand)OrderEntry(0);    

   if (iOpen(NULL,0,0)>PreviousOpenUpperBand)OrderEntry(1);

   }   

void OrderEntry (int direction)

   {

   

   PreviousCloseUpperBand      =NormalizeDouble(iBands(NULL,0,CloseBandsPeriod,CloseBandsDeviations,CloseBandsShift,CloseBandsAppliedTo,1,1),5);

   PreviousCloseMovingAverage  =NormalizeDouble(iBands(NULL,0,CloseBandsPeriod,CloseBandsDeviations,CloseBandsShift,CloseBandsAppliedTo,0,1),5);

   PreviousCloseLowerBand      =NormalizeDouble(iBands(NULL,0,CloseBandsPeriod,CloseBandsDeviations,CloseBandsShift,CloseBandsAppliedTo,2,1),5);

   

   if (direction==0)

      if (OrdersTotal()<6)

         TicketNumber=OrderSend(Symbol(),OP_BUY,LotSize,Ask,7,0,0,NULL,0,0,Green);

         if (TicketNumber>-1)

            {

            if (OrderSelect(TicketNumber,SELECT_BY_TICKET,MODE_TRADES)==true)

               {

               Print ("Order Selected",OrderOpenPrice());

               if (PreviousCloseUpperBand<(iOpen(NULL,0,0)+(TP*Point)))    

                  {

                  TicketNumber=OrderModify(TicketNumber,OrderOpenPrice(),0,PreviousCloseUpperBand,0,Green);

                  }

               if (PreviousCloseUpperBand>(iOpen(NULL,0,0)+(TP*Point)))

                  {

                  TicketNumber=OrderModify(TicketNumber,OrderOpenPrice(),0,(iOpen(NULL,0,0)+(TP*Point)),0,Green);

                  }   

               }

               else

               Print ("Order Select get error",GetLastError());   

            }   

         

   if (direction==1)

      if (OrdersTotal()<6)

         TicketNumber=OrderSend(Symbol(),OP_SELL,LotSize,Bid,7,0,0,NULL,0,0,Red);

         if (TicketNumber>-1)

            {

            if (OrderSelect(TicketNumber,SELECT_BY_TICKET,MODE_TRADES)==true)

               {

               Print ("Order Selected",OrderOpenPrice());

               if (PreviousCloseLowerBand<(iOpen(NULL,0,0)-(TP*Point)+(Spread*Point)))

                  {

                  TicketNumber=OrderModify(TicketNumber,OrderOpenPrice(),0,(iOpen(NULL,0,0)-(TP*Point)+(Spread*Point)),0,Green);

                  }

               if (PreviousCloseLowerBand>(iOpen(NULL,0,0)-(TP*Point)+(Spread*Point)))

                  {

                  TicketNumber=OrderModify(TicketNumber,OrderOpenPrice(),0,PreviousCloseLowerBand,0,Green);

                  }   

               }

               else

               Print ("Order Select get error",GetLastError());   

            }

             

         

         if( TicketNumber > 0 )

         {

         Print("Order placed # ", TicketNumber);

         }

         else

         {

         Print("Order Send failed, error # ", GetLastError() );

         }

   }

  

//+------------------------------------------------------------------+

 
 

It could be that after a new start of the terminal or the EA (after weekend, after upgrade) OnTick() didn't have all the values it needs.

In this case your BB-bands might have returned EMPTY_VALUE (=2147483647, default for not yet calculated and not to be drawn)

and your if (..)   becomes true:

 if (iOpen(NULL,0,0)<PreviousOpenLowerBand)OrderEntry(0);


In order not to be trapped that way I use:

bool IsIni = false;
void OnTick(){
   if ( !IsIni ) {
       int o = OrdersHistoryTotal();
       if ( o>0 ) IsIni = true;
       else return;
   }
   ...
}


BTW you use for direction 0 for sell and 1 for buy, it could be easier for you read your code if you use predefined numbers: OP_BUY and OP_SELL.

 

mr. gooly, THANK YOU SO MUCH for ur reply. . but I still don't understand ur logic. . this is the characteristic of the EA anomaly I talk about:

1. it happen only in EA with custom made Bollinger Bands (not the standard MT4 B. Bands)

2. it only happen at the opening of the week (Monday at 00:00)

3. my broker is Alpari UK.

4.  it only open SELL position

5. it happen 3x in 2 weeks. .

 

I don't understand. . I use PREVIOUS day Bol. band. . it's already not moving. . it's value is DEAD how can my EA make wrong logic. . (P.S. I use custom made Weighted Bollinger Bands). .

 

Have you checked the values of you  "Bands with changes 2.mq4" at those moments?

May be they are  'getting mad' due to a bigger weekend gap?

Print them out or look at the chart.
 
gooly:

Have you checked the values of you  "Bands with changes 2.mq4" at those moments?

May be they are  'getting mad' due to a bigger weekend gap?

Print them out or look at the chart.

How can I print it?? it only happen at milisecond around 00:00 - 00:01. . .

 I have question, is the Metaquotes Bands built-in did not make the same mistake at weekend??

 

if so, what part of the code that did this>? (MQL4 code that make iBands didn't behave strangely at week opening?) 

 
Pandapika:

How can I print it?? it only happen at milisecond around 00:00 - 00:01. . .

 I have question, is the Metaquotes Bands built-in did not make the same mistake at weekend??

 

if so, what part of the code that did this>? (MQL4 code that make iBands didn't behave strangely at week opening?) 

Please don't open several topics on the same thing. I removed your other ones.
 
Pandapika:

How can I print it?? it only happen at milisecond around 00:00 - 00:01. . .

   if ( hour()==0  && Minute() == 0  && Seconds()< 10 ) Print(..);
   if ( hour()==23 && Minute() == 50 && Seconds()> 50 ) Print(..);
This way you can catch every Tick around midnight and print what you need..
Reason: