No one who can and is willing to help in coding this afl?
This TA script (including extra hidden reversals) is coding for realtime calculation and drawing of PR and NR as they occur.
Hopefully someone can translate this into afl. It will prove to be a very powerfull tool in setting targets in intraday an EOD trading.
---
var
N, Peak, Trough :Integer;
NH, NL :Integer;
NDoud, NToud :Integer;
NHD1, NHD2 :Integer;
StartBar :Integer; // Calculate last bars to reduce calculation time
RSISeries :TSeries; // RSI(14)
less :Boolean;
ShowHidden :Boolean;
V6040 :Boolean;
TP :array of integer;
const
OB = 70;
OS = 30;
RSIperiod = 14;
Function NextTrough(Serie:Tseries; NStart:Integer) :Integer;
Var
I :Integer;
begin // find possible next trough
// left and right value must be higher
Result := Barcount-1;
for I := Nstart+1 to Barcount-3 do
if (serie <= serie[I-1]) and (serie < Serie[I+1]) then
begin
Result := I;
I := Barcount-1;
end;
end;
Function NextPeak(Serie:Tseries; NStart:Integer) :Integer;
Var
I :Integer;
begin // find possible next peak
// left and right value must be lower
Result := Barcount-1;
for I := Nstart+1 to Barcount-3 do
if (serie >= serie[I-1]) and (serie > Serie[I+1]) then
begin
Result := I;
I := Barcount-1;
end;
end;
Procedure HiddenNR(start,stop:Integer);
var
N, I, J, K :Integer;
HDV :Real;
HDBar :Array[0..42] of integer;
HDCounter :Integer;
txt :string;
TL :TTrendline;
begin
// extra loop to find hidden reversals
N := start;
HDCounter := 0;
NHD1 := Start;
NHD2 := Stop;
While N < Stop do
begin
N := NextPeak(RSISeries,N);
if (N < Stop) and (HDCounter < 40) then
begin
HDBar[HDCounter] := N;
if HDCounter < 40 then HDCounter := HDCounter + 1;
end;
end;
if HDCounter > 4 then
begin
for N := 0 to HDCounter-3 do
begin
for I := N+2 to HDCounter-1 do
begin
if RSISeries[HDBar[N]] < RSISeries[HDBar] then
if Close[HDBar[N]] > Close[HDBar] then
if Highest(RSISeries, HDBar[N]+1,HDBar-1) < RSISeries[HDBar[N]] then
begin
J := LowestIndex(RSISeries,HDBar[N],HDBar);
if RSISeries[J] < RSISeries[HDBar[N]] then
begin
HDV := close[HDBar]-close[HDBar[N]]+close[J];
if (HDBar[N] > NHD1) and (HDBar < NHD2) then
begin
NHD1 := HDBar[N];
NHD2 := HDBar;
TL := CreateTrendline(Barposition[HDBar[N]], RSISeries[HDBar[N]],
Barposition[HDBar], RSISeries[HDBar])
if TrendlineValue(TL,Barposition[Barcount-1]) < 100 then
TL.color := ClYellow else TL.color := clGray;
if lowest(low,HDBar,barcount-2) > HDV then
txt := 'HNR '+ formatFloat('0.00',HDV)
else txt := '(' + formatFloat('0.00',HDV) + ')';
K := round((HDBar[N]+HDBar)/2);
with CreateText(BarPosition[K], RSISeries[HDBar[N]],txt) do
begin
VertPosition := vpTop;
color := TL.color;
end;
TP[HDBar] := -3;
TP[HDBar[N]] := -3;
N := I;
end;
end;
end;
end;
end;
end;
// einde extra loop
end;
Procedure HiddenPR(start,stop:Integer);
var
N, I, J, K :Integer;
HDV :Real;
HDBar :Array[0..42] of integer;
HDCounter :Integer;
txt :string;
TL :TTrendline;
begin
// extra loop to find hidden reversals
N := start;
HDCounter := 0;
NHD1 := Start;
NHD2 := Stop;
While N < Stop do
begin
N := NextTrough(RSISeries,N);
if (N < Stop) and (HDCounter < 40) then
begin
HDBar[HDCounter] := N;
if HDCounter < 40 then HDCounter := HDCounter + 1;
end;
end;
if HDCounter > 4 then
begin
for N := 0 to HDCounter-3 do
begin
for I := N+2 to HDCounter-1 do
begin
if RSISeries[HDBar[N]] > RSISeries[HDBar] then
if Close[HDBar[N]] < Close[HDBar] then
if lowest(RSISeries, HDBar[N]+1,HDBar-1) > RSISeries[HDBar[N]] then
begin
J := HighestIndex(RSISeries,HDBar[N],HDBar);
if RSISeries[J] > RSISeries[HDBar[N]] then
begin
HDV := close[HDBar]-close[HDBar[N]]+close[J];
if (HDBar[N] > NHD1) and (HDBar < NHD2) then
begin
NHD1 := HDBar[N];
NHD2 := HDBar;
TL := CreateTrendline(Barposition[HDBar[N]], RSISeries[HDBar[N]],
Barposition[HDBar], RSISeries[HDBar])
if TrendlineValue(TL,Barposition[Barcount-1]) > 0 then
TL.color := clAqua else TL.color := clGray;
if Highest(High,HDBar,Barcount-1) < HDV then
txt := 'HPR '+ formatFloat('0.00',HDV)
else txt := '(' + formatFloat('0.00',HDV) + ')';
K := round((HDBar[N]+HDBar)/2);
CreateText(BarPosition[K], RSISeries[HDBar[N]],txt).color := TL.color;
TP[HDBar[N]] := +3;
TP[HDBar] := +3;
N := I;
end;
end;
end;
end;
end;
end;
// einde extra loop
end;
Function TestNR(Top:Integer; var L:Integer) :Boolean;
Var
NT, Nx, N :Integer; // NT= position next high
Slope1, Slope2 :Real;
NR :Real;
VTL :Real; // Valua trendline at position x
txt :string;
TL :TTrendline;
begin
(* calculate and draw NR *)
Nx := Top+2;
if Nx > Barcount-1 then Nx := Barcount-1;
NT := Barcount-1;
Slope1 := 0;
while Nx <= Barcount-2 do
begin
Slope2 := (RSISeries[Top]-RSISeries[Nx])/(Nx-Top);
If Slope2 <= Slope1 then
begin
NT := Nx;
Slope1 := Slope2;
end;
Nx := Nx+1;
end;
if (Close[Top] > Close[NT]) and
(RSISeries[Top] < RSISeries[NT])then
begin
Result := True;
L := lowestIndex(RSISeries,Top,NT);
NR := Close[L] + close[NT] - close[Top];
if NR > 10 then
begin
if lowest(low,NT,barcount-1) > NR then
txt := 'NR '+ formatFloat('0.00',NR)
else txt := '(' + formatFloat('0.00',NR) + ')';
end else
begin
if lowest(low,NT,barcount-1) > NR then
txt := 'NR '+ formatFloat('0.000',NR)
else txt := '(' + formatFloat('0.000',NR) + ')';
end;
TL := CreateTrendline(Barposition[Top], RSISeries[Top], Barposition[NT], RSISeries[NT]);
TL.style := LsDot;
VTL := TrendlineValue(TL, BarPosition[Barcount-1]);
if VTL < 100 then TL.color := clYellow else TL.color := clGray;
if less and (NToud <> NT) then
begin
N := round((Top+NT)/2);
with CreateText(BarPosition[N], RSISeries[NT], txt) do
begin
VertPosition := vpTop;
color := TL.color;
end
end else
begin
with CreateText(BarPosition[Top], RSISeries[Top], txt) do
begin
VertPosition := vpTop;
color := TL.color;
end
end;
NToud := NT;
TP[NT] := -2;
TP[Top] := -2;
if ShowHidden then HiddenNR(Top,NT);
end;
end;
Function TestPR(Dal:Integer; var H:Integer) :Boolean;
Var
ND, Nx, N :Integer;
Slope1, Slope2 :Real;
PR :Real;
VTL :Real;
txt :string;
TL :TTrendline;
begin
(* calculate and draw *)
Nx := Dal+2;
if Nx > Barcount-1 then Nx := Barcount-1;
ND := Barcount-1;
Slope1 := 0;
while Nx <= Barcount-2 do
begin
Slope2 := (RSISeries[Dal]-RSISeries[Nx])/(Nx-Dal);
If Slope2 >= Slope1 then
begin
ND := Nx;
Slope1 := Slope2;
end;
Nx := Nx+1;
end;
if (close[Dal] < close[ND]) and
(RSISeries[Dal] > RSISeries[ND]) then
begin
Result := true;
H := HighestIndex(RSISeries,Dal,ND);
PR := Close[H] + close[ND] - close[Dal];
if PR > 10 then
begin
if Highest(High,ND,Barcount-1) < PR then
txt := 'PR '+ formatFloat('0.00',PR)
else txt := '(' + formatFloat('0.00',PR) + ')';
end else
begin
if Highest(High,ND,Barcount-1) < PR then
txt := 'PR '+ formatFloat('0.000',PR)
else txt := '(' + formatFloat('0.000',PR) + ')';
end;
TL := CreateTrendline(Barposition[Dal], RSISeries[Dal], Barposition[ND], RSISeries[ND]);
TL.style := LsDot;
VTL := TrendlineValue(TL, BarPosition[Barcount-1]);
if VTL > 0 then TL.color := claqua else TL.color := clGray;
if less and (NDoud <> ND) then
begin
N := round((Dal+ND)/2);
with CreateText(BarPosition[N], RSISeries[ND], txt) do
begin
color := TL.color;
if RSISeries[ND]>30 then VertPosition := vpBottom;
end;
end else
begin
with CreateText(BarPosition[Dal], RSISeries[Dal], txt) do
begin
if RSISeries[ND]>30 then VertPosition := vpBottom;
color := TL.color;
end;
end;
NDoud := ND;
TP[ND] := 2;
TP[Dal] := 2;
if ShowHidden then HiddenPR(Dal+2,ND-2);
end;
end;
Procedure TradeSignals;
var
N :Integer;
begin
for N :=startbar to barcount-2 do
begin
if TP[N] > 1 then enterlong(N+1);
if TP[N] < -1 then entershort(N+1);
end;
end;
Procedure TradeSignalsLevels;
var
N :Integer;
PInfo :TPositionInfo;
begin
for N :=startbar to barcount-2 do
begin
if RSISeries[N] > 60 then enterlong(N+1);
if RSISeries[N] < 40 then entershort(N+1);
CalcPositionInfo(N,PInfo);
If PInfo.long and (RSISeries[N] < 50) then exitlong(N+1);
if (not PInfo.long) and (RSISeries[N] > 50) then exitshort(N+1);
end;
end;
begin
with Indicator do
begin
ShortName := 'NRPR';
RequiredBars := 30;
SafeRefreshInterval := 10;
FutureBars := 3;
HiddenParams:= True;
end;
// ShowHidden := CreateParameterBoolean('Hidden reversals', True, false);
// less := CreateParameterBoolean('less lines', True, false);
V6040 := CreateParameterBoolean('Use 60 and 40 level', True, True);
ShowHidden := True;
less := True;
SetArrayLength(TP, BarCount);
for N := 0 to barcount-1 do TP[N] := 0;
if Toonhidden then
StartBar := Barcount - 1000 // confine calculation to last 1000 bars
else
StartBar := Barcount - 5000; // without hidden more bars
If StartBar < 20 then StartBar := 20;
if Barcount > 20 then
begin
RSISeries := RSI(Close, RSIperiod);
For N := StartBar to Barcount-5 do
begin
Peak := NextPeak(RSISeries,N);
if TestNR(Peak,NL) then
begin
N :=Peak;
if less then
if N < NL then N := NL;
end else
begin
N :=Peak;
end;
end;
For N := StartBar to Barcount-5 do
begin
Trough := NextTrough(RSISeries,N);
if TestPR(Trough, NH) then
begin
N := Trough;
if less then
if N < NH then N := NH;
end else
begin
N := Trough;
end;
end;
if V6040 then
TradesignalsLevels
else
Tradesignals;
with CreateTrendline(Barposition[1], OB, Barposition[BarCount-2], OB) do
begin
color := clRed;
ValueVisible := False;
ExtendRight:= True;
DrawBehindChart := True;
end;
with CreateTrendline(Barposition[1], OS, Barposition[BarCount-2], OS) do
begin
color := clRed;
ValueVisible := False;
ExtendRight:= True;
DrawBehindChart := True;
end;
end;
with CreateLine(RSISeries) do
begin
Name := 'RSI';
Color := RGB(0,200,0);
colortype := ctIndicator;
ValueVisible := True;
end;
end.
---