ok yes on the edge. I took some other code and took some code out that I thought was not necessary. Maybe see if this version is better
SetBarsRequired( sbrAll, sbrAll );
GfxSetZOrder( -5 );
GfxSetCoordsMode( 1 );
per = Param( "ATR Period", 10, 1, 500, 1 );
mult = Param( "ATR Multiple", 3, 0.1, 100, 0.1 );
priceswitch = ParamToggle( "Select", "Close|High or Low", 0 );
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
pk = tr = Buy = Sell = BuyPrice = SellPrice = pkn = trn = 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;
}
// ATR
maatrper = mult * ATR( per );
//boxpct = Param("Box Pct", 7, 0.25, 30, 0.25);
//maatrper = C * boxpct / 100;
sup = round( ( prch - maatrper ) / TickSize ) * TickSize;
res = round( ( prcl + maatrper ) / TickSize ) * TickSize;
trend = 1;
topprc = 0;
topidx = 0;
botprc = 0;
botidx = 0;
slip = 0;
for( i = per + 1; i < BarCount; i++ )
{
if( trend > 0 )
{
// trend turning down, avoid same bar as the peak
if( prcl[i] < trailarrayup[i - 1] ) //AND prch[i] < topprc )
{
pk[topidx] = 1;
botprc = prcl[i];
botidx = i;
trend = -1;
Sell[i] = 1;
SellPrice[i] = C[i] - slip;
trailarraydn[i] = res[i];
trailarrayup[i] = trailarrayup[i - 1];
}
else
// still in uptrend but new top reached
if( prch[i] >= topprc )
{
topprc = prch[i];
topidx = i;
trailarrayup[i] = Max( trailarrayup[i - 1], sup[i] );
pkn[i] = 1;
}
// continuation inside uptrend
else
{
trailarrayup[i] = Max( trailarrayup[i - 1], sup[i] );
}
}
else
if( trend < 0 )
{
// trend turning up, avoid same bar as the trough
if( prch[i] > trailarraydn[i - 1] )// AND prcl[i] > botprc )
{
tr[botidx] = 1;
topprc = prch[i];
topidx = i;
trend = 1;
Buy[i] = 1;
BuyPrice[i] = C[i] + slip;
trailarrayup[i] = sup[i];
trailarraydn[i] = trailarraydn[i - 1];
}
else
// still in downtrend but new trough reached
if( prcl[i] <= botprc )
{
botprc = prcl[i];
botidx = i;
trailarraydn[i] = Min( trailarraydn[i - 1], res[i] );
trn[i] = 1;
}
// continuation inside downtrend
else
{
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 ) );
}
SetChartBkColor( ColorRGB( 0, 0, 0 ) );
xx = SparseCompress( pk OR tr, IIf( pk, prch, prcl ) );
Plot( xx, "", colorwhite, styleDots, Null, Null, 0, 1, 1 );
for( i = lvb; i > fvb; i-- )
{
if( !IsEmpty( xx[i] ) AND xx[i] > xx[i - 1] )
{
GfxSelectSolidBrush( colorBlue );
GfxRectangle( i - 0.5 , xx[i], i + 0.5, xx[i - 1] );
}
if( !IsEmpty( xx[i] ) AND xx[i] < xx[i - 1] )
{
GfxSelectSolidBrush( colorRed );
GfxRectangle( i - 0.5 , xx[i], i + 0.5, xx[i - 1] );
}
}
Title = "ATR VALUE: " + maatrper;