setpositionsize(priceclose(bar)*1.1);
bs := false;
ss := false;
ATRValue := ATR(Bar, Lookback);
VP := 100 * ATRValue / PriceClose(Bar);
Reversal := Int(VPFactor * VP);
{Find peaks and troughs}
P1 := Peak(Bar, #High, F1 * Reversal);
P1Bar := PeakBar(Bar, #High, F1 * Reversal);
if P1Bar > - 1 then
begin
P2 := Peak(P1Bar, #High, Reversal);
P2Bar := PeakBar(P1Bar, #High, Reversal);
end;
T1 := Trough(Bar, #Low, F1 * Reversal);
T1Bar := TroughBar(Bar, #Low, F1 * Reversal);
if T1Bar > -1 then
begin
T2 := Trough(T1Bar, #Low, Reversal);
T2Bar := TroughBar(T1Bar, #Low, Reversal);
end;
{Test for a bullish 222}
{Trough X is T2}
{Peak A is P2}
{Trough B is T1}
{Peak C is P1}
{D is the buy point}
D := PriceLow(Bar);
PTValid := (P1Bar > T1Bar) and (T1Bar > P2Bar) and (P2Bar > T2Bar);
HLValid := (P1 < P2) and (T1 > T2) and (P1 > T1);
InZone := (D < T1) and (D > T2);
if PTValid and HLValid and InZone then
begin
XA := P2 - T2;
AB := P2 - T1;
BC := P1 - T1;
CD := P1 - D;
AD := P2 - D;
ABdXA := AB / XA; {AB should be 61.8% of XA}
C1 := (ABdXA > F1 - Tolerance) and (ABdXA < F1 + Tolerance);
BCdAB := BC / AB; {BC should be 61.8-78.6% of AB}
C2 := (BCdAB > F1 - Tolerance) and (BCdAB < F2 + Tolerance);
CDdBC := CD / BC; {CD should be 127-161.8% of BC}
C3 := (CDdBC > F3 - Tolerance) and (CDdBC < F4 + Tolerance);
ADdXA := AD / XA; {AD should be 78.6% of XA}
C4 := (ADdXA > F2 - Tolerance) and (ADdXA < F2 + Tolerance);
if C1 and C2 and C3 and C4 then
begin
DrawLine(P2Bar, P2, T2Bar, T2, 0, #GREEN, #Solid);
DrawLine(T1Bar, T1, P2Bar, P2, 0, #GREEN, #Solid);
DrawLine(P1Bar, P1, T1Bar, T1, 0, #GREEN, #Solid);
DrawLine(Bar, D, P1Bar, P1, 0, #GREEN, #Solid);
DrawLine(Bar, D, T1Bar, T1, 0, #GREEN, #Dotted);
DrawLine(Bar, D, T2Bar, T2, 0, #GREEN, #Dotted);
AnnotateBar('B', Bar, True, #GREEN, 10);
bs := true;
ss := false;
end;
end;
{Test for a bearish 222}
{Peak X is P2}
{Trough A is T2}
{Peak B is P1}
{Trough C is T1}
{D is the short point}
D := PriceHigh(Bar);
PTValid := (T1Bar > P1Bar) and (P1Bar > T2Bar) and (T2Bar > P2Bar);
HLValid := (T1 > T2) and (P1 < P2) and (T1 < P1);
InZone := (D > P1) and (D < P2);
if PTValid and HLValid and InZone then
begin
XA := P2 - T2;
AB := P1 - T2;
BC := P1 - T1;
CD := D - T1;
AD := D - T2;
ABdXA := AB / XA; {AB should be 61.8% of XA}
C1 := (ABdXA > F1 - Tolerance) and (ABdXA < F1 + Tolerance);
BCdAB := BC / AB; {BC should be 61.8-78.6% of AB}
C2 := (BCdAB > F1 - Tolerance) and (BCdAB < F2 + Tolerance);
CDdBC := CD / BC; {CD should be 127-161.8% of BC}
C3 := (CDdBC > F3 - Tolerance) and (CDdBC < F4 + Tolerance);
ADdXA := AD / XA; {AD should be 78.6% of XA}
C4 := (ADdXA > F2 - Tolerance) and (ADdXA < F2 + Tolerance);
if C1 and C2 and C3 and C4 then
begin
DrawLine(T2Bar, T2, P2Bar, P2, 0, #RED, #Solid);
DrawLine(P1Bar, P1, T2Bar, T2, 0, #RED, #Solid);
DrawLine(T1Bar, T1, P1Bar, P1, 0, #RED, #Solid);
DrawLine(Bar, D, T1Bar, T1, 0, #RED, #Solid);
DrawLine(Bar, D, P1Bar, P1, 0, #RED, #Dotted);
DrawLine(Bar, D, P2Bar, P2, 0, #RED, #Dotted);
AnnotateBar('S', Bar, False, #RED, 10);
ss := true;
bs := false;
end;
end;
if LastPositionActive then
begin
zaehler := zaehler + 1;
if PositionLong( LastPosition ) then
{ Long Position Exit Rules }
begin
if zaehler=#OptVar1 then
SellAtMarket(Bar+1, LastPosition,'') ;
end
else
{ Short Position Exit Rules }
begin
if zaehler=#OptVar1 then
CoverAtMarket(Bar+1,LastPosition,'');
end;
end
else
begin
zaehler := 0;
{ Long Position Entry Rules }
if bs then
BuyAtMarket( Bar+1,'');
{ Short Position Entry Rules }
if ss then
ShortAtMarket( Bar+1,'');
end;
end;