# Trendline Breakout AFL

#### KelvinHand

##### Well-Known Member
Kelvin,

It is working now in Version 5.5. Problem 2 solved by removing last zeros in the Plot() statements wherever it occurred.
For example-
Plot(LineArray(i, StopLoss - 0.01, BarCount - 1, StopLoss - 0.01, 0), "", ColorRGB(255, 0, 0), styleDots | styleNoLabel, Null, Null, 0, 0, 1);
was changed to
Plot(LineArray(i, StopLoss - 0.01, BarCount - 1, StopLoss - 0.01, 0), "", ColorRGB(255, 0, 0), styleDots | styleNoLabel, Null, Null, 0,1);

Code:
``````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));

//Demand Point
colorDemandPoint = ParamColor("Demand Line", ColorRGB(0, 128, 255));
DemandPoint = (Ref(L, -1) < Ref(L, -2)) & (L < Ref(L, -1)) & (L < Ref(L, 1)) & (Ref(L, 1) < Ref(L, 2));

//Supply Point
colorSupplyPoint = ParamColor("Supply Line", ColorRGB(255, 128, 0));
SupplyPoint = (Ref(H, -1) > Ref(H, -2)) & (H > Ref(H, -1)) & (H > Ref(H, 1)) & (Ref(H, 1) > Ref(H, 2));

CountTrendBars = 0;
//CurrentBar = BarCount - 1;
dx0 = dx1 = dy0 = dy1 = 0;
sx0 = sx1 = sy0 = sy1 = 0;

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) {
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;
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;
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;
}
}
}

//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\[email protected]" + 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,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\[email protected]" + 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;
}
}

//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 = 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);*/``````

You do it without understanding what are you doing.

What the meaning of
Plot(LineArray(i, StopLoss - 0.01, BarCount - 1, StopLoss - 0.01, 0), "", ColorRGB(255, 0, 0), styleDots | styleNoLabel, Null, Null, 0,1);

You don't even read the help guide.
Look properly the Plot() statement, what the last argument stand for in the orgin code?

and what it stand for after you modified as the above statement ?

Last edited:

#### KelvinHand

##### Well-Known Member
The code is working but SL, Buy, Sell, and target logic to be examined. I am getting these properly in 5 and 15 min time frame. T3 came at S1 pivot point. However, they did not change in hourly chart.
I don't do realtime. So you had to work out yourself.

T1, T2, T3 is calculated based on the S/L range to Entry price and extend 1.0, 1.272 and 1.618.

Search for the code segment as follows:

Code:
``````			t1 = C[i] + (C[i] - StopLoss);
t2 = C[i] + ((C[i] - StopLoss) * 1.272);
t3 = C[i] + ((C[i] - StopLoss) * 1.618);

OR

t1 = C[i] + (C[i] - StopLoss);
t2 = C[i] + ((C[i] - StopLoss) * 1.272);
t3 = C[i] + ((C[i] - StopLoss) * 1.618);``````
Try to debug yourself. Print the info to log window OR
In the program, use _TRACE("t3 = " + numtostr(t3);

#### josh1

##### Well-Known Member
You do it without understanding what are you doing.

What the meaning of
Plot(LineArray(i, StopLoss - 0.01, BarCount - 1, StopLoss - 0.01, 0), "", ColorRGB(255, 0, 0), styleDots | styleNoLabel, Null, Null, 0,1);

You don't even read the help guide.
Look properly the Plot() statement, what the last argument stand for in the orgin code?

and what it stand for after you modified as the above statement ?
Almighty Kelvin,

Why are you asking me if you have read and understood the guide? I have been successful in getting the charts and I have already stated in my last post that logic for SL, Targets and entry is to be examined.

By the way, parameters for Plot() statement have increased in Version 5.6

#### KelvinHand

##### Well-Known Member
Almighty Kelvin,

Why are you asking me if you have read and understood the guide? I have been successful in getting the charts and I have already stated in my last post that logic for SL, Targets and entry is to be examined
Well. If you think it work OK for you. Then it OK.
If you read then you can spot the minor difference.

Case Closed.

AmiBroker 1
AmiBroker 2
MetaStock 2
Forex 0
Forex 0