Skip to content

Instantly share code, notes, and snippets.

@smnuman
Created August 13, 2014 14:02
Show Gist options
  • Save smnuman/e8dc9607c6d85ba19ea2 to your computer and use it in GitHub Desktop.
Save smnuman/e8dc9607c6d85ba19ea2 to your computer and use it in GitHub Desktop.
_SECTION_BEGIN("GIP-3");
/// This whole thing is found and copied here for reference and experiment by Numan on 2014-08-13
/// Original from : http://www.stockmaniacs.net/downloads/afl/gap-finder-plus-gip.afl
/// Details : http://www.stockmaniacs.net/blog/scott-andrews-12-reasons-to-trade-gaps-gap-trading-amibroker-afl/
SetChartOptions(0,chartShowArrows|chartShowDates);
SetChartBkColor(colorGrey50);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Cr = IIf(RSI(9) <= LLV(RSI(9),9) , colorRed , IIf(RSI(9) >=HHV(RSI(9),9) , colorGreen ,IIf(C > O , colorWhite , colorBlack)));
SetBarFillColor( Cr );
Plot( C, "Close", Cr, styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
if( ParamToggle("Tooltip shows", "All Values|Only Prices" ) )
{
ToolTip=StrFormat("Open: %g\nHigh: %g\nLow: %g\nClose: %g (%.1f%%)\nVolume: "+NumToStr( V, 1 ), O, H, L, C, SelectedValue( ROC( C, 1 )));
}
_SECTION_BEGIN("Gap Finder");
///////////////////////////////////////////////////////////////////////////////
// Gap Finder
// AFL that plots that unfilled Gaps encountered within the last N bars.
// Author : Adheer Pai ([email protected])
///////////////////////////////////////////////////////////////////////////////
// Input : The number of bars ago from where to start looking for gaps.
// Default is 250 : So, by default we search for Gaps found in last 250 trading days ( 1 year )
period = Param("Lookback Period", 250, 15, 500);
// If we do not have enough bars, adjust the period accordingly.
if( BarCount - period - 1 < 0 ) period = BarCount - 2;
bIsGapUp = ( L > Ref(H, -1) ); // Identify GapUp bars
bIsGapDn = ( H < Ref(L, -1) ); // Identify GapDown bars
// We are not interested in Gap Ups and Gap Downs before the Period e.g. before the last 250 bars.
// So we clear the values for bars before the ones we are interested in.
for( i = 0 ; i < (BarCount - 1 - period) ; i++ )
{
bIsGapUp[i] = False;
bIsGapDn[i] = False;
}
// Now plot GapUp bars with a Up-Triangle below its low.
PlotShapes(IIf(bIsGapUp, shapeSmallUpTriangle, shapeNone), colorGreen, 0, L, -12 );
// Now plot GapDown bars with a Down-Triangle below its high.
PlotShapes(IIf(bIsGapDn, shapeSmallDownTriangle, shapeNone), colorPink, 0, H );
// Now walk from the first bar (e.g 250 bars ago, to the current bar)
for( i = (BarCount - period - 1) ; i <= (BarCount - 1) ; i++ )
{
dUpper = 0.0; dLower = 0.0; bFilled = True;
// Is the current bar a Gap-Up bar ?
if( bIsGapUp[i] == True )
{
// If yes, the store the Gap (Upper value) and Lower values.
dUpper = L[i]; dLower = H[i-1]; bFilled = False;
// Now walk till the current bar and see if the Gap values have been filled from Above.
// I.e prices are falling and the gap is being falled.
for( j = i+1; j <= (BarCount - 1) ; j++ )
{
// Check whether the current low is lesser than the Gap high. If yes, the Gap
// has been penetrated.
if( L[j] < dUpper )
{
dUpper = L[j];
// Determine whether the Gap has been fully penetrated - if yes, then the
// Gap has been filled.
if( dUpper <= dLower ) bFilled = True;
}
if( bFilled == True ) break;
}
}
else if( bIsGapDn[i] == True )
{
// Same logic as GapUp - except we check whether the Gap has been pierced from Below.
// i.e Prices are rising and the Gap has been filled.
dUpper = L[i-1]; dLower = H[i]; bFilled = False;
for( j = i+1; j <= (BarCount - 1) ; j++ )
{
if( H[j] > dLower )
{
dLower = H[j];
if( dLower >= dUpper ) bFilled = True;
}
if( bFilled == True ) break; // Gap was filled, so move on to find the next gap.
}
}
if( bFilled == False ) // Gap Not filled - so plot horizontal line at the Gap Level.
{
pLine = LineArray(i-1, dLower, BarCount-1,dLower, colorWhite);
Plot(pLine, "", colorPink, styleDashed);
pLine = LineArray(i-1, dUpper, BarCount-1, dUpper, colorWhite);
Plot(pLine, "", colorGreen, styleDashed);
}
}
_SECTION_END();
_SECTION_BEGIN("Gap Finder");
///////////////////////////////////////////////////////////////////////////////
// Gap Finder
// AFL that plots that unfilled Gaps encountered within the last N bars.
// Author : Adheer Pai ([email protected])
///////////////////////////////////////////////////////////////////////////////
// Input : The number of bars ago from where to start looking for gaps.
// Default is 250 : So, by default we search for Gaps found in last 250 trading days ( 1 year )
period = Param("Lookback Period", 250, 15, 500);
// If we do not have enough bars, adjust the period accordingly.
if( BarCount - period - 1 < 0 ) period = BarCount - 2;
bIsGapUp = ( L > Ref(H, -1) ); // Identify GapUp bars
bIsGapDn = ( H < Ref(L, -1) ); // Identify GapDown bars
// We are not interested in Gap Ups and Gap Downs before the Period e.g. before the last 250 bars.
// So we clear the values for bars before the ones we are interested in.
for( i = 0 ; i < (BarCount - 1 - period) ; i++ )
{
bIsGapUp[i] = False;
bIsGapDn[i] = False;
}
// Now plot GapUp bars with a Up-Triangle below its low.
PlotShapes(IIf(bIsGapUp, shapeSmallUpTriangle, shapeNone), colorGreen, 0, L, -12 );
// Now plot GapDown bars with a Down-Triangle below its high.
PlotShapes(IIf(bIsGapDn, shapeSmallDownTriangle, shapeNone), colorPink, 0, H );
// Now walk from the first bar (e.g 250 bars ago, to the current bar)
for( i = (BarCount - period - 1) ; i <= (BarCount - 1) ; i++ )
{
dUpper = 0.0; dLower = 0.0; bFilled = True;
// Is the current bar a Gap-Up bar ?
if( bIsGapUp[i] == True )
{
// If yes, the store the Gap (Upper value) and Lower values.
dUpper = L[i]; dLower = H[i-1]; bFilled = False;
// Now walk till the current bar and see if the Gap values have been filled from Above.
// I.e prices are falling and the gap is being falled.
for( j = i+1; j <= (BarCount - 1) ; j++ )
{
// Check whether the current low is lesser than the Gap high. If yes, the Gap
// has been penetrated.
if( L[j] < dUpper )
{
dUpper = L[j];
// Determine whether the Gap has been fully penetrated - if yes, then the
// Gap has been filled.
if( dUpper <= dLower ) bFilled = True;
}
if( bFilled == True ) break;
}
}
else if( bIsGapDn[i] == True )
{
// Same logic as GapUp - except we check whether the Gap has been pierced from Below.
// i.e Prices are rising and the Gap has been filled.
dUpper = L[i-1]; dLower = H[i]; bFilled = False;
for( j = i+1; j <= (BarCount - 1) ; j++ )
{
if( H[j] > dLower )
{
dLower = H[j];
if( dLower >= dUpper ) bFilled = True;
}
if( bFilled == True ) break; // Gap was filled, so move on to find the next gap.
}
}
if( bFilled == False ) // Gap Not filled - so plot horizontal line at the Gap Level.
{
pLine = LineArray(i-1, dLower, BarCount-1,dLower, colorWhite);
Plot(pLine, "", colorPink, styleDashed);
pLine = LineArray(i-1, dUpper, BarCount-1, dUpper, colorWhite);
Plot(pLine, "", colorGreen, styleDashed);
}
}
_SECTION_END();
Filter= bIsGapUp OR bIsGapDn;
AddColumn(bIsGapDn,"GapDown");
AddColumn(bIsGapup,"GapDown");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment