// Downloaded From https://www.WiseStockTrader.com
_SECTION_BEGIN("TDST_DeMark_Perl");

/*  Set Parameters.  */

Plotall=ParamToggle("Plot all lines ", "No|Yes",1);
Plotpercent=Param("Plot percentage above/below visible range  ",20,0,100,5,0);
gapprox=Param("Proximity Gap Multiplier",1,0,5,0.5,0);
bluedist=Param("Blue_Multiplier",1.75,0,10,0.25,0);
reddist=Param("Red_multiplier",0.25,0,10,0.25,0);

/*  Initialise variables.  */

sup1=sup2=sup3=sup4=sup5=sup6=sup7=sup8=sup9=Null;
res1=res2=res3=res4=res5=res6=res7=res8=res9=Null;

TDSeqBuy=False;
TDSeqbuydigits=0;
TDSeqbuyrisk=TDSeqbuyreward=TDSeqbuyrrratio=TDSeqbuystop=0;

TDSeqSell=False;
TDSeqselldigits=0;
TDSeqsellrisk=TDSeqsellreward=TDSeqsellrrratio=TDSeqsellstop=0;

supinuse=resinuse=Null;
supvio=resvio=0;
tfarray=True;
distance=ATR(10);

BCDplot=False;
BSetupind=BCDind=BCDrisk=0;
BSmaxr=BSminr=BScnt=BSmaxrc=BSminrc=0;
CurBSmaxr=CurBSminr=CurBScnt=CurBSmaxrc=CurBSminrc=0;
PreBSmaxr=PreBSminr=PreBScnt=PreBSmaxrc=PreBSminrc=0;
CurBCDstart=CurBCD=CurBCD8=CurBCDmaxr=CurBCDminr=CurBCDtext=CurBCDfin=0;
PreBCDstart=PreBCD=PreBCD8=PreBCDmaxr=PreBCDminr=PreBCDtext=PreBCDfin=0;

SCDplot=False;
SSetupind=SCDind=SCDrisk=0;
SSmaxr=SSminr=SScnt=SSmaxrc=SSminrc=0;
CurSSmaxr=CurSSminr=CurSScnt=CurSSmaxrc=CurSSminrc=0;
PreSSmaxr=PreSSminr=PreSScnt=PreSSmaxrc=PreSSminrc=0;
CurSCDstart=CurSCD=CurSCD8=CurSCDmaxr=CurSCDminr=CurSCDtext=CurSCDfin=0;
PreSCDstart=PreSCD=PreSCD8=PreSCDmaxr=PreSCDminr=PreSCDtext=PreSCDfin=0;


/*  Identify functions and procedures.  */

/*  This function checks the TD Sequential Digits for a valid sequence.  */

function seqcheck(TDSeqdigits,ival)
{
  Loop=0;
  if (TDSeqdigits[ival-1]>0 AND TDSeqdigits[ival-1]<9)
  {
     Loop=TDSeqdigits[ival-1];
     for (jloop=0; jloop<Loop; jloop++)
     {
       TDSeqdigits[ival-1-jloop]=0;
     }
  }
  return TDSeqdigits;
}

/*  Fuction to set up support and resistance arrays  */

function supres(supresarray,jval,ival,supresValue)
{
  global tfarray;

  if (IsEmpty(supresarray[jval]))
  {  
    tfarray[ival]=False;
    for (jcount=jval; jcount<(ival+1); jcount++)
    {
       supresarray[jcount]=supresvalue;
    }
  }

  return supresarray;
}

/*  Prodedures to reset TD Sequential Buy arrays.  */

procedure ResetBScur(Loop,Ival)
{
  for (j=Loop; j<Ival; j++)
  {
    CurBSmaxr[j]=0;
    CurBSminr[j]=0;
    CurBScnt[j]=0;
    CurBSmaxrc[j]=0;
    CurBSminrc[j]=0;

    CurBCDstart[j]=0;
    CurBCD[j]=0;
    CurBCDmaxr[j]=0;
    CurBCDminr[j]=0;
    CurBCD8[j]=0;
//    CurBCDfin[j]=0;
    CurBCDtext[j]=0;
  }
  return;
}

procedure ResetBSpre(Loop,Ival)
{
  for (j=Loop; j<Ival; j++)
  {
    PreBSmaxr[j]=0;
    PreBSminr[j]=0;
    PreBScnt[j]=0;
    PreBSmaxrc[j]=0;
    PreBSminrc[j]=0;

    PreBCDstart[j]=0;
    PreBCD[j]=0;
    PreBCDmaxr[j]=0;
    PreBCDminr[j]=0;
    PreBCD8[j]=0;
//    PreBCDfin[j]=0;
    PreBCDtext[j]=0;
  }
  return;
}

/*  Procedure to copy the TD Setup Buy arrays (previous) into the TD Setup Buy arrays (current).  */

procedure CopyBSpre(Loop,Ival)
{
  for (j=Loop; j<Ival; j++)
  {
    CurBSmaxr[j]=PreBSmaxr[j];
    CurBSminr[j]=PreBSminr[j];
    CurBScnt[j]=PreBScnt[j];
    CurBSmaxrc[j]=PreBSmaxrc[j];
    CurBSminrc[j]=PreBSminrc[j];

    CurBCDstart[j]=PreBCDstart[j];
    CurBCD[j]=PreBCD[j];
    CurBCDmaxr[j]=PreBCDmaxr[j];
    CurBCDminr[j]=PreBCDminr[j];
    CurBCD8[j]=PreBCD8[j];
    CurBCDfin[j]=PreBCDfin[j];
    CurBCDtext[j]=PreBCDtext[j];
  }
  return;
} 


/*  Prodedures to reset TD Sequential Sell arrays.  */

procedure ResetSScur(Loop,Ival)
{
  for (j=Loop; j<Ival; j++)
  {
    CurSSmaxr[j]=0;
    CurSSminr[j]=0;
    CurSScnt[j]=0;
    CurSSmaxrc[j]=0;
    CurSSminrc[j]=0;

    CurSCDstart[j]=0;
    CurSCD[j]=0;
    CurSCDmaxr[j]=0;
    CurSCDminr[j]=0;
    CurSCD8[j]=0;
//    CurSCDfin[j]=0;
    CurSCDtext[j]=0;
  }
  return;
}

procedure ResetSSpre(Loop,Ival)
{
  for (j=Loop; j<Ival; j++)
  {
    PreSSmaxr[j]=0;
    PreSSminr[j]=0;
    PreSScnt[j]=0;
    PreSSmaxrc[j]=0;
    PreSSminrc[j]=0;

    PreSCDstart[j]=0;
    PreSCD[j]=0;
    PreSCDmaxr[j]=0;
    PreSCDminr[j]=0;
    PreSCD8[j]=0;
//    PreSCDfin[j]=0;
    PreSCDtext[j]=0;
  }
  return;
}

/*  Procedure to copy the TD Setup Sell arrays (previous) into the TD Setup Sell arrays (current).  */

procedure CopySSpre(Loop,Ival)
{
  for (j=Loop; j<Ival; j++)
  {
    CurSSmaxr[j]=PreSSmaxr[j];
    CurSSminr[j]=PreSSminr[j];
    CurSScnt[j]=PreSScnt[j];
    CurSSmaxrc[j]=PreSSmaxrc[j];
    CurSSminrc[j]=PreSSminrc[j];

    CurSCDstart[j]=PreSCDstart[j];
    CurSCD[j]=PreSCD[j];
    CurSCDmaxr[j]=PreSCDmaxr[j];
    CurSCDminr[j]=PreSCDminr[j];
    CurSCD8[j]=PreSCD8[j];
    CurSCDfin[j]=PreSCDfin[j];
    CurSCDtext[j]=PreSCDtext[j];
  }
  return;
} 



/*  Begin Assignments.  */

SetBarsRequired(sbrAll);

/*  Identify Bullish and Bearish Price Flips. Then, latch these values. */

Bullflip=IIf(Close>Ref(Close,-4) AND Ref(Close,-1)<Ref(Close,-5),True,False);
Bearflip=IIf(Close<Ref(Close,-4) AND Ref(Close,-1)>Ref(Close,-5),True,False);

Bullhold=Flip(Bullflip,Bearflip);
Bearhold=Flip(Bearflip,Bullflip);

/*  Identify TD Setup's - Buy and Sell.  */

Buyconsecdays=IIf(Close<Ref(Close,-4),True,False);
Buy9Bars=BarsSince(BarsSince(Buyconsecdays));
Buy9Signal=IIf(Buy9Bars==9,True,False);
Buy9Req=IIf(Buy9Signal==True AND Ref(Bearflip,-8)==True,True,False);

BSetupind=IIf(Bearhold==True AND Buy9Bars>=9,True,False);
BSmaxr=IIf(BSetupind==True,HHV(High,Buy9Bars),0);
BSminr=IIf(BSetupind==True,LLV(Low,Buy9Bars),0);
BScnt=Sum(BSetupind,BarsSince(BSetupind==False))+8;
BSmaxrc=IIf(BSetupind==True,HHV(Close,Buy9Bars),0);
BSminrc=IIf(BSetupind==True,LLV(Close,Buy9Bars),0);


Sellconsecdays=IIf(Close>Ref(Close,-4),True,False);
Sell9Bars=BarsSince(BarsSince(Sellconsecdays));
Sell9Signal=IIf(Sell9Bars==9,True,False);
Sell9Req=IIf(Sell9Signal==True AND Ref(Bullflip,-8)==True,True,False);

SSetupind=IIf(Bullhold==True AND Sell9Bars>=9,True,False);
SSmaxr=IIf(SSetupind==True,HHV(High,Sell9Bars),0);
SSminr=IIf(SSetupind==True,LLV(Low,Sell9Bars),0);
SScnt=Sum(SSetupind,BarsSince(SSetupind==False))+8;
SSmaxrc=IIf(SSetupind==True,HHV(Close,Sell9Bars),0);
SSminrc=IIf(SSetupind==True,LLV(Close,Sell9Bars),0);


/*  Prepare Countdown Arrays for TD Setup's - Buy and Sell.  */

Buyconseccloses=IIf(Close<=Ref(Low,-2),True,False);
Sellconseccloses=IIf(Close>=Ref(High,-2),True,False);

/*  Get the highest high's and the lowests low's to have occurred in the last 9 days.  */

HHV9=HHV(High,9);
LLV9=LLV(Low,9);

WhenHHV9=HHVBars(High,9);
WhenLLV9=LLVBars(Low,9);

/*  Begin TD Sequential - Defining the Trend and Identifying Exhaustion Points.  */

for (i=0; i<BarCount; i++)
{

  /*  Obtain TD Sequential Digits to be plotted.  */

  if (Buy9Bars[i]>0 AND Buy9Bars[i]<10 AND Bearflip[i+1-Buy9Bars[i]]==True) TDSeqbuydigits[i]=Buy9Bars[i];
  if (Sell9Bars[i]>0 AND Sell9Bars[i]<10 AND Bullflip[i+1-Sell9Bars[i]]==True) TDSeqselldigits[i]=Sell9Bars[i];

  /*  Delete numbers that were created if there is a break in the sequence before 9 is reached.  */

  if (i>0 AND TDSeqbuydigits[i-1]>TDSeqbuydigits[i]) TDSeqbuydigits=seqcheck(TDSeqbuydigits,i);
  if (i>0 AND TDSeqselldigits[i-1]>TDSeqselldigits[i]) TDSeqselldigits=seqcheck(TDSeqselldigits,i);

  /*  Update current running support and resistance lines  */
  /*  How many the program will need is unknown !! - Limit is 9 for each.  */

  if (i>0)
  {
    if (sup1[i-1]>-1 AND sup1[i-1]<Close[i]) sup1[i]=sup1[i-1];
    if (sup2[i-1]>-1 AND sup2[i-1]<Close[i]) sup2[i]=sup2[i-1];
    if (sup3[i-1]>-1 AND sup3[i-1]<Close[i]) sup3[i]=sup3[i-1];
    if (sup4[i-1]>-1 AND sup4[i-1]<Close[i]) sup4[i]=sup4[i-1];
    if (sup5[i-1]>-1 AND sup5[i-1]<Close[i]) sup5[i]=sup5[i-1];
    if (sup6[i-1]>-1 AND sup6[i-1]<Close[i]) sup6[i]=sup6[i-1];
    if (sup7[i-1]>-1 AND sup7[i-1]<Close[i]) sup7[i]=sup7[i-1];
    if (sup8[i-1]>-1 AND sup8[i-1]<Close[i]) sup8[i]=sup8[i-1];
    if (sup9[i-1]>-1 AND sup9[i-1]<Close[i]) sup9[i]=sup9[i-1];

    if (res1[i-1]>-1 AND res1[i-1]>Close[i]) res1[i]=res1[i-1];
    if (res2[i-1]>-1 AND res2[i-1]>Close[i]) res2[i]=res2[i-1];
    if (res3[i-1]>-1 AND res3[i-1]>Close[i]) res3[i]=res3[i-1];
    if (res4[i-1]>-1 AND res4[i-1]>Close[i]) res4[i]=res4[i-1];
    if (res5[i-1]>-1 AND res5[i-1]>Close[i]) res5[i]=res5[i-1];
    if (res6[i-1]>-1 AND res6[i-1]>Close[i]) res6[i]=res6[i-1];
    if (res7[i-1]>-1 AND res7[i-1]>Close[i]) res7[i]=res7[i-1];
    if (res8[i-1]>-1 AND res8[i-1]>Close[i]) res8[i]=res8[i-1];
    if (res9[i-1]>-1 AND res9[i-1]>Close[i]) res9[i]=res9[i-1];
  }

  /*  The following code is essentially for the drawing of the TDST support and resistance lines.  */
  /*  Test for a new support level and place in an empty support array  */ 

  if (Sell9Req[i]==True)
  {
    /*  Determine which support array to use  */

    tfarray[i]=True;
    if (tfarray[i]==True) sup1= supres(sup1,(i-WhenLLV9[i]),i,LLV9[i]);
    if (tfarray[i]==True) sup2= supres(sup2,(i-WhenLLV9[i]),i,LLV9[i]);
    if (tfarray[i]==True) sup3= supres(sup3,(i-WhenLLV9[i]),i,LLV9[i]);
    if (tfarray[i]==True) sup4= supres(sup4,(i-WhenLLV9[i]),i,LLV9[i]);
    if (tfarray[i]==True) sup5= supres(sup5,(i-WhenLLV9[i]),i,LLV9[i]);
    if (tfarray[i]==True) sup6= supres(sup6,(i-WhenLLV9[i]),i,LLV9[i]);
    if (tfarray[i]==True) sup7= supres(sup7,(i-WhenLLV9[i]),i,LLV9[i]);
    if (tfarray[i]==True) sup8= supres(sup8,(i-WhenLLV9[i]),i,LLV9[i]);
    if (tfarray[i]==True) sup9= supres(sup9,(i-WhenLLV9[i]),i,LLV9[i]);  

    /*  Check to see if we need to erase the lowest support line (all 9 in-use) to make way for the newest.  */

    if (tfarray[i]==True)
    {
      for (j=0; j<BarCount; j++)
      {
        sup1[j]=sup2[j];
        sup2[j]=sup3[j];
        sup3[j]=sup4[j];
        sup4[j]=sup5[j];
        sup5[j]=sup6[j];
        sup6[j]=sup7[j];
        sup7[j]=sup8[j];
        sup8[j]=sup9[j];
        sup9[j]=Null;
      }
      sup9= supres(sup9,(i-WhenLLV9[i]),i,LLV9[i]);
    }  

    /*  Check to ensure the latest addition is in the correct 'sup' array.  */

    if ((NOT(IsEmpty(sup9[i]) OR IsEmpty(sup8[i]))) AND sup9[i]<sup8[i])
    {
      for (j=0; J<BarCount; j++)
      {
        swap[j]=sup9[j];
        sup9[j]=sup8[j];
        sup8[j]=swap[j];
      }
    }
    if ((NOT(IsEmpty(sup8[i]) OR IsEmpty(sup7[i]))) AND sup8[i]<sup7[i])
    {
      for (j=0; J<BarCount; j++)
      {
        swap[j]=sup8[j];
        sup8[j]=sup7[j];
        sup7[j]=swap[j];
      }
    }
    if ((NOT(IsEmpty(sup7[i]) OR IsEmpty(sup6[i]))) AND sup7[i]<sup6[i])
    {
      for (j=0; J<BarCount; j++)
      {
        swap[j]=sup7[j];
        sup7[j]=sup6[j];
        sup6[j]=swap[j];
      }
    }
    if ((NOT(IsEmpty(sup6[i]) OR IsEmpty(sup5[i]))) AND sup6[i]<sup5[i])
    {
      for (j=0; J<BarCount; j++)
      {
        swap[j]=sup6[j];
        sup6[j]=sup5[j];
        sup5[j]=swap[j];
      }
    }
    if ((NOT(IsEmpty(sup5[i]) OR IsEmpty(sup4[i]))) AND sup5[i]<sup4[i])
    {
      for (j=0; J<BarCount; j++)
      {
        swap[j]=sup5[j];
        sup5[j]=sup4[j];
        sup4[j]=swap[j];
      }
    }
    if ((NOT(IsEmpty(sup4[i]) OR IsEmpty(sup3[i]))) AND sup4[i]<sup3[i])
    {
      for (j=0; J<BarCount; j++)
      {
        swap[j]=sup4[j];
        sup4[j]=sup3[j];
        sup3[j]=swap[j];
      }
    }
    if ((NOT(IsEmpty(sup3[i]) OR IsEmpty(sup2[i]))) AND sup3[i]<sup2[i])
    {
      for (j=0; J<BarCount; j++)
      {
        swap[j]=sup3[j];
        sup3[j]=sup2[j];
        sup2[j]=swap[j];
      }
    }
    if ((NOT(IsEmpty(sup2[i]) OR IsEmpty(sup1[i]))) AND sup2[i]<sup1[i])
    {
      for (j=0; J<BarCount; j++)
      {
        swap[j]=sup2[j];
        sup2[j]=sup1[j];
        sup1[j]=swap[j];
      }
    }
  }

  /*  Test for a new resistance level and place in empty resistance array  */ 

  if (Buy9Req[i]==True)
  {

    /*  Determine which resistance array to use  */

    tfarray[i]=True;
    if (tfarray[i]==True) res1= supres(res1,(i-WhenHHV9[i]),i,HHV9[i]);
    if (tfarray[i]==True) res2= supres(res2,(i-WhenHHV9[i]),i,HHV9[i]);
    if (tfarray[i]==True) res3= supres(res3,(i-WhenHHV9[i]),i,HHV9[i]);
    if (tfarray[i]==True) res4= supres(res4,(i-WhenHHV9[i]),i,HHV9[i]);
    if (tfarray[i]==True) res5= supres(res5,(i-WhenHHV9[i]),i,HHV9[i]);
    if (tfarray[i]==True) res6= supres(res6,(i-WhenHHV9[i]),i,HHV9[i]);
    if (tfarray[i]==True) res7= supres(res7,(i-WhenHHV9[i]),i,HHV9[i]);
    if (tfarray[i]==True) res8= supres(res8,(i-WhenHHV9[i]),i,HHV9[i]);
    if (tfarray[i]==True) res9= supres(res9,(i-WhenHHV9[i]),i,HHV9[i]);  

    /*  Check to see if we need to erase the highest resistance line (all 9 in-use) to make way for the newest.  */

    if (tfarray[i]==True)
    {
      for (j=0; j<BarCount; j++)
      {
        res1[j]=res2[j];
        res2[j]=res3[j];
        res3[j]=res4[j];
        res4[j]=res5[j];
        res5[j]=res6[j];
        res6[j]=res7[j];
        res7[j]=res8[j];
        res8[j]=res9[j];
        res9[j]=Null;
      }
      res9= supres(res9,(i-WhenHHV9[i]),i,HHV9[i]); 
    }

    /*  Check to ensure the latest addition is in the correct 'res' array.  */

    if ((NOT(IsEmpty(res9[i]) OR IsEmpty(res8[i]))) AND res9[i]>res8[i])
    {
      for (j=0; J<BarCount; j++)
      {
        swap[j]=res9[j];
        res9[j]=res8[j];
        res8[j]=swap[j];
      }
    }
    if ((NOT(IsEmpty(res8[i]) OR IsEmpty(res7[i]))) AND res8[i]>res7[i])
    {
      for (j=0; J<BarCount; j++)
      {
        swap[j]=res8[j];
        res8[j]=res7[j];
        res7[j]=swap[j];
      }
    }
    if ((NOT(IsEmpty(res7[i]) OR IsEmpty(res6[i]))) AND res7[i]>res6[i])
    {
      for (j=0; J<BarCount; j++)
      {
        swap[j]=res7[j];
        res7[j]=res6[j];
        res6[j]=swap[j];
      }
    }
    if ((NOT(IsEmpty(res6[i]) OR IsEmpty(res5[i]))) AND res6[i]>res5[i])
    {
      for (j=0; J<BarCount; j++)
      {
        swap[j]=res6[j];
        res6[j]=res5[j];
        res5[j]=swap[j];
      }
    }
    if ((NOT(IsEmpty(res5[i]) OR IsEmpty(res4[i]))) AND res5[i]>res4[i])
    {
      for (j=0; J<BarCount; j++)
      {
        swap[j]=res5[j];
        res5[j]=res4[j];
        res4[j]=swap[j];
      }
    }
    if ((NOT(IsEmpty(res4[i]) OR IsEmpty(res3[i]))) AND res4[i]>res3[i])
    {
      for (j=0; J<BarCount; j++)
      {
        swap[j]=res4[j];
        res4[j]=res3[j];
        res3[j]=swap[j];
      }
    }
    if ((NOT(IsEmpty(res3[i]) OR IsEmpty(res2[i]))) AND res3[i]>res2[i])
    {
      for (j=0; J<BarCount; j++)
      {
        swap[j]=res3[j];
        res3[j]=res2[j];
        res2[j]=swap[j];
      }
    }
    if ((NOT(IsEmpty(res2[i]) OR IsEmpty(res1[i]))) AND res2[i]>res1[i])
    {
      for (j=0; J<BarCount; j++)
      {
        swap[j]=res2[j];
        res2[j]=res1[j];
        res1[j]=swap[j];
      }
    }
  }

  /*  Support and resistance arrays ready for plotting.  */

  /*  Determine which TDST lines are currently being used.  */

  supval[0]=sup1[i];
  supval[1]=sup2[i];
  supval[2]=sup3[i];
  supval[3]=sup4[i];
  supval[4]=sup5[i];
  supval[5]=sup6[i];
  supval[6]=sup7[i];
  supval[7]=sup8[i];
  supval[8]=sup9[i];

  resval[0]=res1[i];
  resval[1]=res2[i];
  resval[2]=res3[i];
  resval[3]=res4[i];
  resval[4]=res5[i];
  resval[5]=res6[i];
  resval[6]=res7[i];
  resval[7]=res8[i];
  resval[8]=res9[i];

  /*  Hopefully, get a valid support value into the top location.  */

  Count=0;
  while (IsEmpty(supval[8]) AND Count<8)
  {
    for (j=0; j<8; j++)
    {
      supval[8-j]=supval[7-j];
    }
    Count=Count+1;
  }

  /*  Hopefully, get a valid resistance value into the top location.  */

  Count=0;
  while (IsEmpty(resval[8]) AND Count<8)
  {
    for (j=0; j<8; j++)
    {
      resval[8-j]=resval[7-j];
    }
    Count=Count+1;
  }

  /*  Record support and resistance values.  */

  supinuse[i]=supval[8];
  resinuse[i]=resval[8];

  /*  Determine if the current close halts a current support or resistance line.  */

  if (TDSeqbuydigits[i]>0 AND (NOT IsEmpty(supinuse[i-1])) AND Close[i]<supinuse[i-1]) supvio[i]=True;
  if (TDSeqselldigits[i]>0 AND (NOT IsEmpty(resinuse[i-1])) AND Close[i]>resinuse[i-1]) resvio[i]=True; 

  /*  Set flags to indicate completed TD Buy and Sell Countdown sequences.  */

  if (i>0)
  {
    BCDind[i]=BCDind[i-1];      
    SCDind[i]=SCDind[i-1];

    PreBCDfin[i]=PreBCDfin[i-1];
    CurBCDfin[i]=CurBCDfin[i-1];

    PreSCDfin[i]=PreSCDfin[i-1];
    CurSCDfin[i]=CurSCDfin[i-1]; 
  }

  /*  Check Perl's Rules for Trading TD Buy Setups Objectively.  */

  if (Buy9Req[i]==True)
  {

    /*  Perl's Rule 1: Check for perfected TD Buy Setup.  */

    if (Low[i]<Low[i-1])
      tmpval[i]=Low[i];
    else
      tmpval[i]=Low[i-1];

    if (tmpval[i]<=Low[i-2] AND tmpval[i]<=Low[i-3]) 
    {

      /*  Perl's Rule 2: Check for TDST support violation.  */

      tmpval[i]=0;
      for (j=i-8; j<i+1; j++)
      {
        tmpval[i]=tmpval[i]+supvio[j];
      }

      if (tmpval[i]==0)
      {

        /*  Perl's Rule 3:  Check the close of TD Buy Setup bar 9 is in close proximity to TDST support.  */
        /*  This rule is highly subjective.  I have chosen the low of bar 9 to be within one ATR distance of the TDST support.  */

        if (NOT IsEmpty(supinuse[i]))
        {
          if ((Low[i]-supinuse[i])<distance[i]*gapprox)
          {

            /*  Perl's Risk Management: Calculating the TD Risk Level for Trading a TD Buy Setup.  */

            TDSeqBuy[i]=True;
            TDSeqbuystop[i]=LLV9[i]-(High[i-WhenLLV9[i]]-LLV9[i]);
            TDSeqbuyrisk[i]=Close[i]-TDSeqbuystop[i];
            TDSeqbuyreward[i]=resinuse[i]-Close[i];
            TDSeqbuyrrratio[i]=TDSeqbuyreward[i]/TDSeqbuyrisk[i];
          }
        }
      }
    }

    /*  Initiate setup for TD Buy Countdown.  */

    BCDind[i]=BCDind[i]+True;

    /*  Check to ensure only two TD Sequential Buy patterns are being considered.  */

    if (BCDind[i]>2)
    {
      BCDind[i]=BCDind[i]-1;
      kval=PreBCDstart[i];
      ResetBSpre(kval,i+1);
      PreBCDfin[i]=False;
    }

    /*  Filters That Cancel a Developing TD Buy Countdown.  */

    /*  1.  If the price action rallies and generates a TD Sell Setup, or  */
    /*  2.  If the market trades higher and posts a true low above the true high of the prior TD Buy Setup - that is, TDST Resistance.  */

    /*  The second condition is dealt with later in the program.  */

    if (SCDind[i]>0)
    {
      if (SCDind[i]>1)
      {
        SCDind[i]=SCDind[i]-1;
        kval=PreSCDstart[i];
        ResetSSpre(kval,i+1);
      }

      SCDind[i]=SCDind[i]-1;
      kval=CurSCDstart[i];
      ResetSScur(kval,i+1);
     
    }

    PreSCDfin[i]=False;
    CurSCDfin[i]=False;
    
    /*  Determine if another Buy TD Sequential Setup has been initiated before the completion of the previous TD Buy Countdown.  */

    if (BCDind[i]==2)
    {

      /*  Record details of the previous Buy TD Sequential Setup and Countdown.  */

      for (j=CurBCDstart[i-1]; j<i; j++)
      {
        PreBSmaxr[j]=CurBSmaxr[j];
        PreBSminr[j]=CurBSminr[j];
        PreBScnt[j]=CurBScnt[j];
        PreBSmaxrc[j]=CurBSmaxrc[j];
        PreBSminrc[j]=CurBSminrc[j];
        
        PreBCD[j]=CurBCD[j];
        PreBCDmaxr[j]=CurBCDmaxr[j];
        PreBCDminr[j]=CurBCDminr[j];
        PreBCD8[j]=CurBCD8[j];
        PreBCDtext[j]=CurBCDtext[j];
        PreBCDstart[j]=CurBCDstart[j];
        PreBCDfin[j]=CurBCDfin[j];
      }

      ResetBScur(CurBCDstart[i-1],i);
    }

    /*  Record details of the new Buy TD Sequential Setup.  */

    CurBCD[i]=0;
    CurBCDmaxr[i]=-999999;
    CurBCDminr[i]=999999;
    CurBCDtext[i]=0;
    CurBCDstart[i]=i;
  }

  /*  Check Perl's Rules for Trading TD Sell Setups Objectively.  */

  if (Sell9Req[i]==True)
  {

    /*  Perl's Rule 1: Check for perfected TD Sell Setup.  */

    if (High[i]>High[i-1])
      tmpval[i]=High[i];
    else
      tmpval[i]=High[i-1];

    if (tmpval[i]>=High[i-2] AND tmpval[i]>=High[i-3]) 
    {

      /*  Perl's Rule 2: Check for TDST resistance violation.  */

      tmpval[i]=0;
      for (j=i-8; j<i+1; j++)
      {
        tmpval[i]=tmpval[i]+resvio[j];
      }

      if (tmpval[i]==0)
      {

        /*  Perl's Rule 3:  Check the close of TD Sell Setup bar 9 is in close proximity to TDST resistance.  */
        /*  This rule is highly subjective.  I have chosen the high of bar 9 to be within one ATR distance of the TDST resistance.  */

        if (NOT IsEmpty(resinuse[i]))
        {
          if ((resinuse[i]-High[i])<distance[i]*gapprox)
          {

            /*  Perl's Risk Management: Calculating the TD Risk Level for Trading a TD Sell Setup.  */

            TDSeqSell[i]=True;
            TDSeqsellstop[i]=HHV9[i]+(HHV9[i]-Low[i-WhenHHV9[i]]);
            TDSeqsellrisk[i]=TDSeqsellstop[i]-Close[i];
            TDSeqsellreward[i]=Close[i]-supinuse[i];
            TDSeqsellrrratio[i]=TDSeqsellreward[i]/TDSeqsellrisk[i];
          }
        }
      }
    }

    /*  Initiate setup for TD Sell Countdown.  */

    SCDind[i]=SCDind[i]+True;

    /*  Check to ensure only two TD Sequential Buy patterns are being considered.  */

    if (SCDind[i]>2)
    {
      SCDind[i]=SCDind[i]-1;
      kval=PreSCDstart[i];
      ResetSSpre(kval,i+1);
      PreSCDfin[i]=False;
    }


    /*  Filters That Cancel a Developing TD Sell Countdown.  */

    /*  1.  If the price action declines and generates a TD Buy Setup, or  */
    /*  2.  If the market trades lower and posts a true high below the true low of the prior TD Sell Setup - that is, TDST support.  */

    /*  The second condition is dealt with later in the program.  */

    if (BCDind[i]>0)
    {
      if (BCDind[i]>1)
      {
        BCDind[i]=BCDind[i]-1;
        kval=PreBCDstart[i];
        ResetBSpre(kval,i+1);
      }

      BCDind[i]=BCDind[i]-1;
      kval=CurBCDstart[i];
      ResetBScur(kval,i+1);
     
    }

    PreBCDfin[i]=False;
    CurBCDfin[i]=False;
    
    /*  Determine if another Sell TD Sequential Setup has been initiated before the completion of the previous TD Sell Countdown.  */

    if (SCDind[i]==2)
    {

      /*  Record details of the previous Sell TD Sequential Setup and Countdown.  */

      for (j=CurSCDstart[i-1]; j<i; j++)
      {
        PreSSmaxr[j]=CurSSmaxr[j];
        PreSSminr[j]=CurSSminr[j];
        PreSScnt[j]=CurSScnt[j];
        PreSSmaxrc[j]=CurSSmaxrc[j];
        PreSSminrc[j]=CurSSminrc[j];
        
        PreSCD[j]=CurSCD[j];
        PreSCDmaxr[j]=CurSCDmaxr[j];
        PreSCDminr[j]=CurSCDminr[j];
        PreSCD8[j]=CurSCD8[j];
        PreSCDtext[j]=CurSCDtext[j];
        PreSCDstart[j]=CurSCDstart[j];
        PreSCDfin[j]=CurSCDfin[j];
      }

      ResetSScur(CurSCDstart[i-1],i);
    }

    /*  Record details of the new Sell TD Sequential Setup.  */

    CurSCD[i]=0;
    CurSCDmaxr[i]=-999999;
    CurSCDminr[i]=999999;
    CurSCDtext[i]=0;
    CurSCDstart[i]=i;
  }

  /*  Perl's Rules for trading TD Countdowns.  */
  /*  Check to see if a TD Buy Countdown is being processed.  */

  if (BCDind[i]>0)
  {
    if (BCDind[i]>1)
    {

      /*  Record details of the ongoing (previous) Buy TD Sequential Setup.  */

      PreBSmaxr[i]=PreBSmaxr[i-1];
      PreBSminr[i]=PreBSminr[i-1];
      PreBScnt[i]=PreBScnt[i-1];
      PreBSmaxrc[i]=PreBSmaxrc[i-1];
      PreBSminrc[i]=preBSminrc[i-1];

      /*  Check to see if the ongoing (previous) TD Buy Countdown is still in progress and record details.  */

      PreBCDstart[i]=PreBCDstart[i-1];
      PreBCD[i]=PreBCD[i-1]+Buyconseccloses[i];
      PreBCDmaxr[i]=PreBCDmaxr[i-1];
      PreBCDminr[i]=PreBCDminr[i-1];
      if (PreBCDminr[i]>Low[i] AND PreBCD[i]>0) 
      {
        PreBCDmaxr[i]=High[i];
        PreBCDminr[i]=Low[i];
      }
      PreBCD8[i]=PreBCD8[i-1];
      if (PreBCD[i]==8 AND PreBCD[i-1]!=8) PreBCD8[i]=Close[i];

      /*  Perl's Rules to Complete a TD Buy Countdown.  */
      /*  1.  The Low of TD Buy Countdown bar 13 must be less than, or equal to, the close of TD Buy Countdown bar 8.  */
      /*  2.  The close of TD Buy Countdown bar 13 must be less than, or equal to, the low two bars earlier.  */

      if (Buyconseccloses[i]==True)
      {
        if (PreBCD[i]<13)
        {
          PreBCDtext[i]=PreBCD[i];
        }
        else
        {
          if (PreBCD[i]>12 AND Low[i]<=PreBCD8[i])
          {
            PreBCDtext[i]=13;
            BCDplot[i]=PreBCDtext[i];

            /*  Perl's Risk Management for a TD Buy Countdown.  */

            BCDrisk[i]=PreBCDminr[i]-(PreBCDmaxr[i]-PreBCDminr[i]);
            
            kval=PreBCDstart[i];
            ResetBSpre(kval,i+1);
            BCDind[i]=BCDind[i]-1;
            PreBCDfin[i]=i;
          }
          else
          {
            PreBCDtext[i]=43;    //  Deferred Countdown print "+".
          }
        }
      }
    }

    /*  Record details of the current Buy TD Sequential Setup.  */

    if (CurBCDmaxr[i]<0)
    {
      CurBSmaxr[i]=BSmaxr[i];
      CurBSminr[i]=BSminr[i];
      CurBScnt[i]=BScnt[i];
      CurBSmaxrc[i]=BSmaxrc[i];
      CurBSminrc[i]=BSminrc[i];

      /*  Record details of the current TD Buy Countdown.  */

      CurBCD[i]=CurBCD[i]+Buyconseccloses[i];
      if (Buyconseccloses[i]==False)
      {
        CurBCDmaxr[i]=0;
      }
      else
      {
        CurBCDmaxr[i]=High[i];
        CurBCDminr[i]=Low[i];
      }
    }
    else
    {

      /*  Record details of the current Buy TD Sequential Setup.  */

      if (BSetupind[i]==True)
      {
        CurBSmaxr[i]=BSmaxr[i];
        CurBSminr[i]=BSminr[i];
        CurBScnt[i]=BScnt[i];
        CurBSmaxrc[i]=BSmaxrc[i];
        CurBSminrc[i]=BSminrc[i];
      }
      else
      {
        CurBSmaxr[i]=CurBSmaxr[i-1];
        CurBSminr[i]=CurBSminr[i-1];
        CurBScnt[i]=CurBScnt[i-1];
        CurBSmaxrc[i]=CurBSmaxrc[i-1];
        CurBSminrc[i]=CurBSminrc[i-1];
      }

      /*  Record details of the current TD Buy Countdown.  */

      CurBCDstart[i]=CurBCDstart[i-1];
      CurBCD[i]=CurBCD[i-1]+Buyconseccloses[i];
      CurBCDmaxr[i]=CurBCDmaxr[i-1];
      CurBCDminr[i]=CurBCDminr[i-1];
      if (CurBCDminr[i]>Low[i] AND CurBCD[i]>0)
      {
        CurBCDmaxr[i]=High[i];
        CurBCDminr[i]=Low[i];
      }
      CurBCD8[i]=CurBCD8[i-1];
      if (CurBCD[i]==8 AND CurBCD[i-1]!=8) CurBCD8[i]=Close[i];
    }

    /*  Perl's Rules to Complete a TD Buy Countdown.  */
    /*  1.  The Low of TD Buy Countdown bar 13 must be less than, or equal to, the close of TD Buy Countdown bar 8.  */
    /*  2.  The close of TD Buy Countdown bar 13 must be less than, or equal to, the low two bars earlier.  */

    if (Buyconseccloses[i]==True)
    {
      if (CurBCD[i]<13)
      {
        CurBCDtext[i]=CurBCD[i];
      }
      else
      {
        if (CurBCD[i]>12 AND Low[i]<=CurBCD8[i])
        {
          CurBCDtext[i]=13;
          BCDplot[i]=CurBCDtext[i];

          /*  Perl's Risk Management for a TD Buy Countdown.  */

          BCDrisk[i]=CurBCDminr[i]-(CurBCDmaxr[i]-CurBCDminr[i]);
          BCDrisk[i-1]=CurBCDminr[i];

          kval=CurBCDstart[i];
          ResetBScur(kval,i+1);
          BCDind[i]=BCDind[i]-1;
          CurBCDfin[i]=i;
        }
        else
        {
          CurBCDtext[i]=43;    //  Deferred Countdown use "+" sign.
        }
      }
    }

    /*  Filters That Cancel a Developing TD Buy Countdown.  */
    /*  1.  If the price action rallies and generates a TD Sell Setup, or  */
    /*  2.  If the market trades higher and posts a true low above the true high of the prior TD Buy Setup - that is, TDST Resistance.  */

    /*  The first condition is dealt with earlier in the program.  */

    if (Low[i]>HHV9[CurBCDstart[i]] AND CurBCDstart[i]>0)
    {
      BCDind[i]=BCDind[i]-1;
      kval=CurBCDstart[i];
      ResetBScur(kval,i+1);

      /*  If we have an ongoing (previous) TD Buy Countdown then, make it current.  */

      if (BCDind[i]>0)
      {
        kval=PreBCDstart[i];
        CopyBSpre(kval,i+1);
        ResetBSpre(kval,i+1);
      }
    }

    /*  TD Buy Countdown Cancellation and Recycle Qualifiers.  */

    if (BCDind[i]>1 AND BSetupind[i]==False AND BSetupind[i-1]==True)
    {

      /*  TD Buy Countdown Cancellation Qualifier 1.  */

      /*  If the size of the true range of the most recently completed TD Buy Setup is equal to, or greater than,  */
      /*  the size of the previous TD Buy Setup, but less than 1.618 times it's size then,  */
      /*  a TD Setup Recycle will occcur; that is, whichever TD Buy Setup has the larger true range will become  */
      /*  the active TD Buy Setup.  */

      TRcur=CurBSmaxr[i]-CurBSminr[i];
      TRpre=PreBSmaxr[i]-PreBSminr[i];
      if (TRcur>=TRpre AND TRcur<1.618*TRpre)
      {
        if (TRcur>TRpre)
        {

          /*  Reset the TD Buy Setup previous arrays.  */

          BCDind[i]=BCDind[i]-1;
          kval=PreBCDstart[i];
          ResetBSpre(kval,i+1);
        }
        else
        {
          BCDind[i]=BCDind[i]-1;
          kval=CurBCDstart[i];
          ResetBSCur(kval,i+1);

          kval=PreBCDstart[i];
          CopyBSpre(kval,i+1);
          ResetBSpre(kval,i+1);
        }
      }

      /*  TD Buy Countdown Cancellation Qualifier 2 (a TD Buy Setup Within a TD Buy Setup).  */

      /*  If the market has completed a TD Buy Setup that has a closing range within the true range  */
      /*  of the prior TD Buy Setup, without recording a TD Sell Setup between the two, and if  */
      /*  the current TD buy Setup has a price extreme within the true range of the prior TD Buy Setup, then  */
      /*  the prior TD BUY Setup is the active TD Setup, and the TD Buy Countdown relating to it remains intact.  */

      if (BCDind[i]>1 AND CurBSmaxrc[i]<PreBSmaxr[i] AND CurBSminrc[i]>PreBSminr[i] AND (CurBSmaxr[i]<PreBSmaxr[i] OR CurBSminr[i]>PreBSminr[i]))
      {
        BCDind[i]=BCDind[i]-1;
        kval=CurBCDstart[i];
        ResetBSCur(kval,i+1);

        kval=PreBCDstart[i];
        CopyBSpre(kval,i+1);
        ResetBSpre(kval,I+1);
      }
    }

    /*  TD Buy Countdown Recycle Qualifier.  */

    /*  An 'R' will appear when a TD Buy Setup that began before, on, or after the completion of a developing TD Buy Countdown,  */
    /*  but prior to a bullish TD Price Flip, extends to eighteen bars - that is, eighteen closes, with each one less than than  */
    /*  the close four price bars earlier.  */

    if (CurBScnt[i]==18 AND CurBScnt[i-1]!=18)
    {
      BCDplot[i]=82;    //  Recycle Qualifier use the letter "R".
    }

    /*  Requirements for Validation of a TD Sequential 9-13-9 Buy Count.  */

    /*  1.  The TD Buy Setup must not begin before or on the same price bar as the completed TD Buy Countdown.  */
    /*  2.  The ensuing bullish TD Buy Setup must be preceded by a TD price flip, and  */
    /*  3.  No complted TD Sell Setup should occur prior to the appearence of the TD Buy Setup.  */

    if (CurBCDfin[i]>0 AND TDSeqbuydigits[i]==9)
    {
      if (CurBCDfin[i]<(i-8))
      {
        BCDplot[i]=9139;
        
        /*  Risk Management for the TD Sequential 9-13-9  */

        /*  Subtract the true range of the price bar with the lowest true low in the TD Buy Countdown  */
        /*  and ensuing TD Buy Setup from the true low of that bar.  */

        if (BCDrisk[CurBCDfin[i]-1]<LLV9[i])
        {
          BCDrisk[i]=BCDrisk[CurBCDfin[i]];
        }
        else
        {
          BCDrisk[i]=LLV9[i]-(High[i-whenLLV9[i]]-LLV9[i]);
        }
      }
      CurBCDfin[i]=False;
    }
  }

  /*  Check to see if a TD Sell Countdown is being processed.  */

  if (SCDind[i]>0)
  {
    if (SCDind[i]>1)
    {

      /*  Record details of the ongoing (previous) Sell TD Sequential Setup.  */

      PreSSmaxr[i]=PreSSmaxr[i-1];
      PreSSminr[i]=PreSSminr[i-1];
      PreSScnt[i]=PreSScnt[i-1];
      PreSSmaxrc[i]=PreSSmaxrc[i-1];
      PreSSminrc[i]=preSSminrc[i-1];

      /*  Check to see if the ongoing (previous) TD Sell Countdown is still in progress and record details.  */

      PreSCDstart[i]=PreSCDstart[i-1];
      PreSCD[i]=PreSCD[i-1]+Sellconseccloses[i];
      PreSCDmaxr[i]=PreSCDmaxr[i-1];
      PreSCDminr[i]=PreSCDminr[i-1];
      if (PreSCDmaxr[i]<High[i] AND PreSCD[i]>0) 
      {
        PreSCDmaxr[i]=High[i];
        PreSCDminr[i]=Low[i];
      }
      PreSCD8[i]=PreSCD8[i-1];
      if (PreSCD[i]==8 AND PreSCD[i-1]!=8) PreSCD8[i]=Close[i];

      /*  Perl's Rules to Complete a TD Sell Countdown.  */
      /*  1.  The High of TD Sell Countdown bar 13 must be greater than, or equal to, the close of TD Sell Countdown bar 8.  */
      /*  2.  The close of TD Sell Countdown bar 13 must be greater than, or equal to, the high two bars earlier.  */

      if (Sellconseccloses[i]==True)
      {
        if (PreSCD[i]<13)
        {
          PreSCDtext[i]=PreSCD[i];
        }
        else
        {
          if (PreSCD[i]>12 AND High[i]>=PreSCD8[i])
          {
            PreSCDtext[i]=13;
            SCDplot[i]=PreSCDtext[i];

            /*  Perl's Risk Management for a TD Buy Countdown.  */

            SCDrisk[i]=PreSCDmaxr[i]+(PreSCDmaxr[i]-PreSCDminr[i]);

            kval=PreSCDstart[i];
            ResetSSpre(kval,i+1);
            SCDind[i]=SCDind[i]-1;
            PreSCDfin[i]=i;
          }
          else
          {
            PreSCDtext[i]=43;
          }
        }
      }
    }

    /*  Record details of the current Sell TD Sequential Setup.  */

    if (CurSCDmaxr[i]<0)
    {
      CurSSmaxr[i]=SSmaxr[i];
      CurSSminr[i]=SSminr[i];
      CurSScnt[i]=SScnt[i];
      CurSSmaxrc[i]=SSmaxrc[i];
      CurSSminrc[i]=SSminrc[i];

      /*  Record details of the current TD Sell Countdown.  */

      CurSCD[i]=CurSCD[i]+Sellconseccloses[i];
      if (Sellconseccloses[i]==False)
      {
        CurSCDmaxr[i]=0;
      }
      else
      {
        CurSCDmaxr[i]=High[i];
        CurSCDminr[i]=Low[i];
      }
    }
    else
    {

      /*  Record details of the current Sell TD Sequential Setup.  */

      if (SSetupind[i]==True)
      {
        CurSSmaxr[i]=SSmaxr[i];
        CurSSminr[i]=SSminr[i];
        CurSScnt[i]=SScnt[i];
        CurSSmaxrc[i]=SSmaxrc[i];
        CurSSminrc[i]=SSminrc[i];
      }
      else
      {
        CurSSmaxr[i]=CurSSmaxr[i-1];
        CurSSminr[i]=CurSSminr[i-1];
        CurSScnt[i]=CurSScnt[i-1];
        CurSSmaxrc[i]=CurSSmaxrc[i-1];
        CurSSminrc[i]=CurSSminrc[i-1];
      }

      /*  Record details of the current TD Sell Countdown.  */

      CurSCDstart[i]=CurSCDstart[i-1];
      CurSCD[i]=CurSCD[i-1]+Sellconseccloses[i];
      CurSCDmaxr[i]=CurSCDmaxr[i-1];
      CurSCDminr[i]=CurSCDminr[i-1];
      if (CurSCDmaxr[i]<High[i] AND CurSCD[i]>0)
      {
        CurSCDmaxr[i]=High[i];
        CurSCDminr[i]=Low[i];
      }
      CurSCD8[i]=CurSCD8[i-1];
      if (CurSCD[i]==8 AND CurSCD[i-1]!=8) CurSCD8[i]=Close[i];
    }

    /*  Perl's Rules to Complete a TD Sell Countdown.  */
    /*  1.  The high of TD Sell Countdown bar 13 must be greater than, or equal to, the close of TD Sell Countdown bar 8.  */
    /*  2.  The close of TD Sell Countdown bar 13 must be greater than, or equal to, the high two bars earlier.  */

    if (Sellconseccloses[i]==True)
    {
      if (CurSCD[i]<13)
      {
        CurSCDtext[i]=CurSCD[i];
      }
      else
      {
        if (CurSCD[i]>12 AND High[i]>=CurSCD8[i])
        {
          CurSCDtext[i]=13;
          SCDplot[i]=CurSCDtext[i];

          /*  Perl's Risk Management for a TD Buy Countdown.  */

          SCDrisk[i]=CurSCDmaxr[i]+(CurSCDmaxr[i]-CurSCDminr[i]);
          SCDrisk[i-1]=CurSCDmaxr[i];

          kval=CurSCDstart[i];
          ResetSScur(kval,i+1);
          SCDind[i]=SCDind[i]-1;
          CurSCDfin[i]=i;
        }
        else
        {
          CurSCDtext[i]=43;    //  Deferred Countdown use "+" sign.
        }
      }   
    }

    /*  Filters That Cancel a Developing TD Sell Countdown.  */
    /*  1.  If the price action declines and generates a TD Buy Setup, or  */
    /*  2.  If the market trades lower and posts a true high below the true low of the prior TD Sell Setup - that is, TDST support.  */

    /*  The first condition is dealt with earlier in the program.  */

    if (High[i]<LLV9[CurSCDstart[i]] AND CurSCDstart[i]>0)
    {
      SCDind[i]=SCDind[i]-1;
      kval=CurSCDstart[i];
      ResetSScur(kval,i+1);

      /*  If we have an ongoing (previous) TD Sell Countdown then, make it current.  */

      if (SCDind[i]>0)
      {
        kval=PreSCDstart[i];
        CopySSpre(kval,i+1);
        ResetSSpre(kval,i+1);
      }
    }

    /*  TD Sell Countdown Cancellation and Recycle Qualifiers.  */

    if (SCDind[i]>1 AND SSetupind[i]==False AND SSetupind[i-1]==True)
    {

      /*  TD Sell Countdown Cancellation Qualifier 1.  */

      /*  If the size of the true range of the most recently completed TD Sell Setup is equal to, or greater than,  */
      /*  the size of the previous TD Sell Setup, but less than 1.618 times it's size then,  */
      /*  a TD Setup Recycle will occcur; that is, whichever TD Sell Setup has the larger true range will become  */
      /*  the active TD Sell Setup.  */

      TRcur=CurSSmaxr[i]-CurSSminr[i];
      TRpre=PreSSmaxr[i]-PreSSminr[i];
      if (TRcur>=TRpre AND TRcur<1.618*TRpre)
      {
        if (TRcur>TRpre)
        {

          /*  Reset the TD Sell Setup previous arrays.  */

          SCDind[i]=SCDind[i]-1;
          kval=PreSCDstart[i];
          ResetSSpre(kval,i+1);
        }
        else
        {
          SCDind[i]=SCDind[i]-1;
          kval=CurSCDstart[i];
          ResetSSCur(kval,i+1);

          kval=PreSCDstart[i];
          CopySSpre(kval,i+1);
          ResetSSpre(kval,i+1);
        }
      }

      /*  TD Sell Countdown Cancellation Qualifier 2 (a TD Sell Setup Within a TD Sell Setup).  */

      /*  If the market has completed a TD Sell Setup that has a closing range within the true range  */
      /*  of the prior TD Sell Setup, without recording a TD Buy Setup between the two, and if  */
      /*  the current TD Sell Setup has a price extreme within the true range of the prior TD Buy Setup, then  */
      /*  the prior TD Sell Setup is the active TD Setup, and the TD Sell Countdown relating to it remains intact.  */

      if (SCDind[i]>1 AND CurSSmaxrc[i]<PreSSmaxr[i] AND CurSSminrc[i]>PreSSminr[i] AND (CurSSmaxr[i]<PreSSmaxr[i] OR CurSSminr[i]>PreSSminr[i]))
      {
        SCDind[i]=SCDind[i]-1;
        kval=CurSCDstart[i];
        ResetSSCur(kval,i+1);

        kval=PreSCDstart[i];
        CopySSpre(kval,i+1);
        ResetSSpre(kval,i+1);
      }
    }

    /*  TD Buy Countdown Recycle Qualifier.  */

    /*  An 'R' will appear when a TD Sell Setup that began before, on, or after the completion of a developing TD Sell Countdown,  */
    /*  but prior to a bearish TD Price Flip, extends to eighteen bars - that is, eighteen closes, with each one greater than than  */
    /*  the close four price bars earlier.  */

    if (CurSScnt[i]==18 AND CurSScnt[i-1]!=18)
    {
      SCDplot[i]=82;    //  Recycle Qualifier use the letter "R".
    }

    /*  Requirements for Validation of a TD Sequential 9-13-9 Sell Count.  */

    /*  1.  The TD Sell Setup must not begin before or on the same price bar as the completed TD Sell Countdown.  */
    /*  2.  The ensuing bullish TD Sell Setup must be preceded by a TD price flip, and  */
    /*  3.  No completed TD Buy Setup should occur prior to the appearence of the TD Sell Setup.  */

    if (CurSCDfin[i]>0 AND TDSeqselldigits[i]==9)
    {
      if (CurSCDfin[i]<(i-8))
      {
        SCDplot[i]=9139;
        
        /*  Risk Management for the TD Sequential 9-13-9  */

        /*  Add the true range of the price bar with the highest true high in the TD Sell Countdown  */
        /*  and ensuing TD Sell Setup to the true high of that bar.  */

        if (SCDrisk[CurSCDfin[i]-1]>HHV9[i])
        {
          SCDrisk[i]=SCDrisk[CurSCDfin[i]];
        }
        else
        {
          SCDrisk[i]=HHV9[i]+(HHV9[i]-Low[i-WhenHHV9[i]]);
        }
      }
      CurSCDfin[i]=False;
    }
  }                      
}

/*  Sort plot area  */

Lowestlow=LowestVisibleValue(Low);
Highesthigh=HighestVisibleValue(High);

Plotrange=Highesthigh-Lowestlow;
Plotlow=IIf(Plotall==False,(Lowestlow-(Plotpercent/100*Plotrange)),0);
Plothigh=IIf(Plotall==False,(Highesthigh+(Plotpercent/100*Plotrange)),999999);

/*  perform plot routines.  */

Plot(C, "", -1, styleCandle);

/*  Plot red buy digits and pink Countdown digits. */

PlotShapes( IIf( BCDplot==13, shapeUpArrow, IIf(BCDplot==9139, shapeHollowUpArrow, shapeNone)), colorDarkRed,0,IIf(BCDplot>0,Low,High));

for (i=0; i<BarCount; i++)
{
  dist1[i] = reddist[i]*distance[i]; 
  if (TDSeqbuydigits[i]>0) PlotText( " " + NumToStr(TDSeqbuydigits[ i ],1.0), i, H[ i ]+dist1[i], colorRed );

  if (BCDplot[i]==82) PlotText("R",i,IIf(TDSeqbuydigits[i]>0,H[i]+2*dist1[i],H[i]+dist1[i]), colorDarkRed);
 
  if (BCDind[i]>1)
  {
    if (PreBCDtext[i]>0 AND PreBCDtext[i]<13) PlotText(" " + NumToStr(PreBCDtext[ i ],1.0),IIf(PreBCDtext[i]>9,i-0.5,i),IIf(TDSeqbuydigits[i]>0,H[i]+2*dist1[i],H[i]+dist1[i]), colorPink);
    if (PreBCDtext[i]==43) PlotText("+",i+0.25,IIf(TDSeqbuydigits[i]>0,H[i]+2*dist1[i],H[i]+dist1[i]), colorPink);
  }
  if (BCDind[i]==1)
  {
    if (CurBCDtext[i]>0 AND CurBCDtext[i]<13) PlotText(" " + NumToStr(CurBCDtext[ i ],1.0),IIf(CurBCDtext[i]>9,i-0.5,i),IIf(TDSeqbuydigits[i]>0,H[i]+2*dist1[i],H[i]+dist1[i]), colorDarkRed);
    if (CurBCDtext[i]==43) PlotText("+",i+0.25,IIf(TDSeqbuydigits[i]>0,H[i]+2*dist1[i],H[i]+dist1[i]), colorDarkRed);
  }

}

/*  plot blue sell digits.  */

PlotShapes( IIf( SCDplot==13, shapeDownArrow, IIf(SCDplot==9139, shapeHollowDownArrow, shapeNone)), colorIndigo,0,IIf(SCDplot>0,High,Low));

for (i=0; i<BarCount; i++)
{
  dist1[i] = bluedist[i]*distance[i];
  if (TDSeqselldigits[i]>0) PlotText( " " + NumToStr(TDSeqselldigits[ i ],1.0), i, L[ i ]-dist1[i], colorBlue ); 

   if (SCDplot[i]==82) PlotText("R",i,IIf(TDSeqselldigits[i]>0,L[i]-2*dist1[i],L[i]-dist1[i]), colorIndigo);

  if (SCDind[i]>1)
  {
    if (PreSCDtext[i]>0 AND PreSCDtext[i]<13) PlotText(" " + NumToStr(PreSCDtext[ i ],1.0),IIf(PreSCDtext[i]>9,i-0.5,i),IIf(TDSeqselldigits[i]>0,L[i]-2*dist1[i],L[i]-dist1[i]), colorTurquoise);
    if (PreSCDtext[i]==43) PlotText("+",i+0.25,IIf(TDSeqselldigits[i]>0,L[i]-2*dist1[i],L[i]-dist1[i]), colorTurquoise);
  }
  if (SCDind[i]==1)
  {
    if (CurSCDtext[i]>0 AND CurSCDtext[i]<13) PlotText(" " + NumToStr(CurSCDtext[ i ],1.0),IIf(CurSCDtext[i]>9,i-0.5,i),IIf(TDSeqselldigits[i]>0,L[i]-2*dist1[i],L[i]-dist1[i]), colorIndigo);
    if (CurSCDtext[i]==43) PlotText("+",i+0.25,IIf(TDSeqselldigits[i]>0,L[i]-2*dist1[i],L[i]-dist1[i]), colorIndigo);
  }
}  

PlotShapes( IIf( TDSeqBuy, shapeUpArrow, shapeNone ), colorYellow,0,IIf(TDSeqbuy,Low,High));
PlotShapes( IIf( TDSeqsell, shapeDownArrow, shapeNone ), colorYellow,0,IIf(TDSeqsell,High,Low)); 
//PlotShapes( IIf( supvio, IIf(supvio, shapeHollowDownTriangle, shapeUpTriangle), shapeNone ), colorYellow,0,IIf(supvio,High,Low));
//PlotShapes( IIf( resvio, IIf(resvio, shapeHollowUpTriangle, shapeDownTriangle), shapeNone ), colorYellow,0,IIf(resvio,Low,High));

Plot(IIf(res1<Plothigh,res1,Null),"res1",colorOrange,styleLine);
Plot(IIf(res2<Plothigh,res2,Null),"res2",colorYellow,styleLine);
Plot(IIf(res3<Plothigh,res3,Null),"res3",colorBlue,styleLine);
Plot(IIf(res4<Plothigh,res4,Null),"res4",colorGreen,styleLine);
Plot(IIf(res5<Plothigh,res5,Null),"res5",colorRed,styleLine);
Plot(IIf(res6<Plothigh,res6,Null),"res6",colorWhite,styleLine);
Plot(IIf(res7<Plothigh,res7,Null),"res7",colorBrown,styleLine);
Plot(IIf(res8<Plothigh,res8,Null),"res8",colorIndigo,styleLine);
Plot(IIf(res9<Plothigh,res9,Null),"res9",colorDarkGreen,styleLine);
Plot(IIf(sup1>PlotLow,sup1,Null),"sup1",colorOrange,styleLine);
Plot(IIf(sup2>PlotLow,sup2,Null),"sup2",colorYellow,styleLine);
Plot(IIf(sup3>PlotLow,sup3,Null),"sup3",colorBlue,styleLine);
Plot(IIf(sup4>PlotLow,sup4,Null),"sup4",colorGreen,styleLine);
Plot(IIf(sup5>PlotLow,sup5,Null),"sup5",colorRed,styleLine);
Plot(IIf(sup6>PlotLow,sup6,Null),"sup6",colorWhite,styleLine);
Plot(IIf(sup7>PlotLow,sup7,Null),"sup7",colorBrown,styleLine);
Plot(IIf(sup8>PlotLow,sup8,Null),"sup8",colorIndigo,styleLine);
Plot(IIf(sup9>PlotLow,sup9,Null),"sup9",colorDarkGreen,styleLine);

/*  Automatic Analysis Explore Routines.  */

/*  Setup Column Information.  */

NumColumns=8;
Column0=IIf(TDSeqBuy==True,True,False);
Column1=IIf(BCDplot==13,True,False);
Column2=IIf(BCDplot==9139,True,False);
Column3=IIf(BCDplot==82,True,False);
Column4=IIf(TDSeqSell==True,True,False);
Column5=IIf(SCDplot==13,True,False);
Column6=IIf(SCDplot==9139,True,False);
Column7=IIf(SCDplot==82,True,False);


Column0Name = "TDSeq Buy ";
Column1Name = "BCD 13 ";
Column2Name = "BCD 9-13-9  ";
Column3Name = "TDST Buy 'R'  ";
Column4Name = "TDSeq Sell ";
Column5Name = "SCD 13 ";
Column6Name = "SCD 9-13-9 ";
Column7Name = "TDST Sell 'R' ";

Filter= IIf(TDSeqBuy==True OR BCDplot==13 OR BCDplot==9139 OR BCDplot==82,True,False) OR IIf(TDSeqSell==True OR SCDplot==13 OR SCDplot==9139 OR SCDplot==82,True,False);


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Title = "{{DATE}} - "+Name()+" ("+ FullName()+ ") - "+" Open="+O+", High="+H+", Low="+L+", Close="+C+StrFormat(" (%.2f %.1f%%) ",IIf(ROC(C,1)==0,0,C-Ref(C,-1)),SelectedValue( ROC( C, 1 )))+
"\n"+EncodeColor(colorBlue) + WriteIf(TDSeqBuy==True, "\nPotential TDST Buy :    Risk= " + WriteVal(TDSeqbuyrisk,8.2) + "    Reward= " + WriteVal(TDSeqbuyreward,8.2) + "    Reward Risk Ratio= " + WriteVal(TDSeqbuyrrratio,8.2) + "    Stop @     " + WriteVal(TDSeqbuystop,8.2),"") +
""+EncodeColor(colorRed) +  WriteIf(TDSeqsell==True, "\nPotential TDST Short :    Risk= " + WriteVal(TDSeqsellrisk,8.2) + "    Reward= " + WriteVal(TDSeqsellreward,8.2) + "    Reward Risk Ratio= " + WriteVal(TDSeqsellrrratio,8.2) + "    Stop @     " + WriteVal(TDSeqsellstop,8.2),"") +
"\n"+EncodeColor(colorBlue) + WriteIf(BCDplot==13, "\nBuy CD 13 trade :  Stop loss at " + WriteVal(BCDRisk,8.2) + "    Risk is " + WriteVal(Close-BCDRisk,8.2),"") +
""+EncodeColor(colorBlue) + WriteIf(BCDplot==9139, "\nBuy CD 9-13-9 trade :  Stop loss at " + WriteVal(BCDRisk,8.2) + "    Risk is " + WriteVal(Close-BCDRisk,8.2),"") +
"\n"+EncodeColor(colorRed) + WriteIf(SCDplot==13, "\nSell CD 13 trade :  Stop loss at " + WriteVal(SCDRisk,8.2) + "    Risk is " + WriteVal(SCDRisk-Close,8.2),"") +
""+EncodeColor(colorRed) + WriteIf(SCDplot==9139, "\nSell CD 9-13-9 trade :  Stop loss at " + WriteVal(SCDRisk,8.2) + "    Risk is " + WriteVal(SCDRisk-Close,8.2),"") +
"\n"+EncodeColor(colorGold)+"     "+WriteVal(BarCount)+"  =BarCount" + "    bar Number =  " + WriteVal(Cum(1)-1,8.0);

_SECTION_END() ;