my code is based on either the close price of a bar or the H/L of a bar. In your example chart I can not even imagine how such a curve would look like. In my opinion it is just a waste of time. In that case just trade tick charts then you have 1 price per bar. Then you will get every wiggle.
I attach my latest code on this ATR code. More interesting is it for you to figure out good point of entry and exit.
For instance the code that I add is highly profitable if you enter at the price shown in the chart. However, in real trading this is not possible because the signal is only known at the close (in default mode, using the close price). But if you manage to enter and exit (on average) at the price shown in the chart then it is very profitable. Sometimes you can get even a better entry/exit price but not always. So if you want to trade this then you need to find good entry and exit points within the ATR trend
per = Param( "ATR Period", 10, 1, 500, 1 );
mult = Param( "ATR Multiple", 3, 0.5, 20, 0.1 );
priceswitch = ParamToggle( "Select", "Close|High or Low", 0 );
labelsswitch = ParamToggle( "Show Labels", "Off|On", 1 );
showseparator = ParamToggle( "Show Separator", "No|Yes", 0 );
showmacd = ParamToggle( "Show MACD", "No|Yes", 0 );
plottrailswitch = ParamToggle( "Show Trail Line", "No|Yes", 1 );
ft = ParamList( "Font Type", "Tahoma Bold|Arial Black|Verdana Bold|Courier New Bold|Comic Sans MS Bold|Arial Bold|Candara Bold|Calibri Bold|Constantia Bold|Georgia Bold|Gadugi Bold|Segoe UI Bold", 1 );
sz = Param( "Font Size", 10, 8, 50, 1 );
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
pk = tr = Buy = Sell = BuyPrice = SellPrice = pkn = trn = 0;
trailarrayup = trailarraydn = Null;
colorlightblue1 = ColorRGB( 128, 213, 255 );
colorlightorange1 = ColorRGB( 255, 213, 128 );
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;
}
// calculate format for display purposes
fmt = "" + TickSize;
fmt = StrReplace( fmt, ".", "," );
fmt = StrExtract( fmt, 1 );
fmt = "1" + "." + StrLen( fmt );
fmt = StrToNum( fmt );
slip = 0;
// ATR
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 = 0;
topidx = 0;
botprc = 0;
botidx = 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 ) );
}
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", IIf( Outside(), colorAqua, IIf( C > O, colorBlue, IIf( C < O, colorRed, colorLightGrey ) ) ), styleCandle, Null, Null, 0, 0, 1 );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 1 );
//Plot( IIf( !IsEmpty( line1 ), line1, Null ), "", ColorRGB( 255, 255, 0 ), styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, -1, 4 );
Plot( ZigZag, "", IIf( uptrend1, colorLightBlue1, colorLightOrange1 ), styleLine | styleNoRescale | styleNoLabel, Null, Null, 0, 0, 1 );
PlotShapes( IIf( tr, shapeSmallCircle, shapeNone ), colorGreen, 0, L, -10 );
PlotShapes( IIf( pk, shapeSmallCircle, shapeNone ), colorRed, 0, H, 10 );
PlotShapes( IIf( trn, shapeHollowSmallCircle, shapeNone ), ColorRGB( 0, 80, 0 ), 0, L, -25 );
PlotShapes( IIf( pkn, shapeHollowSmallCircle, shapeNone ), ColorRGB( 135, 0, 0 ), 0, H, 25 );
BuyPrice = Max( O, trailarraydn );
SellPrice = Min( O, trailarrayup );
ShortPrice = Min( O, trailarrayup );
CoverPrice = Max( O, trailarraydn );
//BuyPrice = SellPrice = ShortPrice = CoverPrice = C;
// crossing trail indicator
crossup = Cross( C, trailarraydn );
crossdn = Cross( trailarrayup, C );
iBuy = BarsSince( crossdn ) >= 1 AND BarsSince( crossdn ) < BarsSince( crossup ) AND H >= Ref( trailarraydn, -1 ) OR crossup;
iBuyPrice = Max( O, Ref( trailarraydn, -1 ) );
iShort = BarsSince( crossup ) >= 1 AND BarsSince( crossdn ) > BarsSince( crossup ) AND L <= Ref( trailarrayup, -1 ) OR crossdn;
iShortPrice = Min( O, Ref( trailarrayup, -1 ) );
iSell = iShort;
iSellPrice = iShortPrice;
iCover = iBuy;
iCoverPrice = iBuyPrice;
PlotShapes( IIf( iBuy, shapeSmallCircle, shapeNone ) , colorAqua, 0, trailarraydn, 0 );
PlotShapes( IIf( iSell, shapeSmallCircle, shapeNone ), colorGold, 0, trailarrayup, 0 );
PlotShapes( IIf( iCover, shapeSmallCircle, shapeNone ) , colorAqua, 0, trailarraydn, 0 );
PlotShapes( IIf( iShort, shapeSmallCircle, shapeNone ), colorGold, 0, trailarrayup, 0 );
if( plottrailswitch )
{
Plot( trailarrayup, "trail up", colorLightBlue1, styleLine | styleStaircase | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( trailarraydn, "trail dn", colorLightOrange1, styleLine | styleStaircase | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( trailarrayup, "trail up", colorLightBlue1, styleLine | styleStaircase | styleNoRescale | styleNoLabel, Null, Null, 0, 1, 1 );
Plot( trailarraydn, "trail dn", colorLightOrange1, styleLine | styleStaircase | styleNoRescale | styleNoLabel, Null, Null, 0, 1, 1 );
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorLightBlue1, 0, L, -15 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorLightOrange1, 0, H, -15 );
PlotShapes( IIf( Buy, shapeSmallSquare, shapeNone ), colorLightBlue1, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeSmallSquare, shapeNone ), colorLightOrange1, 0, SellPrice, 0 );
}
Cover = Buy;
Short = Sell;
longResult = round( IIf( Sell, SellPrice - ValueWhen( Buy, BuyPrice ), 0 ) / TickSize ) * TickSize;
shortResult = round( IIf( Cover, ValueWhen( Short, ShortPrice ) - CoverPrice, 0 ) / TickSize ) * TickSize;
clr = colorDefault;
shiftLabels = 10;//Param( "Shift Labels", 12, -20, 100, 1 );
upColor = colorLightBlue1;
dnColor = colorLightOrange1;
if( labelsswitch )
{
str1 = "";
str2 = "";
for( i = fvb; i <= lvb; i++ )
{
if( pk[i] )
{
if( dt[i] )
str1 = "DT";
if( hh[i] )
str1 = "HH";
if( lh[i] )
str1 = "LH";
PlotTextSetFont( str1, ft, sz, i, H[i], colorRed, clr, sz * 3 );
}
if( tr[i] )
{
str1 = "";
if( db[i] )
str1 = "DB";
if( ll[i] )
str1 = "LL";
if( hl[i] )
str1 = "HL";
PlotTextSetFont( str1, ft, sz, i, L[i], colorBlue, clr, -sz * 4 );
}
if( Buy[i] && Cover[i] )
{
str = "Buy: " + BuyPrice[i];// + " (" + shortResult[i] + ")";
PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -35 - shiftLabels );
str = "Cover: " + CoverPrice[i] + " (" + shortResult[i] + ")";
PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -50 - shiftLabels );
}
if( !Buy[i] && Cover[i] )
{
str = "Cover: " + CoverPrice[i] + " (" + shortResult[i] + ")";
PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -35 - shiftLabels );
}
if( Buy[i] && !Cover[i] )
{
str = "Buy: " + BuyPrice[i];
PlotTextSetFont( str, ft, sz, i, L[i], upColor, clr, -35 - shiftLabels );
}
if( Short[i] && Sell[i] )
{
str = "Short: " + ShortPrice[i];// + " (" + longResult[i] + ")";
PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 25 + shiftLabels );
str = "Sell: " + SellPrice[i] + " (" + longResult[i] + ")";
PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 40 + shiftLabels );
}
if( !Short[i] && Sell[i] )
{
str = "Sell: " + SellPrice[i] + " (" + longResult[i] + ")";
PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 25 + shiftLabels );
}
if( Short[i] && !Sell[i] )
{
str = "Short: " + ShortPrice[i];
PlotTextSetFont( str, ft, sz, i, H[i], dnColor, clr, 25 + shiftLabels );
}
}
}
// back test for futures
SetTradeDelays( 0, 0, 0, 0 );
SetOption( "FuturesMode", True );
SetOption( "PriceBoundChecking", False );
SetOption( "AllowSameBarExit", True );
SetOption( "CommissionMode", 3 );
SetOption( "CommissionAmount", 4 );
SetPositionSize( 1, spsShares );
/*
iBuy = Buy;
iSell = Sell;
iShort = Short;
iCover = Cover;
Short = iBuy;
Cover = iSell;
Buy = iShort;
Sell = iCover;
BuyPrice = SellPrice = ShortPrice = CoverPrice = C;
*/