StdDev based EA triggers one candle too late

 

Hello everyone,

I ran into a problem that I cannot really explain, so I hope someone is able to explain to me where my line of thought is wrong. I have written a draft version of an Expert Advisor, that is based on Standard Deviation (Entry-Trigger) and ADX (Direction-Trigger for Buy/Sell). However, my EA does work the way I want it to, but simply enters one candle too late. This can be seen here:

Candle

The basic idea is, that if the (Standard Deviation of the current period) > (Standard Deviation of last period * Multiplier), then the EA opens a position into the direction of what is higher of ADX (so if PLUSDI > MINUSDI in the current period, then it opens a BUY and vice versa). Now I don't see where I went wrong in my coding, but I assume I have a logical mistake because I only allow the EA to act once every time a new candle is opened. I first thought it is because it was because of the Multiplier setting, but I tested several settings and it didn't change anything.

StdDev values in the picture are as follows (starting within the red circle):

Small Black Candle: 0.0008

Large White Candle: 0.0018

Entry Candle: 0.0030


I obviously want it to already enter on the large white candle, but I cannot get it to do so. Maybe I missed something in my indicator settings or the "only when new candle" part screws it up? The code is as below:

//+------------------------------------------------------------------+
//|                                                    StdDev_v2.mq4 |
//|                                                            Chris |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Chris"
#property link      ""
#property version   "2.00"

// Global Parameters
int           PO              = 0;

// Parameter definitions
extern double Multiply     = 1.5;      // needs to be bigger than 1 = increasing volatility
extern double ADX_Period   = 2;

extern double MagicNumber  =  1111;
extern double Slippage     =  10;
extern double Lots         =  1.0;
extern double StopLoss     =  200;
extern double TakeProfit   =  300;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   static datetime timeCur; datetime timePre = timeCur; timeCur = Time[0];
   bool isNewBar = timeCur != timePre;
   
   if(isNewBar)
   {
   // only on new Bar
   RefreshRates();
   
   // Indicator Setup
   double Std_Cur   = iStdDev(NULL,0,10,0,MODE_SMA,PRICE_OPEN,0);
   double Std_Old   = iStdDev(NULL,0,10,0,MODE_SMA,PRICE_OPEN,1);

   double ADX_Plus  = iADX(NULL,0,ADX_Period,PRICE_CLOSE,MODE_PLUSDI,0);
   double ADX_Minus = iADX(NULL,0,ADX_Period,PRICE_CLOSE,MODE_MINUSDI,0);
   
   // BUY Signal
   if(Std_Cur>(Std_Old*Multiply) && ADX_Plus>ADX_Minus && OrdersTotal()==0)
      PO = OrderSend(NULL,OP_BUY,Lots,Ask,Slippage,Ask-StopLoss*Point,Ask+(TakeProfit*Point),"BUY",MagicNumber,0,Green);
      if(PO>0)
         Print("Buy Order was opened");
         else Print(GetLastError());
         
   // SELL Signal
   if(Std_Cur>(Std_Old*Multiply) && ADX_Minus>ADX_Plus && OrdersTotal()==0)
      PO = OrderSend(NULL,OP_SELL,Lots,Bid,Slippage,Bid+StopLoss*Point,Bid-(TakeProfit*Point),"SELL",MagicNumber,0,Red);
      if(PO>0)
         Print("Sell Order was opened");
         else Print(GetLastError());
   
   }
   
   // exery tick

  }


Most likely it is an obvious mistake that I am simply not able to see. Hope someone can explain to me what I seem to be missing.

Thank you very much!

- Chris


(I noticed I might have posted this accidently in the wrong section. If so, can one of the mods please be so kind to move it to the right one? Sorry and thanks!)

 

You are using PRICE_OPEN to get standard deviation value.

   double Std_Cur   = iStdDev(NULL,0,10,0,MODE_SMA,PRICE_OPEN,0);
   double Std_Old   = iStdDev(NULL,0,10,0,MODE_SMA,PRICE_OPEN,1);


Check if the StdDev indicator on the image is set to use open price and then verify conditions.

 
GermanInChina:
 StdDev values in the picture are as follows (starting within the red circle):

Small Black Candle: 0.0008

Large White Candle: 0.0018

Entry Candle: 0.0030

I think your problem is checking just on opening a new candle. I'm sure that the value of StdDev on Open[Large White Candle] isn't 0.0018 but less. You have to decide if you check values only on opening a new candle or continuously.

As @Drazen Penic said you are using PRICE_OPEN and it's a little bit strange.

Reason: