Sincerely request to try create Trash's Demand Supply zones and Edward's Gap Zones without loops. Will be a great knowledge upgrade opportunity for me. Below are the 2 AFLs.
1. Trash's Demand and Supply Zones
// by trash, vers.1.5
// from https://www.traderji.com/community/threads/supply-demand-formula-from-ninjacator.88984/post-843771
RequestTimedRefresh( 0.1 );
procedure GFXBlinkingMsg( Msg, x1, y1, x2, y2, TextColor, FontSize, FontName )
{
// originally by Herman van den Bergen, edited by trash
global FontName;
local BlinkState, textcolor, fontSize, msg;
BlinkState = GetPerformanceCounter() - Nz( StaticVarGet( "~BlinkState" ) );
if ( BlinkState / 1000 > 2 )
{
StaticVarSet( "~BlinkState", GetPerformanceCounter( 1 ) ) ;
}
else
{
GfxSetBkMode( 1 );
GfxSelectFont( FontName, FontSize, 700 );
GfxSetTextColor( TextColor );
GfxDrawText( Msg, x1, y1, x2, y2, 37 );
}
}
SetBarsRequired(sbrAll,sbrAll);
SDswitch = ParamToggle( "Supply/Demand Zones", "OFF|ON", 1 );
sig = ParamToggle( "Signals", "OFF|ON", 0 );
MTFswitch = ParamList( "Multi TF Support", "OFF|Intra-day|EOD", 1 );
intratmfrm = Param( "Input Intra-day TF of S/D Zones (minutes)", 5, 1, 1440, 1 ) * 60;
EODmode = ParamList( "Choose EOD TF of S/D Zones", "Daily|Weekly|Monthly", 0 );
SDmode = ParamList( "Supply/Demand Mode", "Small|Large|Extra Large", 0 );
Sensitivity = Param( "Sensitivity (%)", 1.5, 0.00001, 1000, 0.00001 );
NumZones = Param( "Number of S/D Zones", 12, 2, 60, 2 ) / 2;
style1 = styleNoRescale+styleCloud; //ParamStyle( "Style 1", styleNoRescale | styleCloud);
style2 = styleNoRescale+styleThick ;//ParamStyle( "Style 2", styleNoRescale | styleThick );
ColorDem = ParamColor( "Blend Color - Demand Zones", ColorRGB(0,115,0) );
ColFacDem = 0.65;//Param( "Blend Color Factor - Demand Zones", 0.65, 0, 2, 0.01 );
ColorSup = ParamColor( "Blend Color - Supply Zones", colorDarkRed);
ColFacSup = 0.65;//Param( "Blend Color Factor - Supply Zones", 0.65, 0, 2, 0.01 );
Colorline = GetChartBkColor() ;//ParamColor( "Color - Zone Borders", GetChartBkColor() );
switch( EODmode )
{
case "Weekly": eodtmfrm = inWeekly; break;
case "Monthly": eodtmfrm = inMonthly; break;
default: eodtmfrm = inDaily; break;
}
expandmode = expandFirst;
shift = 0;
switch ( MTFswitch )
{
case "Intra-day":
Close_ = TimeFrameGetPrice( "C", intratmfrm, shift, expandmode );
Open_ = TimeFrameGetPrice( "O", intratmfrm, shift, expandmode );
High_ = TimeFrameGetPrice( "H", intratmfrm, shift, expandmode );
Low_ = TimeFrameGetPrice( "L", intratmfrm, shift, expandmode );
break;
case "EOD":
Close_ = TimeFrameGetPrice( "C", eodtmfrm, shift, expandmode );
Open_ = TimeFrameGetPrice( "O", eodtmfrm, shift, expandmode );
High_ = TimeFrameGetPrice( "H", eodtmfrm, shift, expandmode );
Low_ = TimeFrameGetPrice( "L", eodtmfrm, shift, expandmode );
break;
default:
Close_ = C;
Open_ = O;
High_ = H;
Low_ = L;
break;
}
bi = BarIndex();
selectedBI = SelectedValue( bi );
selectedC = SelectedValue( Close_ );
MinVal = Min( Open_, Close_ );
MaxVal = Max( Open_, Close_ );
if ( selectedC != LastValue( C ) )
{
pxcw = Status( "pxchartwidth" );
pxcb = Status( "pxchartbottom" );
x1 = pxcw / 2;
y1 = pxcb;
GFXBlinkingMsg( "Currently viewing historical Supply/Demand zones!", x1 - 300, y1 - 10, x1 + 300, y1 - 30, colorRed, 12, "Helvetica" );
}
switch( SDmode )
{
case "Large":
ValSup = MinVal;
ValDem = MaxVal;
break;
case "Extra Large":
ValSup = Low_;
ValDem = High_;
break;
default:
ValSup = MaxVal;
ValDem = MinVal;
break;
}
for ( i = 1; i <= NumZones; i++ )
{
x1 = selectedBI - SelectedValue( PeakBars( High_, Sensitivity, i ) );
y1 = SelectedValue( Peak( High_, Sensitivity, i ) );
y2 = ValueWhen( bi == x1, High_ - Valsup );
Line1 = LineArray( x1 + 1, y1, selectedBI, y1, 1 );
Line2 = Line1 - y2;
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
x2 = selectedBI - SelectedValue( TroughBars( Low_, Sensitivity, i ) );
y3 = SelectedValue( Trough( Low_, Sensitivity, i ) );
y4 = ValueWhen( bi == x2, ValDem - Low_ );
Line3 = LineArray( x2 + 1, y3, selectedBI, y3, 1 );
Line4 = Line3 + y4;
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
rcol = ColorSup;
scol = ColorDem;
RColor = IIf( selectedC > Line1, scol, rcol );
SColor = IIf( selectedC > Line4, scol, rcol );
if ( SDSwitch )
{
zorder = -i - 2;
zorder2 = zorder + 2;
PlotOHLC( Line1, Line1, Line2, Line2, "", Rcolor, style1, 0, 1, 0, zorder=0 );
Plot( Line1, "", Colorline, style2, 0, 1, 0, zorder=0 );
PlotOHLC( Line3, Line3, Line4, Line4, "", Scolor, style1, 0, 1, 0, zorder=0);
Plot( Line3, "", Colorline, style2, 0, 1, 0, zorder=0 );
}
}
And 2nd, is Edward's Gap Zones:
// http://forum.amibroker.com/t/do-not-disqualify-levels-on-the-next-occurrence/5287
GapBullishPctReq = Param( "Bullish Gap Percentage", 4, 0.1, 20, 0.1 );
GapBullishNumBars = Param( "Bullish Bars Since Inception", 300, 5, 1000, 1 );
GfxSetZOrder( -5 );
GfxSetCoordsMode( 1 );
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
Buy = Sell = Short = Cover = 0;
BuyPrice = SellPrice = ShortPrice = CoverPrice = 0;
GapBullishSetup = Open - Ref( Close, -1 ) > Open * GapBullishPctReq * .01;
GapBullishBarCnt = BarsSince( GapBullishSetup );
GapBullishSetupJumpto = ValueWhen( GapBullishSetup, bi, 1 );
SetChartOptions( 0, chartShowDates );
Plot( C, "Close", colorViolet , styleCandle, Null, Null, 0, 0, 1 );
GfxSelectPen( ColorRGB( 0, 80, 0 ), 2, 0 );
GfxSelectSolidBrush( ColorRGB( 0, 30, 0 ) );
for( i = lvb; i > fvb; i-- )
{
if( GapBullishSetup[i] )
{
// initial coordinated
x0top = i - 1;
x1top = Min( i + GapBullishNumBars, BarCount - 1 );
y0top = O[i];
y1top = O[i];
x0bot = i - 1;
x1bot = Min( i + GapBullishNumBars, BarCount - 1 );
y0bot = C[i - 1];
y1bot = C[i - 1];
gaptop = LineArray( x0top, y0top, x1top, y1top );
gapbot = LineArray( x0bot, y0bot, x1bot, y1bot );
// condition when filled
fillCondition = IIf( !IsEmpty( gapbot ), Cross( gapbot, L ) OR L < gapbot, 0 );
fillCondition = fillCondition && SumSince( i == bi, fillCondition ) == 1;
lx = LastValue( ValueWhen( fillCondition, bi ) );
lidx = lx;
if( lx == 0 ) lx = x1bot;
// adjusted coordinates for fill condition
x0top = i - 1;
x1top = lx;
y0top = O[i];
y1top = O[i];
x0bot = i - 1;
x1bot = lx;
y0bot = C[i - 1];
y1bot = C[i - 1];
gaptop = LineArray( x0top, y0top, x1top, y1top );
gapbot = LineArray( x0bot, y0bot, x1bot, y1bot );
GfxRectangle( x0bot, y0bot, x1top, y1top );
//Plot( gaptop, "", ColorRGB( 255, 155, 0 ), styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 1, 2 );
//Plot( gapbot, "", ColorRGB( 0, 155, 255 ), styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 1, 2 );
if( lidx != 0 )
{
Buy[lidx] = 1;
BuyPrice[lidx] = C[lidx];
}
}
i = GapBullishSetupJumpto[i - 1] + 1;
}
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorGreen, 0, L, -15 );
PlotShapes( IIf( Buy, shapeSmallCircle, shapeNone ), colorWhite, 0, BuyPrice, 0 );