GANN swing trading script

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
dvk1970
Posts: 2
Joined: 16 Aug 2012

GANN swing trading script

Postby dvk1970 » 26 Aug 2012

Hi there, just came across an old trading system I saved and used years ago. Its written in TA-script http://www.ta-script.com/forum/.

I am new to MC so will see if I can convert it.

{- Filename: Gann Swing Trading -}

type
TTrendType = (ttUnknown, ttUp, ttDown);

var
TrendPeriode, MAPeriode: integer; // parameterwaarde
TrendType: TTrendType;
LastLoIndex, LastHiIndex, i, HiCount, LoCount, Swing, PrevHiIndex, PrevLoIndex: integer;
Trend, Activator, MALow, MAHigh: TSeries;
Position: integer;
begin
// Indicator parameter definities
TrendPeriode := CreateParameterInteger('Trend periode', 1, 999, 2, true);
MAPeriode := CreateParameterInteger('MA periode', 1, 999, 3, true);

// Indicator definitie
with Indicator do
begin
ShortName := 'Gann Swing Trading';
RequiredBars := TrendPeriode * 2 + MAPeriode;
NewBand := false;
ScaleRange := srCommon;
end;

Trend := CreateSeries(BarCount);
Activator := CreateSeries(BarCount);
TrendType := ttUnknown;
HiCount := 0;
LoCount := 0;
Swing := 0;
Position := 0;
LastHiIndex := FirstValidIndex(High);
LastLoIndex := FirstValidIndex(Low);
PrevHiIndex := -1;
PrevLoIndex := -1;

MALow := MA(Low, maSimple, MAPeriode);
MAHigh := MA(High, maSimple, MAPeriode);

for i:=FirstValidIndex(Close)+1 to BarCount-1 do
begin
if (High > High[LastHiIndex]) and (TrendType = ttUp) then
LastHiIndex := i;

if High > High[i-1] then
begin
HiCount := HiCount+1;
if HiCount >= TrendPeriode then
begin
if TrendType <> ttUp then
begin
{ er is een omslag naar een upswing; teken nu de downswing }
Trend[LastHiIndex] := High[LastHiIndex];
Trend[LastLoIndex] := Low[LastLoIndex];
PrevHiIndex := LastHiIndex;
LastHiIndex := i;
TrendType := ttUp;
end;
end;
end else
HiCount := 0;

if (Low < Low[LastLoIndex]) and (TrendType = ttDown) then
LastLoIndex := i;

if Low < Low[i-1] then
begin
LoCount := LoCount+1;
if LoCount >= TrendPeriode then
begin
if TrendType <> ttDown then
begin
{ er is een omslag naar een downswing; teken nu de upswing }
Trend[LastLoIndex] := Low[LastLoIndex];
Trend[LastHiIndex] := High[LastHiIndex];
PrevLoIndex := LastLoIndex;
LastLoIndex := i;
TrendType := ttDown;
end;
end;
end else
LoCount := 0;

{ Bereken de HiLo Activator }
if (i >= 2) and IsValid(MAHigh[i-2]) then // als deze geldig is dan is de rest dat ook
begin
if (Close < MALow[i-1]) and (Close[i-1] >= MALow[i-2]) then
Swing := -1;
if (Close > MAHigh[i-1]) and (Close[i-1] >= MAHigh[i-2]) then
Swing := 1;
if Swing = 1 then
Activator := MALow
else if Swing = -1 then
Activator := MAHigh;
end;

if MarketPosition(i) = 0 then
begin
if (TrendType = ttUp) and (Swing = 1) and (PrevHiIndex >= 0) and (Close[i] > High[PrevHiIndex]) then
EnterLong(i);
end;
if (MarketPosition(i) = 1) and (Swing = -1) then ExitLong(i);
if MarketPosition(i) = 0 then
begin
if (TrendType = ttDown) and (Swing = -1) and (PrevLoIndex >= 0) and (Close[i] < Low[PrevLoIndex]) then
EnterShort(i);
end;
if (MarketPosition(i) = -1) and (Swing = 1) then ExitShort(i);
end;

with CreateLine(Activator) do
begin
Color := clWhite;
LineType := ltDot;
end;
CreateLine(Trend).Color := clRed;
end.

Return to “User Contributed Studies and Indicator Library”