I have made following changes in the Trend line Break AFL given at http://www.wisestocktrader.com/indic...-line-breakout
1. Definition of Demandpoint and Supplypoint
2. Commented out Stoploss and Targets section as it was not working for me.
3. Added Pivot point, volume, Yesterday Hi Lo and 3EMA.
The original code was not working in Version 5.5 giving following error-
1. warning :502: you are calling plot()plotOHLC() function over 500 times,it's highly inefficient reduce number of calls
There are 4 solutions given by Kelvinhand for the first error:
1. To solve this problem, you need to add a max counter =500 in the program to prevent it from overflow.
2. Do only plot those that are in the visible screen. This is Window Slicing.
3. Copy the section linearray data which is y=ax+b into a big array of size Barcount. then just plot once will do.
4: Use Gfx Move() and LineTo() to draw the trendlines
The code was changed by Kelvinhand with solution 2. After that it is working in Version 5.5. It is drawing excellent Trend lines. It is the best AFL I have seen so far for plotting Trend Lines.
Now I require explanatory comments in the "Plot Trend Lines" Section only so that I can understand and make improvements. I have already added comments where I understood. (It has been years since I learnt Trigonometry.)
I would also like to have code using 3rd or 4th solution
Here is the new code-
Here is sample chart
![]()
1. Definition of Demandpoint and Supplypoint
2. Commented out Stoploss and Targets section as it was not working for me.
3. Added Pivot point, volume, Yesterday Hi Lo and 3EMA.
The original code was not working in Version 5.5 giving following error-
1. warning :502: you are calling plot()plotOHLC() function over 500 times,it's highly inefficient reduce number of calls
There are 4 solutions given by Kelvinhand for the first error:
1. To solve this problem, you need to add a max counter =500 in the program to prevent it from overflow.
2. Do only plot those that are in the visible screen. This is Window Slicing.
3. Copy the section linearray data which is y=ax+b into a big array of size Barcount. then just plot once will do.
4: Use Gfx Move() and LineTo() to draw the trendlines
The code was changed by Kelvinhand with solution 2. After that it is working in Version 5.5. It is drawing excellent Trend lines. It is the best AFL I have seen so far for plotting Trend Lines.
Now I require explanatory comments in the "Plot Trend Lines" Section only so that I can understand and make improvements. I have already added comments where I understood. (It has been years since I learnt Trigonometry.)
I would also like to have code using 3rd or 4th solution
Here is the new code-
Code:
/*
First understand the Trendlines drawn are called Tom Demark Supply/Demand Lines
Second understand the Horizontal lines are called the Entry/Stoploss/Target Lines
How it work:
========
Supply Line is Orange, Demand Line is Blue.
Trigger: Buy candle is Yellow, Sell Candle is Magenta
Buy Condition:
When Price Cross up Supply Line, Buy candle trigger, Entry/SL/TGT lines ploted upward.
Sell Condition:
When Price Cross down Demand Line, Sell candle trigger, Entry/SL/TGT lines ploted downward.
How to find out?
There is a [Bar Replay] button, use it.
Play it with your historical data, watch it develop
That simple
There were 2 errors here in Version below 5.6
1. warning :502: you are calling plot()plotOHLC() function over 500 times,it's highly inefficient reduce number of calls
2. error 16 ; too many arguments. (This was for Stoploss and Targets Section, corrected by removing last parameter in Plot() statement.
There are 4 solutions given by Kelvinhand for the first error:
Solution 1. To solve this problem, you need to add a max counter =500 in the program to prevent it from overflow.
Solution 2. Do only plot those that are in the visible screen. This is Window Slicing.
Solution 3. Copy the section linearray data which is y=ax+b into a big array of size Barcount. then just plot once will do.
Solution 4: Use Gfx Move() and LineTo() to draw the trendlines
Code below is for Solution 2.
*/
SetChartOptions(0, chartShowArrows | chartShowDates);
SetChartBkColor(ParamColor("Outer Panel", colorBlack));
SetChartBkGradientFill(ParamColor("Upper Chart", colorDarkGrey), ParamColor("Lower Chart", colorDarkGrey));
GraphXSpace = Param("GraphXSpace", 10, 0, 100, 1);
colorHighliter = IIf(C >= O, ColorRGB(0, 128, 0), ColorRGB(128, 0, 0));
_SECTION_BEGIN("Demand/Supply Point");
/*
Demand Point is that Bar whose Low is lower than the Low of two bars immediately before AND ahead it AND
its Low may be equal to the Low of previous bar (to cover case of double bottom).
*/
DemandPoint = (L < Ref(L, -2)) & L <= Ref(L, -1) & L < Ref(L, 1) & L < Ref(L, 2);
//Colour of Demand Line is user defined. Default is ColorRGB(0, 128, 255) i.e.blueish
colorDemandPoint = ParamColor("Demand Line", ColorRGB(0, 128, 255));
// Supply Point is that Bar whose High is Higher than the High of two bars immediately before and ahead it and
// its High may be equal to the High of previous bar (to cover case of double top).
SupplyPoint = (H > Ref(H, -2) & H >= Ref(H, -1) & H > Ref(H, 1) & H > Ref(H, 2));
//Colour of Supply Line is user defined. Default is ColorRGB(255, 128, 0) i.e. Orange.
colorSupplyPoint = ParamColor("Supply Line", ColorRGB(255, 128, 0));
_SECTION_END();
_SECTION_BEGIN("Plot Trend Lines");
/*
Trend Lines are drawn by joining two Demand Points (dx0 and dx1) or two Supply points (sx0 and sx1).
Where x0, x1 are on Time axis and y0, y1 are on price axis.
Demand line is closed when it is a candle closes below it. Supply line is broken when a candle closes above it.
A Trend Lines is extended until it is broken by any candle.
In this section we identify these points AND then Plot TLs. These points are initialsed as 0 in the beginning.
*/
CountTrendBars = 0;
dx0 = dx1 = dy0 = dy1 = 0;
sx0 = sx1 = sy0 = sy1 = 0;
//-- Modified by KelvinHand ----------- to plot only on visible chart
function VisiBarsPerChart()
{
lvb=Status("lastvisiblebar");
fvb=Status("firstvisiblebar");
return Min(lvb-fvb,BarCount-fvb);
}
nBars=1.5*VisiBarsPerChart();
if (nBars <BarCount)
iLimit = (BarCount-1) - nBars;
else
iLimit = 0;
//------------------------------------
//_TRACE( "Limit = "+NumToStr(iLimit) );
//_TRACE( "Barcount = "+NumToStr(BarCount) );
for (i = BarCount-1; i>iLimit; i--) {
if (DemandPoint[i]) {
if (dx1 == 0 & dy1 == 0) {
dx1 = i;
dy1 = L[i];
} else {
dx0 = i;
dy0 = L[i];
}
if (dx0 != 0 & dx1 != 0 & dy0 != 0 & dy1 != 0) {
if (dy0 < dy1) {
// We calculate the slope of TL in the next line
a = (-dy0 + dy1) / (-dx0 + dx1);
b = dy0 - dx0 * a;
for (j = dx1; j < BarCount; j++) {
if (j != dx1) {
y2 = a * j + b;
if (C[j] < y2) {
dy1 = y2;
dx1 = j;
// Highlight candle breaking below demand line with Purple colour
colorHighliter[j] = ColorRGB(128, 0, 128);
CountTrendBars[j] = dx1 - dx0 - 1;
break;
}
}
}
if (dy1 != y2) {
dy1 = y2;
dx1 = BarCount - 1;
}
Plot(LineArray(dx0, dy0, dx1, dy1, 0), "", colorDemandPoint, styleLine, Null, Null, 0, 2);
}
dx1 = dx0;
dy1 = dy0;
dx0 = dy0 = 0;
}
}
if (SupplyPoint[i]) {
if (sx1 == 0 & sy1 == 0) {
sx1 = i;
sy1 = H[i];
} else {
sx0 = i;
sy0 = H[i];
}
if (sx0 != 0 & sx1 != 0 & sy0 != 0 & sy1 != 0) {
if (sy0 > sy1) {
a = (-sy0 + sy1) / (-sx0 + sx1);
b = sy0 - sx0 * a;
for (j = sx1; j < BarCount; j++) {
if (j != sx1) {
y2 = a * j + b;
if (C[j] > y2) {
sy1 = y2;
sx1 = j;
// Highlight candle breaking above Supply line with Yellow colour
colorHighliter[j] = ColorRGB(128, 128, 0);
CountTrendBars[j] = sx1 - sx0 - 1;
break;
}
}
}
if (sy1 != y2) {
sy1 = y2;
sx1 = BarCount - 1;
}
Plot(LineArray(sx0, sy0, sx1, sy1, 0), "", colorSupplyPoint, styleLine, Null, Null, 0, 2);
}
sx1 = sx0;
sy1 = sy0;
sx0 = sy0 = 0;
}
}
}
_SECTION_END();
/*
//Stop Loss & Targets
for (i = BarCount-1; i>iLimit; i--) {
if (colorHighliter[i] == ColorRGB(128, 0, 128)) {
StopLoss = 0;
for (j = i - CountTrendBars[i]; j <= i; j++) {
StopLoss = Max(H[j], StopLoss);
}
if (i - (BarCount - 1) != 0) {
t1 = C[i] - (StopLoss - C[i]);
t2 = C[i] - ((StopLoss - C[i]) * 1.272);
t3 = C[i] - ((StopLoss - C[i]) * 1.618);
Plot(LineArray(i, StopLoss + 0.01, BarCount - 1, StopLoss + 0.01, 0), "", ColorRGB(255, 0, 0), styleDots | styleNoLabel, Null, Null, 0, 1);
Plot(LineArray(i, C[i], BarCount - 1, C[i], 0), "", ColorRGB(255, 255, 0), styleDots, Null, Null, 0, 1);
Plot(LineArray(i, t1, BarCount - 1, t1, 0), "", ColorRGB(0, 255, 0), styleDots | styleNoLabel, Null, Null, 0, 1);
Plot(LineArray(i, t2, BarCount - 1, t2, 0), "", ColorRGB(0, 255, 0), styleDots | styleNoLabel, Null, Null, 0, 1);
Plot(LineArray(i, t3, BarCount - 1, t3, 0), "", ColorRGB(0, 255, 0), styleDots | styleNoLabel, Null, Null, 0, 1);
PlotText("Stop Loss\n@" + WriteVal(StopLoss + 0.01, 1.2), BarCount, StopLoss + 0.01, ColorRGB(255, 255, 255));
PlotText("T1 @" + WriteVal(t1, 1.2), BarCount, t1, ColorRGB(255, 255, 255));
PlotText("T2 @" + WriteVal(t2, 1.2), BarCount, t2, ColorRGB(255, 255, 255));
PlotText("T3 @" + WriteVal(t3, 1.2), BarCount, t3, ColorRGB(255, 255, 255));
}
break;
}
if (colorHighliter[i] == ColorRGB(128, 128, 0)) {
StopLoss = 9999;
for (j = i - CountTrendBars[i]; j <= i; j++) {
StopLoss = Min(L[j], StopLoss);
}
if (i - (BarCount - 1) != 0) {
t1 = C[i] + (C[i] - StopLoss);
t2 = C[i] + ((C[i] - StopLoss) * 1.272);
t3 = C[i] + ((C[i] - StopLoss) * 1.618);
Plot(LineArray(i, StopLoss - 0.01, BarCount - 1, StopLoss - 0.01, 0), "", ColorRGB(255, 0, 0), styleDots | styleNoLabel, Null, Null, 0,0);
Plot(LineArray(i, C[i], BarCount - 1, C[i], 0), "", ColorRGB(255, 255, 0), styleDots, Null, Null, 0, 0);
Plot(LineArray(i, t1, BarCount - 1, t1, 0), "", ColorRGB(0, 255, 0), styleDots | styleNoLabel, Null, Null, 0, 0);
Plot(LineArray(i, t2, BarCount - 1, t2, 0), "", ColorRGB(0, 255, 0), styleDots | styleNoLabel, Null, Null, 0, 0);
Plot(LineArray(i, t3, BarCount - 1, t3, 0), "", ColorRGB(0, 255, 0), styleDots | styleNoLabel, Null, Null, 0, 0);
PlotText("Stop Loss\n@" + WriteVal(StopLoss - 0.01, 1.2), BarCount, StopLoss - 0.01, ColorRGB(255, 255, 255));
PlotText("T1 @" + WriteVal(t1, 1.2), BarCount, t1, ColorRGB(255, 255, 255));
PlotText("T2 @" + WriteVal(t2, 1.2), BarCount, t2, ColorRGB(255, 255, 255));
PlotText("T3 @" + WriteVal(t3, 1.2), BarCount, t3, ColorRGB(255, 255, 255));
}
break;
}
}
*/
_SECTION_BEGIN("Volume");
Plot( Volume, _DEFAULT_NAME(), ParamColor("Color", colorBlue ), ParamStyle( "Style", styleHistogram | styleOwnScale , maskHistogram ), 2 );
_SECTION_END();
_SECTION_BEGIN("HI LO");
//Previous Days HI LO //
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;
Today = LastValue(Day());
YHL = ParamToggle("Yesterday HI LO","Show|Hide",0);
if(YHL==1) {
Plot(IIf(Today == Day(),DayL,Null),"YL",colorAqua,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(IIf(Today==Day(),DayH,Null),"YH",colorLightYellow,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
// PlotText("-" , LastValue(BarIndex()-1), DayHI, colorTurquoise);
PlotText(" YH " , LastValue(BarIndex())-(numbars/Hts), DayHI, colorTurquoise);
PlotText(" YL " , LastValue(BarIndex())-(numbars/Hts), DayLI, colorTurquoise);
}
_SECTION_END();
_SECTION_BEGIN("PIVOTS");
// 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
Today = LastValue(Day());
ppl = ParamToggle("Pivot Levels","Show|Hide",1);
if(ppl==1) {
Plot(IIf(Today == Day(),PP,Null), "PP",colorGold,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(IIf(Today == Day(),R1,Null), "R1",colorLightGrey,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(IIf(Today == Day(),S1,Null), "S1",colorPaleGreen,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(IIf(Today == Day(),R2,Null), "R2",colorLightGrey,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(IIf(Today == Day(),S2,Null), "S2",colorPaleGreen,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(IIf(Today == Day(),R3,Null), "R3",colorLightGrey,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(IIf(Today == Day(),S3,Null), "S3",colorPaleGreen,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
PlotText(" Pivot ", LastValue(BarIndex())-(numbars/Hts), PPI, colorGold);
PlotText(" R1 " , LastValue(BarIndex())-(numbars/Hts), R1I, colorLightGrey);
PlotText(" S1 " , LastValue(BarIndex())-(numbars/Hts), S1I, colorPaleGreen);
PlotText(" R2 " , LastValue(BarIndex())-(numbars/Hts), R2I, colorLightGrey);
PlotText(" S2 " , LastValue(BarIndex())-(numbars/Hts), S2I, colorPaleGreen);
PlotText(" R3 " , LastValue(BarIndex())-(numbars/Hts), R3I, colorLightGrey);
PlotText(" S3 " , LastValue(BarIndex())-(numbars/Hts), S3I, colorPaleGreen);
}
_SECTION_END();
_SECTION_BEGIN("EMA");
P = ParamField("Price field",3);
Periods = Param("Periods", 3, 2, 300, 1, 10 );
Plot( EMA( P, Periods ), _DEFAULT_NAME(), ParamColor( "Color", colorAqua ), ParamStyle("Style") );
_SECTION_END();
//Price
SetBarFillColor(colorHighliter);
Plot(C, "Close", IIf(colorHighliter == ColorRGB(128, 0, 128), ColorRGB(255, 0, 255), IIf(colorHighliter == ColorRGB(128, 128, 0), ColorRGB(255, 255, 0), IIf(C > O, ColorRGB(0, 255, 0), IIf(C < O, ColorRGB(255, 0, 0), ColorRGB(255, 255, 255))))), styleCandle, Null, Null, 0, 1);
//Volume
/*colorVolume = ParamColor("Volume Area", ColorRGB(0, 0, 0));
Plot(Volume, "", colorVolume, styleArea | styleOwnScale | styleNoLabel, Null, Null, 0, 0, 1);*/
Title = "Trend-Break-3 " + Name() + " - {{Interval}} - {{Date}} - Open = " + NumToStr(O, 1.2) + ", High = " + NumToStr(H, 1.2) + ", Low = " + NumToStr(L, 1.2) + ", Close = " + NumToStr(C, 1.2) + " (" + WriteVal(ROC(C, 1), 1.2) + "%)"; /*, Volume = " + WriteVal(Volume, 1.2);*/
//Buy Sell Logic
B1=colorHighliter == ColorRGB(128, 128, 0);
s1=colorHighliter == ColorRGB(128, 0, 128);
bSetup = B1;
sSetup = s1;
BuySetupValue = ValueWhen(bSetup,H,1);
SellsetupValue = ValueWhen(sSetup,L,1);
Buysetup = bSetup;
Sellsetup = sSetup;
Longa = Flip(Buysetup,Sellsetup);
shrta = Flip(Sellsetup,Buysetup);
Buy = Cover =(Longa AND Cross(C,BuySetupValue)) ;
Sell = Short = (shrta AND Cross(SellsetupValue,C)) ;
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorLime, 0,L, -35);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorRed, 0, H, -30);
BuyPrice=ValueWhen(Buy,C,1);
SellPrice=ValueWhen(Sell,C,1);
Filter=Buy OR Sell ;
AddColumn(IIf(Buy,BuyPrice,Null),"Buy", 1.2,1,colorGreen,50);
AddColumn(IIf(Sell,SellPrice,Null),"Sell " ,1.2,1,colorOrange,50);
RequestTimedRefresh(0.1,True);
