Simple Coding Help - No Promise.

amitrandive

Well-Known Member
Not sure what you want me to do. Second code of 1591 plots text for yesterday's high as you can see below.

But there is a semicolon in first code line missing at the end of that line.

trash

The semicolon is missing because , I have posted only part of the code in post#1591.Am reproducing the entire AFL here.


Code:
//ESSENTIAL TRADER TOOLS//

_SECTION_BEGIN("Price");
SetChartBkGradientFill( ParamColor("BgTop",colorBlack),ParamColor("BgBottom",colorBlack),ParamColor("Titleblock",colorLightGrey));
SetChartOptions(0,chartShowDates|chartShowArrows|chartLogarithmic|chartWrapTitle);
GraphXSpace = 5;
Plot(C,"",colorWhite,styleCandle);
_SECTION_END();

//Previous Days HI LO CL//

DayH = TimeFrameGetPrice("H", inDaily, -1);     DayHI = LastValue (DayH,1);// yesterdays high
DayL = TimeFrameGetPrice("L", inDaily, -1);     DayLI = LastValue (DayL,1);    // yesterdays low
DayC = TimeFrameGetPrice("C", inDaily, -1);       DayCI = LastValue (DayC,1);    // yesterdays close
DayO = TimeFrameGetPrice("O", inDaily);            // current day open
DayH2= TimeFrameGetPrice("H", inDaily, -2);  DayH2I = LastValue (DayH2,1); // Two days before high
DayL2= TimeFrameGetPrice("L", inDaily, -2);  DayL2I = LastValue (DayL2,1);  // Two days before low
DayC2= TimeFrameGetPrice("C", inDaily, -2);  DayC2I = LastValue (DayC2,1);  // Two days before close
DayH3= TimeFrameGetPrice("H", inDaily, -3);  DayH3I = LastValue (DayH3,1);  // Three days before high
DayL3= TimeFrameGetPrice("L", inDaily, -3);  DayL3I = LastValue (DayL3,1);  // Three days before low
DayC3= TimeFrameGetPrice("C", inDaily, -3);  DayC3I = LastValue (DayC3,1);  // Three days before close
numbars = LastValue(Cum(Status("barvisible")));
hts  = -33.5;

YHL = ParamToggle("Yesterday HI LO CL ","Show|Hide",1);
if(YHL==1) {
  Plot(DayL,"YL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(DayH,"YH",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(DayC,"YC",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  PlotText(" YH " ,     LastValue(BarIndex())-(numbars/Hts), DayHI, colorTurquoise);
  PlotText(" YL " ,     LastValue(BarIndex())-(numbars/Hts), DayLI, colorTurquoise);
PlotText(" YC " ,     LastValue(BarIndex())-(numbars/Hts), DayCI, colorTurquoise);
}

TDBHL = ParamToggle("2/3Days before HI LO CL","Show|Hide",1);
if(TDBHL==1) {
  Plot(DayL2,"2DBL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(DayH2,"2DBH",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(DayC2,"2DBH",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(DayL3,"3DBL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(DayH3,"3DBH",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
 Plot(DayC3,"3DBC",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  PlotText(" 2DBH " ,     LastValue(BarIndex())-(numbars/Hts), DayH2I, colorTurquoise);
  PlotText(" 2DBL " ,     LastValue(BarIndex())-(numbars/Hts), DayL2I, colorTurquoise);
 PlotText(" 2DBC " ,     LastValue(BarIndex())-(numbars/Hts), DayC2I, colorTurquoise);
  PlotText(" 3DBH " ,     LastValue(BarIndex())-(numbars/Hts), DayH3I, colorTurquoise);
  PlotText(" 3DBL " ,     LastValue(BarIndex())-(numbars/Hts), DayL3I, colorTurquoise);
 PlotText(" 3DBC " ,     LastValue(BarIndex())-(numbars/Hts), DayC3I, colorTurquoise);
}

// Pivot Levels //
PP = (DayL + DayH + DayC)/3;  PPI = LastValue (PP,1);   // Pivot
R1  =  (PP * 2) - DayL;       R1I = LastValue (R1,1);   // Resistance 1
S1  =  (PP * 2)  - DayH;      S1I = LastValue (S1,1);   // Support 1
R2  =  PP + R1 - S1;          R2I = LastValue (R2,1);   // Resistance 2
S2  =  PP - R1 + S1;          S2I = LastValue (S2,1);   // Support 2
R3  =  PP + R2 - S1;          R3I = LastValue (R3,1);   // Resistance 3
S3  =  PP - R2 + S1;          S3I = LastValue (S3,1);   // Support 3

ppl = ParamToggle("Pivot Levels","Show|Hide",1);
if(ppl==1)  {
  Plot(PP, "PP",colorYellow,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(R1, "R1",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(S1, "S1",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(R2, "R2",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(S2, "S2",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(R3, "R3",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(S3, "S3",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);

  PlotText(" Pivot ",  LastValue(BarIndex())-(numbars/Hts), PPI, colorYellow);
  PlotText(" R1 " ,    LastValue(BarIndex())-(numbars/Hts), R1I, colorViolet);
  PlotText(" S1 " ,    LastValue(BarIndex())-(numbars/Hts), S1I, colorViolet);
  PlotText(" R2 " ,    LastValue(BarIndex())-(numbars/Hts), R2I, colorViolet);
  PlotText(" S2 " ,    LastValue(BarIndex())-(numbars/Hts), S2I, colorViolet);
  PlotText(" R3 " ,    LastValue(BarIndex())-(numbars/Hts), R3I, colorViolet);
  PlotText(" S3 " ,    LastValue(BarIndex())-(numbars/Hts), S3I, colorViolet);
}
// Camerilla Levels //

rg = (DayH - DayL);

H5=DayC+1.1*rg;     H5I = LastValue (H5,1);
H4=DayC+1.1*rg/2;   H4I = LastValue (H4,1);
H3=DayC+1.1*rg/4;   H3I = LastValue (H3,1);
H2=DayC+1.1*rg/6;   H2I = LastValue (H2,1);
H1=DayC+1.1*rg/12;  H1I = LastValue (H1,1);
L1=DayC-1.1*rg/12;  L1I = LastValue (L1,1);
L2=DayC-1.1*rg/6;   L2I = LastValue (L2,1);
L3=DayC-1.1*rg/4;   L3I = LastValue (L3,1);
L4=DayC-1.1*rg/2;   L4I = LastValue (L4,1);
L5=DayC-1.1*rg;     L5I = LastValue (L5,1);

pcl = ParamToggle("Camerilla Levels","Show|Hide",0);
if(pcl==1)  {
   Plot(H5,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(H4,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(H3,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(H2,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(H1,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(L1,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(L2,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(L3,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(L4,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   Plot(L5,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
   PlotText(" H5 = " ,     LastValue(BarIndex())-(numbars/Hts), H5I  +0.05, colorRose);
   PlotText(" H4 = " ,     LastValue(BarIndex())-(numbars/Hts), H4I  +0.05, colorRose);
   PlotText(" H3 = " ,     LastValue(BarIndex())-(numbars/Hts), H3I  +0.05, colorRose);
   PlotText(" H2 = " ,     LastValue(BarIndex())-(numbars/Hts), H2I  +0.05, colorRose);
   PlotText(" H1 = " ,     LastValue(BarIndex())-(numbars/Hts), H1I  +0.05, colorRose);
   PlotText(" L1 = " ,     LastValue(BarIndex())-(numbars/Hts), L1I  +0.05, colorRose);
   PlotText(" L2 = " ,     LastValue(BarIndex())-(numbars/Hts), L2I  +0.05, colorRose);
   PlotText(" L3 = " ,     LastValue(BarIndex())-(numbars/Hts), L3I  +0.05, colorRose);
   PlotText(" L4 = " ,     LastValue(BarIndex())-(numbars/Hts), L4I  +0.05, colorRose);
   PlotText(" L5 = " ,     LastValue(BarIndex())-(numbars/Hts), L5I  +0.05, colorRose);
}

// Current Days Hi Lo //
THL = ParamToggle("Todays Hi Lo","Show|Hide",1);
if(THL==1)  {
  isRth = TimeNum() >= 084500 & TimeNum() <= 085959;
  isdRth = TimeNum() >= 084500 & TimeNum() <= 160000;
  aRthL = IIf(isRth, L, 1000000);
  aRthH = IIf(isdRth, H, Null);
  aRthLd = IIf(isdRth, L, 1000000);
  DayH = TimeFrameCompress( aRthH, inDaily, compressHigh );
  DayH = TimeFrameExpand( DayH, inDaily, expandFirst );
  DayL = TimeFrameCompress( aRthLd, inDaily, compressLow );
  DayL = TimeFrameExpand( DayL, inDaily, expandFirst );
  Bars = BarsSince(TimeNum() >= 94500 AND TimeNum() < 095959);//,BarIndex(),1); // AND DateNum()==LastValue(DateNum());
  x0 = BarCount-LastValue(Bars);
  x1 = BarCount-1;
  DayHline=LineArray(x0,LastValue(DayH),x1,LastValue (DayH),0);
  DayLline=LineArray(x0,LastValue(DayL),x1,LastValue (DayL),0);
  DayHlineI = LastValue (DayHline,1);
  DayLlineI = LastValue (DayLline,1);
  Plot(DayHline,"DayH",colorYellow,styleBar|styleNoRescale|styleNoTitle);
  Plot(DayLline,"DayL",colorYellow,styleBar|styleNoRescale|styleNoTitle);
  PlotText(" Day Hi " ,     LastValue(BarIndex())-(numbars/Hts), DayHlineI  +0.05, colorYellow);
  PlotText(" Day Lo " ,     LastValue(BarIndex())-(numbars/Hts), DayLlineI  +0.05, colorYellow);
 }

Cellclinic's(one who originally wanted this modified code) two requirements are
1)The plotted lines should not retrace to the previous day &
2)The line's parameters like thickness,color,type should be changeable
 
Hi


Amit, what trash is pointing at is that you are providing an array as value for Y in plottext . . .


Code:
DayH = TimeFrameGetPrice("H", inDaily, -1);
DayHI =IIf(DayH!=Ref(DayH,-1),Null,DayH);
PriceLineColor=ParamColor("PriceLineColor",ColorRGB(82,82,82));
numbars = LastValue(Cum(Status("barvisible")));
hts  = -33.5;
Plot(DayHI,"YH",PriceLineColor,ParamStyle("LineStyle",styleLine|styleDashed|styleNoTitle|styleNoLabel|styleThick,maskAll));
PlotText("YH",LastValue(BarIndex())+2,LastValue(DayHI),colorWhite);

and what i was pointing out in my earlier post was that the two things plotting of lines and text are not related . . .
anyway the above code is working


Happy :)
 

amitrandive

Well-Known Member
Hi


Amit, what trash is pointing at is that you are providing an array as value for Y in plottext . . .


Code:
DayH = TimeFrameGetPrice("H", inDaily, -1);
DayHI =IIf(DayH!=Ref(DayH,-1),Null,DayH);
PriceLineColor=ParamColor("PriceLineColor",ColorRGB(82,82,82));
numbars = LastValue(Cum(Status("barvisible")));
hts  = -33.5;
Plot(DayHI,"YH",PriceLineColor,ParamStyle("LineStyle",styleLine|styleDashed|styleNoTitle|styleNoLabel|styleThick,maskAll));
PlotText("YH",LastValue(BarIndex())+2,LastValue(DayHI),colorWhite);
Happy :)
Happy Sir

I am new to AFL programming,I am just trying to help cellclinic with the modification of his code.It looks complicated ,that is why it seems out of my reach.
:(.But I think with yours and trash's hints I would be able to modify this code as per requirement.:D
 
Last edited:
Happy Sir

I am new to AFL programming,I am just trying to help cellclinic with the modification of his code.It looks complicated ,that is why it seems out of my reach.
:(
not at all complicated . . .

For lines . . . . just suppressing the vertical shifts from previous day to next for all daily TF values . . .

And for the plot text statement to work right we need to give it X,Y co-ordinates which will plot the text where we want . ..

Happy :)
 

pratapvb

Well-Known Member
Happy Sir

I am new to AFL programming,I am just trying to help cellclinic with the modification of his code.It looks complicated ,that is why it seems out of my reach.
:(.But I think with yours and trash's hints I would be able to modify this code as per requirement.:D
Plot(iif(DayHI== ref(DayHI, 1), DayHI, null), "YH",PriceLineColor,ParamStyle("LineStyle",styleLine|styleDashed|styleNoTitle|styleNoLabel|styleThick,maskAll));
PlotText("YH",LastValue(BarIndex())+2,LastValue(DayHI),colorWhite);

P.S. display is showing some spaces in middle of words but when I go to edit it it is not showing....those spaces are not there...so take care if copy-pasting
 
Last edited:

ocil

Well-Known Member
Try this code. not sure this will help you or not....

HiDay = TimeFrameGetPrice("H", inDaily);
LoDay = TimeFrameGetPrice("L", inDaily);
HiHrly = TimeFrameGetPrice("H", inHourly);
LoHrly = TimeFrameGetPrice("L", inHourly);
PlotShapes(shapeSmallCircle*(H==HiDay),colorOrange,0 ,H,20);
PlotShapes(shapeSmallCircle*(L==Loday),colorGreen,0 ,L,-20);

Title =
EncodeColor(colorWhite)+ "Hi-Lo" + " | " + Name() + " | " + EncodeColor(colorWhite)+ Interval(2)
+ EncodeColor(colorWhite) + " | " + Date() +" | "
+"\n"+EncodeColor(2) +"Open ="+O +","+" High ="+H +" ,"+" Low ="+L +","+ " Close ="+C + " | Volume : "+ WriteVal(V,1.0)+
"\n"+ EncodeColor(colorWhite)+"\n"+


EncodeColor(colorWhite) + "Day's Range = " + EncodeColor(colorWhite) + StrToNum(NumToStr((HiDay - LoDay), 1.2))+ "\n"+
EncodeColor(colorWhite) + "Day's Hi = " + EncodeColor(colorWhite) + Hiday + "\n" +
EncodeColor(colorWhite) + "Day's Lo= " + EncodeColor(colorWhite) + Loday ;

DayH = TimeFrameGetPrice("H", inDaily, -1); DayHI = LastValue (DayH,1);// yesterdays high
DayL = TimeFrameGetPrice("L", inDaily, -1); DayLI = LastValue (DayL,1); // yesterdays low
DayC = TimeFrameGetPrice("C", inDaily, -1); // yesterdays close
DayO = TimeFrameGetPrice("O", inDaily); // current day open
numbars = LastValue(Cum(Status("barvisible")));
hts = -33.5;

YHL = 1;
if(YHL==1) {
Plot(DayL,"YL",colorTurquoise,styleNoLine|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayH,"YH",colorTurquoise,styleNoLine|styleNoLine|styleNoRescale|styleNoTitle);
PlotText(" YH " , LastValue(BarIndex())-(numbars/Hts), DayHI, colorWhite);
PlotText(" YL " , LastValue(BarIndex())-(numbars/Hts), DayLI, colorWhite);
}
 

cellclinic

Well-Known Member
Thanks A Lot Amit for taking so much care :)

Kindly check this formula ... got a little problem rest seems all ok or better will be much better :)

Regards ...


pic hosting

//Previous Days HI LO CL//

DayH = TimeFrameGetPrice("H", inDaily, -1); DayHI = LastValue (DayH,1);// yesterdays high
DayL = TimeFrameGetPrice("L", inDaily, -1); DayLI = LastValue (DayL,1); // yesterdays low
DayC = TimeFrameGetPrice("C", inDaily, -1); DayCI = LastValue (DayC,1); // yesterdays close
DayO = TimeFrameGetPrice("O", inDaily); // current day open
DayH2= TimeFrameGetPrice("H", inDaily, -2); DayH2I = LastValue (DayH2,1); // Two days before high
DayL2= TimeFrameGetPrice("L", inDaily, -2); DayL2I = LastValue (DayL2,1); // Two days before low
DayC2= TimeFrameGetPrice("C", inDaily, -2); DayC2I = LastValue (DayC2,1); // Two days before close
DayH3= TimeFrameGetPrice("H", inDaily, -3); DayH3I = LastValue (DayH3,1); // Three days before high
DayL3= TimeFrameGetPrice("L", inDaily, -3); DayL3I = LastValue (DayL3,1); // Three days before low
DayC3= TimeFrameGetPrice("C", inDaily, -3); DayC3I = LastValue (DayC3,1); // Three days before close
numbars = LastValue(Cum(Status("barvisible")));
hts = -33.5;

YHL = ParamToggle("Yesterday HI LO CL ","Show|Hide",1);
if(YHL==1) {
Plot(DayL,"YL",colorRed,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayH,"YH",colorBlue,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayC,"YC",colorDarkYellow,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
PlotText(" YH " , LastValue(BarIndex())-(numbars/Hts), DayHI, colorBlue);
PlotText(" YL " , LastValue(BarIndex())-(numbars/Hts), DayLI, colorRed);
PlotText(" YC " , LastValue(BarIndex())-(numbars/Hts), DayCI, colorDarkYellow);
}

TDBHL = ParamToggle("2/3Days before HI LO CL","Show|Hide",1);
if(TDBHL==1) {
Plot(DayL2,"2DBL",colorOrange,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayH2,"2DBH",colorGreen,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayC2,"2DBC",colorWhite,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayL3,"3DBL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayH3,"3DBH",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayC3,"3DBC",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
PlotText(" 2DBH " , LastValue(BarIndex())-(numbars/Hts), DayH2I, colorGreen);
PlotText(" 2DBL " , LastValue(BarIndex())-(numbars/Hts), DayL2I, colorOrange);
PlotText(" 2DBC " , LastValue(BarIndex())-(numbars/Hts), DayC2I, colorWhite);
PlotText(" 3DBH " , LastValue(BarIndex())-(numbars/Hts), DayH3I, colorTurquoise);
PlotText(" 3DBL " , LastValue(BarIndex())-(numbars/Hts), DayL3I, colorTurquoise);
PlotText(" 3DBC " , LastValue(BarIndex())-(numbars/Hts), DayC3I, colorTurquoise);
}

// Pivot Levels //
PP = (DayL + DayH + DayC)/3; PPI = LastValue (PP,1); // Pivot
R1 = (PP * 2) - DayL; R1I = LastValue (R1,1); // Resistance 1
S1 = (PP * 2) - DayH; S1I = LastValue (S1,1); // Support 1
R2 = PP + R1 - S1; R2I = LastValue (R2,1); // Resistance 2
S2 = PP - R1 + S1; S2I = LastValue (S2,1); // Support 2
R3 = PP + R2 - S1; R3I = LastValue (R3,1); // Resistance 3
S3 = PP - R2 + S1; S3I = LastValue (S3,1); // Support 3

ppl = ParamToggle("Pivot Levels","Show|Hide",1);
if(ppl==1) {
Plot(PP, "PP",colorYellow,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(R1, "R1",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(S1, "S1",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(R2, "R2",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(S2, "S2",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(R3, "R3",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(S3, "S3",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);

PlotText(" Pivot ", LastValue(BarIndex())-(numbars/Hts), PPI, colorYellow);
PlotText(" R1 " , LastValue(BarIndex())-(numbars/Hts), R1I, colorViolet);
PlotText(" S1 " , LastValue(BarIndex())-(numbars/Hts), S1I, colorViolet);
PlotText(" R2 " , LastValue(BarIndex())-(numbars/Hts), R2I, colorViolet);
PlotText(" S2 " , LastValue(BarIndex())-(numbars/Hts), S2I, colorViolet);
PlotText(" R3 " , LastValue(BarIndex())-(numbars/Hts), R3I, colorViolet);
PlotText(" S3 " , LastValue(BarIndex())-(numbars/Hts), S3I, colorViolet);
}
// Camerilla Levels //

rg = (DayH - DayL);

H5=DayC+1.1*rg; H5I = LastValue (H5,1);
H4=DayC+1.1*rg/2; H4I = LastValue (H4,1);
H3=DayC+1.1*rg/4; H3I = LastValue (H3,1);
H2=DayC+1.1*rg/6; H2I = LastValue (H2,1);
H1=DayC+1.1*rg/12; H1I = LastValue (H1,1);
L1=DayC-1.1*rg/12; L1I = LastValue (L1,1);
L2=DayC-1.1*rg/6; L2I = LastValue (L2,1);
L3=DayC-1.1*rg/4; L3I = LastValue (L3,1);
L4=DayC-1.1*rg/2; L4I = LastValue (L4,1);
L5=DayC-1.1*rg; L5I = LastValue (L5,1);

pcl = ParamToggle("Camerilla Levels","Show|Hide",0);
if(pcl==1) {
Plot(H5,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(H4,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(H3,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(H2,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(H1,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L1,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L2,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L3,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L4,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L5,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
PlotText(" H5 = " , LastValue(BarIndex())-(numbars/Hts), H5I +0.05, colorRose);
PlotText(" H4 = " , LastValue(BarIndex())-(numbars/Hts), H4I +0.05, colorRose);
PlotText(" H3 = " , LastValue(BarIndex())-(numbars/Hts), H3I +0.05, colorRose);
PlotText(" H2 = " , LastValue(BarIndex())-(numbars/Hts), H2I +0.05, colorRose);
PlotText(" H1 = " , LastValue(BarIndex())-(numbars/Hts), H1I +0.05, colorRose);
PlotText(" L1 = " , LastValue(BarIndex())-(numbars/Hts), L1I +0.05, colorRose);
PlotText(" L2 = " , LastValue(BarIndex())-(numbars/Hts), L2I +0.05, colorRose);
PlotText(" L3 = " , LastValue(BarIndex())-(numbars/Hts), L3I +0.05, colorRose);
PlotText(" L4 = " , LastValue(BarIndex())-(numbars/Hts), L4I +0.05, colorRose);
PlotText(" L5 = " , LastValue(BarIndex())-(numbars/Hts), L5I +0.05, colorRose);
}

// Current Days Hi Lo //
THL = ParamToggle("Todays Hi Lo","Show|Hide",1);
if(THL==1) {
isRth = TimeNum() >= 084500 & TimeNum() <= 085959;
isdRth = TimeNum() >= 084500 & TimeNum() <= 160000;
aRthL = IIf(isRth, L, 1000000);
aRthH = IIf(isdRth, H, Null);
aRthLd = IIf(isdRth, L, 1000000);
DayH = TimeFrameCompress( aRthH, inDaily, compressHigh );
DayH = TimeFrameExpand( DayH, inDaily, expandFirst );
DayL = TimeFrameCompress( aRthLd, inDaily, compressLow );
DayL = TimeFrameExpand( DayL, inDaily, expandFirst );
Bars = BarsSince(TimeNum() >= 94500 AND TimeNum() < 095959);//,BarIndex(),1); // AND DateNum()==LastValue(DateNum());
x0 = BarCount-LastValue(Bars);
x1 = BarCount-1;
DayHline=LineArray(x0,LastValue(DayH),x1,LastValue (DayH),0);
DayLline=LineArray(x0,LastValue(DayL),x1,LastValue (DayL),0);
DayHlineI = LastValue (DayHline,1);
DayLlineI = LastValue (DayLline,1);
Plot(DayHline,"DayH",colorYellow,styleBar|styleNoRescale|styleNoTitle);
Plot(DayLline,"DayL",colorYellow,styleBar|styleNoRescale|styleNoTitle);
PlotText(" Day Hi " , LastValue(BarIndex())-(numbars/Hts), DayHlineI +0.05, colorYellow);
PlotText(" Day Lo " , LastValue(BarIndex())-(numbars/Hts), DayLlineI +0.05, colorYellow);
}


Happy Sir

I am new to AFL programming,I am just trying to help cellclinic with the modification of his code.It looks complicated ,that is why it seems out of my reach.
:(.But I think with yours and trash's hints I would be able to modify this code as per requirement.:D
 

amitrandive

Well-Known Member
Thanks A Lot Amit for taking so much care :)

Kindly check this formula ... got a little problem rest seems all ok or better will be much better :)

Regards ...


pic hosting

//Previous Days HI LO CL//

DayH = TimeFrameGetPrice("H", inDaily, -1); DayHI = LastValue (DayH,1);// yesterdays high
DayL = TimeFrameGetPrice("L", inDaily, -1); DayLI = LastValue (DayL,1); // yesterdays low
DayC = TimeFrameGetPrice("C", inDaily, -1); DayCI = LastValue
.
.
.

PlotText(" Day Lo " , LastValue(BarIndex())-(numbars/Hts), DayLlineI +0.05, colorYellow);
}
Cellclinic

I will modify the formula and try to post it by this weekend.

Regarding the image you have posted,I do not have crude oil data to verify what you have posted ,but the code you have posted is working fine at my end for equities.

This code has time limits set for equity markets.We need to modify this code for commodity market.

Check if this works.I have modified the AFL for Commodity market timings.
Code:
_SECTION_BEGIN("Price1");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

//Previous Days HI LO CL//

DayH = TimeFrameGetPrice("H", inDaily, -1); DayHI = LastValue (DayH,1);// yesterdays high
DayL = TimeFrameGetPrice("L", inDaily, -1); DayLI = LastValue (DayL,1); // yesterdays low
DayC = TimeFrameGetPrice("C", inDaily, -1); DayCI = LastValue (DayC,1); // yesterdays close
DayO = TimeFrameGetPrice("O", inDaily); // current day open
DayH2= TimeFrameGetPrice("H", inDaily, -2); DayH2I = LastValue (DayH2,1); // Two days before high
DayL2= TimeFrameGetPrice("L", inDaily, -2); DayL2I = LastValue (DayL2,1); // Two days before low
DayC2= TimeFrameGetPrice("C", inDaily, -2); DayC2I = LastValue (DayC2,1); // Two days before close
DayH3= TimeFrameGetPrice("H", inDaily, -3); DayH3I = LastValue (DayH3,1); // Three days before high
DayL3= TimeFrameGetPrice("L", inDaily, -3); DayL3I = LastValue (DayL3,1); // Three days before low
DayC3= TimeFrameGetPrice("C", inDaily, -3); DayC3I = LastValue (DayC3,1); // Three days before close
numbars = LastValue(Cum(Status("barvisible")));
hts = -33.5;

YHL = ParamToggle("Yesterday HI LO CL ","Show|Hide",1);
if(YHL==1) {
Plot(DayL,"YL",colorRed,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayH,"YH",colorBlue,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayC,"YC",colorDarkYellow,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
PlotText(" YH " , LastValue(BarIndex())-(numbars/Hts), DayHI, colorBlue);
PlotText(" YL " , LastValue(BarIndex())-(numbars/Hts), DayLI, colorRed);
PlotText(" YC " , LastValue(BarIndex())-(numbars/Hts), DayCI, colorDarkYellow);
}

TDBHL = ParamToggle("2/3Days before HI LO CL","Show|Hide",1);
if(TDBHL==1) {
Plot(DayL2,"2DBL",colorOrange,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayH2,"2DBH",colorGreen,styleDots|styleNoLine |styleNoRescale|styleNoTitle);
Plot(DayC2,"2DBC",colorWhite,styleDots|styleNoLine |styleNoRescale|styleNoTitle);
Plot(DayL3,"3DBL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayH3,"3DBH",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayC3,"3DBC",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
PlotText(" 2DBH " , LastValue(BarIndex())-(numbars/Hts), DayH2I, colorGreen);
PlotText(" 2DBL " , LastValue(BarIndex())-(numbars/Hts), DayL2I, colorOrange);
PlotText(" 2DBC " , LastValue(BarIndex())-(numbars/Hts), DayC2I, colorWhite);
PlotText(" 3DBH " , LastValue(BarIndex())-(numbars/Hts), DayH3I, colorTurquoise);
PlotText(" 3DBL " , LastValue(BarIndex())-(numbars/Hts), DayL3I, colorTurquoise);
PlotText(" 3DBC " , LastValue(BarIndex())-(numbars/Hts), DayC3I, colorTurquoise);
}

// Pivot Levels //
PP = (DayL + DayH + DayC)/3; PPI = LastValue (PP,1); // Pivot
R1 = (PP * 2) - DayL; R1I = LastValue (R1,1); // Resistance 1
S1 = (PP * 2) - DayH; S1I = LastValue (S1,1); // Support 1
R2 = PP + R1 - S1; R2I = LastValue (R2,1); // Resistance 2
S2 = PP - R1 + S1; S2I = LastValue (S2,1); // Support 2
R3 = PP + R2 - S1; R3I = LastValue (R3,1); // Resistance 3
S3 = PP - R2 + S1; S3I = LastValue (S3,1); // Support 3

ppl = ParamToggle("Pivot Levels","Show|Hide",1);
if(ppl==1) {
Plot(PP, "PP",colorYellow,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(R1, "R1",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(S1, "S1",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(R2, "R2",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(S2, "S2",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(R3, "R3",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(S3, "S3",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);

PlotText(" Pivot ", LastValue(BarIndex())-(numbars/Hts), PPI, colorYellow);
PlotText(" R1 " , LastValue(BarIndex())-(numbars/Hts), R1I, colorViolet);
PlotText(" S1 " , LastValue(BarIndex())-(numbars/Hts), S1I, colorViolet);
PlotText(" R2 " , LastValue(BarIndex())-(numbars/Hts), R2I, colorViolet);
PlotText(" S2 " , LastValue(BarIndex())-(numbars/Hts), S2I, colorViolet);
PlotText(" R3 " , LastValue(BarIndex())-(numbars/Hts), R3I, colorViolet);
PlotText(" S3 " , LastValue(BarIndex())-(numbars/Hts), S3I, colorViolet);
}
// Camerilla Levels //

rg = (DayH - DayL);

H5=DayC+1.1*rg; H5I = LastValue (H5,1);
H4=DayC+1.1*rg/2; H4I = LastValue (H4,1);
H3=DayC+1.1*rg/4; H3I = LastValue (H3,1);
H2=DayC+1.1*rg/6; H2I = LastValue (H2,1);
H1=DayC+1.1*rg/12; H1I = LastValue (H1,1);
L1=DayC-1.1*rg/12; L1I = LastValue (L1,1);
L2=DayC-1.1*rg/6; L2I = LastValue (L2,1);
L3=DayC-1.1*rg/4; L3I = LastValue (L3,1);
L4=DayC-1.1*rg/2; L4I = LastValue (L4,1);
L5=DayC-1.1*rg; L5I = LastValue (L5,1);

pcl = ParamToggle("Camerilla Levels","Show|Hide",0);
if(pcl==1) {
Plot(H5,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(H4,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(H3,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(H2,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(H1,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L1,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L2,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L3,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L4,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L5,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
PlotText(" H5 = " , LastValue(BarIndex())-(numbars/Hts), H5I +0.05, colorRose);
PlotText(" H4 = " , LastValue(BarIndex())-(numbars/Hts), H4I +0.05, colorRose);
PlotText(" H3 = " , LastValue(BarIndex())-(numbars/Hts), H3I +0.05, colorRose);
PlotText(" H2 = " , LastValue(BarIndex())-(numbars/Hts), H2I +0.05, colorRose);
PlotText(" H1 = " , LastValue(BarIndex())-(numbars/Hts), H1I +0.05, colorRose);
PlotText(" L1 = " , LastValue(BarIndex())-(numbars/Hts), L1I +0.05, colorRose);
PlotText(" L2 = " , LastValue(BarIndex())-(numbars/Hts), L2I +0.05, colorRose);
PlotText(" L3 = " , LastValue(BarIndex())-(numbars/Hts), L3I +0.05, colorRose);
PlotText(" L4 = " , LastValue(BarIndex())-(numbars/Hts), L4I +0.05, colorRose);
PlotText(" L5 = " , LastValue(BarIndex())-(numbars/Hts), L5I +0.05, colorRose);
}

// Current Days Hi Lo //
THL = ParamToggle("Todays Hi Lo","Show|Hide",1);
if(THL==1) {
isRth = TimeNum() >= 084500 & TimeNum() <= 085959;
isdRth = TimeNum() >= 084500 & TimeNum() <= 235500;
aRthL = IIf(isRth, L, 1000000);
aRthH = IIf(isdRth, H, Null);
aRthLd = IIf(isdRth, L, 1000000);
DayH = TimeFrameCompress( aRthH, inDaily, compressHigh );
DayH = TimeFrameExpand( DayH, inDaily, expandFirst );
DayL = TimeFrameCompress( aRthLd, inDaily, compressLow );
DayL = TimeFrameExpand( DayL, inDaily, expandFirst );
Bars = BarsSince(TimeNum() >= 94500 AND TimeNum() < 095959);//,BarIndex(),1); // AND DateNum()==LastValue(DateNum());
x0 = BarCount-LastValue(Bars);
x1 = BarCount-1;
DayHline=LineArray(x0,LastValue(DayH),x1,LastValue (DayH),0);
DayLline=LineArray(x0,LastValue(DayL),x1,LastValue (DayL),0);
DayHlineI = LastValue (DayHline,1);
DayLlineI = LastValue (DayLline,1);
Plot(DayHline,"DayH",colorYellow,styleBar|styleNoRescale|styleNoTitle);
Plot(DayLline,"DayL",colorYellow,styleBar|styleNoRescale|styleNoTitle);
PlotText(" Day Hi " , LastValue(BarIndex())-(numbars/Hts), DayHlineI +0.05, colorYellow);
PlotText(" Day Lo " , LastValue(BarIndex())-(numbars/Hts), DayLlineI +0.05, colorYellow);
}
 
Last edited:
Hello, guys! I'm here again to bother you... eheheh


I've been working on the same system (buy when the prices are above the MA and it touchs the MA and vice-versa). I've added a filter:

Only buy when the Linear Regression slope is above 0;
Only sell when the Linear Regression slope is below 0.

In the optimization I made, the best results gave me the Linear Regression Slope Period of 2. In the backtest, as it consider only the close price when checking the Slope, there are many trades that would not happen in Real Time, since at the moment of the touch, the slope would be different.

So, can I determine how the slope will be the moment price touch the MA?

A picture to show better my doubt:


Then, I went to study session... that lasted two full days.

First, I found how to calculate the slope using only the data on the chart. I found it here:

http://www.statisticshowto.com/how-to-find-a-linear-regression-slope/

As you can see, we can find the slope putting the data in a table. So, in translation, it become like this:

X axis = we numerate the bars, in crescent order, from 1 to last (here we use 3 bars);
Y axis = prices. we use the close prices in bars 1 and 2, but uses the MA price in bar 3;
The rest of the table doesnt really need explaining, does it?



So, I began making a "for loop" that would numerate (put an index) the bars acordingly with the regression period I wanted to use. I did it, and it became like this:



As you can see, the loop is working. If I put period 10, it will numarate bar [barcount -10] as 1 and bar [barcount -1] as 10. Then I just mounted the equation, as the code below:

//ProjectedMA Construction ===========================================================


SetPositionSize( 1, spsShares );


ArrayMA = ParamField("Price field", 3 );
Periods = Optimize("MAPeriods", 4, 4, 58, 2 );
ProjectedMA = Ref( WMA( arrayma, periods -1) , -1);
Color = ParamColor("Color", colorCycle );
Style = ParamStyle("Style", styleLine | styleNoLabel);

CenterLine = WMA( ArrayMA, Periods );



//Slope Antecipado=====================================================================
//Antecipated Slope


//Here i start the loop.

per=3; //period of the regression.
n=BarCount-per;
indiceX = Null;

function indeX(per)
{

for( i=n , j=1; i<BarCount && j<=per; i++, j++)
indiceX = i - (i-j);

return indiceX;

}

Plot(index(per),"",colorRed);

SumX = Sum(indeX(per), per); //the sum of all the indexes

indiceX2 = indeX(per)^2; //Here I define the X^2 that im gonna need for the equation.
SumX2 = Sum( indiceX2 , per); //the sum off the indexes^2


XY = IndeX(per)*Close; //index * closes

//slope da banda
//Slope when the prices touch the MA

SumXY = (Sum( Ref(XY , -1) , per-1)) + (ProjectedMA*per); //the sum of all X*Y. The lastvalue used is from the projectedMA
SumY = (Sum(Ref(Close, -1) , per-1)) + (ProjectedMA); // again, last value is from the MA


LinNumerator = (per*SumXY) - (SumX*SumY); //here is the up part of the final equation
LinDenominator = (SumX2) - (sumX^2); //here is the down part of the final equation
Slope = LinNumerator / LinDenominator; // here is the result, the slope.




//Plot(slope, "",colorRed, styleLine, per);


//Fim/end of slope calculus======================================================================

//Now for the signals==========================================================================



//BuyCondTL = TL1 > TL2;

BuyCondSlope = Ref(LinRegSlope(C, per) , -1) >0 AND slope> 0; //slope is above 0

BuyCondBandTouch = (Ref(Low , -1) > Ref( ProjectedMA , -1)) AND (Low <= ProjectedMA); //prices touch the MA from above

BuyCondTimeLimit = TimeNum() >= 093000 AND TimeNum() <= 170000;



//ShortCondTL = TL2 > TL1;

ShortCondSlope = Ref(LinRegSlope(C , per) , -1) <0 AND slope < 0; //slope is below 0

ShortCondBandTouch = (Ref(High , -1) < Ref( ProjectedMA , -1)) AND (High >= ProjectedMA); //prices touch the MA from below

ShortCondTimeLimit = TimeNum() >= 093000 AND TimeNum() <= 170000;




TickSize = 5;

BuyPrice = ProjectedMA;
ShortPrice = ProjectedMA;
Buy = BuyCondSlope AND BuyCondBandTouch AND BuyCondTimeLimit;
Short = ShortCondSlope AND ShortCondBandTouch AND ShortCondTimeLimit;


StopLoss_Mult = Optimize("StopLossMult" , 25 , 10 , 60 , 5);

StopLoss = StopLoss_Mult*TickSize;


Sell = TimeNum() > 171500;
Cover = TimeNum() > 171500;


ApplyStop(stopTypeLoss , stopModePoint , StopLoss , 1 , False , 0 );
ApplyStop(stopTypeProfit , stopModePoint , StopLoss , 1 , False , 0 );


//END OF ALL CODE======================================================================================


Well, the slope of each bar is calculated correctly. But I cant plot it. When I try, it appears like this:



And if I try to run a backtest, it shows no result.
If I try optimization, it shows all the trades as 0,00.



So, what did I do wrong? Can anyone help me? Mr Trash? :)

Thanks for the atention, guys!

Best regards!
 
Last edited:

cellclinic

Well-Known Member
Thanks a lot amit for caring so much ...

afl is now ok ....


post images

But agin 1 request only if it is possible ...


image url

When i squeeze chart is it possible to increase number of bars to view high more backwards ?

& also if it is possible to get Title on the top of the line or in the left as when chart is squeezed text like DH DL etc not gets visible ...
Regards ...
Sumit Lama
 
Last edited:

Similar threads