MTF

Back to topics list To post a new topic, please log in or register
avatar
93
mrmedia 2015.01.04 13:27
 



I have an indicator and MTF indicator that work when backtesting in visual mode.

I took this template and created two new indicators ,  MyEnvelopes   and  MTF_MyEnvelopes.

Problem is when in visual backtesting, for some reason the MTF_MyEnvelopes fails to paint and update. 

Can you see what is wrong, can you help?


..

//+------------------------------------------------------------------+
//|                                               MyEnvelopes.mq4 |
//+------------------------------------------------------------------+
//

//---- property of indicator ----------------------------------------+
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_color1 DeepSkyBlue
#property indicator_color2 DeepSkyBlue
#property indicator_width1 1
#property indicator_width2 1

//---- external parameters ------------------------------------------+
//extern int    TimeFrame           = 240;    // 0 = CurrentChart, 1 = M1, 5 = M5, 15 = M15, 30 = M30,
                                          // 60 = H1, 240 = H4, 1440 = D1, 10080 = W1, 43200 = MN1
extern int    Envelopes_Period    = 6;   // the period
extern int    Envelopes_Method    = 2;    // the method: 0 = Simple, 1 = Exponential, 2 = Smoothed, 3 = Weighted
extern int    Envelopes_Price     = 1;    // the price: 0 = Close, 1 = Open, 2 = High, 3 = Low, 4 = Median, 5 = Typical, 6 = Weighted
extern int    Envelopes_Shift     = 0;    // the shift
extern double Envelopes_Deviation = 0.05;  // the deviation (in percents)
extern color      MyColour             = DeepSkyBlue;
extern int        MyWidth1             = 1;
extern int        MyWidth2             = 1;


//---- buffers ------------------------------------------------------+
static double EnvUpper[];
static double EnvLower[];

//-------------------------------------------------------------------+
//---- initialization of indicator ----------------------------------+
//-------------------------------------------------------------------+
int init()
  {
   //---- set a "short" name of the indicator -----------------------+
   IndicatorShortName(StringConcatenate("MyEnvelopes"));
   //---- set a style for line --------------------------------------+
   //SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,5);
   //SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,5);
   //---- set a arrays for line -------------------------------------+
   SetIndexBuffer(0,EnvUpper);
   SetIndexBuffer(1,EnvLower);
   //---- set a names for lines -------------------------------------+
   SetIndexLabel(0,"EnvUpper");
   SetIndexLabel(1,"EnvLower");
   //---- set a first bar for drawing the line ----------------------+
   SetIndexDrawBegin(0,Envelopes_Period);
   SetIndexDrawBegin(1,Envelopes_Period);
   //---- finish of initialization ----------------------------------+
   
   ///////////////////////////////////////////////////////////////////////////////////////////////////////////
            color tmpcolor1 = indicator_color1;
            color tmpcolor2 = indicator_color2;
            
            int iwidth1 = indicator_width1;
            int iwidth2 = indicator_width2;
                    
          if(MyColour!=DeepSkyBlue)
          {
             tmpcolor1 = MyColour;
             tmpcolor2 = MyColour;

          }
         
         if( MyWidth1!=iwidth1 )
          {
             iwidth1 = MyWidth1;
          }
         if( MyWidth2!=iwidth2 )
          {
             iwidth2 = MyWidth2;
          }
        
            SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,iwidth1,tmpcolor1);
            SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,iwidth2,tmpcolor2);   
   ///////////////////////////////////////////////////////////////////////////////////////////////////////////
   
   
   return(0);
  }
 
//-------------------------------------------------------------------+
//---- deinitialization of indicator --------------------------------+
//-------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }

//-------------------------------------------------------------------+
//---- #MTF_Envelopes -----------------------------------------------+
//-------------------------------------------------------------------+
int start()
  {
   //---- amount not changed bars after last call of the indicator --+
   int counted_bars = IndicatorCounted();
   if(counted_bars < 0) return(-1);
   //---- last counted bar will be counted --------------------------+
   if(counted_bars > 0) counted_bars--;
   int limit = Bars - counted_bars;
  
   
   //for(int i=limit; i>=0; i--)
   //  {
   //   int bar_shift = iBarShift(NULL,0,Time[i]);
   //   EnvUpper[i] = iEnvelopes(NULL,0,Envelopes_Period,Envelopes_Method,Envelopes_Shift,Envelopes_Price,Envelopes_Deviation,MODE_UPPER,bar_shift);
   //   EnvLower[i] = iEnvelopes(NULL,0,Envelopes_Period,Envelopes_Method,Envelopes_Shift,Envelopes_Price,Envelopes_Deviation,MODE_LOWER,bar_shift);
   //  }
     
   int ictr;
   ictr = limit - 1;      
   int xctr;
   
    for ( xctr = ictr; xctr >= 0; xctr-- ) 
       { 
       int bar_shift = iBarShift(NULL,0,Time[xctr]); 
      EnvUpper[xctr] = iEnvelopes(NULL,0,Envelopes_Period,Envelopes_Method,Envelopes_Shift,Envelopes_Price,Envelopes_Deviation,MODE_UPPER,bar_shift);
      EnvLower[xctr] = iEnvelopes(NULL,0,Envelopes_Period,Envelopes_Method,Envelopes_Shift,Envelopes_Price,Envelopes_Deviation,MODE_LOWER,bar_shift);       
       
       }    
   return(0);
  }


  

.

.

//+------------------------------------------------------------------+
//|                                            MTF_MyEnvelopes |
//+------------------------------------------------------------------+


////////////////////////////
//---- property of indicator ----------------------------------------+
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 DeepSkyBlue
#property indicator_color2 DeepSkyBlue
#property indicator_width1 5
#property indicator_width2 5


//---- external parameters ------------------------------------------+
extern int    TimeFrame           = 240;    // 0 = CurrentChart, 1 = M1, 5 = M5, 15 = M15, 30 = M30,
                                          // 60 = H1, 240 = H4, 1440 = D1, 10080 = W1, 43200 = MN1
extern int    Envelopes_Period    = 6;   // the period
extern int    Envelopes_Method    = 2;    // the method: 0 = Simple, 1 = Exponential, 2 = Smoothed, 3 = Weighted
extern int    Envelopes_Price     = 1;    // the price: 0 = Close, 1 = Open, 2 = High, 3 = Low, 4 = Median, 5 = Typical, 6 = Weighted
extern int    Envelopes_Shift     = 0;    // the shift
extern double Envelopes_Deviation = 0.05;  // the deviation (in percents)
extern color      MyColour             = DeepSkyBlue;
extern int        MyWidth1             = 5;
extern int        MyWidth2             = 5;


//---- buffers ------------------------------------------------------+
static double EnvUpper[];
static double EnvLower[];
string ShortName;
//-------------------------------------------------------------------+
//---- initialization of indicator ----------------------------------+
//-------------------------------------------------------------------+
int init()
  {
   //---- set a "short" name of the indicator -----------------------+
   IndicatorShortName(StringConcatenate("MTF_MyEnvelopes(",TimeFrameToStr(TimeFrame),")"));
   //---- set a style for line --------------------------------------+
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,5);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,5);
   //---- set a arrays for line -------------------------------------+
   SetIndexBuffer(0,EnvUpper);
   SetIndexBuffer(1,EnvLower);
   //---- set a names for lines -------------------------------------+
   SetIndexLabel(0,"MTFEnvUpper");
   SetIndexLabel(1,"MTFEnvLower");
   //---- set a first bar for drawing the line ----------------------+
   SetIndexDrawBegin(0,Envelopes_Period);
   SetIndexDrawBegin(1,Envelopes_Period);
   //---- finish of initialization ----------------------------------+

   ///////////////////////////////////////////////////////////////////////////////////////////////////////////
            color tmpcolor1 = indicator_color1;
            color tmpcolor2 = indicator_color2;
            
            int iwidth1 = indicator_width1;
            int iwidth2 = indicator_width2;
                    
          if(MyColour!=DeepSkyBlue)
          {
             tmpcolor1 = MyColour;
             tmpcolor2 = MyColour;

          }
         
         if( MyWidth1!=iwidth1 )
          {
             iwidth1 = MyWidth1;
          }
         if( MyWidth2!=iwidth2 )
          {
             iwidth2 = MyWidth2;
          }
        
            SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,iwidth1,tmpcolor1);
            SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,iwidth2,tmpcolor2);   
   ///////////////////////////////////////////////////////////////////////////////////////////////////////////

   return(0);
  }
 

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                         |
//+------------------------------------------------------------------+
int deinit() {
  
    return( 0 );
}

//+------------------------------------------------------------------+
//| Function run on every tick                                       |
//+------------------------------------------------------------------+
int start() {
    //Comment( ShortName );
    
    int ictr;
    int counted_bars = IndicatorCounted();
    
    // Check for errors
    if ( counted_bars < 0 ) {
        return( -1 );
    }

    // Last bar will be recounted
    if ( counted_bars > 0 ) {
        counted_bars--;
    }
    
    int limit = Bars - counted_bars;
    ictr = limit - 1;
    
    if ( Bars < Envelopes_Period ) {
       return( 0 );
    }
    
//////////////////////////////////////////////////////
   datetime TimeArray[];
   int      i,y=0;
 //,limit
 //,counted_bars=IndicatorCounted()
 
 
// Plot defined time frame on to current time frame
   ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame); 
   
   limit=Bars-counted_bars+TimeFrame/Period();
   for(i=0,y=0;i<limit;i++)
   {
   if (Time[i]<TimeArray[y]) y++;
   
      EnvUpper[i]      = iCustom(NULL,TimeFrame,"MyEnvelopes",Envelopes_Period,Envelopes_Method,Envelopes_Price,Envelopes_Shift,Envelopes_Deviation,MyColour,MyWidth1,MyWidth2,0,y);  
   //   TrStopLevelMid[i]           = iCustom(NULL,TimeFrame,"MT4-LevelStop-Reverse-vB0-4CCCB8TMP2SARB",UseATRMode,NonATRStopPips,ATRPeriod,ATRMultiplier,ATRSmoothing,Distance,DistanceDiv,MyShiftTime,MyColour,MyWidth1,MyWidth2,MyWidth3,MyWidth4,MyWidth5,2,y);  
        EnvLower[i]      = iCustom(NULL,TimeFrame,"MyEnvelopes",Envelopes_Period,Envelopes_Method,Envelopes_Price,Envelopes_Shift,Envelopes_Deviation,MyColour,MyWidth1,MyWidth2,1,y);  
        }
return(0);
//////////////////////////////////////////////////////
}
                                                                                                                                                                                             

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

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

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


int stringToTimeFrame(string tfs)
{
   tfs = StringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}

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


string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

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

string StringUpperCase(string str)
{
   string   s = str;
   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int Achar = StringGetChar(s, length);
         if((Achar > 96 && Achar < 123) || (Achar > 223 && Achar < 256))
                     s = StringSetChar(s, length, Achar - 32);
         else if(Achar > -33 && Achar < 0)
                     s = StringSetChar(s, length, Achar + 224);
   }
   return(s);
}

//-------------------------------------------------------------------+
//---- TimeFrameToStr -----------------------------------------------+
//-------------------------------------------------------------------+
string TimeFrameToStr(int period)
  {
   switch(period)
     {
      case     1: string TimeFrameStr = "M1";          break;
      case     5:        TimeFrameStr = "M5";          break;
      case    15:        TimeFrameStr = "M15";         break;
      case    30:        TimeFrameStr = "M30";         break;
      case    60:        TimeFrameStr = "H1";          break;
      case   240:        TimeFrameStr = "H4";          break;
      case  1440:        TimeFrameStr = "D1";          break;
      case 10080:        TimeFrameStr = "W1";          break;
      case 43200:        TimeFrameStr = "MN1";         break;
      default:           TimeFrameStr = TimeFrameToStr(Period());
     }
   return(TimeFrameStr);
  }

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


.

.

.

.

.

.

.

.

.

.


.

.

.

.

.

avatar
273
fx1.net 2015.01.08 11:57 #
 
hire some freelancer
Back to topics list  

To add comments, please log in or register