below the code where I added the cumulative volume
// Amibroker code, ATR pivots + trail
// Aug 2020, E.M.Pottasch
per = Param( "ATR Period", 50, 1, 100, 1 ); // you need at least "per" data points to run this code
mult = Param( "ATR Multiple", 3, 1, 20, 0.1 );
priceswitch = ParamToggle( "Use Close or High and Low price", "Use Close|Use High and Low", 1 );
labelsswitch = ParamToggle( "Show Labels", "Off|On", 1 );
plottrailswitch = ParamToggle( "Show Trail Line", "No|Yes", 1 );
sz = Param( "Text Size", 8, 5, 20, 1 );
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
pk = tr = Buy = Sell = BuyPrice = SellPrice = 0;
trailarrayup = trailarraydn = Null;
if( priceswitch )
{
prch = H;
prcl = L;
}
else
{
prch = C;
prcl = C;
}
// if ticksize is not set in code or information window use 0.01
if( TickSize == 0 )
{
TickSize = 0.01;
}
sup = round( ( prch - mult * ATR( per ) ) / TickSize ) * TickSize;
res = round( ( prcl + mult * ATR( per ) ) / TickSize ) * TickSize;
trend = 1; // start trend at 1 or -1 doesnt matter
topprc = prch[per];
topidx = 0;
botprc = prcl[per];
botidx = 0;
trailarrayup[per] = sup[per];
trailarraydn[per] = res[per];
for( i = per + 1; i < BarCount; i++ )
{
if( trend > 0 )
{
if( prcl[i] <= trailarrayup[i - 1] AND prch[i] < topprc )
{
// trend turning down, avoid same bar as the peak
pk[topidx] = 1;
botprc = prcl[i];
botidx = i;
trend = -1;
Sell[i] = 1;
trailarraydn[i] = res[i];
trailarrayup[i] = trailarrayup[i - 1];
if( priceswitch )
SellPrice[i] = Min( O[i], trailarrayup[i] );
else
SellPrice[i] = C[i];
}
else
if( prch[i] >= topprc )
{
// still in uptrend but new top reached
topprc = prch[i];
topidx = i;
trailarrayup[i] = Max( trailarrayup[i - 1], sup[i] );
}
else
{
// continuation inside uptrend
trailarrayup[i] = Max( trailarrayup[i - 1], sup[i] );
}
}
else
if( trend < 0 )
{
if( prch[i] >= trailarraydn[i - 1] AND prcl[i] > botprc )
{
// trend turning up, avoid same bar as the trough
tr[botidx] = 1;
topprc = prch[i];
topidx = i;
trend = 1;
Buy[i] = 1;
trailarrayup[i] = sup[i];
trailarraydn[i] = trailarraydn[i - 1];
if( priceswitch )
BuyPrice[i] = Max( O[i], trailarraydn[i] );
else
BuyPrice[i] = C[i];
}
else
if( prcl[i] <= botprc )
{
// still in downtrend but new trough reached
botprc = prcl[i];
botidx = i;
trailarraydn[i] = Min( trailarraydn[i - 1], res[i] );
}
else
{
// continuation inside downtrend
trailarraydn[i] = Min( trailarraydn[i - 1], res[i] );
}
}
}
line1 = Null;
lastidxpk = LastValue( ValueWhen( pk, bi ) );
lastidxtr = LastValue( ValueWhen( tr, bi ) );
lastvalpk = LastValue( ValueWhen( pk, prch ) );
lastvaltr = LastValue( ValueWhen( tr, prcl ) );
lastidxbuy = LastValue( ValueWhen( Buy, bi ) );
lastidxsell = LastValue( ValueWhen( Sell, bi ) );
valpk = LastValue( HighestSince( Ref( tr, -1 ), prch , 1 ) );
idxpk = LastValue( ValueWhen( prch == valpk, bi ) );
valtr = LastValue( LowestSince( Ref( pk, -1 ), prcl, 1 ) );
idxtr = LastValue( ValueWhen( prcl == valtr, bi ) );
if( lastidxsell > lastidxbuy AND lastidxsell > lastidxpk AND lastidxpk > lastidxtr )
{
x0 = lastidxpk;
y0 = lastvalpk;
x1 = idxtr;
y1 = valtr;
line1 = linedn = LineArray( x0, y0, x1, y1 );
tr[idxtr] = 1;
valpk = LastValue( HighestSince( Ref( tr, -1 ), prch, 1 ) );
idxpk = LastValue( ValueWhen( prch == valpk, bi ) );
}
if( lastidxsell < lastidxbuy AND lastidxbuy > lastidxtr AND lastidxpk < lastidxtr )
{
x0 = lastidxtr;
y0 = lastvaltr;
x1 = idxpk;
y1 = valpk;
line1 = lineup = LineArray( x0, y0, x1, y1 );
pk[idxpk] = 1;
valtr = LastValue( LowestSince( Ref( pk, -1 ), prcl, 1 ) );
idxtr = LastValue( ValueWhen( prcl == valtr, bi ) );
}
for( i = 0; i < 3; i++ )
{
VarSet( "px" + i, ValueWhen( pk, bi, i ) );
VarSet( "tx" + i, ValueWhen( tr, bi, i ) );
VarSet( "ph" + i, ValueWhen( pk, H, i ) );
VarSet( "tl" + i, ValueWhen( tr, L, i ) );
}
ll = tr AND tl1 < tl2;
hl = tr AND tl1 > tl2;
hh = pk AND ph1 > ph2;
lh = pk AND ph1 < ph2;
dt = pk AND ph1 == ph2;
db = tr AND tl1 == tl2;
// create ZIGZAG array line
zigup = Flip( tr, pk );
zigupLow = ValueWhen( tr, prcl, 1 );
zigupHigh = ValueWhen( pk, prch, 0 );
zigupLowIndex = ValueWhen( tr, bi, 1 );
zigupHighIndex = ValueWhen( pk, bi, 0 );
slopeup = IIf( zigup, ( zigupHigh - zigupLow ) / ( zigupHighIndex - zigupLowIndex ) , Null );
zigupLine = IIf( zigup, zigupLow + slopeup * BarsSince( tr ), Null );
zigdn = Flip( pk, tr );
zigdnLow = ValueWhen( tr, prcl, 0 );
zigdnHigh = ValueWhen( pk, prch, 1 );
zigdnLowIndex = ValueWhen( tr, bi, 0 );
zigdnHighIndex = ValueWhen( pk, bi, 1 );
slopedn = IIf( zigdn, ( zigdnLow - zigdnHigh ) / ( zigdnLowIndex - zigdnHighIndex ) , Null );
zigdnLine = IIf( zigdn, zigdnHigh + slopedn * BarsSince( pk ), Null );
ZigZag = IIf( zigup, zigupLine, IIf( zigdn, zigdnLine, Null ) );
ZigZag = IIf( bi > Max( LastValue( ValueWhen( tr, bi ) ), LastValue( ValueWhen( pk, bi ) ) ), Null, ZigZag );
ZigZag = IIf( !IsEmpty( line1 ), line1, ZigZag );
uptrend1 = Flip( Buy, Sell );
dntrend1 = Flip( Sell, Buy );
trn = LowestSince( pk, prcl ) == prcl && dntrend1;
pkn = HighestSince( tr, prch ) == prch && uptrend1;
SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 0 );
Plot( IIf( !IsEmpty( line1 ), line1, Null ), "", ColorRGB( 255, 255, 0 ), styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 2 );
Plot( ZigZag, "", IIf( uptrend1, ColorRGB( 0, 0, 255 ), ColorRGB( 255, 0, 0 ) ), styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 2 );
PlotShapes( shapeSmallDownTriangle * trn, ColorRGB( 100, 0, 0 ), 0, L, 10 );
PlotShapes( shapeSmallUpTriangle * pkn, ColorRGB( 0, 60, 0 ), 0, H, 10 );
PlotShapes( shapeSmallCircle * tr, colorGreen, 0, L, -10 );
PlotShapes( shapeSmallCircle * pk, colorRed, 0, H, 10 );
if( plottrailswitch )
{
Plot( trailarrayup, "trail up", colorLightBlue, styleDots | styleNoRescale | styleNoLabel, Null, Null, 0, 1, 1 );
Plot( trailarraydn, "trail dn", colorOrange, styleDots | styleNoRescale | styleNoLabel, Null, Null, 0, 1, 1 );
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorLightBlue, 0, L, -15 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorOrange, 0, H, -15 );
PlotShapes( IIf( Buy, shapeSmallSquare, shapeNone ), colorYellow, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeSmallSquare, shapeNone ), colorYellow, 0, SellPrice, 0 );
}
// volume
CumVolumeUpMBoxChart = SumSince( tr, Volume );
CumVolumeDnMBoxChart = SumSince( pk, Volume );
ft = "Arial Black";
clr = colorDefault;
//sz = 7;
GfxSetZOrder( -1 );
GfxSetCoordsMode( 1 );
if( labelsswitch )
{
str1 = "";
for( i = fvb; i <= lvb; i++ )
{
if( pk[i] )
{
if( dt[i] )
str1 = "DT";
else
if( hh[i] )
str1 = "HH";
else
if( lh[i] )
str1 = "LH";
PlotTextSetFont( str1, ft, sz, i, H[i], colorRed, clr, sz * 3 );
PlotTextSetFont( "" + prch[i], ft, sz, i, H[i], colorRed, clr, sz * 5 );
PlotTextSetFont( "" + CumVolumeUpMBoxChart[i], ft, sz, i, H[i], colorBlue, colorWhite, sz * 7 );
}
if( tr[i] )
{
str1 = "";
if( db[i] )
str1 = "DB";
else
if( ll[i] )
str1 = "LL";
else
if( hl[i] )
str1 = "HL";
PlotTextSetFont( str1, ft, sz, i, L[i], colorGreen, clr, -sz * 4 );
PlotTextSetFont( "" + prcl[i], ft, sz, i, L[i], colorGreen, clr, -sz * 6 );
PlotTextSetFont( "" + CumVolumeDnMBoxChart[i], ft, sz, i, L[i], colorRed, colorWhite, -sz * 8 );
}
}
}