Download MetaTrader 5

MT4 Indicator - Same arrow being displayed when they should vary

To add comments, please log in or register
imamushroom
157
imamushroom  

Hi,

 I'm learning to write indicators and I've a problem which I don't understand.

 The indicator should display an green up or red down arrow depending on certain conditions but instead is displaying all red down arrows. The different conditions are being met as per the printed output but I don't understand why I'm not getting the desired results. I'm sure it's something simple.

 Can you help?

 

 Here's my code:


#property strict


#property indicator_chart_window

#property indicator_buffers 4

#property indicator_color1 Green

#property indicator_color2 Red

#property indicator_color3 DarkBlue

#property indicator_color4 Orange

#property indicator_width1 1

#property indicator_width2 1

#property indicator_width3 2

#property indicator_width3 2


extern int Fast_DEMA_period=4;

extern int Slow_DEMA_period=9;


//--- indicator buffer

double ExtUpArrowBuffer[];

double ExtDownArrowBuffer[];

double ExtDemaFast[];

double ExtDemaSlow[];

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

//| Custom indicator initialization function                         |

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

int OnInit(void)

  {

   string short_name;

   int    draw_begin=0;

//--- indicator short name

   short_name="BO_BuySell DEMA("+IntegerToString(Fast_DEMA_period)+") ("+IntegerToString(Slow_DEMA_period)+")";

   IndicatorShortName(short_name);

   IndicatorDigits(Digits);

   SetIndexStyle(0,DRAW_ARROW);

   SetIndexShift(0,0);

   SetIndexDrawBegin(0,draw_begin);

   SetIndexArrow(0,233);

   SetIndexBuffer(0,ExtUpArrowBuffer);

   SetIndexStyle(1,DRAW_ARROW);

   SetIndexShift(1,0);

   SetIndexDrawBegin(1,draw_begin);

   SetIndexArrow(1,234);

   SetIndexBuffer(1,ExtDownArrowBuffer);

   SetIndexStyle(2,DRAW_LINE);

   SetIndexShift(2,0);

   SetIndexDrawBegin(2,draw_begin);

   SetIndexBuffer(2,ExtDemaFast);

   SetIndexStyle(3,DRAW_LINE);

   SetIndexShift(3,0);

   SetIndexDrawBegin(3,draw_begin);

   SetIndexBuffer(3,ExtDemaSlow);

//--- initialization done

   return(INIT_SUCCEEDED);

  }

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

//|  Moving Average                                                  |

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

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

//--- check for bars count

   if(rates_total<0)

      return(0);

//--- counting from 0 to rates_total

   ArraySetAsSeries(ExtUpArrowBuffer,false);

   ArraySetAsSeries(ExtDownArrowBuffer,false);

   ArraySetAsSeries(close,false);

   ArraySetAsSeries(open,false);

//--- first calculation or number of bars was changed

   if(prev_calculated==0)

   {

      ArrayInitialize(ExtUpArrowBuffer,0);

      ArrayInitialize(ExtDownArrowBuffer,0);

   }

   int i,limit;

//--- first calculation or number of bars was changed

   if(prev_calculated==0)   

     {

      limit=4;

      //--- calculate first visible value

      double firstValue=2;

      for(i=0; i<limit; i++)

         firstValue+=close[i];

      firstValue/=4;

      ExtUpArrowBuffer[limit-1]=firstValue;

      ExtDownArrowBuffer[limit-1]=firstValue;

     }

   else

      limit=prev_calculated-1;

//--- main loop


   static double lastFastEMA, lastFastEMA_of_EMA;

   static double lastSlowEMA, lastSlowEMA_of_EMA;

   double fast_weight=2.0/(1.0+Fast_DEMA_period);

   double slow_weight=2.0/(1.0+Slow_DEMA_period);

   if(IndicatorCounted()==0)

     {

      ExtDemaFast[limit]  =Close[limit];

      lastFastEMA        =Close[limit];

      lastFastEMA_of_EMA  =Close[limit];

      ExtDemaSlow[limit]  =Close[limit];

      lastSlowEMA        =Close[limit];

      lastSlowEMA_of_EMA  =Close[limit];

      limit--;

     }


   for(i=limit; i<rates_total && !IsStopped(); i++)

   {

      lastFastEMA        =fast_weight*Close[i]   + (1.0-fast_weight)*lastFastEMA;

      lastFastEMA_of_EMA  =fast_weight*lastFastEMA   + (1.0-fast_weight)*lastFastEMA_of_EMA;

      ExtDemaFast[i]=2.0*lastFastEMA - lastFastEMA_of_EMA;

      lastSlowEMA        =slow_weight*Close[i]   + (1.0-slow_weight)*lastSlowEMA;

      lastSlowEMA_of_EMA  =slow_weight*lastSlowEMA   + (1.0-slow_weight)*lastSlowEMA_of_EMA;

      ExtDemaSlow[i]=2.0*lastSlowEMA - lastSlowEMA_of_EMA;


      bool up = ExtDemaFast[i] > ExtDemaSlow[i];

      bool down = ExtDemaFast[i] < ExtDemaSlow[i];

      

      int arrow = 233;

      color colour = clrGreen;

      double display_level = 0;


      Print("up = "+IntegerToString(up)+ " down = "+IntegerToString(down));               

      if( up && open[i] < ExtDemaSlow[i] )

      {

         display_level = open[i] - (1.5*_Point*10);

         Print("Up Slow "+DoubleToString(display_level));

         ExtUpArrowBuffer[i] = display_level;

         ExtDownArrowBuffer[i] = EMPTY_VALUE;

      }

      if( up && open[i] > ExtDemaFast[i] )

      {

         display_level = open[i] + (1.5*_Point*10);

         Print("Up Fast "+DoubleToString(display_level));

         ExtUpArrowBuffer[i] = EMPTY_VALUE;

         ExtDownArrowBuffer[i] = display_level;

      }

      if( down && open[i] > ExtDemaSlow[i] )

      {

         display_level = open[i] + (1.5*_Point*10);

         Print("Down Slow "+DoubleToString(display_level));

         ExtUpArrowBuffer[i] = EMPTY_VALUE;

         ExtDownArrowBuffer[i] = display_level;

      }

      if( down && open[i] < ExtDemaFast[i] )

      {

         display_level = open[i] - (1.5*_Point*10);

         Print("Down Fast "+DoubleToString(display_level));

         ExtUpArrowBuffer[i] = display_level;

         ExtDownArrowBuffer[i] = EMPTY_VALUE;

      }

   }

   

   double FastEMA        =fast_weight*Close[0]   + (1.0-fast_weight)*lastFastEMA,

   FastEMA_of_EMA  =fast_weight*FastEMA      + (1.0-fast_weight)*lastFastEMA_of_EMA;

   ExtDemaFast[0]=2.0*FastEMA - FastEMA_of_EMA;

   double SlowEMA        =slow_weight*Close[0]   + (1.0-slow_weight)*lastSlowEMA,

   SlowEMA_of_EMA  =slow_weight*SlowEMA      + (1.0-slow_weight)*lastSlowEMA_of_EMA;

   ExtDemaSlow[0]=2.0*SlowEMA - SlowEMA_of_EMA;

   

//--- return value of prev_calculated for next call

   return(rates_total);

  } 


Output sample

 

Thanks in advance for your help. 

Alain Verleyen
Moderator
31206
Alain Verleyen  

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


imamushroom
157
imamushroom  
Thank you
imamushroom
157
imamushroom  
Bump - anyone can help me?
Alain Verleyen
Moderator
31206
Alain Verleyen  
imamushroom:

Hi,

 I'm learning to write indicators and I've a problem which I don't understand.

 The indicator should display an green up or red down arrow depending on certain conditions but instead is displaying all red down arrows. The different conditions are being met as per the printed output but I don't understand why I'm not getting the desired results. I'm sure it's something simple.

 Can you help?

 

 Here's my code:




 

Thanks in advance for your help. 

That's funny as I tested your indicator and only get Green arrow :

MetaTrader Trading Platform Screenshots

AUDUSD, H1, 2015.08.31

International Capital Markets Pty Ltd., MetaTrader 4, Demo

Forum indicator...

AUDUSD, H1, 2015.08.31, International Capital Markets Pty Ltd., MetaTrader 4, Demo


Marco vd Heijden
Moderator
4753
Marco vd Heijden  

probably the first one red or green is continuously re drawn. 

you can try to declare globally not sure if it will help do. 

imamushroom
157
imamushroom  

Thanks for getting back to me.

 

Any ideas as to why it's doing this as I can't see the problem with the code?

 

Thanks again. 

To add comments, please log in or register