Please help dissect and modify the code.

1
Hi All,

I am trying to understand this code. and I want to modify it so that is can show the volume cycles:

Code: Select all

#property copyright ""
#property link      ""

#property indicator_separate_window
#property indicator_buffers 8

//---- input parameters

extern int       iPeriod=240;
extern int       iStartFrom=1; 
extern string    AddToObjName="1";
extern color     HandlerColor=Gray;
extern color     TextColor=Black; 
extern bool      Cycle1 = true;
extern bool      Cycle2 = true;
extern bool      Cycle3 = true;
extern bool      Cycle4 = true;
extern bool      Cycle5 = true;
extern bool      Cycle6 = true;
extern bool      Cycle7 = true;
extern bool      CompositeCycle = false;
extern bool      CycleMeter = false;
extern string    UniqueID = "spectrometr1";
extern int       FutureLineStyle = STYLE_DOT;
extern color     Color1 = Red;
extern color     Color2 = Orange;
extern color     Color3 = Yellow;
extern color     Color4 = Lime;
extern color     Color5 = Blue;
extern color     Color6 = DodgerBlue;
extern color     Color7 = DarkViolet;
extern color     Color8 = DimGray;
 

string ShortName;

int LastTime;

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
double ExtMapBuffer6[];
double ExtMapBuffer7[];
double ExtMapBuffer8[];


int LastLeftBar;
int LastRightBar;
int LastLeftTime;
int LastRightTime;
int LastStartFrom;
int LastiStartFrom;
int LastiPeriod;

int window;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init(){
   ShortName=UniqueID+UniqueID+"_"+AddToObjName;   
   UniqueID=UniqueID+"_"+AddToObjName+"_";  

    
   
   IndicatorShortName(ShortName);

    SetIndexBuffer(0,ExtMapBuffer1);
    SetIndexBuffer(1,ExtMapBuffer2);
    SetIndexBuffer(2,ExtMapBuffer3);    
    SetIndexBuffer(3,ExtMapBuffer4); 
    SetIndexBuffer(4,ExtMapBuffer5);  
    SetIndexBuffer(5,ExtMapBuffer6); 
    SetIndexBuffer(6,ExtMapBuffer7);
    SetIndexBuffer(7,ExtMapBuffer8);
    
 if (!CompositeCycle)
 {
   SetIndexStyle(0,DRAW_LINE,DRAW_LINE,2);
   SetIndexStyle(1,DRAW_LINE,DRAW_LINE,2);
   SetIndexStyle(2,DRAW_LINE,DRAW_LINE,2);
   SetIndexStyle(3,DRAW_LINE,DRAW_LINE,2);
   SetIndexStyle(4,DRAW_LINE,DRAW_LINE,2);
   SetIndexStyle(5,DRAW_LINE,DRAW_LINE,2);
   SetIndexStyle(6,DRAW_LINE,DRAW_LINE,2);
   SetIndexStyle(7,DRAW_NONE);
}

else

  {
  
   SetIndexStyle(0,DRAW_NONE);
   SetIndexStyle(1,DRAW_NONE);
   SetIndexStyle(2,DRAW_NONE);
   SetIndexStyle(3,DRAW_NONE);
   SetIndexStyle(4,DRAW_NONE);
   SetIndexStyle(5,DRAW_NONE);
   SetIndexStyle(6,DRAW_NONE);
   SetIndexStyle(7,DRAW_LINE,DRAW_LINE,2);
   
   } 
   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   deleteFuture();
//----
   fObjDeleteByPrefix(UniqueID);
//----
   return(0);
  }
void deleteFuture()
{
   string searchFor    = UniqueID+":";
   int    searchLength = StringLen(searchFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string name = ObjectName(i); if (StringSubstr(name,0,searchLength) == searchFor)
                                       ObjectDelete(name);
   }
}  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start(){

   window = WindowFind(ShortName);
      
      string ObjName=UniqueID+"Íóëåâàÿ ëèíèÿ";
         
         if(ObjectFind(ObjName)!=WindowFind(ShortName)){
            LastStartFrom=iStartFrom;//
            LastLeftBar=iStartFrom+iPeriod-1;
            LastRightBar=iStartFrom;            
            LastLeftTime=Time[LastLeftBar];
            LastRightTime=Time[LastRightBar];         
            fObjTrendLine(ObjName,LastLeftTime,0,LastRightTime,0,false,HandlerColor,3,WindowFind(ShortName),0,true);
         }
         
      int NowLeftTime=ObjectGet(ObjName,OBJPROP_TIME1);
      int NowRightTime=ObjectGet(ObjName,OBJPROP_TIME2); 
      if(NowRightTime>Time[1])NowRightTime=Time[1];
      
      int NowLeftBar=iBarShift(NULL,0,NowLeftTime,false);
      int NowRightBar=iBarShift(NULL,0,NowRightTime,false);  
//      iPeriod=NowLeftBar-NowRightBar+1; 
      int LastStartFromTime=iBarShift(NULL,0,LastRightTime,false);  
//      iStartFrom=(NowRightBar-LastStartFromTime)+LastStartFrom; 
      
      LastStartFrom=iStartFrom;
      LastLeftBar=iStartFrom+iPeriod-1;
      LastRightBar=iStartFrom;            
      LastLeftTime=Time[LastLeftBar];
      LastRightTime=Time[LastRightBar];         
      
      ObjName=UniqueID+"iPeriod";
      fObjLabel(ObjName,100,5,"Period: "+iPeriod,3,TextColor,8,WindowFind(ShortName),"Arial Black",false);
      ObjName=UniqueID+"iStartFrom";      
      fObjLabel(ObjName,10,5," StartFrom: "+iStartFrom,3,TextColor,8,WindowFind(ShortName),"Arial Black",false);
      
      
   //=====================================================================================================    

      static int LastBars=0;

//      if(iStartFrom==LastiStartFrom && iPeriod==LastiPeriod)if(Bars<LastBars)return(0);
      LastiStartFrom=iStartFrom;
      LastiPeriod=iPeriod;
      LastBars=Bars;
         
      static bool initialized=false;
             if (!initialized)
             {
               initialized = true;
               if (!CompositeCycle)
               {
                  SetIndexStyle(0,DRAW_LINE,EMPTY,EMPTY,Color1);
                  SetIndexStyle(1,DRAW_LINE,EMPTY,EMPTY,Color2);
                  SetIndexStyle(2,DRAW_LINE,EMPTY,EMPTY,Color3);
                  SetIndexStyle(3,DRAW_LINE,EMPTY,EMPTY,Color4);
                  SetIndexStyle(4,DRAW_LINE,EMPTY,EMPTY,Color5);
                  SetIndexStyle(5,DRAW_LINE,EMPTY,EMPTY,Color6);
                  SetIndexStyle(6,DRAW_LINE,EMPTY,EMPTY,Color7);
                  SetIndexStyle(7,DRAW_NONE);
               }                  
               else SetIndexStyle(7,DRAW_LINE,EMPTY,EMPTY,Color8);
             }
   
   //=====================================================================================================    

      int tPeriod; 
      double tVal_0;
      double tVal_1;
      double tB;
      double tMaxDev;
      double tStdError; 
      double tRSquared;
      double Arr[];

      deleteFuture();
      fLinearRegressionAll2(iStartFrom,iStartFrom+iPeriod-1,0,iPeriod,tVal_0,tVal_1,tB,tMaxDev,tStdError,tRSquared,Arr);
                
   //======================================================================================================      

      double A[],B[],R[],F[];
      fFurie(Arr,A,B,R,F);
      int N=ArraySize(Arr);
      ObjName=UniqueID+"Íóëåâàÿ ëèíèÿ";
      fObjTrendLine(ObjName,LastLeftTime,0,LastRightTime+N*Period()*60,0,false,HandlerColor,3,WindowFind(ShortName),0,true);
   //======================================================================================================      
         
         for(int i=0;i<N;i++)
         {
            int ii=i+iStartFrom;
          
              
            if(Cycle1==true)
            {
               ExtMapBuffer1[ii]=  A[1]*MathSin(1*6.28*i    /(N-1))+B[1]*MathCos(1*6.28*i    /(N-1));
               drawSegment(":1:",i,A[1]*MathSin(1*6.28*i    /(N-1))+B[1]*MathCos(1*6.28*i    /(N-1)),
                                   A[1]*MathSin(1*6.28*(i+1)/(N-1))+B[1]*MathCos(1*6.28*(i+1)/(N-1)),Color1,N-2,iStartFrom);
            }
               else
               {
                  ExtMapBuffer1[ii]= 0;
               }
            
            if (Cycle2==true)
            {
               ExtMapBuffer2[ii]=  A[2]*MathSin(2*6.28*i    /(N-1))+B[2]*MathCos(2*6.28*i    /(N-1));  
               drawSegment(":2:",i,A[2]*MathSin(2*6.28*i    /(N-1))+B[2]*MathCos(2*6.28*i    /(N-1)),
                                   A[2]*MathSin(2*6.28*(i+1)/(N-1))+B[2]*MathCos(2*6.28*(i+1)/(N-1)),Color2,N-2,iStartFrom);
            }
               else
               {
                  ExtMapBuffer2[ii] = 0;
               }   
                
            if (Cycle3==true)
            {    
               ExtMapBuffer3[ii]  =A[3]*MathSin(3*6.28*i    /(N-1))+B[3]*MathCos(3*6.28*i    /(N-1));
               drawSegment(":3:",i,A[3]*MathSin(3*6.28*i    /(N-1))+B[3]*MathCos(3*6.28*i    /(N-1)),
                                   A[3]*MathSin(3*6.28*(i+1)/(N-1))+B[3]*MathCos(3*6.28*(i+1)/(N-1)),Color3,N-2,iStartFrom);
            }
               else
               {
                  ExtMapBuffer3[ii]= 0;
               }

            if (Cycle4==true)
            {
               ExtMapBuffer4[ii]  =A[4]*MathSin(4*6.28*i    /(N-1))+B[4]*MathCos(4*6.28*i    /(N-1));    
               drawSegment(":4:",i,A[4]*MathSin(4*6.28*i    /(N-1))+B[4]*MathCos(4*6.28*i    /(N-1)),
                                   A[4]*MathSin(4*6.28*(i+1)/(N-1))+B[4]*MathCos(4*6.28*(i+1)/(N-1)),Color4,N-2,iStartFrom);
            }
               else
               {
                  ExtMapBuffer4[ii]= 0;
               }

            if (Cycle5==true)
            {
           
               ExtMapBuffer5[ii]  =A[5]*MathSin(5*6.28*i    /(N-1))+B[5]*MathCos(5*6.28*i    /(N-1)); 
               drawSegment(":5:",i,A[5]*MathSin(5*6.28*i    /(N-1))+B[5]*MathCos(5*6.28*i    /(N-1)),
                                   A[5]*MathSin(5*6.28*(i+1)/(N-1))+B[5]*MathCos(5*6.28*(i+1)/(N-1)),Color5,N-2,iStartFrom);
            }
               else
               {
                  ExtMapBuffer5[ii]= 0;
               }

            if (Cycle6==true)
            {              
               ExtMapBuffer6[ii]  =A[6]*MathSin(6*6.28*i    /(N-1))+B[6]*MathCos(6*6.28*i    /(N-1));
               drawSegment(":6:",i,A[6]*MathSin(6*6.28*i    /(N-1))+B[6]*MathCos(6*6.28*i    /(N-1)),
                                   A[6]*MathSin(6*6.28*(i+1)/(N-1))+B[6]*MathCos(6*6.28*(i+1)/(N-1)),Color6,N-2,iStartFrom);
            }
               else
               {
                  ExtMapBuffer6[ii]= 0;
               }

            if (Cycle7==true)
            {

               ExtMapBuffer7[ii]  =A[7]*MathSin(7*6.28*i    /(N-1))+B[7]*MathCos(7*6.28*i/(N-1));   
               drawSegment(":7:",i,A[7]*MathSin(7*6.28*i    /(N-1))+B[7]*MathCos(7*6.28*i    /(N-1)),
                                   A[7]*MathSin(7*6.28*(i+1)/(N-1))+B[7]*MathCos(7*6.28*(i+1)/(N-1)),Color7,N-2,iStartFrom);
            }
               else
               {
                  ExtMapBuffer7[ii]= 0;
               }
  
            if (CompositeCycle==true)
            {
               //ExtMapBuffer8[ii]=A[8]*MathSin(8*6.28*i/(N-1))+B[8]*MathCos(8*6.28*i/(N-1));  
               ExtMapBuffer8[ii]=ExtMapBuffer1[ii]+ExtMapBuffer2[ii]+ExtMapBuffer3[ii]+ExtMapBuffer4[ii]+ExtMapBuffer5[ii]+ExtMapBuffer6[ii]+ExtMapBuffer7[ii];
            }
            else
            {
               ExtMapBuffer8[ii]= 0;
            }
         }
         
         //
         //
         //
         //
         //
         
         if (CompositeCycle==true)
            for(i=0;i<N;i++)
            {
               ii=i+iStartFrom;
               if (ii==iStartFrom)
                     drawSegment(":8:",i,ExtMapBuffer8[ii],ExtMapBuffer8[N-ii],Color8,N-2,iStartFrom,true);
               else  drawSegment(":8:",i,ExtMapBuffer8[ii],ExtMapBuffer8[ii+1],Color8,N-2,iStartFrom,true);
            }               
   
   //======================================================================================================      
                                   //ëèíèè ñïðàâà
      
     if(CycleMeter)
     {
      if (Cycle1==true){fObjTrendLine(UniqueID+"1",Time[0]+Period()*60*3,R[1] ,Time[0]+Period()*60*3,-R[1] ,false,Color1,8,WindowFind(ShortName),0,false);}
      if (Cycle2==true){fObjTrendLine(UniqueID+"2",Time[0]+Period()*60*5,R[2] ,Time[0]+Period()*60*5,-R[2] ,false,Color2,8,WindowFind(ShortName),0,false);}
      if (Cycle3==true){fObjTrendLine(UniqueID+"3",Time[0]+Period()*60*7,R[3] ,Time[0]+Period()*60*7,-R[3] ,false,Color3,8,WindowFind(ShortName),0,false);}
      if (Cycle4==true){fObjTrendLine(UniqueID+"4",Time[0]+Period()*60*9,R[4] ,Time[0]+Period()*60*9,-R[4] ,false,Color4,8,WindowFind(ShortName),0,false);}
      if (Cycle5==true){fObjTrendLine(UniqueID+"5",Time[0]+Period()*60*11,R[5],Time[0]+Period()*60*11,-R[5],false,Color5,8,WindowFind(ShortName),0,false);}
      if (Cycle6==true){fObjTrendLine(UniqueID+"6",Time[0]+Period()*60*13,R[6],Time[0]+Period()*60*13,-R[6],false,Color6,8,WindowFind(ShortName),0,false);}
      if (Cycle7==true){fObjTrendLine(UniqueID+"7",Time[0]+Period()*60*15,R[7],Time[0]+Period()*60*15,-R[7],false,Color7,8,WindowFind(ShortName),0,false);}
     }
         
   return(0);
}

int toInt(int val)
{
   return(val);
}
void drawSegment(string add, int i, double price1, double price2, color theColor, int windowSizeFuture, int shift, bool isComposite=false)
{
   if ((CompositeCycle && !isComposite) || price2==EMPTY_VALUE) return;
   string cname = UniqueID+add+i;
      ObjectCreate(cname,OBJ_TREND,window,0,0);
         if ((windowSizeFuture-shift-i+1)<0)
               datetime time1 = Time[toInt(MathAbs(windowSizeFuture-shift-i+1))];
         else           time1 = Time[0]+Period()*60*(windowSizeFuture-shift-i+1);
         if ((windowSizeFuture-shift-i)<0)
               datetime time2 = Time[toInt(MathAbs(windowSizeFuture-shift-i))];
         else           time2 = Time[0]+Period()*60*(windowSizeFuture-shift-i);
         
         ObjectSet(cname,OBJPROP_TIME1, time1);
         ObjectSet(cname,OBJPROP_TIME2, time2);
         ObjectSet(cname,OBJPROP_PRICE1,price1);
         ObjectSet(cname,OBJPROP_PRICE2,price2);
         ObjectSet(cname,OBJPROP_RAY,false);
         ObjectSet(cname,OBJPROP_COLOR,theColor);
         ObjectSet(cname,OBJPROP_STYLE,FutureLineStyle);
}


//+------------------------------------------------------------------+
 
                                //íàäïèñü íèæíèé ëåâûé óãîë                              
void fObjLabel(
   string aObjectName,     // 1 èìÿ
   int aX,                 // 2 õ
   int aY,                 // 3 ó
   string aText,           // 4 òåêñò
   int aCorner=0,          // 5 óãîë   0  1
                           //          2  3
   color aColor=Black,       // 6 öâåò
   int aFontSize=8,        // 7 ðàçìåð øðèôòà
   int aWindowNumber=0,    // 8 îêíî
   string aFont="Arial Black",   // 9 øðèôò
   bool aBack=false        // 10 ôîí
   ){     

    
      if(ObjectFind(aObjectName)!=aWindowNumber){
         ObjectCreate(aObjectName,OBJ_LABEL,aWindowNumber,0,0);
      }      
   ObjectSet(aObjectName,OBJPROP_XDISTANCE,aX);
   ObjectSet(aObjectName,OBJPROP_YDISTANCE,aY);   
   ObjectSetText(aObjectName,aText,aFontSize,aFont,aColor);
   ObjectSet(aObjectName,OBJPROP_BACK,aBack);
   ObjectSet(aObjectName,OBJPROP_CORNER,aCorner);   
}

                               //îòðèñîâêà âåðòèê ëèíèé
void fObjTrendLine(
   string aObjectName,  // 1 èìÿ
   datetime aTime_1,    // 2 âðåìÿ 1
   double aPrice_1,     // 3 öåíà 1
   datetime aTime_2,    // 4 âðåìÿ 2
   double aPrice_2,     // 5 öåíà 2
   bool aRay=false,     // 6 ëó÷
   color aColor=Black,    // 7 öâåò
   int aWidth=1,        // 8 òîëùèíà
   int aWindowNumber=0, // 9 îêíî
   int aStyle=1,        // 10 0-STYLE_SOLID, 1-STYLE_DASH, 2-STYLE_DOT, 3-STYLE_DASHDOT, 4-STYLE_DASHDOTDOT
   bool aBack=false     // 11 ôîí
   ){                              
      if(ObjectFind(aObjectName)!=aWindowNumber){
         ObjectCreate(aObjectName,OBJ_TREND,aWindowNumber,aTime_1,aPrice_1,aTime_2,aPrice_2);
      }      
   ObjectSet(aObjectName,OBJPROP_TIME1,aTime_1);
   ObjectSet(aObjectName,OBJPROP_PRICE1,aPrice_1);   
   ObjectSet(aObjectName,OBJPROP_TIME2,aTime_2);       
   ObjectSet(aObjectName,OBJPROP_PRICE2,aPrice_2); 
   ObjectSet(aObjectName,OBJPROP_RAY,aRay);     
   ObjectSet(aObjectName,OBJPROP_COLOR,aColor);
   ObjectSet(aObjectName,OBJPROP_WIDTH,aWidth);
   ObjectSet(aObjectName,OBJPROP_BACK,aBack);
   ObjectSet(aObjectName,OBJPROP_STYLE,aStyle);                
}


int fLinearRegressionAll2(int i0,int i1, int aPrice, int aPeriod, double aVal_0, double aVal_1, double aB, double aMaxDev, double aStdError, double aRSquared,double & aArr[]){
   int rRetError=0;
   double x,y,y1,y2,sumy,sumx,sumxy,sumx2,sumy2,sumx22,sumy22,div1,div2;   
 //  aPeriod=i1-i0+1;
   sumy=0.0;sumx=0.0;sumxy=0.0;sumx2=0.0;sumy2=0.0;
      for(int i=0; i<aPeriod; i++){
         y=iMA(NULL,0,1,0,0,aPrice,i0+i);
         x=i;
         sumy+=y;
         sumxy+=y*i;
         sumx+=x;
         sumx2+=MathPow(x,2);  
         sumy2+=MathPow(y,2);         
      } 
   sumx22=MathPow(sumx,2);  
   sumy22=MathPow(sumy,2);      
   div1=sumx2*aPeriod-sumx22;
   div2=MathSqrt((aPeriod*sumx2-sumx22)*(aPeriod*sumy2-sumy22));   

   //---- regression line ----
   
      if(div1!=0.0){
         aB=(sumxy*aPeriod-sumx*sumy)/div1;
         aVal_0=(sumy-sumx*aB)/aPeriod;
         aVal_1=aVal_0+aB*(aPeriod-1);
         rRetError+=-1;
      }
      else{
         rRetError+=-1;      
      }

   //--- stderr & maxdev

   aMaxDev=0;aStdError=0;
   
      for(i=0;i<aPeriod;i++){
         y1=iMA(NULL,0,1,0,0,aPrice,i0+i);
         y2=aVal_0+aB*i;
         aMaxDev=MathMax(MathAbs(y1-y2),aMaxDev);
         aStdError+=MathPow(y1-y2,2);
      }
      
   aStdError=MathSqrt(aStdError/aPeriod);

   //--- rsquared ---

      if(div2!=0){
         aRSquared=MathPow((aPeriod*sumxy-sumx*sumy)/div2,2);   
      }   
      else{
         rRetError+=-2;
      }
   
   //----
   
   ArrayResize(aArr,aPeriod);
      for(i=0; i<aPeriod; i++){
         y=iMA(NULL,0,1,0,0,aPrice,i0+i);
         x=aVal_0+i*(aVal_1-aVal_0)/aPeriod;
         aArr[i]=y-x;
      } 
   return(rRetError);   
}

void fFurie(double aArr[],double & aA[],double & aB[],double & aR[], double & aF[]){
   int tN=ArraySize(aArr);
   int tM=tN/2;
   
   ArrayResize(aA,tM);
   ArrayResize(aB,tM);  
   ArrayResize(aR,tM); 
   ArrayResize(aF,tM);                            
   
      for (int ti=1;ti<tM;ti++){
         aA[ti]=0;
         aB[ti]=0;
            for(int tj=0;tj<tN;tj++){
               aA[ti]+=aArr[tj]*MathSin(ti*6.28*tj/tN);
               aB[ti]+=aArr[tj]*MathCos(ti*6.28*tj/tN);
            }
         aA[ti]=2*aA[ti]/tN;
         aB[ti]=2*aB[ti]/tN;  
         aR[ti]=MathSqrt(MathPow(aA[ti],2)+MathPow(aB[ti],2));
         aF[ti]=fMyArcTan(aB[ti],aA[ti]);
      }
            
}

double fMyArcTan(double aS,double aC){
   if(aS==0){
      return(0);   
   }
   if(aC==0){
      if(aS>0){
         return(MathArctan(1)*2);
      }
      else{
         if(aS<0){
            return(MathArctan(1)*6);         
         }
      }
   }
   else{
      if(aS>0){
         if(aC>0){
            return(MathArctan(aS/aC));  
         }
         else{
            return(MathArctan(aS/aC)+MathArctan(1)*4);          
         }   
      }
      else{
         if(aC>0){
            return(MathArctan(aS/aC)+MathArctan(1)*8);           
         }
         else{
            return(MathArctan(aS/aC)+MathArctan(1)*4);           
         }      
      }
   }
}



void fObjDeleteByPrefix(string aPrefix){
   for(int i=ObjectsTotal()-1;i>=0;i--){
      if(StringFind(ObjectName(i),aPrefix,0)==0){
         ObjectDelete(ObjectName(i));
      }
   }
}
Can someone please help what exactly should be changed where to see the volume cycles?

Regards
Know Thy Setup. Know Thyself.


Who is online

Users browsing this forum: No registered users and 11 guests