Skip to content

Instantly share code, notes, and snippets.

@DTM-SC
Last active May 22, 2019 17:49
Show Gist options
  • Save DTM-SC/d7d8a7bfefb88db2180e7d9fee662d47 to your computer and use it in GitHub Desktop.
Save DTM-SC/d7d8a7bfefb88db2180e7d9fee662d47 to your computer and use it in GitHub Desktop.
Fibonacci Retracement AFL
/*This AFL looks back 60 candles and plots the Fibonnaci lines from 60 till latest candle based on High and Low during that period
Tried to keep this simple to understand and plot. This can be converted into a Strategy based on your requirement */
_SECTION_BEGIN("DTM Fibonacci Retracement");
GraphXSpace=2;
Plot(C,"", colorWhite,styleCandle);
EndBar = EndValue(BarIndex());
StartBar = EndBar-60; // To plot Fibonnaci lines from last 60 bars, this can be customised based on your requirement
StartBar_C = Close[StartBar];
i = StartBar;
Prd = EndBar - StartBar;
Lo = LLV(L,Prd);
Hi = HHV(H,Prd);
Line0 = Line1 = Line2 = Line3 = Line4 = Line5 = Line100 = 0;
for( i = StartBar; i < EndBar; i++ )
{
if(EndValue(C)<StartBar_C)
{
Line0 = EndValue(Lo);
Line100 = EndValue(Hi);
Line1 = Line0 + abs(Line100-Line0)*0.236;
Line2 = Line0 + abs(Line100-Line0)*0.382;
Line3 = Line0 + abs(Line100-Line0)*0.5;
Line4 = Line0 + abs(Line100-Line0)*0.618;
Line5 = Line0 + abs(Line100-Line0)*0.786;
}
else
{
Line100 = EndValue(Lo);
Line0 = EndValue(Hi);
Line1 = Line0 - abs(Line100-Line0)*0.236;
Line2 = Line0 - abs(Line100-Line0)*0.382;
Line3 = Line0 - abs(Line100-Line0)*0.5;
Line4 = Line0 - abs(Line100-Line0)*0.618;
Line5 = Line0 - abs(Line100-Line0)*0.786;
}
}
//////////////////////// CONDITIONS /////////////////////////////////
fib0 = LineArray(StartBar, Line0, EndBar, Line0, 0, 1);
fib100 = LineArray(StartBar, Line100, EndBar, Line100, 0, 1);
// depth of middle lines
n = round((EndBar-StartBar)/2);
// middle lines
fib1= LineArray((EndBar-n), Line1, EndBar, Line1, 0, 1);
fib2= LineArray((EndBar-n), Line2, EndBar, Line2, 0, 1);
fib3= LineArray((EndBar-n), Line3, EndBar, Line3, 0, 1);
fib4= LineArray((EndBar-n), Line4, EndBar, Line4, 0, 1);
fib5= LineArray((EndBar-n), Line5, EndBar, Line5, 0, 1);
Plot(fib0," 0.00%=", colorWhite);
Plot(fib1,"23.6%=", colorGreen);
Plot(fib2,"38.6%=", colorBlue);
Plot(fib3,"50%=", colorGold);
Plot(fib4,"61.8%=", colorAqua);
Plot(fib5,"78.6%=", colorYellow);
Plot(fib100,"100%=", colorRed);
PlotText(" 0.00% ",EndBar+2, Line0,colorWhite);
PlotText(" 23.6% ",EndBar+2, Line1,colorGreen);
PlotText(" 38.6% ",EndBar+2, Line2,colorBlue);
PlotText(" 50% ",EndBar+2, Line3,colorGold);
PlotText(" 61.8% ",EndBar+2, Line4,colorAqua);
PlotText(" 78.6% ",EndBar+2, Line5,colorYellow);
PlotText(" 100% ",EndBar+2, Line100,colorRed);
_SECTION_END();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment