Thanks for that reference. I've studied the `A_MoveTrendLine_s` function and based a new code version on it:
Code: Select all
if (Close[1] < TL_GetValue_s(trendLineArray[x], Date[1], Time_s[1])) then begin
TL_SetEnd_s(trendLineArray[x], Date, Time_s, Close);
TL_SetBegin_s(trendLineArray[x], TL_GetBeginDate(trendLineArray[x]),
TL_GetBeginTime_s(trendLineArray[x]), TL_GetBeginVal(trendLineArray[x]));
end;
This didn't help, probably the begin point of the trend line is fixed. But it did give me a good idea: extending the trendline one bar further than `TL_GetValue_s` says the line has crossed (so that the crossing is made visible). That did seemingly work.
So with this code:
Code: Select all
if (Close[1] < TL_GetValue_s(trendLineArray[x], Date[1], Time_s[1])) then begin
TL_SetEnd_s(trendLineArray[x], Date, Time_s, Close);
barNumExtension = CurrentBar;
end else begin
if ((barNumExtension + 1) = CurrentBar) or ((barNumExtension + 2) = CurrentBar) then
TL_SetEnd_s(trendLineArray[x], Date, Time_s, Low)
else
trendLineArray[x] = -1; // Discard trendline; already closed below
end;
Gives the following chart:
This seems correct to me: the bar on which the trend line ends, is the bar that closes above the trend line.
PS: I do wonder how it works in real-time, because Pivots are already suffering from hindsight, and these trend lines make that even worse. That is because they are extend (redrawn) on every bar, until the price closes above it. In other words, the final trend line that is seen on historical data is not the same as the trend drawn during the actual move itself.
* * * *
Code: Select all
{
19-9-14
Trend line should extend to the right until a candle closes above it.
}
Inputs:
LeftStrength(3),
RightStrength(3);
Variables:
isPivotHigh(False),
trendLineID(0),
highOfPivot(0),
arrin(0), // Array index
barNumExtension(0),
x(0);
// We need an array since multiple pivots can occur before
// the trend line of the first pivot is invalided (close > trend line)
Arrays:
trendlineArray[](0);
once cleardebug;
if (BarStatus(1) = 2) then begin
isPivotHigh = PivotHighVSBar(1, High, LeftStrength, RightStrength,
RightStrength + 1) <> -1;
if (isPivotHigh) then begin
Plot1[RightStrength](High[RightStrength], "PivotHigh");
arrin = arrin + 1;
Array_SetMaxIndex(trendLineArray, arrin);
trendlineArray[arrin] =
TL_New_s(Date[RightStrength], Time_s[RightStrength],
High[RightStrength], Date, Time_s, Close);
TL_SetSize(trendlineArray[arrin], 2);
TL_SetColor(trendlineArray[arrin], RGB(65, 105, 225));
end else NoPlot(1);
// Adjust the still active trendlines
for x = 0 to Array_GetMaxIndex(trendLineArray) begin
if (trendLineArray[x] > 0) then begin
if (Close[1] < TL_GetValue_s(trendLineArray[x], Date[1], Time_s[1])) then begin
TL_SetEnd_s(trendLineArray[x], Date, Time_s, Close);
barNumExtension = CurrentBar;
end else begin
if ((barNumExtension + 1) = CurrentBar) or ((barNumExtension + 2) = CurrentBar) then
TL_SetEnd_s(trendLineArray[x], Date, Time_s, Low)
else
trendLineArray[x] = -1; // Discard trendline; already closed below
end;
end;
end; //: Array loop
end; //: BarClose