Skip to main content

Gann & Pitchfork Geometric Trading System for Amibroker (AFL)

mujibur 8 days ago Amibroker (AFL)

  • Rating:
    0 / 5 (Votes 0)
  • Tags:
    gann, pitchfork, buy, SELL, indicator, amibroker

Gann & Pitchfork Geometric Trading System (Non-Repainting)
Strategy: Andrews’ Pitchfork + Gann Fan + Gann Box Structural Breakouts

Indicator / Formula

Copy & Paste Friendly
// ====================================================================
// Gann & Pitchfork Geometric Trading System (Non-Repainting)
// Strategy: Andrews' Pitchfork + Gann Fan + Gann Box Structural Breakouts
// ====================================================================

_SECTION_BEGIN("Price Chart");
SetChartOptions(0, chartShowDates | chartShowArrows);
PlotOHLC(O, H, L, C, "Price", colorDefault, styleCandle);
_SECTION_END();

_SECTION_BEGIN("Non-Repainting Fractal Pivots");
// 5-Bar Fractal Pivots (2-bar confirmation lag, completely stable on chart edges)
PivotHigh = Ref(H, -2) > Ref(H, -4) AND Ref(H, -2) > Ref(H, -3) AND Ref(H, -2) > Ref(H, -1) AND Ref(H, -2) > H;
PivotLow  = Ref(L, -2) < Ref(L, -4) AND Ref(L, -2) < Ref(L, -3) AND Ref(L, -2) < Ref(L, -1) AND Ref(L, -2) < L;

// Grab values and bar indexes of the confirmed fractal points
// PL2 = Start of structure (Pivot A)
// PH1 = Upper handle of the fork (Pivot B)
// PL1 = Lower handle of the fork (Pivot C)
PL2_Val = ValueWhen(PivotLow, Ref(L, -2), 2);
PL2_Bar = ValueWhen(PivotLow, BarIndex() - 2, 2);

PH1_Val = ValueWhen(PivotHigh, Ref(H, -2), 1);
PH1_Bar = ValueWhen(PivotHigh, BarIndex() - 2, 1);

PL1_Val = ValueWhen(PivotLow, Ref(L, -2), 1);
PL1_Bar = ValueWhen(PivotLow, BarIndex() - 2, 1);

// Chronological safety check
ValidPivots = PL2_Bar < PH1_Bar AND PH1_Bar < PL1_Bar;
_SECTION_END();

_SECTION_BEGIN("Andrew's Pitchfork Mathematics");
// Midpoint of BC (Upper and Lower handles)
M_val = (PH1_Val + PL1_Val) / 2;
M_bar = (PH1_Bar + PL1_Bar) / 2;

// Calculate slope running from A (PL2) through the midpoint M
PF_Divisor = M_bar - PL2_Bar;
PF_Divisor = IIf(PF_Divisor == 0, 1, PF_Divisor); // Division-by-zero protection
PF_Slope = (M_val - PL2_Val) / PF_Divisor;

// Generate lines
BI = BarIndex();
ML  = PL2_Val + PF_Slope * (BI - PL2_Bar);  // Median Line
UPL = PH1_Val + PF_Slope * (BI - PH1_Bar);  // Upper Parallel Line
LPL = PL1_Val + PF_Slope * (BI - PL1_Bar);  // Lower Parallel Line

// Restrict lines from plotting before their starting pivot points
ML  = IIf(BI >= PL2_Bar AND ValidPivots, ML, Null);
UPL = IIf(BI >= PH1_Bar AND ValidPivots, UPL, Null);
LPL = IIf(BI >= PL1_Bar AND ValidPivots, LPL, Null);

// Plot Pitchfork
Plot(ML, "Pitchfork Median", colorLightGrey, styleLine | styleThick);
Plot(UPL, "Pitchfork Upper Parallel", colorGrey40, styleLine);
Plot(LPL, "Pitchfork Lower Parallel", colorGrey40, styleLine);
_SECTION_END();

_SECTION_BEGIN("Gann Fan Mathematics");
pGannScale = Param("Gann Scale (Points/Bar)", 0.25, 0.01, 50.0, 0.01);

G2x1 = PL1_Val + (pGannScale * 2.0) * (BI - PL1_Bar); // Fast Rise Angle
G1x1 = PL1_Val + pGannScale * (BI - PL1_Bar);        // 1x1 Balance Line
G1x2 = PL1_Val + (pGannScale / 2.0) * (BI - PL1_Bar); // Slow Rise Angle

G2x1 = IIf(BI >= PL1_Bar AND ValidPivots, G2x1, Null);
G1x1 = IIf(BI >= PL1_Bar AND ValidPivots, G1x1, Null);
G1x2 = IIf(BI >= PL1_Bar AND ValidPivots, G1x2, Null);

// Plot Gann Fan
Plot(G2x1, "Gann 2x1", colorYellow, styleDashed | styleNoLabel);
Plot(G1x1, "Gann 1x1 (Balance)", colorGold, styleLine | styleThick);
Plot(G1x2, "Gann 1x2", colorOrange, styleDashed | styleNoLabel);
_SECTION_END();

_SECTION_BEGIN("Gann Box Mathematics");
GannHeight = PH1_Val - PL2_Val;

G_025 = PL2_Val + 0.25 * GannHeight;
G_050 = PL2_Val + 0.50 * GannHeight;
G_075 = PL2_Val + 0.75 * GannHeight;

G_025 = IIf(BI >= PL2_Bar AND BI <= PL1_Bar + 50 AND ValidPivots, G_025, Null);
G_050 = IIf(BI >= PL2_Bar AND BI <= PL1_Bar + 50 AND ValidPivots, G_050, Null);
G_075 = IIf(BI >= PL2_Bar AND BI <= PL1_Bar + 50 AND ValidPivots, G_075, Null);

// Plot Gann Box
Plot(G_025, "Gann 0.25", colorDarkBlue, styleDashed | styleNoLabel);
Plot(G_050, "Gann 0.50", colorSkyBlue, styleLine | styleNoLabel);
Plot(G_075, "Gann 0.75", colorDarkBlue, styleDashed | styleNoLabel);
_SECTION_END();

_SECTION_BEGIN("Execution Signals");
VolMA = MA(V, 20);

// BUY: Price breaks above either the Pitchfork Median Line or the Gann 1x1 Balance Line
PFBuy  = Cross(C, ML);
GannBuy = Cross(C, G1x1);
BuySignal = (PFBuy OR GannBuy) AND V > VolMA;

// SELL: Price violates the Pitchfork Lower Parallel or drops below the Gann 1x2 Angle
PFSell   = Cross(LPL, C);
GannSell = Cross(G1x2, C);
SellSignal = PFSell OR GannSell;

Buy = ExRem(BuySignal, SellSignal);
Sell = ExRem(SellSignal, Buy);

PlotShapes(IIf(Buy, shapeUpArrow, shapeNone), colorBrightGreen, 0, L, -20);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone), colorRed, 0, H, -20);
_SECTION_END();

_SECTION_BEGIN("Exploration Scanner");
// Includes any active signal OR day where a physical structural pivot was confirmed
Filter = Buy OR Sell OR PivotHigh OR PivotLow;

StatusText = WriteIf(Buy, "BUY BREAKOUT",
             WriteIf(Sell, "SELL VIOLATION",
             WriteIf(PivotHigh, "CONFIRMED PEAK",
             WriteIf(PivotLow, "CONFIRMED TROUGH", "Scanning"))));

BGColor = IIf(Buy, colorDarkGreen, 
          IIf(Sell, colorDarkRed, 
          IIf(PivotHigh, colorBlue, 
          IIf(PivotLow, colorDarkYellow, colorDefault))));

AddColumn(C, "Close Price", 1.2);
AddColumn(ML, "Pitchfork ML", 1.2);
AddColumn(G1x1, "Gann 1x1", 1.2);
AddTextColumn(StatusText, "Signal Status", 1.0, colorWhite, BGColor, 180);
_SECTION_END();

0 comments

Leave Comment

Please login here to leave a comment.