OnCalculate function error

 

Hi, can anybody help me with this code? It gives me an error OnCalculate function, I'm trying to create an indicator in Separate Chart, that shows an Stochastic and EMA/SEMA at the same time.

Here is the code:

   


 

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button

 
Sorry!! Here it is my code
// List of MAs:

// MA_Method= 0: SMA        - Simple Moving Average

// MA_Method= 1: EMA        - Exponential Moving Average



// List of Prices:

// Price    = 0 - Close  

// Price    = 1 - Open  

// Price    = 2 - High  

// Price    = 3 - Low  

// Price    = 4 - Median Price   = (High+Low)/2  

// Price    = 5 - Typical Price  = (High+Low+Close)/3  

// Price    = 6 - Weighted Close = (High+Low+Close*2)/4

// Price    = 7 - Heiken Ashi Close  

// Price    = 8 - Heiken Ashi Open

// Price    = 9 - Heiken Ashi High

// Price    =10 - Heiken Ashi Low

#include <stdlib.mqh> 

#property copyright "Jesús Tortosa"

#property link      "http://www.jesustortosa.com"



#property indicator_separate_window

#property indicator_minimum 0.00

#property indicator_maximum 100.00

#property indicator_level1 50

#property indicator_buffers 6

#property indicator_color1  Silver

#property indicator_width1  5  

#property indicator_color2  DeepSkyBlue

#property indicator_width2  5  

#property indicator_color3  Tomato

#property indicator_width3  5  



#property indicator_color4  White

#property indicator_width4  2  

#property indicator_color5  White

#property indicator_width5  1  





//---- 

extern int     TimeFrame    = 15;

extern int     Price        =  0;

extern int     MA_Period    = 14;

extern int     MA_Shift     =  0;

extern int     MA_Method    =  1;

extern int     Color_Mode   =  1;

extern int     Sound_Mode   =  0; //0-off,1-on(works only with Color_Mode=1)

extern int     Sound_Shift  =  0; //0-open bar(multiple),1-closed bar(once)

extern string  Buy_Sound    =  "alert.wav";

extern string  Sell_Sound   =  "alert2.wav";



extern string  PriceMode    = "";

extern string  _0           = "Close";

extern string  _1           = "Open";

extern string  _2           = "High";

extern string  _3           = "Low";

extern string  _4           = "Median";

extern string  _5           = "Typical";

extern string  _6           = "Weighted Close";

extern string  _7           = "Heiken Ashi Close";

extern string  _8           = "Heiken Ashi Open";

extern string  _9           = "Heiken Ashi High";

extern string  _10          = "Heiken Ashi Low";

extern string  MAMode       = "";

extern string  __0          = "SMA";

extern string  __1          = "EMA";







double MA[];

double Up[];

double Dn[];

double aPrice[];

//----

double tmp[][2];

double haClose[2], haOpen[2], haHigh[2], haLow[2];

int    draw_begin, arraysize; 

string IndicatorName, TF, short_name;

int    sUp = 0, sDn =0; 

datetime prevtime, prevhatime;  



//| Stocatic Special Convertion Functions                                     |

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



int LastTradeTime;

double ExtHistoBuffer[];

double ExtHistoBuffer2[];



//void SetLoopCount(int loops)

//{

//}



void SetIndexValue(int shift, double value)

{

  ExtHistoBuffer[shift] = value;

//  Print ("ExtHistoBuffer :" ,value);    // green

}



void SetIndexValue2(int shift, double value)

{

  ExtHistoBuffer2[shift] = value;

//  Print ("ExtHistoBuffer2 :" ,value);    // green

}



double GetIndexValue(int shift)

{

  return(ExtHistoBuffer[shift]);

}



double GetIndexValue2(int shift)

{

  return(ExtHistoBuffer2[shift]);

}



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

//| End                                                              |

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



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

//| Custom indicator initialization function                         |

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

int init()

{

//---- 

   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));

   

   if(TimeFrame == 0 || TimeFrame < Period()) TimeFrame = Period();



//----

   IndicatorBuffers(6); 

   

   SetIndexBuffer(0,    MA); SetIndexStyle(0,DRAW_LINE); SetIndexShift(0,MA_Shift*TimeFrame/Period());

   SetIndexBuffer(1,    Up); SetIndexStyle(1,DRAW_LINE); SetIndexShift(1,MA_Shift*TimeFrame/Period());

   SetIndexBuffer(2,    Dn); SetIndexStyle(2,DRAW_LINE); SetIndexShift(2,MA_Shift*TimeFrame/Period());

   SetIndexBuffer(3,aPrice);

   SetIndexBuffer(4, ExtHistoBuffer);

   SetIndexBuffer(5, ExtHistoBuffer2);

   

   draw_begin=2*MathCeil(0.5*(MA_Period+1))*TimeFrame/Period();

   SetIndexDrawBegin(0,draw_begin);   

   SetIndexDrawBegin(1,draw_begin);

   SetIndexDrawBegin(2,draw_begin);

   SetIndexStyle(4, DRAW_LINE, STYLE_SOLID);

   SetIndexStyle(5, DRAW_LINE, STYLE_SOLID);



//---- 

   

   

   switch(MA_Method)

   {

   case 1 : short_name="EMA(";  break;

   default: MA_Method=0; short_name="SMA(";

   }

   

   switch(TimeFrame)

   {

   case 1     : TF = "M1" ; break;

   case 5     : TF = "M5" ; break;

   case 15    : TF = "M15"; break;

   case 30    : TF = "M30"; break;

   case 60    : TF = "H1" ; break;

   case 240   : TF = "H4" ; break;

   case 1440  : TF = "D1" ; break;

   case 10080 : TF = "W1" ; break;

   case 43200 : TF = "MN1"; break;

   default    : TF = "Current";

   } 

   

   IndicatorName = WindowExpertName(); 

   

   IndicatorShortName(short_name+MA_Period+")"+" "+TF);

   

   SetIndexLabel(1,short_name+MA_Period+")"+" "+TF+" UpTrend");

   SetIndexLabel(2,short_name+MA_Period+")"+" "+TF+" DnTrend");

   SetIndexLabel(0,short_name+MA_Period+")"+" "+TF);

      

//----   

   ArrayResize(tmp,arraysize);

      

   return(0);

}

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

//| AllAverages_v3.1                                                 |

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

int start()

{



//localvariables EMA



double MA, Up, Dn; //EMA

int limit, y i; //EMA

int cnt_bars = IndicatorCounted(); //EMA



   

  

  // }



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

//| Local variables      Stocastic                                            |

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

double AA = 0;

double bb = 0;

double aa1 = 0;

double cnt1 = 0;

int shift = 0;

double cnt = 0;

double loopbegin = 0;

double loopbegin2 = 0;

double loopbegin3 = 0;

bool first = True;

double prevbars = 0;

double sum = 0;

double smconst = 0;

double smconst1 = 0;

double prev = 0;

double prev1 = 0;

double prev2 = 0;

double prev3 = 0;

double weight = 0;

double linear = 0;

double MAValue = 0;

double MAValue2 = 0;

double mavalue3 = 0;

string MAstring = "";

double MyHigh = 0;

double MyLow = 0;

int counter = 0;

//double Price = 0;

//double Price1 = 0;

double tmpDevAA = 0;





SetLoopCount(0);

smconst = 2 / (1+Slw);

smconst1 = 2 / (1+Slwsignal);

      

loopbegin = loopbegin+1; 

for(shift =Barcount;shift >=0 ;shift --)

{ 

    prev = GetIndexValue2(shift+1);

    

    // Yousky 15/05/2006 - Change to avoid Zero divide exception.

    AA = 0;

    tmpDevAA = (High[Highest(NULL, 0, MODE_HIGH,shift+Pds,Pds)] - Low[Lowest(NULL, 0, MODE_LOW,shift+Pds,Pds)]);

    

    if (tmpDevAA != 0)

      AA = 100* ((Close[shift] - Low[Lowest(NULL, 0, MODE_LOW,shift+Pds,Pds)]) / tmpDevAA);

    // ---

      

    MAValue2 = smconst * (AA-prev) + prev;

    

    SetIndexValue2(shift,MAValue2);

    

   

loopbegin = loopbegin-1; 



} 









loopbegin2 = loopbegin2+1; 

for(shift =Barcount-Pds;shift >=0 ;shift --){ 

MyHigh = -999999;

MyLow = 99999999;

for(counter =shift;counter <=Pds + shift ;counter ++){ 

Price= GetIndexValue2(counter);

if( Price > MyHigh ) 

MyHigh = Price;

if( Pds <= 0 ) 

MyHigh = Price;

if( Price < MyLow ) 

MyLow = Price;

if( Pds <= 0 ) 

MyLow = Price;

} 



prev1 = GetIndexValue(shift+1);

aa1=GetIndexValue2(shift);



// Yousky 15/05/2006 - Change to avoid Zero divide exception.

bb= 0;

if ((MyHigh-MyLow) != 0)

  bb=100*(aa1-MyLow)/(MyHigh-MyLow);

// ---







  

MAValue = smconst * (bb-prev1) + prev1;


SetIndexValue(shift,MAValue);



loopbegin2 = loopbegin2-1; 

} 



//Print (MAValue);  // green



loopbegin3 = loopbegin3+1; 

for(shift =Barcount;shift >=0 ;shift --){ 

prev2=GetIndexValue2(shift+1);

prev3=GetIndexValue(shift);

mavalue3= smconst1 * (prev3-prev2) +prev2;





SetIndexValue2(shift,mavalue3);

loopbegin3 = loopbegin3-1;





}













´

 if(cnt_bars > 0)  limit = Bars - cnt_bars - 1;

   if(cnt_bars < 0)  return(0);    

   if(cnt_bars < 1)

   {

   limit = Bars - 1;

   

      for(i=Bars-1;i>0;i--) 

      { 

      MA[i] = EMPTY_VALUE; 

      Up[i] = EMPTY_VALUE;

      Dn[i] = EMPTY_VALUE;

      }







   

   

//---- 

   if(TimeFrame != Period())

{

   limit = MathMax(limit,TimeFrame/Period()+1);   

      

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

      {

      y = iBarShift(NULL,TimeFrame,Time[shift]);

      

      MA[shift] = iCustom(NULL,TimeFrame,IndicatorName,0,Price,MA_Period,MA_Shift,MA_Method,Color_Mode,Sound_Mode,Sound_Shift,Buy_Sound,Sell_Sound,0,y);    

         

         if(Color_Mode > 0)

         {

         Up[shift] = iCustom(NULL,TimeFrame,IndicatorName,0,Price,MA_Period,MA_Shift,MA_Method,Color_Mode,Sound_Mode,Sound_Shift,Buy_Sound,Sell_Sound,1,y);    

         Dn[shift] = iCustom(NULL,TimeFrame,IndicatorName,0,Price,MA_Period,MA_Shift,MA_Method,Color_Mode,Sound_Mode,Sound_Shift,Buy_Sound,Sell_Sound,2,y);    

         }

      }  


return(0);

}

   else

   {

      for(shift=limit;shift>=0;shift--) 

      {

         if(arraysize > 1 && prevtime != Time[shift])

         {

         for(i=0;i<arraysize;i++) tmp[i][1] = tmp[i][0];

         prevtime = Time[shift];

         }



         

         if(Price <= 6) aPrice[shift] = iMA(NULL,0,1,0,0,Price,shift);   

         else

         if(Price > 6 && Price <= 10) aPrice[shift] = HeikenAshi(Price-7,shift);



         switch(MA_Method)

         {

         case 1 : MA[shift] = EMA(aPrice[shift],MA[shift+1],MA_Period,shift); break;

         default: MA[shift] = SMA(aPrice,MA_Period,shift); break;

         }

                  

        

         if(Color_Mode == 1)

         {

         Up[shift] = EMPTY_VALUE; 

         Dn[shift] = EMPTY_VALUE;

            

            if(MA[shift] > MA[shift+1]) Up[shift] = MA[shift]; 

            else

            if(MA[shift] < MA[shift+1]) Dn[shift] = MA[shift];

                    

      

            if(Sound_Mode == 1 && shift == 0)

            {

               if(((Sound_Shift > 0 && sUp == 0) || Sound_Shift == 0) && MA[shift+Sound_Shift] > MA[shift+1+Sound_Shift] && MA[shift+1+Sound_Shift] <= MA[shift+2+Sound_Shift]) 

               {

               if(Sound_Shift > 0) {sUp = 1; sDn = 0;}

               PlaySound(Buy_Sound);

               }

               else

               if(((Sound_Shift > 0 && sDn == 0) || Sound_Shift == 0) && MA[shift+Sound_Shift] < MA[shift+1+Sound_Shift] && MA[shift+1+Sound_Shift] >= MA[shift+2+Sound_Shift]) 

               {

               if(Sound_Shift > 0) {sUp = 0; sDn = 1;}

               PlaySound(Sell_Sound);

               }

            }

         }

      }

   }

   



   return(0);

}













// MA_Method=0: SMA - Simple Moving Average

double SMA(double array[],int per,int bar)

{

   double Sum = 0;

   for(int i = 0;i < per;i++) Sum += array[bar+i];

   

   return(Sum/per);

}                

// MA_Method=1: EMA - Exponential Moving Average

double EMA(double price,double prev,int per,int bar)

{

   if(bar >= Bars - 2) double ema = price;

   else 

   ema = prev + 2.0/(1+per)*(price - prev); 

   

   return(ema);

}
 
  1. elfuncionari: Sorry!! Here it is my code

    You should have just edited your original post.
              Messages Editor

    Is your actual code all double spaced; or did you not paste the actual code in the SRC box? Unreadable.

  2. elfuncionari:

    Hi, can anybody help me with this code? It gives me an error OnCalculate function

    Your code has no OnCalculate function.
  3. "An error?" "Doesn't work" is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
  4. Help you with what? You haven't stated a problem. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?

  5. Your string inputs are unworkable. Don't use ints when you mean bool or an enum. Don't use strings when you mean enum.
    Unworkable
    extern string  MAMode       = "";
    extern string  __0          = "SMA";
    extern string  __1          = "EMA";
    
    extern int     MA_Method    =  1;
       switch(MA_Method){
       case 1 : short_name="EMA(";  break;
       default: MA_Method=0; short_name="SMA(";
       }
    
    Simple
    enum eMode { MA_SMA, // Simple MA
                 MA_EMA  // Exponential MA
               }; 
    
    extern eMode MA_Method=MA_SMA;
       switch(MA_Method){
       case MA_EMA : short_name="EMA(";  break;
       default: short_name="SMA(";
       }