NLD_TrendPainter

Author: 2019 © NELODI.com
0 Views
0 Downloads
0 Favorites
NLD_TrendPainter
ÿþ//+---------------------------------------------------+

//|                              NLD_TrendPainter.mq5 |

//|                             http://www.nelodi.com |

//|                     Copyright (c) 2019 NELODI.com |

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

#property copyright   "2019 © NELODI.com"

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

#property description "NELODI - Trend Painter"



#include "NLD_TP_Include.mqh"



#property indicator_chart_window

#property indicator_buffers 8

#property indicator_plots   8



#property indicator_color1  Red,Green

#property indicator_color2  Red,Green

#property indicator_color3  Red,Green

#property indicator_color4  Red,Green

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



enum MyDrawStyle

  {

   dtNowPoints=1,    // 4 Trend Points

   dtFutPoints=2,    // 4 Future Points

   dtAllPoints=3,    // 8 Trend+Future Points



   dtNowLines=4,     // 4 Trend Lines

   dtFutLines=5,     // 4 Future Lines

   dtAllLines=6,     // 8 Trend+Future Lines



   dtNowRibons=7,    // 2 Trend Ribons

   dtFutRibons=8,    // 2 Future Ribons

   dtAllRibons=9,    // 4 Trend+Future Ribons



   dtNowCloud=10,    // 1 Trend Cloud

   dtFutCloud=11,    // 1 Future Cloud

   dtAllClouds=12,   // 2 Trend+Future Clouds



   dtGlowRocks=20,// Glow Rocks



   dtSunHills=31,    // Sunny Hills

   dtSunLakes=32,    // Sunny Lakes

   dtSunRivers=33,   // Sunny Rivers



   dtMoonHills=41,    // Moon Hills

   dtMoonLakes=42,    // Moon Lakes

   dtMoonRivers=43,   // Moon Rivers

   dtMoonCanyons=44   // Moon Canyons

  };



//--- Input parameters

input bool RatesMode=true;   // Use Rates? {true}

input bool TickMode=true;    // Use Ticks? {true}

input bool FastMode=false;   // Quick Turns? {false}



input int InpTimeFrame=1;    // Period: Frame {1}

input int InpMinInterval=1;  // Period: Signal {1}

input int InpMaxInterval=20; // Period: Border {20}

input double InpOverlap=10;  // Period: Overlap {10}

input int InpScan=0;         // Period: Scan [0..7] {0}

input int InpIron=0;         // Period: Smoothen {0}

input bool BackPaint=false;  // Period: Back-Paint? {false}



input MyDrawStyle MyStyle=dtMoonHills;  // Graph Type {Moon Hills}



input double InpSpreadWeight=0; // Spread: Weight {0}

input double InpSpreadSig=1;    // Spread: On Signal {1}

input double InpSpreadCld=1;    // Spread: On Border {1}



input int InpDelay=0;       // Span: Delay {0}

input int InpPriceLine=0;   // Span: Price {0}

input int InpSignalLine=2;  // Span: Signal {2}

input int InpLineShift=4;   // Span: Shift {4}

input int InpSupportLine=6; // Span: Support {6}

input int InpBorderLine=8;  // Span: Border {8}



//--- Negative Color values = Reverse order

input int MyBullClrR=0;    // Bulls color: R [-100 .. 100] {0}

input int MyBullClrG=100;  // Bulls color: G [-100 .. 100] {100}

input int MyBullClrB=50;   // Bulls color: B [-100 .. 100] {50}



input int MyBearClrR=100;  // Bears color: R [-100 .. 100] {100}

input int MyBearClrG=50;   // Bears color: G [-100 .. 100] {50}

input int MyBearClrB=0;    // Bears color: B [-100 .. 100] {0}



//--- INPUT parameters

input MyPriceType InpHighTop=ptHighest;      // Price: High Top {>}

input MyPriceType InpHighCenter=ptWeighted;  // Price: High Center {~}

input MyPriceType InpHighBottom=ptLowest;    // Price: High Bottom {<}



input MyPriceType InpLowTop=ptHighest;       // Price: Low Top {>}

input MyPriceType InpLowCenter=ptWeighted;   // Price: Low Center {~}

input MyPriceType InpLowBottom=ptLowest;     // Price: Low Bottom {<}



//--- Drawing Buffers

double ExtSig1a_Buffer[];

double ExtSig1b_Buffer[];

double ExtSig2a_Buffer[];

double ExtSig2b_Buffer[];

double ExtSig3a_Buffer[];

double ExtSig3b_Buffer[];

double ExtSig4a_Buffer[];

double ExtSig4b_Buffer[];



double sig1a,sig1b;

double sig2a,sig2b;

double sig3a,sig3b;

double sig4a,sig4b;

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



color Clr(int i,int sR,int sG,int sB)

  {

   int iR=i*sR/100;

   if(iR>255 || iR<-255) iR=255;

   else if(iR<0) iR=255+iR;



   int iG=i*sG/100;

   if(iG>255 || iG<-255) iG=255;

   else if(iG<0) iG=255+iG;



   int iB=i*sB/100;

   if(iB>255 || iB<-255) iB=255;

   else if(iB<0) iB=255+iB;



   return(StringToColor(

          IntegerToString(iR)+","+

          IntegerToString(iG)+","+

          IntegerToString(iB))

          );

  }

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



color BearClr(int i)

  {

   return(Clr(i,MyBearClrR,MyBearClrG,MyBearClrB));

  }

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



color BullClr(int i)

  {

   return(Clr(i,MyBullClrR,MyBullClrG,MyBullClrB));

  }

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



void MyDrawInit()

  {

   color Clr1a;

   color Clr1b;

   color Clr2a;

   color Clr2b;

   color Clr3a;

   color Clr3b;

   color Clr4a;

   color Clr4b;



   int ExtDrawType1;

   int ExtDrawType2;

   int ExtDrawType3;

   int ExtDrawType4;



   int ExtDrawStyle1;

   int ExtDrawStyle2;

   int ExtDrawStyle3;

   int ExtDrawStyle4;



   int ExtDrawWidth1;

   int ExtDrawWidth2;

   int ExtDrawWidth3;

   int ExtDrawWidth4;



//--- Init drawing styles

   ExtDrawWidth1=1;

   ExtDrawWidth2=1;

   ExtDrawWidth3=1;

   ExtDrawWidth4=1;



   ExtDrawType1=DRAW_NONE;

   ExtDrawType2=DRAW_NONE;

   ExtDrawType3=DRAW_NONE;

   ExtDrawType4=DRAW_NONE;



   ExtDrawStyle1=STYLE_SOLID;

   ExtDrawStyle2=STYLE_SOLID;

   ExtDrawStyle3=STYLE_SOLID;

   ExtDrawStyle4=STYLE_SOLID;



   Clr1a=clrNONE;

   Clr1b=clrNONE;

   Clr2a=clrNONE;

   Clr2b=clrNONE;

   Clr3a=clrNONE;

   Clr3b=clrNONE;

   Clr4a=clrNONE;

   Clr4b=clrNONE;



   MyPush=(MyStyle<=dtAllClouds);



   if(MyStyle<=dtAllLines) // Points & Lines

     {

      if(MyStyle<=dtAllPoints)

        {

         ExtDrawType1=DRAW_ARROW;

         ExtDrawType2=DRAW_ARROW;

         ExtDrawType3=DRAW_ARROW;

         ExtDrawType4=DRAW_ARROW;



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

            PlotIndexSetInteger(i,PLOT_ARROW,158);

        }

      else

        {

         ExtDrawType1=DRAW_LINE;

         ExtDrawType2=DRAW_LINE;

         ExtDrawType3=DRAW_LINE;

         ExtDrawType4=DRAW_LINE;

        }



      Clr1a=C'200,200,200';  // White

      Clr1b=C'0,200,0';      // Green

      Clr2a=C'0,200,200';    // Aqua

      Clr2b=C'100,100,200';  // Sky



      Clr3a=C'200,0,200';    // Pink

      Clr3b=C'200,100,100';  // Lila

      Clr4a=C'200,100,0';    // Orange

      Clr4b=C'200,200,0';    // Yellow



      ExtDrawWidth1=1;

      ExtDrawWidth2=1;

      ExtDrawWidth3=1;

      ExtDrawWidth4=1;

     }

   else

     {

      ExtDrawType1=DRAW_FILLING;

      ExtDrawType2=DRAW_FILLING;

      ExtDrawType3=DRAW_FILLING;

      ExtDrawType4=DRAW_FILLING;



      switch(MyStyle)

        {

         case dtSunLakes:

         case dtSunRivers:

           {

            Clr1a=BullClr(100);

            Clr2a=BullClr(192);

            Clr3a=BullClr(80);

            Clr4a=BullClr(200);



            Clr1b=BearClr(100);

            Clr2b=BearClr(192);

            Clr3b=BearClr(80);

            Clr4b=BearClr(200);



            break;

           }



         case dtSunHills:

           {

            Clr1a=BullClr(80);

            Clr2a=BullClr(192);

            Clr3a=BullClr(100);

            Clr4a=BullClr(200);



            Clr1b=BearClr(80);

            Clr2b=BearClr(192);

            Clr3b=BearClr(100);

            Clr4b=BearClr(200);



            break;

           }



         case dtMoonLakes:

           {

            Clr1a=BullClr(80);

            Clr2a=BullClr(200);

            Clr3a=BullClr(160);

            Clr4a=BullClr(192);



            Clr1b=BearClr(80);

            Clr2b=BearClr(200);

            Clr3b=BearClr(160);

            Clr4b=BearClr(192);

            break;

           }



         case dtMoonRivers:

           {

            Clr1a=BullClr(80);

            Clr2a=BullClr(255);

            Clr3a=BullClr(100);

            Clr4a=BullClr(160);



            Clr1b=BearClr(80);

            Clr2b=BearClr(255);

            Clr3b=BearClr(100);

            Clr4b=BearClr(160);

            break;

           }



         case dtMoonHills:

           {

            Clr1a=BullClr(80);

            Clr2a=BullClr(128);

            Clr3a=BullClr(96);

            Clr4a=BullClr(127);



            Clr1b=BearClr(80);

            Clr2b=BearClr(128);

            Clr3b=BearClr(96);

            Clr4b=BearClr(127);



            break;

           }



         case dtMoonCanyons:

           {

            Clr1a=BullClr(80);

            Clr2a=BullClr(220);

            Clr3a=BullClr(100);

            Clr4a=BullClr(192);



            Clr1b=BearClr(80);

            Clr2b=BearClr(220);

            Clr3b=BearClr(100);

            Clr4b=BearClr(192);

            break;

           }



         case dtGlowRocks:

           {

            Clr1a=BullClr(24);

            Clr2a=BullClr(36);

            Clr3a=BullClr(66);

            Clr4a=BullClr(129);



            Clr1b=BearClr(24);

            Clr2b=BearClr(36);

            Clr3b=BearClr(66);

            Clr4b=BearClr(129);

            break;

           }



         default:

           {

            Clr1a=BullClr(192);

            Clr2a=BullClr(80);

            Clr3a=BullClr(100);

            Clr4a=BullClr(200);



            Clr1b=BearClr(192);

            Clr2b=BearClr(80);

            Clr3b=BearClr(100);

            Clr4b=BearClr(200);

           }

        }

     }



   int x=0;

   string sLine="Trend",sigA,sigB;

//--- Span 4 ...

   sigA="4a";sigB="4b";

   PlotIndexSetDouble(x,PLOT_EMPTY_VALUE,0);

   PlotIndexSetInteger(x,PLOT_DRAW_TYPE,ExtDrawType4);

   if(ExtDrawType4==DRAW_FILLING)

     {

      SetIndexBuffer(0,ExtSig4a_Buffer,INDICATOR_DATA);

      SetIndexBuffer(1,ExtSig4b_Buffer,INDICATOR_DATA);



      PlotIndexSetInteger(x,PLOT_LINE_COLOR,0,Clr4a);

      PlotIndexSetInteger(x,PLOT_LINE_COLOR,1,Clr4b);

      PlotIndexSetString(x,PLOT_LABEL,sLine+" "+sigA+";"+sLine+" "+sigB);



      if(MyPush) PlotIndexSetInteger(x,PLOT_SHIFT,perLineShift);

      else PlotIndexSetInteger(x,PLOT_SHIFT,0);

     }

   else

     {

      SetIndexBuffer(0,ExtSig4b_Buffer,INDICATOR_DATA);

      SetIndexBuffer(1,ExtSig4a_Buffer,INDICATOR_DATA);



      PlotIndexSetInteger(x,PLOT_LINE_STYLE,ExtDrawStyle4);

      PlotIndexSetInteger(x,PLOT_LINE_WIDTH,ExtDrawWidth4);

      PlotIndexSetInteger(x,PLOT_LINE_COLOR,Clr4b);

      PlotIndexSetString(x,PLOT_LABEL,sLine+" "+sigB);



      if(MyPush) PlotIndexSetInteger(x,PLOT_SHIFT,perLineShift);

      else PlotIndexSetInteger(x,PLOT_SHIFT,0);



      x++;

      PlotIndexSetDouble(x,PLOT_EMPTY_VALUE,0);

      PlotIndexSetInteger(x,PLOT_DRAW_TYPE,ExtDrawType4);



      PlotIndexSetInteger(x,PLOT_LINE_STYLE,ExtDrawStyle4);

      PlotIndexSetInteger(x,PLOT_LINE_WIDTH,ExtDrawWidth4);

      PlotIndexSetInteger(x,PLOT_LINE_COLOR,Clr4a);

      PlotIndexSetString(x,PLOT_LABEL,sLine+" "+sigA);



      PlotIndexSetInteger(x,PLOT_SHIFT,0);

     }

//--- Span 3 ...

   sigB="3b"; sigA="3a";

   x++;

   PlotIndexSetDouble(x,PLOT_EMPTY_VALUE,0);

   PlotIndexSetInteger(x,PLOT_DRAW_TYPE,ExtDrawType3);

   if(ExtDrawType3==DRAW_FILLING)

     {

      SetIndexBuffer(2,ExtSig3a_Buffer,INDICATOR_DATA);

      SetIndexBuffer(3,ExtSig3b_Buffer,INDICATOR_DATA);



      PlotIndexSetInteger(x,PLOT_LINE_COLOR,0,Clr3a);

      PlotIndexSetInteger(x,PLOT_LINE_COLOR,1,Clr3b);

      PlotIndexSetString(x,PLOT_LABEL,sLine+" "+sigA+";"+sLine+" "+sigB);



      if(MyPush) PlotIndexSetInteger(x,PLOT_SHIFT,perLineShift);

      else PlotIndexSetInteger(x,PLOT_SHIFT,0);

     }

   else

     {

      SetIndexBuffer(2,ExtSig3b_Buffer,INDICATOR_DATA);

      SetIndexBuffer(3,ExtSig3a_Buffer,INDICATOR_DATA);



      PlotIndexSetInteger(x,PLOT_LINE_STYLE,ExtDrawStyle3);

      PlotIndexSetInteger(x,PLOT_LINE_WIDTH,ExtDrawWidth3);

      PlotIndexSetInteger(x,PLOT_LINE_COLOR,Clr3b);

      PlotIndexSetString(x,PLOT_LABEL,sLine+" "+sigB);



      if(MyPush) PlotIndexSetInteger(x,PLOT_SHIFT,perLineShift);

      else PlotIndexSetInteger(x,PLOT_SHIFT,0);



      x++;

      PlotIndexSetDouble(x,PLOT_EMPTY_VALUE,0);

      PlotIndexSetInteger(x,PLOT_DRAW_TYPE,ExtDrawType3);



      PlotIndexSetInteger(x,PLOT_LINE_STYLE,ExtDrawStyle3);

      PlotIndexSetInteger(x,PLOT_LINE_WIDTH,ExtDrawWidth3);

      PlotIndexSetInteger(x,PLOT_LINE_COLOR,Clr3a);

      PlotIndexSetString(x,PLOT_LABEL,sLine+" "+sigA);



      PlotIndexSetInteger(x,PLOT_SHIFT,0);

     }

//--- Span 2 ...

   sigB="2b";      sigA="2a";

   x++;

   PlotIndexSetDouble(x,PLOT_EMPTY_VALUE,0);

   PlotIndexSetInteger(x,PLOT_DRAW_TYPE,ExtDrawType2);

   if(ExtDrawType2==DRAW_FILLING)

     {

      SetIndexBuffer(4,ExtSig2a_Buffer,INDICATOR_DATA);

      SetIndexBuffer(5,ExtSig2b_Buffer,INDICATOR_DATA);



      PlotIndexSetInteger(x,PLOT_LINE_COLOR,0,Clr2a);

      PlotIndexSetInteger(x,PLOT_LINE_COLOR,1,Clr2b);

      PlotIndexSetString(x,PLOT_LABEL,sLine+" "+sigA+";"+sLine+" "+sigB);



      PlotIndexSetInteger(x,PLOT_SHIFT,0);

     }

   else

     {

      SetIndexBuffer(4,ExtSig2b_Buffer,INDICATOR_DATA);

      SetIndexBuffer(5,ExtSig2a_Buffer,INDICATOR_DATA);



      PlotIndexSetInteger(x,PLOT_LINE_STYLE,ExtDrawStyle2);

      PlotIndexSetInteger(x,PLOT_LINE_WIDTH,ExtDrawWidth2);

      PlotIndexSetInteger(x,PLOT_LINE_COLOR,Clr2b);

      PlotIndexSetString(x,PLOT_LABEL,sLine+" "+sigB);



      if(MyPush) PlotIndexSetInteger(x,PLOT_SHIFT,perLineShift);

      else PlotIndexSetInteger(x,PLOT_SHIFT,0);



      x++;

      PlotIndexSetDouble(x,PLOT_EMPTY_VALUE,0);

      PlotIndexSetInteger(x,PLOT_DRAW_TYPE,ExtDrawType2);



      PlotIndexSetInteger(x,PLOT_LINE_STYLE,ExtDrawStyle2);

      PlotIndexSetInteger(x,PLOT_LINE_WIDTH,ExtDrawWidth2);

      PlotIndexSetInteger(x,PLOT_LINE_COLOR,Clr2a);

      PlotIndexSetString(x,PLOT_LABEL,sLine+" "+sigA);



      PlotIndexSetInteger(x,PLOT_SHIFT,0);

     }

//--- Span 1 ...

   sigB="1b";      sigA="1a";

   x++;

   PlotIndexSetDouble(x,PLOT_EMPTY_VALUE,0);

   PlotIndexSetInteger(x,PLOT_DRAW_TYPE,ExtDrawType1);

   if(ExtDrawType1==DRAW_FILLING)

     {

      SetIndexBuffer(6,ExtSig1a_Buffer,INDICATOR_DATA);

      SetIndexBuffer(7,ExtSig1b_Buffer,INDICATOR_DATA);



      PlotIndexSetInteger(x,PLOT_LINE_COLOR,0,Clr1a);

      PlotIndexSetInteger(x,PLOT_LINE_COLOR,1,Clr1b);

      PlotIndexSetString(x,PLOT_LABEL,sLine+" "+sigA+";"+sLine+" "+sigB);



      PlotIndexSetInteger(x,PLOT_SHIFT,0);

     }

   else

     {

      SetIndexBuffer(6,ExtSig1b_Buffer,INDICATOR_DATA);

      SetIndexBuffer(7,ExtSig1a_Buffer,INDICATOR_DATA);



      PlotIndexSetInteger(x,PLOT_LINE_STYLE,ExtDrawStyle1);

      PlotIndexSetInteger(x,PLOT_LINE_WIDTH,ExtDrawWidth1);

      PlotIndexSetInteger(x,PLOT_LINE_COLOR,Clr1b);

      PlotIndexSetString(x,PLOT_LABEL,sLine+" "+sigB);



      if(MyPush) PlotIndexSetInteger(x,PLOT_SHIFT,perLineShift);

      else PlotIndexSetInteger(x,PLOT_SHIFT,0);



      x++;

      PlotIndexSetDouble(x,PLOT_EMPTY_VALUE,0);

      PlotIndexSetInteger(x,PLOT_DRAW_TYPE,ExtDrawType1);



      PlotIndexSetInteger(x,PLOT_LINE_STYLE,ExtDrawStyle1);

      PlotIndexSetInteger(x,PLOT_LINE_WIDTH,ExtDrawWidth1);

      PlotIndexSetInteger(x,PLOT_LINE_COLOR,Clr1a);

      PlotIndexSetString(x,PLOT_LABEL,sLine+" "+sigA);



      PlotIndexSetInteger(x,PLOT_SHIFT,0);

     }

  }

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



void OnInit()

  {

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);



   MyHighTop=InpHighTop;

   MyHighCenter=InpHighCenter;

   MyHighBottom=InpHighBottom;



   MyLowTop=InpLowTop;

   MyLowCenter=InpLowCenter;

   MyLowBottom=InpLowBottom;



   MyPriceLine=InpPriceLine;

   MySignalLine=InpSignalLine;

   MySupportLine=InpSupportLine;



   MyBorderLine=InpBorderLine;

   MyLineShift=InpLineShift;

   MyDelay=InpDelay;

   MyScan=InpScan;

   MyIron=InpIron;



   MyTimeFrame=InpTimeFrame;

   MyMinInterval=InpMinInterval;

   MyMaxInterval=InpMaxInterval;



   MySpreadWeight=InpSpreadWeight;

   MySpreadSig=InpSpreadSig;

   MySpreadCld=InpSpreadCld;



   MyOverlap=InpOverlap;



   MyCalcInit();

   MyDrawInit();



   MyPrintInfo("INIT("+IntegerToString(MaxLineShift)+")");

  }

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



double sigT,sigK,sigSA,sigSB;

double sigT2,sigK2,sigSA2,sigSB2;

int old_rates_total=0;

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



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

  {

//--- counting from 0 to rates_total

   ArraySetAsSeries(ExtSig1a_Buffer,false);

   ArraySetAsSeries(ExtSig1b_Buffer,false);



   ArraySetAsSeries(ExtSig2a_Buffer,false);

   ArraySetAsSeries(ExtSig2b_Buffer,false);



   ArraySetAsSeries(ExtSig3a_Buffer,false);

   ArraySetAsSeries(ExtSig3b_Buffer,false);



   ArraySetAsSeries(ExtSig4a_Buffer,false);

   ArraySetAsSeries(ExtSig4b_Buffer,false);



   ArraySetAsSeries(low,false);

   ArraySetAsSeries(open,false);

   ArraySetAsSeries(high,false);

   ArraySetAsSeries(close,false);



   ArraySetAsSeries(time,false);

   ArraySetAsSeries(tick_volume,false);



   int i=0,pos=0,first=0,last=0,cnt=0,spr=0;

   int pos2=0,first2=0,last2=0;



   datetime timeNow=0,timeNow2;

   bool next=false;



//--- Calculate

   if(rates_total<=0)

      return(rates_total);



   if(TickMode || RatesMode || BackPaint)

     {

      if(ExtReady)

         if(rates_total<old_rates_total || prev_calculated>rates_total || prev_calculated<=0)

           {

            MyCalc_ReInit();

            ExtReady=false;

           }

      old_rates_total=rates_total;



      if(!ExtReady)

        {

         if(RatesMode)

           {

            MqlRates rates[];

            int copied=CopyRates(Symbol(),0,0,rates_total,rates);

            if(copied>0)

              {

               i=0; pos=rates_total-copied; first=pos; ExtIdx=pos;

               while(!IsStopped() && i<copied && pos<rates_total)

                 {

                  MyUpdate_Rate(timeNow,rates[i],FastMode);



                  last=pos; cnt=last-first+1;

                  if(cnt>=MyTimeFrame)

                    {

                     if(!BackPaint)

                        MyDrawItAll(ExtLen-1,first,last,timeNow);

                     else if(last>=MathFloor(MyTimeFrame*MyOverlap))

                       {

                        first2=first-(int)MathFloor(MyTimeFrame*MyOverlap);

                        last2=last-(int)MathFloor(MyTimeFrame*MyOverlap);

                        timeNow2=timeNow-(datetime)(ExtPeriodSec*MyOverlap);

                        pos2=ExtLen-(int)MathFloor(1+MyOverlap);

                        if(pos2>=0 && pos2<ExtLen)

                           MyDrawItAll(pos2,first2,last2,timeNow2);

                       }

                     first=last+1;

                    }

                  i++; pos++;

                 }

              }

           }

         else

           {

            pos=0; first=pos;

            while(!IsStopped() && pos<rates_total)

              {

               if(ArraySize(spread)<pos) spr=spread[pos]; else spr=0;

               MyUpdate_Period(timeNow,

                               time[pos],

                               open[pos],close[pos],

                               high[pos],low[pos],

                               tick_volume[pos],

                               spr,FastMode);

               last=pos; cnt=last-first+1;



               if(cnt>=MyTimeFrame)

                 {

                  if(!BackPaint)

                     MyDrawItAll(ExtLen-1,first,last,timeNow);

                  else if(last>=MathFloor(MyTimeFrame*MyOverlap))

                    {

                     first2=first-(int)MathFloor(MyTimeFrame*MyOverlap);

                     last2=last-(int)MathFloor(MyTimeFrame*MyOverlap);

                     timeNow2=timeNow-(datetime)(ExtPeriodSec*MyOverlap);

                     pos2=ExtLen-(int)MathFloor(1+MyOverlap);

                     if(pos2>=0 && pos2<ExtLen)

                        MyDrawItAll(pos2,first2,last2,timeNow2);

                    }

                  first=last+1;

                 }

               pos++;

              }

           }

        }



      if(ExtLen==0) return(0);



      pos=rates_total-1; last=pos; first=last-MyTimeFrame+1;



      if(ExtReady)

        {

         if(TickMode)

           {

            MyUpdate_Tick(timeNow,FastMode);

           }

         else if(RatesMode)

           {

            MqlRates rates[1];

            int copied=CopyRates(Symbol(),0,0,1,rates);

            if(copied>0)

               MyUpdate_Rate(timeNow,rates[0],FastMode);

            else

               return(rates_total);

           }

         else

           {

            if(ArraySize(spread)<pos) spr=spread[pos]; else spr=0;

            MyUpdate_Period(timeNow,

                            time[pos],

                            open[pos],close[pos],

                            high[pos],low[pos],

                            tick_volume[pos],

                            spr,FastMode);

           }

        }



      if(!BackPaint)

         MyDrawItAll(ExtLen-1,first,last,timeNow);

      else

        {

         first2=first-(int)MathFloor(MyTimeFrame*MyOverlap);

         last2=last-(int)MathFloor(MyTimeFrame*MyOverlap);

         timeNow2=timeNow-(datetime)(ExtPeriodSec*MyOverlap);

         pos2=ExtLen-(int)MathFloor(1+MyOverlap);

         for(int z=0;z<=MathFloor(MyOverlap) && first2>=0 && last2<rates_total && pos2>=0 && pos2<ExtLen;z++)

           {

            MyDrawItAll(pos2,first2,last2,timeNow2);

            pos2++;first2+=MyTimeFrame;last2+=MyTimeFrame;timeNow2+=ExtPeriodSec;

           }

        }

     }

   else

     {

      while(!IsStopped() && 

            MyCalc_Period(next,pos,first,last,cnt,timeNow,

            time,open,close,high,low,tick_volume,spread))

        {

         MyDrawItAll(pos-ExtIdx,first,last,timeNow);

        }

     }



   if(!ExtReady && ExtLen>0)

     {

      ExtReady=true;

      MyPrintInfo("READY("+IntegerToString(MaxLineShift)+":"+IntegerToString(rates_total)+")");

     }



   return(rates_total);

  }

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

void MyDrawItAll(int i,int first,int last,datetime timeNow)

  {

   MyClear_Signals();



   MyCalc_Trends(i,timeNow);

   if(MyStyle<=dtAllLines)

     {

      MyPaint_RawData();

     }

   else

     {

      MyCalc_Cloud(i,timeNow,

                   sigT,sigK,sigSA,sigSB,

                   sigT2,sigK2,sigSA2,sigSB2);



      MyCenter((sigT+sigT2+sigK+sigK2)/4,

               (sigSA+sigSA2+sigSB+sigSB2)/4);



      switch(MyStyle)

        {

         case dtNowRibons:

         case dtAllRibons:

           {

            MyCld(3,sigSA,sigSA2);

            MyCld(4,sigSB,sigSB2);

            if(MyStyle==dtNowRibons) break;

           }

         case dtFutRibons:

           {

            MySig(1,sigT,sigT2);

            MySig(2,sigK,sigK2);

            break;

           }



         case dtNowCloud:

         case dtAllClouds:

           {

            MySig(1,sigT,(sigK2+sigT2)/2);

            MySig(2,(sigT+sigK)/2,sigK2);

            if(MyStyle==dtNowCloud) break;

           }

         case dtFutCloud:

           {

            MyCld(3,sigSA,(sigSA2+sigSB2)/2);

            MyCld(4,(sigSA+sigSB)/2,sigSB2);

            break;

           }



         case dtGlowRocks:

           {

            MyMid(1,sigT,sigSA);

            MyMid(2,sigT2,sigSA2);

            MyMid(3,sigK,sigSB);

            MyMid(4,sigK2,sigSB2);

            break;

           }

         case dtSunRivers:

           {

            MySig(2,sigT2,sigK);



            MyMid(1,sigT,sigSA);

            MyMid(4,sigK2,sigSB2);



            MyCld(3,sigSA2,sigSB);

            break;

           }

         case dtSunLakes:

           {

            MySig(2,sigT,sigK2);



            MyMid(1,sigT2,sigSA);

            MyMid(4,sigK,sigSB2);



            MyCld(3,sigSA2,sigSB);

            break;

           }

         case dtSunHills:

           {

            MySig(1,sigT,sigK);



            MyMid(3,sigK2,sigSB2);

            MyMid(2,sigT2,sigSA);



            MyCld(4,sigSA2,sigSB);

            break;

           }

         case dtMoonCanyons:

           {

            MyMid(1,sigT,sigSA);

            MyMid(2,(sigT+sigT2)/2,sigSA2);

            MyMid(3,sigK,sigSB);

            MyMid(4,sigK2,sigSB2);

            break;

           }

         case dtMoonLakes:

           {

            MySig(2,sigT,sigK2);



            MyMid(3,(sigT+sigT2)/2,sigSA2);

            MyMid(4,(sigK+sigK2)/2,sigSB);



            MyCld(1,sigSA,sigSB2);

            break;

           }

         case dtMoonRivers:

           {

            MySig(2,sigT2,sigK2);

            MySig(4,sigT2,sigK);



            MyMid(3,(sigT2+sigK2)/2,sigSA2);



            MyCld(1,sigSA,sigSB2);

            break;

           }

         case dtMoonHills:

           {

            MySig(2,sigT2,sigK2);



            MyMid(3,sigT2,sigSA);

            MyMid(1,(sigT2+sigK2)/2,sigSB);



            MyCld(4,sigSA2,sigSB2);

            break;

           }

        }

     }



   MyDraw(first,last);



  }



double sprCenterA,sprCenterB;

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

void MyCenter(double a,double b)

  {

   sprCenterA=a;

   sprCenterB=b;

  }

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



void MySignal(double slA,bool revA,

              double slB,bool revB,

              int i,

              double a,

              double b)

  {

   double dirA=(revA?sprCenterA-a:a-sprCenterA);

   double dirB=(revB?sprCenterB-b:b-sprCenterB);



   bool grA=(revA?a<=b:a>=b);

   bool grB=(revB?b<=a:b>=a);



   double sprA=ExtSpread*(revA?-0.5:0.5)*MySpreadWeight;

   double sprB=ExtSpread*(revB?-0.5:0.5)*MySpreadWeight;



   bool doA=false;

   bool doB=false;



   if(slA>0)

     {

      double spr=ExtSpread*slA;

      doA=(grA && dirA>0 && dirA<spr) || 

          (grB && dirA<0 && dirA>-spr);

     }

   if(slB>0)

     {

      double spr=ExtSpread*slB;

      doB=(grB && dirB>0 && dirB<spr) || 

          (grA && dirB<0 && dirB>-spr);

     }



   if(doA)

     {

      if(dirA>0) a-=sprA;

      else a+=sprA;

     }



   if(doB)

     {

      if(dirB>0) b+=sprB;

      else b-=sprB;

     }



   switch(i)

     {

      case 1: sig1a=a; sig1b=b; break;

      case 2: sig2a=a; sig2b=b; break;

      case 3: sig3a=a; sig3b=b; break;

      case 4: sig4a=a; sig4b=b; break;

     }

  }

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



void MySig(int i,double a,double b)

  {

   MySignal(MySpreadSig,false,

            MySpreadSig,false,

            i,a,b);

  }

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



void MyCld(int i,double a,double b)

  {

   MySignal(MySpreadCld,true,

            MySpreadCld,true,

            i,a,b);

  }

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



void MyMid(int i,double a,double b)

  {

   MySignal(MySpreadSig,false,

            MySpreadCld,true,

            i,a,b);

  }

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



void MyDraw(int first,int last)

  {

   int cnt=last-first+1;

   if(cnt<1) return;



   if(sig1a!=ExtSig1a_Buffer[last]) MyArrayFill(ExtSig1a_Buffer,first,cnt,sig1a);

   if(sig1b!=ExtSig1b_Buffer[last]) MyArrayFill(ExtSig1b_Buffer,first,cnt,sig1b);

   if(sig2a!=ExtSig2a_Buffer[last]) MyArrayFill(ExtSig2a_Buffer,first,cnt,sig2a);

   if(sig2b!=ExtSig2b_Buffer[last]) MyArrayFill(ExtSig2b_Buffer,first,cnt,sig2b);

   if(sig3a!=ExtSig3a_Buffer[last]) MyArrayFill(ExtSig3a_Buffer,first,cnt,sig3a);

   if(sig3b!=ExtSig3b_Buffer[last]) MyArrayFill(ExtSig3b_Buffer,first,cnt,sig3b);

   if(sig4a!=ExtSig4a_Buffer[last]) MyArrayFill(ExtSig4a_Buffer,first,cnt,sig4a);

   if(sig4b!=ExtSig4b_Buffer[last]) MyArrayFill(ExtSig4b_Buffer,first,cnt,sig4b);

  }

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



void MyPaint_RawData()

  {

   if(MyStyle==dtNowLines || MyStyle==dtNowPoints ||

      MyStyle==dtAllLines || MyStyle==dtAllPoints)

     {

      sig1a=line_value[sigPriceLine];

      sig2a=line_value[sigSignalLine];

      sig3a=line_value[sigSupportLine];

      sig4a=line_value[sigBorderLine];

     }

   if(MyStyle==dtFutLines || MyStyle==dtFutPoints ||

      MyStyle==dtAllLines || MyStyle==dtAllPoints)

     {

      sig1b=line_value[sigPriceLine];

      sig2b=line_value[sigSignalLine];

      sig3b=line_value[sigSupportLine];

      sig4b=line_value[sigBorderLine];

     }

  }

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



void MyClear_Signals()

  {

   sig1a=0; sig2a=0;

   sig3a=0; sig4a=0;

   sig1b=0; sig2b=0;

   sig3b=0; sig4b=0;

  }

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



void MyPaint_Signals(const double priceNow,

                     const double price1,

                     const double price2,

                     const double price3,

                     const double price4,

                     double &s1a,double &s1b,

                     double &s2a,double &s2b,

                     double &s3a,double &s3b,

                     double &s4a,double &s4b,

                     int openBuySig,int openSellSig,

                     int closeBuySig,int closeSellSig)

  {

   s1a=0;s2a=0;s3a=0;s4a=0;

   s1b=0;s2b=0;s3b=0;s4b=0;



   switch(MathMax(openBuySig,closeSellSig))

     {

      case 5:

      case 4:

         s4a=(priceNow*2+price4+ExtSpread)/3;

      case 3:

         s3a=(priceNow*2+price3+ExtSpread)/3;

      case 2:

         s2a=(priceNow*2+price2+ExtSpread)/3;

      case 1:

         s1a=(priceNow*2+price1+ExtSpread)/3;

     }

   switch(MathMax(openSellSig,closeBuySig))

     {

      case 5:

      case 4:

         s4b=(priceNow*2+price4)/3;

      case 3:

         s3b=(priceNow*2+price3)/3;

      case 2:

         s2b=(priceNow*2+price2)/3;

      case 1:

         s1b=(priceNow*2+price1)/3;

     }

  }

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



void MyPrintInfo(string status)

  {

   string info="{ ["+IntegerToString(minInterval)+"-"+IntegerToString(maxInterval)+":"+IntegerToString(MyScan)+"/"+IntegerToString(MyIron)+"]"+

               " *"+IntegerToString(MyTimeFrame)+" +"+IntegerToString(MyDelay)+

               " <"+IntegerToString(MyPriceLine)+","+IntegerToString(MySignalLine)+">"+IntegerToString(MyLineShift)+"<"+

               IntegerToString(MySupportLine)+","+IntegerToString(MyBorderLine)+"> }";



   Print(status+" "+info);

  }

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

Comments