Increasing Indicator timeframe

 

Hello, good day to you all..

this indicator have 4 timeframes.. tried to add 5 more timeframes but my mt4 froze

i dont know why it froze

is something wrong with my code

//------------------------------------------------------------------
#property copyright "mladen"
#property link      "www.forex-tsd.cm"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 18
#property indicator_color1  LimeGreen
#property indicator_color2  PaleVioletRed
#property indicator_color3  LimeGreen
#property indicator_color4  PaleVioletRed
#property indicator_color5  LimeGreen
#property indicator_color6  PaleVioletRed
#property indicator_color7  LimeGreen
#property indicator_color8  PaleVioletRed
#property indicator_color9  LimeGreen
#property indicator_color10  PaleVioletRed
#property indicator_color11  LimeGreen
#property indicator_color12  PaleVioletRed
#property indicator_color13  LimeGreen
#property indicator_color14  PaleVioletRed
#property indicator_color15  LimeGreen
#property indicator_color16  PaleVioletRed
#property indicator_color17  LimeGreen
#property indicator_color18  PaleVioletRed
#property indicator_minimum 0
#property indicator_maximum 10

//
//
//
//
//

extern string TimeFrame1            = "Current time frame";
extern string TimeFrame2            = "next1";
extern string TimeFrame3            = "next2";
extern string TimeFrame4            = "next3";
extern string TimeFrame5            = "next4";
extern string TimeFrame6            = "next5";
extern string TimeFrame7            = "next6";
extern string TimeFrame8            = "next7";
extern string TimeFrame9            = "next8";
extern int    Lb                    = 10;
extern int    AdaptPeriod           = 20;
extern string UniqueID              = "4 Time frame assm";
extern string Allert_Settings="=== Alert Settings===";
extern bool   Use_Alert=true;
extern int    Alerts_At_What_Level           = 3;
extern bool   Push_Notifications = true;
extern bool   AlertOnScreen = FALSE;
extern bool   Send_EmailAlerts = FALSE;
extern bool   SoundAlerts = FALSE;
extern string AlertCaption     ="";
extern int    Alert_Shift      =1;
extern bool   OnscreenAlert = false;
extern string SoundFile = "alert.wav";

extern int    LinesWidth            =  0;
extern color  LabelsColor           = DarkGray;
extern int    LabelsHorizontalShift = 5;
extern double LabelsVerticalShift   = 1.5;

//
//
//
//
//

double ST1u[],ST1d[];
double ST2u[],ST2d[];
double ST3u[],ST3d[];
double ST4u[],ST4d[];
double ST5u[],ST5d[];
double ST6u[],ST6d[];
double ST7u[],ST7d[];
double ST8u[],ST8d[];
double ST9u[],ST9d[];

int    timeFrames[9];
bool   returnBars;
bool   calculateValue;
string indicatorFileName;

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,ST1u);SetIndexBuffer(1,ST1d);
   SetIndexBuffer(2,ST2u);SetIndexBuffer(3,ST2d);
   SetIndexBuffer(4,ST3u);SetIndexBuffer(5,ST3d);
   SetIndexBuffer(6,ST4u);SetIndexBuffer(7,ST4d);
   SetIndexBuffer(8,ST5u);SetIndexBuffer(9,ST5d);
   SetIndexBuffer(10,ST6u);SetIndexBuffer(11,ST6d);
   SetIndexBuffer(12,ST7u);SetIndexBuffer(13,ST7d);
   SetIndexBuffer(14,ST8u);SetIndexBuffer(15,ST8d);
   SetIndexBuffer(16,ST9u);SetIndexBuffer(17,ST9d);


      indicatorFileName = WindowExpertName();
      returnBars        = (TimeFrame1=="returnBars");     if (returnBars)     return(0);
      calculateValue    = (TimeFrame1=="calculateValue"); if (calculateValue) return(0);
      
      //
      //
      //
      //
      //
      
      for (int i=0; i<18; i++) 
      {
         SetIndexStyle(i,DRAW_ARROW,EMPTY,LinesWidth); SetIndexArrow(i,110); 
      }
      timeFrames[0] = stringToTimeFrame(TimeFrame1);
      timeFrames[1] = stringToTimeFrame(TimeFrame2);
      timeFrames[2] = stringToTimeFrame(TimeFrame3);
      timeFrames[3] = stringToTimeFrame(TimeFrame4);
      timeFrames[4] = stringToTimeFrame(TimeFrame5);
      timeFrames[5] = stringToTimeFrame(TimeFrame6);
      timeFrames[6] = stringToTimeFrame(TimeFrame7);
      timeFrames[7] = stringToTimeFrame(TimeFrame8);
      timeFrames[8] = stringToTimeFrame(TimeFrame9);
      Alerts_At_What_Level = MathMin(MathMax(Alerts_At_What_Level,3),4);
      IndicatorShortName(UniqueID);
   return(0);
}
int deinit()
{
   for (int t=0; t<9; t++) ObjectDelete(UniqueID+t);
   return(0); 
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

double trend[][2];
#define _up 0
#define _dn 1
int start()
{
   int i,r,counted_bars=IndicatorCounted();
      if(counted_bars < 0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { ST1u[0] = limit+1; return(0); }
         if (calculateValue) { calculateByST(limit); return(0); }

         if (timeFrames[0] != Period()) limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrames[0],indicatorFileName,"returnBars",0,0)*timeFrames[0]/Period()));
         if (timeFrames[1] != Period()) limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrames[1],indicatorFileName,"returnBars",0,0)*timeFrames[1]/Period()));
         if (timeFrames[2] != Period()) limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrames[2],indicatorFileName,"returnBars",0,0)*timeFrames[2]/Period()));
         if (timeFrames[3] != Period()) limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrames[3],indicatorFileName,"returnBars",0,0)*timeFrames[3]/Period()));
         if (timeFrames[4] != Period()) limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrames[4],indicatorFileName,"returnBars",0,0)*timeFrames[4]/Period()));
         if (timeFrames[5] != Period()) limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrames[5],indicatorFileName,"returnBars",0,0)*timeFrames[5]/Period()));
         if (timeFrames[6] != Period()) limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrames[6],indicatorFileName,"returnBars",0,0)*timeFrames[6]/Period()));
         if (timeFrames[7] != Period()) limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrames[7],indicatorFileName,"returnBars",0,0)*timeFrames[7]/Period()));
         if (timeFrames[8] != Period()) limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrames[8],indicatorFileName,"returnBars",0,0)*timeFrames[8]/Period()));
         if (ArrayRange(trend,0)!=Bars) ArrayResize(trend,Bars);

         //
         //
         //
         //
         //
         
         bool initialized = false;
         if (!initialized)
         {
            initialized = true;
            int window = WindowFind(UniqueID);
            for (int t=0; t<9; t++)
            {
               string label = timeFrameToString(timeFrames[t]);
               ObjectCreate(UniqueID+t,OBJ_TEXT,window,0,0);
                  ObjectSet(UniqueID+t,OBJPROP_COLOR,LabelsColor);
                  ObjectSet(UniqueID+t,OBJPROP_PRICE1,t+LabelsVerticalShift);
                  ObjectSetText(UniqueID+t,label,8,"Arial");
            }               
         }
         for (t=0; t<9; t++) ObjectSet(UniqueID+t,OBJPROP_TIME1,Time[0]+Period()*LabelsHorizontalShift*60);

   //
   //
   //
   //
   //
    
   for(i = limit, r=Bars-i-1; i >= 0; i--,r++)
   {
      trend[r][_up] = 0;
      trend[r][_dn] = 0;
      for (int k=0; k<9; k++)
      {
         int y = iBarShift(NULL,timeFrames[k],Time[i]);
            double ssmv = iCustom(NULL,timeFrames[k],indicatorFileName,"calculateValue","","","",Lb,AdaptPeriod,7,y);
            bool   isUp  = (ssmv>0);
            switch (k)
            {
               case 0 : if (isUp) { ST1u[i] = k+1; ST1d[i] = EMPTY_VALUE;}  else { ST1d[i] = k+1; ST1u[i] = EMPTY_VALUE; } break;
               case 1 : if (isUp) { ST2u[i] = k+1; ST2d[i] = EMPTY_VALUE;}  else { ST2d[i] = k+1; ST2u[i] = EMPTY_VALUE; } break;
               case 2 : if (isUp) { ST3u[i] = k+1; ST3d[i] = EMPTY_VALUE;}  else { ST3d[i] = k+1; ST3u[i] = EMPTY_VALUE; } break;
               case 3 : if (isUp) { ST4u[i] = k+1; ST4d[i] = EMPTY_VALUE;}  else { ST4d[i] = k+1; ST4u[i] = EMPTY_VALUE; } break;
               case 4 : if (isUp) { ST5u[i] = k+1; ST5d[i] = EMPTY_VALUE;}  else { ST5d[i] = k+1; ST5u[i] = EMPTY_VALUE; } break;
               case 5 : if (isUp) { ST6u[i] = k+1; ST6d[i] = EMPTY_VALUE;}  else { ST6d[i] = k+1; ST6u[i] = EMPTY_VALUE; } break;
               case 6 : if (isUp) { ST7u[i] = k+1; ST7d[i] = EMPTY_VALUE;}  else { ST7d[i] = k+1; ST7u[i] = EMPTY_VALUE; } break;
               case 7 : if (isUp) { ST8u[i] = k+1; ST8d[i] = EMPTY_VALUE;}  else { ST8d[i] = k+1; ST8u[i] = EMPTY_VALUE; } break;
               case 8 : if (isUp) { ST9u[i] = k+1; ST9d[i] = EMPTY_VALUE;}  else { ST9d[i] = k+1; ST9u[i] = EMPTY_VALUE; } break;
            }
            if (isUp)
                  trend[r][_up] += 1;
            else  trend[r][_dn] += 1;
      }
   }
   manageAlerts();
   return(0);
}


//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

void calculateByST(int limit)
{
    for (int i=limit; i>=0; i--) 
    {
        double dev = iStdDev(NULL,0,AdaptPeriod,0,MODE_SMA,PRICE_CLOSE,i);
        double avg = iSma(dev,AdaptPeriod,i,0);
         if (dev!=0) 
                double period = Lb*avg/dev;
         else          period = Lb; 
         if (period<8) period = 8;
         
         //
         //
         //
         //
         //
         
         double close = Close[i];
         double hi    = iSmooth(iMA(NULL,0,1,0,MODE_SMA,PRICE_HIGH,i+1),period,i+1,0);
         double lo    = iSmooth(iMA(NULL,0,1,0,MODE_SMA,PRICE_LOW, i+1),period,i+1,1);
         
           ST8d[i]  = ST8d[i+1];
         
        if (close > hi) ST9d[i]  =  1;
        if (close < lo) ST9d[i]  = -1;
    }
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

double workSmooth[][10];
double iSmooth(double price, double length, int r, int instanceNo=0)
{
   if (ArrayRange(workSmooth,0)!=Bars) ArrayResize(workSmooth,Bars); instanceNo *= 5; r = Bars-r-1;
        if(r<=2) { workSmooth[r][instanceNo] = price; workSmooth[r][instanceNo+2] = price; workSmooth[r][instanceNo+9] = price; return(price); }
   
   //
   //
   //
   //
   //
   
        double alpha = 0.45*(length-1.0)/(0.45*(length-1.0)+2.0);
          workSmooth[r][instanceNo+0] =  price+alpha*(workSmooth[r-1][instanceNo]-price);
             workSmooth[r][instanceNo+1] = (price - workSmooth[r][instanceNo])*(1-alpha)+alpha*workSmooth[r-1][instanceNo+1];
             workSmooth[r][instanceNo+2] =  workSmooth[r][instanceNo+0] + workSmooth[r][instanceNo+1];
             workSmooth[r][instanceNo+3] = (workSmooth[r][instanceNo+2] - workSmooth[r-1][instanceNo+4])*MathPow(1.0-alpha,2) + MathPow(alpha,2)*workSmooth[r-1][instanceNo+3];
             workSmooth[r][instanceNo+4] =  workSmooth[r][instanceNo+3] + workSmooth[r-1][instanceNo+4]; 
   return(workSmooth[r][instanceNo+9]);
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

double workSma[][2];
double iSma(double price, int period, int r, int instanceNo=0)
{
   if (ArrayRange(workSma,0)!= Bars) ArrayResize(workSma,Bars); instanceNo *= 2; r = Bars-r-1;

   //
   //
   //
   //
   //
      
   workSma[r][instanceNo] = price;
   if (r>=period)
          workSma[r][instanceNo+1] = workSma[r-1][instanceNo+1]+(workSma[r][instanceNo]-workSma[r-period][instanceNo])/period;
   else { workSma[r][instanceNo+1] = 0; for(int k=0; k<period && (r-k)>=0; k++) workSma[r][instanceNo+1] += workSma[r-k][instanceNo];  
          workSma[r][instanceNo+1] /= k; }
   return(workSma[r][instanceNo+1]);
}

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

void manageAlerts()
{
   if (Use_Alert)
   {
      int whichBar = Bars-1;
      if (trend[whichBar][_up] >= Alerts_At_What_Level)
      {
//         if (trend[whichBar][_up] >= Alerts_At_What_Level) doAlert("up"  ,trend[whichBar][_up]);
//         if (trend[whichBar][_dn] >= Alerts_At_What_Level) doAlert("down",trend[whichBar][_dn]);
         if(ST1u[whichBar] !=EMPTY_VALUE) doAlert("up",trend[whichBar][_up]);
         if(ST2u[whichBar] !=EMPTY_VALUE) doAlert("up",trend[whichBar][_up]);
         if(ST3u[whichBar] !=EMPTY_VALUE) doAlert("up",trend[whichBar][_up]);
         if(ST4u[whichBar] !=EMPTY_VALUE) doAlert("up",trend[whichBar][_up]);

      }
      if ( trend[whichBar][_dn] >= Alerts_At_What_Level)
      {
         if(ST1d[whichBar] !=EMPTY_VALUE) doAlert("down",trend[whichBar][_dn]);
         if(ST2d[whichBar] !=EMPTY_VALUE) doAlert("down",trend[whichBar][_dn]);
         if(ST3d[whichBar] !=EMPTY_VALUE) doAlert("down",trend[whichBar][_dn]);
         if(ST4d[whichBar] !=EMPTY_VALUE) doAlert("down",trend[whichBar][_dn]);
      }

   }
}

//
//
//
//
//

void doAlert(string doWhat, int howMany)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
   if (previousAlert != doWhat || previousTime != Time[0]) {
       previousAlert  = doWhat;
       previousTime   = Time[0];

       //
       //
       //
       //
       //

       message =  Symbol()+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" "+howMany+" time frames of adaptive smoother Gann HiLo are aligned "+doWhat;
         if (Push_Notifications) SendNotification(Symbol()+" 4 time frame adaptive smoother Gann HiLo "+message);
         if (OnscreenAlert) Alert(message);
         if (Send_EmailAlerts) SendMail(Symbol()+" 9 time frame adaptive smoother Gann HiLo ",message);
         if (SoundAlerts) PlaySound(SoundFile);
   }
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

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

//
//
//
//
//

int toInt(double value) { return(value); }
int stringToTimeFrame(string tfs)
{
   tfs = stringUpperCase(tfs);
   int max = ArraySize(iTfTable)-1, add=0;
   int nxt = (StringFind(tfs,"NEXT1")>-1); if (nxt>0) { tfs = ""+Period(); add=1; }
       nxt = (StringFind(tfs,"NEXT2")>-1); if (nxt>0) { tfs = ""+Period(); add=2; }
       nxt = (StringFind(tfs,"NEXT3")>-1); if (nxt>0) { tfs = ""+Period(); add=3; }
       nxt = (StringFind(tfs,"NEXT4")>-1); if (nxt>0) { tfs = ""+Period(); add=4; }
       nxt = (StringFind(tfs,"NEXT5")>-1); if (nxt>0) { tfs = ""+Period(); add=5; }
       nxt = (StringFind(tfs,"NEXT6")>-1); if (nxt>0) { tfs = ""+Period(); add=6; }
       nxt = (StringFind(tfs,"NEXT7")>-1); if (nxt>0) { tfs = ""+Period(); add=7; }
       nxt = (StringFind(tfs,"NEXT8")>-1); if (nxt>0) { tfs = ""+Period(); add=8; }

      //
      //
      //
      //
      //
         
      for (int i=max; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[toInt(MathMin(max,i+add))],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 tchar = StringGetChar(s, length);
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                     s = StringSetChar(s, length, tchar - 32);
         else if(tchar > -33 && tchar < 0)
                     s = StringSetChar(s, length, tchar + 224);
   }
   return(s);
}
 
Abubakar Saidu: tried to add 5 more timeframes but my mt4 froze

i dont know why it froze

is something wrong with my code

Do you really expect us to answer? Run it in the debugger and find out where and why.

 
William Roeder:

Do you really expect us to answer? Run it in the debugger and find out where and why.

Debugger? how.. never know that..

ill research and see how it works

Thanks

Reason: