Patterns_By_Correlation_with_ZZ

Indicators Used
Moving average indicatorMoving average indicator
Miscellaneous
It issuies visual alerts to the screenImplements a curve of type %1
0 Views
0 Downloads
0 Favorites
Patterns_By_Correlation_with_ZZ
ÿþ

#property version   "1.00"

#property strict

#property indicator_chart_window



#property indicator_buffers 3

#property indicator_color1  clrWhite

#property indicator_width1  2

#property indicator_color2  clrBlue

#property indicator_width2  2

#property indicator_color3  clrOrange

#property indicator_width3  2



//--- input parameters

input bool     Recomend=true;           //Give buy/sell integral

input double   exponent=1;              //Buy/sell exponent

input int      Forecast_Length  =300;   //Forecat line length, bars

extern int     DaysBackTimeBased=99999; //History to check for TimeBased mode, days

extern int     DaysBackTimeFree =1000;  //History to check for TimeFree mode, days

extern int     DaysBackZZ=4444;         //History to check for ZZ mode, days

input int      Max_lines=5;             //Max. number of forecast lines

input ENUM_APPLIED_PRICE  Price_Type=PRICE_CLOSE; //Forecast based on price

input double   min_corr=0.8;                      //Min. correlation to be shown

input int      fs=14;                             //Font size (buttons scale)

input bool     AutoColor=true;                    //Automatic color for forecast lines (gradient)

input color    ColorTB=clrWhite;                  //Fixed color of TimeBased forecast lines

input color    ColorTF=clrSteelBlue;              //Fixed color of TimeFree forecast lines

input color    Color_FR=clrWheat;                  //Frozen lines color

input bool     LinesInRectangle=false;            //Draw the lines in rectangkes (scaled history data)

input bool     ZeroWhereTheStart=false;           //Start at the left side of rectangle



input int      Best_Fit=15;                       //Best fit, %

input color    ColorBF=clrTomato;                 //Best fit line color

input int      WidthBF=4;                         //Best fit line width



extern bool Use_Close=true; //Based on close price

extern int InpDepth=5;     // ZZ Depth, bars (indicator setting)

 int InpDeviation=5;  // Deviation

 int InpBackstep=3;   // Backstep

extern int Nodes=15;               // Pattern nodes

extern int Nodes_Forecast=30;     // Forecast nodes

extern double min_corr_ZZ=0.95;   // Min correlation for ZZ mode

extern int StepZZ=-1;                //Wave size, bars (0 - Auto)



//---- indicator buffers

double ExtZigzagBuffer[];

double ExtHighBuffer[];

double ExtLowBuffer[];

double up[],dn[];

//--- globals

int    ExtLevel=3; // recounting's depth of extremums



string obj_pref ="Patt_";

string obj_pref2="pObj_";

string obj_pref_f="frz_";



struct CORR

{

 datetime time;

 int      shift_bar;

 double   shift_price;

 double   corr;

 double   scale;

 double   fit;

};



CORR days[],bars[];



bool Use_Best_fit,first_push;



double buy,sell;



int OnInit()

  {

   Butt2(8,"Swing"     ,clrGray  ,fs,true);

   Butt2(7,"ZZ"        ,clrGray  ,fs,true);

   Butt2(6,"BestFit ON",clrOrange,fs,false);Use_Best_fit=true;

   Butt2(5,"Del. rect.",clrOrange,fs,true);

   Butt2(4,"Freeze line",clrBlue ,fs,true);

   Butt2(3,"Del. frozen",clrRed  ,fs,true);

   Butt2(2,"TimeBased" ,clrGray  ,fs,true);

   Butt2(1,"TimeFree"  ,clrGray  ,fs,true);

   Butt2(0,"Del. lines",clrRed   ,fs,true);

   

   DaysBackTimeBased=MathMin(DaysBackTimeBased,20000);

   DaysBackTimeFree =MathMin(DaysBackTimeFree ,20000);



   ArrayResize(days,DaysBackTimeBased);

   if(ArraySize(days)<10) Alert("Please set other value for TimeBased days back: ",DaysBackTimeBased);

   

   

     if(InpBackstep>=InpDepth) InpDepth=InpBackstep+1;

  /*   {

      Print("Backstep cannot be greater or equal to Depth");

      return(INIT_FAILED);

     }

     */

//--- 2 additional buffers

   IndicatorBuffers(5);

   SetIndexStyle (0,DRAW_NONE);

   SetIndexBuffer(0,ExtZigzagBuffer);

   SetIndexBuffer(1,dn);

   SetIndexBuffer(2,up);



   SetIndexStyle(1,DRAW_ARROW);

   SetIndexArrow(1,159);

   SetIndexStyle(2,DRAW_ARROW);

   SetIndexArrow(2,159);

   SetIndexBuffer(3,ExtHighBuffer);

   SetIndexBuffer(4,ExtLowBuffer);



   SetIndexEmptyValue(0,0.0);



   return(INIT_SUCCEEDED);

  }



void OnDeinit(const int reason)

{

 Comment("");

 ObjectsDeleteAll(0,obj_pref);

 ObjectsDeleteAll(0,obj_pref2);

}





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[])

  {





   return(rates_total);

  }

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

//| ChartEvent function                                              |

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

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

  

    //Count ZZ forecast

    if(!ObjectGetInteger(0,obj_pref+"_B2_8_",OBJPROP_STATE))

     {

      ObjectSetInteger(0,obj_pref+"_B2_8_",OBJPROP_BGCOLOR,clrWhite);

      ObjectSetInteger(0,obj_pref+"_B2_8_",OBJPROP_COLOR,clrBlack);

      ObjectSetInteger(0,obj_pref+"_B2_1_",OBJPROP_STATE,true);

      ObjectSetInteger(0,obj_pref+"_B2_2_",OBJPROP_STATE,true);

      ObjectSetInteger(0,obj_pref+"_B2_4_",OBJPROP_STATE,true);

      if(sparam=="")

       {

        if(!first_push) {first_push=true;return;}

        double price1;

        datetime Time1;

        int tmp;

        ChartXYToTimePrice(0,(int)lparam,(int)dparam,tmp,Time1,price1);

        Time1=MathMin(Time1,Time[1]);

        int pos=iBarShift(NULL,0,Time1);

        ObjectSetInteger(0,obj_pref+"_B2_8_",OBJPROP_STATE,true);

        ObjectSetInteger(0,obj_pref+"_B2_8_",OBJPROP_BGCOLOR,clrBlack);

        ObjectSetInteger(0,obj_pref+"_B2_8_",OBJPROP_COLOR,clrGray);

        

        string Name=obj_pref2+"ClicZZ"+IntegerToString(pos);

        if(ObjectCreate(0,Name,OBJ_TREND,0,Time1,0,Time1,High[pos]*2))

        {

         ObjectSetInteger(0,Name,OBJPROP_WIDTH,1);

         ObjectSetInteger(0,Name,OBJPROP_STYLE,STYLE_DASHDOTDOT);

         ObjectSetInteger(0,Name,OBJPROP_RAY_RIGHT,false);

         ObjectSetInteger(0,Name,OBJPROP_COLOR,CountZZ(pos)?clrDarkTurquoise:clrRed);

         ObjectSetInteger(0,Name,OBJPROP_HIDDEN,true);

         ObjectSetInteger(0,Name,OBJPROP_SELECTABLE,false);

         ObjectSetInteger(0,Name,OBJPROP_BACK,false);

        }

        first_push=false;

        

       }

      }

     else 

      {

       ObjectSetInteger(0,obj_pref+"_B2_8_",OBJPROP_BGCOLOR,clrBlack);

       ObjectSetInteger(0,obj_pref+"_B2_8_",OBJPROP_COLOR,clrGray);

       first_push=false;

      }

      

      

    //Draw ZZ

    if(!ObjectGetInteger(0,obj_pref+"_B2_7_",OBJPROP_STATE))

     {

      ObjectSetInteger(0,obj_pref+"_B2_7_",OBJPROP_BGCOLOR,clrWhite);

      ObjectSetInteger(0,obj_pref+"_B2_7_",OBJPROP_COLOR,clrBlack);

      SetIndexStyle(0,DRAW_SECTION);

      SetIndexStyle(1,DRAW_ARROW);

      SetIndexStyle(2,DRAW_ARROW);

     }

    else 

     {

      ObjectSetInteger(0,obj_pref+"_B2_7_",OBJPROP_BGCOLOR,clrBlack);

      ObjectSetInteger(0,obj_pref+"_B2_7_",OBJPROP_COLOR,clrGray);

      SetIndexStyle(0,DRAW_NONE);

      SetIndexStyle(1,DRAW_NONE);

      SetIndexStyle(2,DRAW_NONE);

     }



  

    //Delete lines

    if(!ObjectGetInteger(0,obj_pref+"_B2_0_",OBJPROP_STATE))

     {

      ObjectSetInteger(0,obj_pref+"_B2_8_",OBJPROP_STATE,true);

      ObjectSetInteger(0,obj_pref+"_B2_0_",OBJPROP_STATE,true);

      ObjectSetInteger(0,obj_pref+"_B2_1_",OBJPROP_STATE,true);

      ObjectSetInteger(0,obj_pref+"_B2_1_",OBJPROP_BGCOLOR,clrBlack);

      ObjectSetInteger(0,obj_pref+"_B2_1_",OBJPROP_COLOR,clrGray);

      ObjectSetInteger(0,obj_pref+"_B2_2_",OBJPROP_STATE,true);

      ObjectSetInteger(0,obj_pref+"_B2_2_",OBJPROP_BGCOLOR,clrBlack);

      ObjectSetInteger(0,obj_pref+"_B2_2_",OBJPROP_COLOR,clrGray);

      ObjectsDeleteAll(0,obj_pref2);

      //Alert("Cleaned ",_Symbol," ",StringSubstr(EnumToString(ENUM_TIMEFRAMES(_Period)),7));

     }

     

     //Delete rectangles

     if(!ObjectGetInteger(0,obj_pref+"_B2_5_",OBJPROP_STATE))

     {

      ObjectSetInteger(0,obj_pref+"_B2_8_",OBJPROP_STATE,true);

      ObjectSetInteger(0,obj_pref+"_B2_5_",OBJPROP_STATE,true);

      ObjectSetInteger(0,obj_pref+"_B2_1_",OBJPROP_STATE,true);

      ObjectSetInteger(0,obj_pref+"_B2_1_",OBJPROP_BGCOLOR,clrBlack);

      ObjectSetInteger(0,obj_pref+"_B2_1_",OBJPROP_COLOR,clrGray);

      ObjectSetInteger(0,obj_pref+"_B2_2_",OBJPROP_STATE,true);

      ObjectSetInteger(0,obj_pref+"_B2_2_",OBJPROP_BGCOLOR,clrBlack);

      ObjectSetInteger(0,obj_pref+"_B2_2_",OBJPROP_COLOR,clrGray);

      ObjectsDeleteAll(0,"Rect");

     }

    

    //TimeBased

    if(ObjectGetInteger(0,obj_pref+"_B2_2_",OBJPROP_STATE))

     {

      ObjectSetInteger(0,obj_pref+"_B2_2_",OBJPROP_BGCOLOR,clrBlack);

      ObjectSetInteger(0,obj_pref+"_B2_2_",OBJPROP_COLOR,clrGray);

     }

    else

     {

      if(sparam==obj_pref+"_B2_2_")

       {

        ObjectSetInteger(0,obj_pref+"_B2_8_",OBJPROP_STATE,true);

        ObjectSetInteger(0,obj_pref+"_B2_1_",OBJPROP_STATE,true);

        ObjectSetInteger(0,obj_pref+"_B2_1_",OBJPROP_BGCOLOR,clrBlack);

        ObjectSetInteger(0,obj_pref+"_B2_1_",OBJPROP_COLOR,clrGray);

       }

      ObjectSetInteger(0,obj_pref+"_B2_2_",OBJPROP_BGCOLOR,clrWhite);

      ObjectSetInteger(0,obj_pref+"_B2_2_",OBJPROP_COLOR,clrBlack);

      

      if(StringFind(sparam,"Rect")==0)

       {

        datetime Start =(datetime)MathMin(ObjectGetInteger(0,sparam,OBJPROP_TIME1),ObjectGetInteger(0,sparam,OBJPROP_TIME2));

        datetime Finish=(datetime)MathMin(Time[0],

                                  MathMax(ObjectGetInteger(0,sparam,OBJPROP_TIME1),ObjectGetInteger(0,sparam,OBJPROP_TIME2)));



        ObjectSetInteger(0,sparam,OBJPROP_BACK,false);

        ObjectSetInteger(0,sparam,OBJPROP_WIDTH,4);

        ObjectSetInteger(0,sparam,OBJPROP_COLOR,TimeBased(Start,Finish)?clrWhite:clrRed);

        

        ObjectSetInteger(0,obj_pref+"_B2_2_",OBJPROP_STATE,true);

        ObjectSetInteger(0,obj_pref+"_B2_2_",OBJPROP_BGCOLOR,clrBlack);

        ObjectSetInteger(0,obj_pref+"_B2_2_",OBJPROP_COLOR,clrGray);

       }

     }

    

    //TimeFree

    if(ObjectGetInteger(0,obj_pref+"_B2_1_",OBJPROP_STATE))

     {

      ObjectSetInteger(0,obj_pref+"_B2_1_",OBJPROP_BGCOLOR,clrBlack);

      ObjectSetInteger(0,obj_pref+"_B2_1_",OBJPROP_COLOR,clrGray);

     }

    else

     {

      if(sparam==obj_pref+"_B2_1_")

       {

        ObjectSetInteger(0,obj_pref+"_B2_8_",OBJPROP_STATE,true);

        ObjectSetInteger(0,obj_pref+"_B2_2_",OBJPROP_STATE,true);

        ObjectSetInteger(0,obj_pref+"_B2_2_",OBJPROP_BGCOLOR,clrBlack);

        ObjectSetInteger(0,obj_pref+"_B2_2_",OBJPROP_COLOR,clrGray);

       }

      ObjectSetInteger(0,obj_pref+"_B2_1_",OBJPROP_BGCOLOR,clrWhite);

      ObjectSetInteger(0,obj_pref+"_B2_1_",OBJPROP_COLOR,clrBlack);

      if(StringFind(sparam,"Rect")==0)

       {

        datetime Start =(datetime)MathMin(ObjectGetInteger(0,sparam,OBJPROP_TIME1),ObjectGetInteger(0,sparam,OBJPROP_TIME2));

        datetime Finish=(datetime)MathMin(Time[0],

                                  MathMax(ObjectGetInteger(0,sparam,OBJPROP_TIME1),ObjectGetInteger(0,sparam,OBJPROP_TIME2)));



        ObjectSetInteger(0,sparam,OBJPROP_BACK,false);

        ObjectSetInteger(0,sparam,OBJPROP_WIDTH,4);

        ObjectSetInteger(0,sparam,OBJPROP_COLOR,TimeFree(Start,Finish)?clrWhite:clrRed);

        

        ObjectSetInteger(0,obj_pref+"_B2_1_",OBJPROP_STATE,true);

        ObjectSetInteger(0,obj_pref+"_B2_1_",OBJPROP_BGCOLOR,clrBlack);

        ObjectSetInteger(0,obj_pref+"_B2_1_",OBJPROP_COLOR,clrGray);

       }



     }

   

    //delete frozen

    if(!ObjectGetInteger(0,obj_pref+"_B2_3_",OBJPROP_STATE))

     {

      ObjectSetInteger(0,obj_pref+"_B2_3_",OBJPROP_STATE,true);

      if(StringFind(sparam,obj_pref+"_B2_3_")==0)

        ObjectsDeleteAll(0,obj_pref_f);

     }

    

    //set frozen

    if(ObjectGetInteger(0,obj_pref+"_B2_4_",OBJPROP_STATE))

     {

      ObjectSetInteger(0,obj_pref+"_B2_4_",OBJPROP_BGCOLOR,clrBlack);

      ObjectSetInteger(0,obj_pref+"_B2_4_",OBJPROP_COLOR,clrGray);

      

     }

    else

     {

      ObjectSetInteger(0,obj_pref+"_B2_4_",OBJPROP_BGCOLOR,clrWhite);

      ObjectSetInteger(0,obj_pref+"_B2_4_",OBJPROP_COLOR,clrBlack);

      ObjectSetInteger(0,obj_pref+"_B2_2_",OBJPROP_STATE,true);

      ObjectSetInteger(0,obj_pref+"_B2_1_",OBJPROP_STATE,true);

      if(StringFind(sparam,obj_pref2)==0)

       {

        string pref=StringSubstr(sparam,0,StringFind(sparam,"_",StringLen(obj_pref2)+1)+1);

        for(int i=0;i<ObjectsTotal(0,0,OBJ_TREND);i++)

         {

          string name=ObjectName(0,i,0,OBJ_TREND);

          if(StringFind(name,pref)==0) 

           {

            ObjectSetInteger(0,name,OBJPROP_COLOR,Color_FR);

            ObjectSetInteger(0,name,OBJPROP_BACK,true);

            ObjectSetString (0,name,OBJPROP_NAME ,obj_pref_f+name);

           }

         }

       }

     }

     

   //Best Fit

   if(ObjectGetInteger(0,obj_pref+"_B2_6_",OBJPROP_STATE))

     {

      ObjectSetInteger(0,obj_pref+"_B2_6_",OBJPROP_BGCOLOR,clrBlack);

      ObjectSetInteger(0,obj_pref+"_B2_6_",OBJPROP_COLOR,clrGray);

      ObjectSetString (0,obj_pref+"_B2_6_",OBJPROP_TEXT,"BestFit OFF");

      Use_Best_fit=false;

     }

    else

     {

      ObjectSetInteger(0,obj_pref+"_B2_6_",OBJPROP_BGCOLOR,clrWhite);

      ObjectSetInteger(0,obj_pref+"_B2_6_",OBJPROP_COLOR,clrBlack);

      ObjectSetString (0,obj_pref+"_B2_6_",OBJPROP_TEXT,"BestFit ON");

      Use_Best_fit=true;

     }

   

  }





void Text(int time, double price, string text="",color CLR=clrSalmon,int anch=ANCHOR_LEFT_LOWER,int FS=10)

{

   string name=obj_pref2+"_T1_"+IntegerToString(time)+"_"+DoubleToString(price,_Digits);

   if(ObjectCreate(name,OBJ_TEXT,0,0,0))

   {

    ObjectSetInteger(0,name,OBJPROP_ANCHOR,anch);

    ObjectSetInteger(0,name,OBJPROP_TIME1,time>0?Time[time]:(Time[0]-_Period*60*time));

    ObjectSetDouble (0,name,OBJPROP_PRICE1,price);

    ObjectSetString (0,name,OBJPROP_TEXT,text);

    ObjectSetInteger(0,name,OBJPROP_FONTSIZE,FS);

    ObjectSetString (0,name,OBJPROP_FONT,"Arial");

    ObjectSetInteger(0,name,OBJPROP_COLOR,CLR);

    ObjectSetInteger(0,name,OBJPROP_BACK,false);

    ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);

    ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);

   }

}



void Butt2(int row,string text="",color CLR=0,int FS=10,bool state=true,color BG=clrBlack)

{

   string name=obj_pref+"_B2_"+IntegerToString(row)+"_";

   if(ObjectFind(name)<0) 

   {

   ObjectCreate(name,OBJ_BUTTON,0,0,0);

   ObjectSetInteger(0,name,OBJPROP_CORNER,CORNER_RIGHT_LOWER);

   ObjectSetInteger(0,name,OBJPROP_XDISTANCE,FS*9); 

   ObjectSetInteger(0,name,OBJPROP_YDISTANCE,row*16*FS/10+30); 

   ObjectSetInteger(0,name,OBJPROP_XSIZE,FS*9); 

   ObjectSetInteger(0,name,OBJPROP_YSIZE,16*FS/10);

   ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);

   ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);

   ObjectSetInteger(0,name,OBJPROP_ALIGN,ALIGN_LEFT);

   ObjectSetString (0,name,OBJPROP_FONT,"Arial");

   ObjectSetInteger(0,name,OBJPROP_BORDER_COLOR,clrBlack);

   ObjectSetInteger(0,name,OBJPROP_BACK,false); 

   ObjectSetInteger(0,name,OBJPROP_FONTSIZE,FS);

   ObjectSetInteger(0,name,OBJPROP_ZORDER,10);

   }

   ObjectSetInteger(0,name,OBJPROP_BGCOLOR,BG);

   ObjectSetInteger(0,name,OBJPROP_STATE,state);

   ObjectSetString (0,name,OBJPROP_TEXT,text);

   ObjectSetInteger(0,name,OBJPROP_COLOR,CLR); 

   

}



double CorrelationV(double &arr1[], double &arr2[])

{

   int period=ArraySize(arr1);

   

   if(period!=ArraySize(arr2)) {Alert("Arrays are not equal!! ",period,"!=",ArraySize(arr2));return(0);}



   int i;

   double D1, D2;

   double S1=0;

   double S2=0;

   double S3=0;

   double M1=0;//iMAOnArray(arr1,0,period,0,MODE_SMA,shift);

   double M2=0;//iMAOnArray(arr2,0,period,0,MODE_SMA,shift);

   

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

   {

    M1+=arr1[i];

    M2+=arr2[i];

   }

   M1/=period;

   M2/=period;

   

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

   {

      D1=arr1[i]-M1;

      D2=arr2[i]-M2;

      S1+=D1*D2;

      S2+=D1*D1;

      S3+=D2*D2;

   }



   if(S2*S3!=0)

   return(S1/(MathSqrt(S2*S3)));



   return(0);

}



bool TimeBased(datetime Start,datetime Finish)

 {

  int FinishH=TimeHour  (Finish),

      FinishM=TimeMinute(Finish),

      finish_bar=iBarShift(_Symbol,0,Finish),

      start_bar =iBarShift(_Symbol,0,Start),

      depth=0;

      

  if(_Period>15) {Alert("Period is too High! Use TimeFree method!");return(false);}

   

   double Curr[],

          Temp[],

          bf1[],

          bf2[];

   ArrayResize(Curr,1);

   ArraySetAsSeries(Curr,true);

   ArraySetAsSeries(Temp,true);

   ArraySetAsSeries(bf1,true);

   ArraySetAsSeries(bf2,true);

   

   //for(int j=0;j<depth;j++)

   int j=0,

       curr_bar=finish_bar;

   

   while(curr_bar<start_bar)

    {

     ArrayResize(Curr,j+1);

     int day_of_the_week=TimeDayOfWeek(Time[curr_bar]);

     if(day_of_the_week>0 && day_of_the_week<6)

      Curr[j]=iMA(_Symbol,0,1,0,MODE_SMA,Price_Type,curr_bar);

     else {curr_bar++;continue;}

     curr_bar++;

     j++;

    }

    

  depth=j;

  //Alert(depth);



  ArrayResize(Temp,depth);

  

  int bf_depth=depth*Best_Fit/100;

  ArrayResize(bf1,bf_depth);

  ArrayResize(bf2,bf_depth);



  double volatility=(Curr[ArrayMaximum(Curr)]-Curr[ArrayMinimum(Curr)]);

  

  double best_corr=0;

  int day=-1, cntr=0;

  for(int i=finish_bar+2;i<Bars-PERIOD_D1/_Period-depth;i++)

   {

    if(FinishH!=TimeHour(Time[i]) || FinishM!=TimeMinute(Time[i]) || TimeDayOfWeek(Time[i])==0 || TimeDayOfWeek(Time[i])==6) continue;

    day++;

    if(day>=DaysBackTimeBased) break;

    

    j=0;

    

    curr_bar=iBarShift(_Symbol,0,Time[i]);

    while(j<depth)

    {

     int day_of_the_week=TimeDayOfWeek(Time[curr_bar]);

     if(day_of_the_week>0 && day_of_the_week<6)

      Temp[j]=iMA(_Symbol,0,1,0,MODE_SMA,Price_Type,curr_bar);

     else {curr_bar++;continue;}

     curr_bar++;

     j++;

    }

    //for(j=0;j<depth;j++)

    // Temp[j]=iMA(_Symbol,0,1,0,MODE_SMA,Price_Type,iBarShift(_Symbol,0,Time[i]-_Period*j*60));



    days[day].corr=CorrelationV(Curr,Temp);

    best_corr=MathMax(best_corr,fabs(days[day].corr));

    if(fabs(days[day].corr)<min_corr) {days[day].corr=0;continue;}

    

    days[day].time=Time[i];

    days[day].shift_bar=i;

    days[day].shift_price=Curr[ZeroWhereTheStart?depth-1:0]-iMA(_Symbol,0,1,0,MODE_SMA,Price_Type,ZeroWhereTheStart?i+depth:i);

    if((Temp[ArrayMaximum(Temp)]-Temp[ArrayMinimum(Temp)])==0) {continue;}//Alert(Time[i]);return(false);}

    days[day].scale=volatility/(Temp[ArrayMaximum(Temp)]-Temp[ArrayMinimum(Temp)]);

    

    

    if(Use_Best_fit)

     {

      ArrayCopy(bf1,Curr,0,0,bf_depth);

      ArrayCopy(bf2,Temp,0,0,bf_depth);

      days[day].fit=fabs(CorrelationV(bf1,bf2));

     }



    cntr++;

   }



  if(cntr==0) {Alert("Found nothing with correlation higher then ",min_corr,". Best result was ",DoubleToString(best_corr,3));return(false);}

  

  buy=sell=0;

  

  double best_fit=0,bf_corr=0;

  int    best_fit_day=0;

  for(int i=0;i<Max_lines;i++)

   {

    int best_day=0;

    for(j=1;j<day;j++)

     {

      if(fabs(days[j].corr)>fabs(days[best_day].corr)) best_day=j;

      //if(days[j].corr!=0) Print(days[j].corr);

     }

    

    if(fabs(days[best_day].corr)>=min_corr)

     {

      if(!Use_Best_fit)

      DrawChartU(finish_bar,depth,days[best_day],MathMax(3-i,1),ColorTB);

      if(days[best_day].fit>best_fit)

       {

        best_fit=days[best_day].fit;

        best_fit_day=best_day;

        bf_corr=days[best_day].corr;

       }

      days[best_day].corr=0;

     }

    else break;

   }

  

  //Alert (best_fit_day," -> ",best_fit);

  if(Use_Best_fit)

   {

    days[best_fit_day].corr=bf_corr;

    DrawChartU(finish_bar,depth,days[best_fit_day],WidthBF,ColorBF,"bftb");

   }

  

  if(Recomend) 

   {

    double sell_prc=0,

           buy_prc=0;

           

    //Print(buy, "  ", sell);

    if(sell!=0) sell_prc=-sell/(buy-sell)*100;

                buy_prc =100-sell_prc;

    Text(finish_bar+(Use_Best_fit?0:1),High[finish_bar],(buy_prc>sell_prc?"Buy "+DoubleToString(buy_prc,1)+"%":"Sell "+DoubleToString(sell_prc,1)+"%"),(buy_prc>sell_prc?clrOrchid:clrTomato),(!Use_Best_fit?ANCHOR_RIGHT_LOWER:ANCHOR_LEFT_LOWER),20);

   }

  return(day>0);

 }

 

bool TimeFree(datetime Start,datetime Finish)

 {



  uint begin=GetTickCount();

  

  int FinishH=TimeHour  (Finish),

      FinishM=TimeMinute(Finish),

      finish_bar=iBarShift(_Symbol,0,Finish),

      start_bar =iBarShift(_Symbol,0,Start),

      depth=0;



   double Curr[],

          Temp[],

          bf1[],

          bf2[];

   ArrayResize(Curr,1);

   ArraySetAsSeries(Curr,true);

   ArraySetAsSeries(Temp,true);

   ArraySetAsSeries(bf1,true);

   ArraySetAsSeries(bf2,true);

   

   //for(int j=0;j<depth;j++)

   int j=0,

       curr_bar=finish_bar;

   

   while(curr_bar<start_bar)

    {

     ArrayResize(Curr,j+1);

     int day_of_the_week=TimeDayOfWeek(Time[curr_bar]);

     if(day_of_the_week>0 && day_of_the_week<6)

      Curr[j]=iMA(_Symbol,0,1,0,MODE_SMA,Price_Type,curr_bar);

     else {curr_bar++;continue;}

     curr_bar++;

     j++;

    }

    

  depth=j;



  ArrayResize(Temp,depth);

  

  int bf_depth=depth*Best_Fit/100;

  ArrayResize(bf1,bf_depth);

  ArrayResize(bf2,bf_depth);

  

  double volatility=(Curr[ArrayMaximum(Curr)]-Curr[ArrayMinimum(Curr)]);

  

  ArrayFree(bars);

  ArrayResize(bars,Bars);



  double best_corr=0;

  int cntr=0,day=0;

  for(int i=finish_bar+depth;i<Bars-depth*2 && !IsStopped();i++)

   {

    if(0==TimeHour(Time[i]) && 0==TimeMinute(Time[i])) day++;

    if(day>=DaysBackTimeFree) break;

    

    j=0;

    curr_bar=iBarShift(_Symbol,0,Time[i]);

    while(j<depth)

    {

     int day_of_the_week=TimeDayOfWeek(Time[curr_bar]);

     if(day_of_the_week>0 && day_of_the_week<6)

      Temp[j]=iMA(_Symbol,0,1,0,MODE_SMA,Price_Type,curr_bar);

     else {curr_bar++;continue;}

     curr_bar++;

     j++;

    }

    

    bars[i].corr=CorrelationV(Curr,Temp);

    best_corr=MathMax(best_corr,fabs(bars[i].corr));

    if(fabs(bars[i].corr)<min_corr) continue;

    

    bars[i].time=Time[i];

    bars[i].shift_bar=i;

    bars[i].shift_price=Curr[ZeroWhereTheStart?depth-1:0]-iMA(_Symbol,0,1,0,MODE_SMA,Price_Type,ZeroWhereTheStart?i+depth:i);

    if((Temp[ArrayMaximum(Temp)]-Temp[ArrayMinimum(Temp)])==0) {continue;}//Alert(Time[i]);return(false);}

    bars[i].scale=volatility/(Temp[ArrayMaximum(Temp)]-Temp[ArrayMinimum(Temp)]);

    

    if(Use_Best_fit)

     {

      ArrayCopy(bf1,Curr,0,0,bf_depth);

      ArrayCopy(bf2,Temp,0,0,bf_depth);

      bars[i].fit=fabs(CorrelationV(bf1,bf2));

     }

    

    cntr++;

    

   }



  if(cntr<1) {Alert("Found nothing with correlation higher then ",min_corr,". Best result was ",DoubleToString(best_corr,3));return(false);}

  

  buy=sell=0;

  

  double best_fit=0,bf_corr=0;

  int    best_fit_day=0;

  for(int i=0;i<Max_lines;i++)

   {

    int best_day=0;

    for(j=1;j<Bars-depth*2;j++)

     {

      if(fabs(bars[j].corr)>fabs(bars[best_day].corr)) best_day=j;

      //if(days[j].corr!=0) Print(days[j].corr);

     }

    

    if(fabs(bars[best_day].corr)>=min_corr)

     {

      if(!Use_Best_fit)

      DrawChartU(finish_bar,depth,bars[best_day],MathMax(3-i,1),ColorTF);

      {

        best_fit=bars[best_day].fit;

        best_fit_day=best_day;

        bf_corr=bars[best_day].corr;

      }

      for(int k=best_day-depth;k<best_day+depth;k++)

       bars[k].corr=0;

     }

    else break;

   }

   

   

   if(Use_Best_fit)

   {

    bars[best_fit_day].corr=bf_corr;

    DrawChartU(finish_bar,depth,bars[best_fit_day],WidthBF,ColorBF,"bftf");

   }

  

  if(Recomend) 

   {

    double sell_prc=0,

           buy_prc=0;

           

    //Print(buy, "  ", sell);

    if(sell!=0) sell_prc=-sell/(buy-sell)*100;

                buy_prc =100-sell_prc;

    Text(finish_bar+(Use_Best_fit?2:3),Low[finish_bar],(buy_prc>sell_prc?"Buy "+DoubleToString(buy_prc,1)+"%":"Sell "+DoubleToString(sell_prc,1)+"%"),(buy_prc>sell_prc?clrSkyBlue:clrOrange),(!Use_Best_fit?ANCHOR_RIGHT_UPPER:ANCHOR_LEFT_UPPER),20);

   }

  

  ArrayFree(bars);

  

  Print("Processing time: ",DoubleToString((GetTickCount()-begin)/1000.0,1)," sec");



  return(cntr>0);

 

 }



void DrawChartU(int Finsh_bar,int depth,CORR &data,int width,color Color,string pr="")

 {

  int reverse=data.corr>0?1:-1;

  double zero=iMA(_Symbol,0,1,0,MODE_SMA,Price_Type,ZeroWhereTheStart?Finsh_bar+depth:Finsh_bar);

  //Text(Finsh_bar,zero,"------------->",clrWhite,ANCHOR_RIGHT_LOWER);

  

  double Temp[],curr_price;

  string pref;

  

  if(LinesInRectangle)

  {

  ArrayResize(Temp,depth);

  for(int j=0;j<depth;j++)

   Temp[j]=iMA(_Symbol,0,1,0,MODE_SMA,Price_Type,j+data.shift_bar);



  pref=IntegerToString(data.shift_bar)+pr;

  for(int j=0;j<depth-1;j++)

   {

    DrawLineU(pref,j+Finsh_bar,zero+reverse*(Temp[j  ]+data.shift_price-zero)*data.scale,

                               zero+reverse*(Temp[j+1]+data.shift_price-zero)*data.scale);

                               //+shift_price,

                               //);

   }

  }

 

  ArrayResize(Temp,Forecast_Length);

  for(int j=0;j<Forecast_Length;j++)

   Temp[j]=iMA(_Symbol,0,1,0,MODE_SMA,Price_Type,data.shift_bar-j);

  

  

  double min=min_corr-MathMin(min_corr*0.8,0.0),

         max=MathMax(fabs(data.corr),0.9),

         x = (fabs(data.corr) - min)/(max-min)*255,

         y = (max - fabs(data.corr))/(max-min)*255;



  pref=IntegerToString(data.shift_bar);

  for(int j=0;j<Forecast_Length-1;j++)

   {

    curr_price=zero+reverse*(Temp[j  ]+data.shift_price-zero)*data.scale;

    DrawLineU(pref,Finsh_bar-j,zero+reverse*(Temp[j+1]+data.shift_price-zero)*data.scale,

                               zero+reverse*(Temp[j  ]+data.shift_price-zero)*data.scale,

                               pr==""?(AutoColor?StringToColor(DoubleToString(0,0)+","+DoubleToString(0,0)+","+DoubleToString(x,0)):Color):(AutoColor?(curr_price<zero?clrTomato:clrOrchid):Color),

                               width);

                               //+shift_price,

                               //);

    if(curr_price>zero) buy +=(curr_price-zero)*pow((Forecast_Length-j),exponent)*fabs(data.corr)*width;

    else                sell+=(curr_price-zero)*pow((Forecast_Length-j),exponent)*fabs(data.corr)*width;

   }

  Text(Finsh_bar-Forecast_Length,

       zero+reverse*(Temp[Forecast_Length-1]+data.shift_price-zero)*data.scale,

       DoubleToString(data.corr,3)+" .... "+TimeToString(Time[data.shift_bar],TIME_DATE)+(Use_Best_fit?" fits:"+DoubleToString(data.fit,3):""),

       pr==""?(AutoColor?StringToColor(DoubleToString(0,0)+","+DoubleToString(0,0)+","+DoubleToString(x,0)):Color):Color);

 }

 

void DrawLineU(string pref,int i,double p,double p_prev,color CLR=clrSalmon,int Width=1)//,ENUM_LINE_STYLE style=STYLE_SOLID)

{

  string Name=obj_pref2+pref+"_"+IntegerToString(i);

  if(ObjectCreate(0,Name,OBJ_TREND,0,i<0?Time[0]-_Period*60*i:Time[i],p,i<0?Time[0]-_Period*60*(i+1):Time[i+1],p_prev))

   {

    ObjectSetInteger(0,Name,OBJPROP_WIDTH,Width);

    ObjectSetInteger(0,Name,OBJPROP_STYLE,STYLE_SOLID);

    ObjectSetInteger(0,Name,OBJPROP_RAY_RIGHT,false);

    ObjectSetInteger(0,Name,OBJPROP_COLOR,CLR);

    ObjectSetInteger(0,Name,OBJPROP_HIDDEN,true);

    ObjectSetInteger(0,Name,OBJPROP_SELECTABLE,false);

    ObjectSetInteger(0,Name,OBJPROP_BACK,false);

  }

}









bool CountZZ(int click,bool fill=true)

  {

   

   uint begin=GetTickCount();

  

   int    i,limit=Bars-InpDepth,whatlookfor=0;

   int    back,pos,lasthighpos=0,lastlowpos=0;

   double extremum;

   double curlow=0.0,curhigh=0.0,lasthigh=0.0,lastlow=0.0;



   if(Bars-1<InpDepth || InpBackstep>=InpDepth)

      return(false);



   ArrayInitialize(ExtZigzagBuffer,0.0);

   ArrayInitialize(ExtHighBuffer,0.0);

   ArrayInitialize(ExtLowBuffer,0.0);

   ArrayInitialize(up,0.0);

   ArrayInitialize(dn,0.0);



 if(!fill) return(true);



 if(Use_Close)

  {

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

     {

      //--- find lowest Low in depth of bars

      extremum=Close[iLowest(NULL,0,MODE_CLOSE,InpDepth,i)];

      //--- this lowest has been found previously

      if(extremum==lastlow)

         extremum=0.0;

      else 

        { 

         //--- new last Low

         lastlow=extremum; 

         //--- discard extremum if current Low is too High

         if(Close[i]-extremum>=InpDeviation*Point)

            extremum=0.0;

         else

           {

            //--- clear previous extremums in backstep bars

            for(back=1; back<=InpBackstep; back++)

              {

               pos=i+back;

               if(ExtLowBuffer[pos]!=0 && ExtLowBuffer[pos]>=extremum)

                  ExtLowBuffer[pos]=0.0; 

              }

           }

        }

      //--- found extremum is current Low

      if(Close[i]==extremum) ExtLowBuffer[i]=extremum;

      else                   ExtLowBuffer[i]=0.0;

      dn[i]=ExtLowBuffer[i];

      //--- find highest High in depth of bars

      extremum=Close[iHighest(NULL,0,MODE_CLOSE,InpDepth,i)];

      //--- this highest has been found previously

      if(extremum==lasthigh)

         extremum=0.0;

      else 

        {

         //--- new last High

         lasthigh=extremum;

         //--- discard extremum if current High is too Low

         if(extremum-Close[i]>=InpDeviation*Point)

            extremum=0.0;

         else

           {

            //--- clear previous extremums in backstep bars

            for(back=1; back<=InpBackstep; back++)

              {

               pos=i+back;

               if(ExtHighBuffer[pos]!=0 && ExtHighBuffer[pos]<=extremum)

                  ExtHighBuffer[pos]=0.0; 

              }

           }

        }

      //--- found extremum is current High

      if(Close[i]==extremum) ExtHighBuffer[i]=extremum;

      else                   ExtHighBuffer[i]=0.0;

      up[i]=ExtHighBuffer[i];

     }

    }//Close

  else

  {

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

     {

      //--- find lowest Low in depth of bars

      extremum=Low[iLowest(NULL,0,MODE_LOW,InpDepth,i)];

      //--- this lowest has been found previously

      if(extremum==lastlow)

         extremum=0.0;

      else 

        { 

         //--- new last Low

         lastlow=extremum; 

         //--- discard extremum if current Low is too High

         if(Low[i]-extremum>InpDeviation*Point)

            extremum=0.0;

         else

           {

            //--- clear previous extremums in backstep bars

            for(back=1; back<=InpBackstep; back++)

              {

               pos=i+back;

               if(ExtLowBuffer[pos]!=0 && ExtLowBuffer[pos]>extremum)

                  ExtLowBuffer[pos]=0.0; 

              }

           }

        }

      //--- found extremum is current Low

      if(Low[i]==extremum) ExtLowBuffer[i]=extremum;

      else                 ExtLowBuffer[i]=0.0;

      dn[i]=ExtLowBuffer[i];

      //--- find highest High in depth of bars

      extremum=High[iHighest(NULL,0,MODE_HIGH,InpDepth,i)];

      //--- this highest has been found previously

      if(extremum==lasthigh)

         extremum=0.0;

      else 

        {

         //--- new last High

         lasthigh=extremum;

         //--- discard extremum if current High is too Low

         if(extremum-High[i]>InpDeviation*Point)

            extremum=0.0;

         else

           {

            //--- clear previous extremums in backstep bars

            for(back=1; back<=InpBackstep; back++)

              {

               pos=i+back;

               if(ExtHighBuffer[pos]!=0 && ExtHighBuffer[pos]<extremum)

                  ExtHighBuffer[pos]=0.0;

              }

           }

        }

      //--- found extremum is current High

      if(High[i]==extremum) ExtHighBuffer[i]=extremum;

      else                  ExtHighBuffer[i]=0.0;

      up[i]=ExtHighBuffer[i];

     }

  }





//--- final cutting 

   if(whatlookfor==0)

     {

      lastlow=0.0;

      lasthigh=0.0;  

     }

   else

     {

      lastlow=curlow;

      lasthigh=curhigh;

     }

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

     {

      switch(whatlookfor)

        {

         case 0: // look for peak or lawn 

            if(lastlow==0.0 && lasthigh==0.0)

              {

               if(ExtHighBuffer[i]!=0.0)

                 {

                  lasthigh=Use_Close?Close[i]:High[i];

                  lasthighpos=i;

                  whatlookfor=-1;

                  up[i]=ExtZigzagBuffer[i]=lasthigh;

                 }

               if(ExtLowBuffer[i]!=0.0)

                 {

                  lastlow=Use_Close?Close[i]:Low[i];

                  lastlowpos=i;

                  whatlookfor=1;

                  dn[i]=ExtZigzagBuffer[i]=lastlow;

                 }

              }

             break;  

         case 1: // look for peak

            if(ExtLowBuffer[i]!=0.0 && ExtLowBuffer[i]<=lastlow && ExtHighBuffer[i]==0.0)

              {

               ExtZigzagBuffer[lastlowpos]=0.0;

               lastlowpos=i;

               lastlow=ExtLowBuffer[i];

               dn[i]=ExtZigzagBuffer[i]=lastlow;

              }

            if(ExtHighBuffer[i]!=0.0 && ExtLowBuffer[i]==0.0)

              {

               lasthigh=ExtHighBuffer[i];

               lasthighpos=i;

               up[i]=ExtZigzagBuffer[i]=lasthigh;

               whatlookfor=-1;

              }   

            break;               

         case -1: // look for lawn

            if(ExtHighBuffer[i]!=0.0 && ExtHighBuffer[i]>=lasthigh && ExtLowBuffer[i]==0.0)

              {

               ExtZigzagBuffer[lasthighpos]=0.0;

               lasthighpos=i;

               lasthigh=ExtHighBuffer[i];

               up[i]=ExtZigzagBuffer[i]=lasthigh;

              }

            if(ExtLowBuffer[i]!=0.0 && ExtHighBuffer[i]==0.0)

              {

               lastlow=ExtLowBuffer[i];

               lastlowpos=i;

               dn[i]=ExtZigzagBuffer[i]=lastlow;

               whatlookfor=1;

              }   

            break;               

        }

     }



   double arr[][3];

   ArraySetAsSeries(arr,true);

   ArrayResize(arr,Bars);



   int first_node=click,ud=0;

   for(first_node=click;first_node<Bars-2;first_node++)

    {

     if(up[first_node]!=0) {ud=+1;break;}

     if(dn[first_node]!=0) {ud=-1;break;}

    }



   for(i=first_node;i<Bars-2;i++)

     if((ExtZigzagBuffer[i]!=0 && ExtZigzagBuffer[i]==ExtHighBuffer[i] && ud==-1) ||

        (ExtZigzagBuffer[i]!=0 && ExtZigzagBuffer[i]==ExtLowBuffer [i] && ud==+1)) break;



   first_node=i;

 

   int cntr=0,day=0;

   for(i=first_node;i<Bars-2 && day<DaysBackZZ;i++)

    {

     if(TimeDay(Time[i])!=TimeDay(Time[i+1])) day++;

     if(ExtZigzagBuffer[i]!=0)

      {

       arr[cntr][0]=ExtZigzagBuffer[i];

       arr[cntr][1]=i;

       arr[cntr][2]=ExtZigzagBuffer[i]==ExtHighBuffer[i]?+1:-1;

       cntr++;

      }

    }

   int finish_bar=int(arr[0][1]);

   

   double Curr[],

          Temp[];

   ArrayResize(Curr,Nodes);

   ArrayResize(Temp,Nodes);

   ArraySetAsSeries(Curr,true);

   ArraySetAsSeries(Temp,true);



   for(int j=0;j<Nodes;j++)

    Curr[j]=arr[j][0];



  double volatility=(Curr[ArrayMaximum(Curr)]-Curr[ArrayMinimum(Curr)]);

  

  ArrayFree(bars);

  ArrayResize(bars,cntr);



  int corr_found=0;

  double best_corr=0;

  for(i=1;i<cntr-Nodes && !IsStopped();i++)

   {



    for(int j=0;j<Nodes;j++)

     Temp[j]=arr[j+i][0];

    

    bars[i].corr=CorrelationV(Curr,Temp);

    best_corr=MathMax(best_corr,fabs(bars[i].corr));

    if(fabs(bars[i].corr)<min_corr_ZZ) continue;

    

//    bars[i].time=Time[int(arr[i][1])];

//    bars[i].shift_bar=i;

    bars[i].shift_price=Temp[0]-Curr[0];

    if((Temp[ArrayMaximum(Temp)]-Temp[ArrayMinimum(Temp)])==0) {continue;}//Alert(Time[i]);return(false);}

    bars[i].scale=volatility/(Temp[ArrayMaximum(Temp)]-Temp[ArrayMinimum(Temp)]);

    

    corr_found++;

   }



  if(corr_found<1) {Alert("Found nothing with correlation higher then ",min_corr_ZZ,". Best result was ",DoubleToString(best_corr,3));return(false);}

  //else Alert("Found ", corr_found);

  

  ArrayResize(Temp,Nodes_Forecast);

  

  int timer[];

  

  ArrayResize(timer,Nodes_Forecast);

  ArraySetAsSeries(timer,true);



  for(i=0;i<(Use_Best_fit?1:Max_lines);i++)

   {

    int best_day=0;

    for(int j=1;j<cntr-Nodes && !IsStopped();j++)

      if(fabs(bars[j].corr)>fabs(bars[best_day].corr)) best_day=j;

    

    if(fabs(bars[best_day].corr)>=min_corr_ZZ)

     {

      for(int j=0;j<Nodes_Forecast;j++)

       if(best_day-j>0)

        { 

         Temp [j]=    arr[best_day-j][0];

         timer[j]=int(arr[best_day-j][1]);

        }

      /*

      DrawZZForecastOLD(i,bars[best_day].corr<0,Temp,bars[best_day].scale,bars[best_day].shift_price,finish_bar,

                    (StepZZ<1?int((arr[best_day+Nodes_Forecast][1]-arr[best_day][1])/Nodes_Forecast):StepZZ),

                     MathMax(3-i,1),ColorTF);*/

      DrawZZForecast(DoubleToString(bars[best_day].corr*1000,3),bars[best_day].corr<0,Temp,timer,bars[best_day].scale,bars[best_day].shift_price,finish_bar,click,

                     MathMax(3-i,1),ColorTF);

      //Print(best_day, " - ",bars[best_day].corr, " - ",arr[best_day][0], " - ",bars[best_day].scale);

      bars[best_day].corr=0;

     }

    else break;

   }



   if(Recomend)

   Text(click,High[click],

       buy>-sell?"Buy "+DoubleToString(buy/(buy-sell)*100,0)+"%":("Sell "+DoubleToString(-sell/(buy-sell)*100,0)+"%"),

       buy>-sell?clrLightBlue:clrYellow,2,15);



   ArrayFree(bars);

   

 //  Print("Processing time: ",DoubleToString((GetTickCount()-begin)/1000.0,1)," sec");



   return(true);

  }





void DrawZZForecast(string pos,bool reverse,double &array[],int &time_arr[], double scale,double shift,int finish_bar,int click, int width, color Color)

{

 double zero =array   [0]-shift;

 int    zerot=time_arr[0];

 double line1[],line2[];

 ArrayResize(line1,Nodes_Forecast/2);

 ArrayResize(line2,Nodes_Forecast/2);

 ArrayInitialize(line1,0);

 ArrayInitialize(line2,0);

 ArraySetAsSeries(line1,true);

 ArraySetAsSeries(line2,true);

 

 if(reverse)

  for(int i=0; i<Nodes_Forecast-1;i++)

   {

    if(i%2==0) line1[ i   /2]=zero-(array[i]-shift-zero)*scale;

    else       line2[(i-1)/2]=zero-(array[i]-shift-zero)*scale;

    DrawLineZZ("ZZ"+pos+"_",finish_bar-(zerot-time_arr[i]),finish_bar-(zerot-time_arr[i+1]),zero-(array[i+1]-shift-zero)*scale,zero-(array[i]-shift-zero)*scale,Color,width);

   }

 else

  for(int i=0; i<Nodes_Forecast-1;i++)

   {

    if(i%2==0) line1[ i   /2]=zero+(array[i]-shift-zero)*scale;

    else       line2[(i-1)/2]=zero+(array[i]-shift-zero)*scale;

    DrawLineZZ("ZZ"+pos+"_",finish_bar-(zerot-time_arr[i]),finish_bar-(zerot-time_arr[i+1]),zero+(array[i+1]-shift-zero)*scale,zero+(array[i]-shift-zero)*scale,Color,width);

   }

   

  if(Recomend)

  {

  double mid1=iMAOnArray(line1,0,Nodes_Forecast/2,0,MODE_SMA,0),

         mid2=iMAOnArray(line2,0,Nodes_Forecast/2-1,0,MODE_SMA,0),

         mid3=iMAOnArray(line1,0,Nodes_Forecast/4,0,MODE_SMA,0),

         mid4=iMAOnArray(line2,0,Nodes_Forecast/4,0,MODE_SMA,0);

  if(Max_lines<2 || Use_Best_fit)

  {

  DrawLineZZ("ZZM1_",click,finish_bar-(zerot-time_arr[Nodes_Forecast  -1]),mid1,Close[click],clrGray     ,1,STYLE_DOT);

  DrawLineZZ("ZZM2_",click,finish_bar-(zerot-time_arr[Nodes_Forecast  -1]),mid2,Close[click],clrGray     ,1,STYLE_DOT);

  DrawLineZZ("ZZM3_",click,finish_bar-(zerot-time_arr[Nodes_Forecast/2-1]),mid3,Close[click],clrAliceBlue,1,STYLE_DOT);

  DrawLineZZ("ZZM4_",click,finish_bar-(zerot-time_arr[Nodes_Forecast/2-1]),mid4,Close[click],clrAliceBlue,1,STYLE_DOT);

  }

         buy=mid1-Close[click]>0?mid1-Close[click]:0+

             mid2-Close[click]>0?mid2-Close[click]:0+

             mid3-Close[click]>0?mid3-Close[click]:0+

             mid4-Close[click]>0?mid4-Close[click]:0;

        sell=mid1-Close[click]<0?mid1-Close[click]:0+

             mid2-Close[click]<0?mid2-Close[click]:0+

             mid3-Close[click]<0?mid3-Close[click]:0+

             mid4-Close[click]<0?mid4-Close[click]:0;

  }

}





void DrawLineZZ(string pref,int i1,int i2,double p,double p_prev,color CLR=clrSalmon,int Width=1,ENUM_LINE_STYLE style=STYLE_SOLID)

{

  string Name=obj_pref2+pref+"_"+IntegerToString(i1,2)+IntegerToString(i2,2);

  datetime t1=i1<0?Time[0]-_Period*60*i1:Time[i1],

           t2=i2<0?Time[0]-_Period*60*i2:Time[i2];

  if(ObjectCreate(0,Name,OBJ_TREND,0,t1,p_prev,t2,p))

   {

    ObjectSetInteger(0,Name,OBJPROP_WIDTH,Width);

    ObjectSetInteger(0,Name,OBJPROP_STYLE,style);

    ObjectSetInteger(0,Name,OBJPROP_RAY_RIGHT,false);

    ObjectSetInteger(0,Name,OBJPROP_COLOR,CLR);

    ObjectSetInteger(0,Name,OBJPROP_HIDDEN,true);

    ObjectSetInteger(0,Name,OBJPROP_SELECTABLE,false);

    ObjectSetInteger(0,Name,OBJPROP_BACK,false);

  }

}



/*

void DrawZZForecastOLD(int pos,bool reverse,double &array[],double scale,double shift,int finish_bar,int Step,int width, color Color)

{

 double zero=array[0]-shift;

 if(reverse)

  for(int i=0; i<Nodes_Forecast-1;i++)

   DrawLineZZOLD("ZZ"+IntegerToString(pos)+"_",finish_bar-i*Step,zero-(array[i+1]-shift-zero)*scale,zero-(array[i]-shift-zero)*scale,Step,clrWhite,width);

 else

  for(int i=0; i<Nodes_Forecast-1;i++)

   DrawLineZZOLD("ZZ"+IntegerToString(pos)+"_",finish_bar-i*Step,zero+(array[i+1]-shift-zero)*scale,zero+(array[i]-shift-zero)*scale,Step,Color,width);

}



void DrawLineZZOLD(string pref,int i,double p,double p_prev,int Step,color CLR=clrSalmon,int Width=1)//,ENUM_LINE_STYLE style=STYLE_SOLID)

{

  string Name=obj_pref2+pref+"_"+IntegerToString(i);

  datetime t1=i<0     ?Time[0]-_Period*60*i:Time[i],

           t2=i-Step<0?t1+Step*_Period*60  :Time[i-Step];

  if(ObjectCreate(0,Name,OBJ_TREND,0,t1,p_prev,t2,p))

   {

    ObjectSetInteger(0,Name,OBJPROP_WIDTH,Width);

    ObjectSetInteger(0,Name,OBJPROP_STYLE,STYLE_SOLID);

    ObjectSetInteger(0,Name,OBJPROP_RAY_RIGHT,false);

    ObjectSetInteger(0,Name,OBJPROP_COLOR,CLR);

    ObjectSetInteger(0,Name,OBJPROP_HIDDEN,true);

    ObjectSetInteger(0,Name,OBJPROP_SELECTABLE,false);

    ObjectSetInteger(0,Name,OBJPROP_BACK,false);

  }

}*/

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---