This post was flagged by the community and is temporarily hidden.
your post is a bit messy. Below my latest code on this. So earliest point you are in an uptrend is at crossup at the price bp. Earliest point of downtrend is crossdn at the price sp.
nbar = Param( "Swing number of Bars", 10, 1, 50, 1 );
priceswitch = ParamList( "Choose Mode", "Close|High&Low|Mixed 1|Mixed 2", 2 );
showTrailline = ParamToggle( "Show Trail Line", "Off|On", 0 );
showprimarypivotlabels = ParamToggle( "Show Primary Pivot Labels", "Off|On", 0 );
handleBothOutside = ParamToggle( "Long AND Short signal at the SAME bar", "Change Trend|Stay with current Trend", 0 );
sz = Param( "Text Size", 10, 5, 22, 1 );
ft = ParamList( "Font", "Tahoma|Helvetica|Arial Black|Verdana|Courier New|Compact|Segoe UI", 4 );
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
pk = tr = 0;
ZigZag = line1 = Null;
prcl = prch = crossup = crossdn = 0;
if( priceswitch == "High&Low" )
{
prch = H;
prcl = L;
mode = "High/Low mode";
// find trend
res = HHV( prch, nbar );
sup = LLV( prcl, nbar );
crossup = prch > Ref( res, -1 );
crossdn = prcl < Ref( sup, -1 );
// trail line price
bp = Max( O, Ref( res, -1 ) );
sp = Min( O, Ref( sup, -1 ) );
}
if( priceswitch == "Close" )
{
prch = C;
prcl = C;
mode = "Close mode";
// find trend
res = HHV( prch, nbar );
sup = LLV( prcl, nbar );
crossup = prch > Ref( res, -1 );
crossdn = prcl < Ref( sup, -1 );
// trail line price
bp = C;
sp = C;
}
if( priceswitch == "Mixed 1" )
{
prch = H;
prcl = L;
mode = "Mixed mode 1";
// find trend
res = HHV( prch, nbar );
sup = LLV( prcl, nbar );
crossup = C > Ref( res, -1 );
crossdn = C < Ref( sup, -1 );
// trail line price
bp = C;
sp = C;
}
if( priceswitch == "Mixed 2" )
{
prch = C;
prcl = C;
mode = "Mixed mode 2";
// find trend
res = HHV( prch, nbar );
sup = LLV( prcl, nbar );
crossup = H > Ref( res, -1 );
crossdn = L < Ref( sup, -1 );
// trail line price
bp = Max( O, Ref( res, -1 ) );
sp = Min( O, Ref( sup, -1 ) );
}
function calculateZigZag()
{
// 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 );
}
function calculateLastSegments()
{
lastidxpk = LastValue( ValueWhen( pk, bi ) );
lastidxtr = LastValue( ValueWhen( tr, bi ) );
lastvalpk = LastValue( ValueWhen( pk, prch ) );
lastvaltr = LastValue( ValueWhen( tr, prcl ) );
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( 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( 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 ) );
}
ZigZag = IIf( !IsEmpty( line1 ), line1, ZigZag );
}
// is used in High&Low mode only
// if both crossup and crossdn are true then,
// when handleBothOutside is true then it will stay in the current trend
// when handleBothOutside is false it will change the trend (default)
if( handleBothOutside )
{
crossup = IIf( ( crossup AND crossdn ) AND( BarsSince( Ref( crossup, -1 ) ) > BarsSince( Ref( crossdn, -1 ) ) ), 0, crossup );
crossdn = IIf( ( crossup AND crossdn ) AND( BarsSince( Ref( crossdn, -1 ) ) > BarsSince( Ref( crossup, -1 ) ) ), 0, crossdn );
}
crossup = ExRem( crossup, crossdn );
crossdn = ExRem( crossdn, crossup );
trendup = Flip( crossup, crossdn );
trenddn = Flip( crossdn, crossup );
// find pivots
rtrenddn = Reverse( trenddn );
rtrendup = Reverse( trendup );
l1 = LowestSince( trenddn AND Ref( trendup, -1 ), prcl );
rL = Reverse( prcl );
l2 = LowestSince( rtrenddn AND Ref( rtrendup, -1 ), rL );
rl2 = Reverse( l2 );
h1 = HighestSince( trendup AND Ref( trenddn, -1 ), prch );
rH = Reverse( prch );
h2 = HighestSince( rtrendup AND Ref( rtrenddn, -1 ), rH );
rh2 = Reverse( h2 );
tr = l1 == rl2 AND trenddn;
pk = h1 == rh2 AND trendup;
rpk = Reverse( pk );
rtr = Reverse( tr );
rpk = ExRem( rpk, rtr );
rtr = ExRem( rtr, rpk );
pk = Reverse( rpk );
tr = Reverse( rtr );
calculateZigZag();
calculateLastSegments();
SetChartBkColor( ColorRGB( 0, 0, 0 ) );
SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "Price", colorWhite, styleCandle, Null, Null, 0, 0, 0 );
if( showtrailline )
{
Plot( IIf( trendup, sup, Null ), "", colorGreen, styleStaircase | styleNoRescale, Null, Null, 0, 0, 1 );
Plot( IIf( trenddn, res, Null ), "", colorRed, styleStaircase | styleNoRescale, Null, Null, 0, 0, 1 );
PlotShapes( IIf( trendup AND Ref( trenddn, -1 ), shapeSmallCircle, shapeNone ), colorBrightGreen, 0, bp, 0 );
PlotShapes( IIf( trenddn AND Ref( trendup, -1 ), shapeSmallCircle, shapeNone ), ColorGold, 0, sp, 0 );
}
PlotShapes( IIf( trendup AND Ref( trenddn, -1 ), shapeSmallCircle, shapeNone ), colorWhite, 0, bp, 0 );
PlotShapes( IIf( trenddn AND Ref( trendup, -1 ), shapeSmallCircle, shapeNone ), colorWhite, 0, sp, 0 );
Plot( zigzag, "ZigZag", colorGold, styleLine | styleNoLabel | styleNoRescale, Null, Null, 0, 1, 1 );
PlotShapes( IIf( tr, shapeSmallCircle, shapeNone ), colorGreen, 0, L, -10 );
PlotShapes( IIf( pk, shapeSmallCircle, shapeNone ), colorRed, 0, H, 10 );
if( showprimaryPivotLabels )
{
clr = colorDefault;
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, prch, i ) );
VarSet( "tl" + i, ValueWhen( tr, prcl, 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;
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 );
}
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], colorBlue, clr, -sz * 4 );
}
}
}
Title = Name() +
" | " + Now( 1 ) + " | " + Now( 2 ) +
" | " + EncodeColor( colorAqua ) + priceswitch + EncodeColor( colorWhite ) +
" | " + EncodeColor( colorLightOrange ) + "nbar: " + nbar + EncodeColor( colorWhite ) +
" | " + EncodeColor( colorGold ) + "Chart time frame (Min): " + Prec( Interval() / 60, 0 ) + " Min" + EncodeColor( colorWhite ) +
" | " + EncodeColor( colorWhite ) + Prec( ( C - Ref( C, -1 ) ) / Ref( C, -1 ) * 100, 2 ) + " %";
/*
// back test for futures
SetTradeDelays( 0, 0, 0, 0 );
SetOption( "FuturesMode", True );
SetOption( "PriceBoundChecking", False );
SetOption( "AllowSameBarExit", True );
SetOption( "CommissionMode", 3 );
SetOption( "CommissionAmount", 2.01 );
SetPositionSize( 1, spsShares );
Buy = crossup;
BuyPrice = bp;
Short = crossdn;
ShortPrice = sp;
Sell = Short;
SellPrice = ShortPrice;
Cover = Buy;
CoverPrice = BuyPrice;
PlotShapes( IIf( Buy, shapeSmallUpTriangle, shapeNone ), colorAqua, 0, L, -20 );
PlotShapes( IIf( Buy, shapeSmallSquare, shapeNone ) , colorAqua, 0, BuyPrice, 0 );
PlotShapes( IIf( Sell, shapeSmallDownTriangle, shapeNone ), colorGold, 0, H, -20 );
PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone ), colorGold, 0, SellPrice, 0 );
PlotShapes( IIf( Cover, shapeSmallUpTriangle, shapeNone ), colorAqua, 0, L, -20 );
PlotShapes( IIf( Cover, shapeSmallSquare, shapeNone ) , colorAqua, 0, CoverPrice, 0 );
PlotShapes( IIf( Short, shapeSmallDownTriangle, shapeNone ), colorGold, 0, H, -20 );
PlotShapes( IIf( Short, shapeSmallSquare, shapeNone ), colorGold, 0, ShortPrice, 0 );
*/
3 Likes
The OP post is messy because he did not use REQUIRED code tags. When posting the formula, please make sure that you use Code Tags (using </>
code button) as explained here: How to use this site.
Code tags are required so formulas can be properly displayed and copied without errors.