I am charting buys with plot for “HighestsinceBuy” and “trailing stop”
The code I am using has date conditions which set out when a buy is done.
This all works okay if I have a single buy date and exit date. However if I introduce a second buy condition the trail stop plot disappears, however the “highestsincebuy” plot remains.This can be seen on these charts
- ETHI.au
and - SYA.au
I as hoping that someone could help me to identify why the trail stop for SYA.au doesn't plot, but is does plot for ETHI.au
The code I am using is
SystemName = StrExtract(StrExtract(GetFormulaPath(),-1,'\\'),-2,'.');
StaticVarSetText("SystemName", SystemName);
_SECTION_BEGIN( "TEST" );
TradingFunds = Param( "Trading Funds - $", 15000, 1000, 100000, 1000 );
SetOption( "InitialEquity", 100000 );
SetOption( "PriceBoundChecking", 1 );
SetOption( "CommissionMode", 2 );
SetOption( "CommissionAmount", 9.5 );
SetOption( "UsePrevBarEquityForPosSizing", 1 );
SetOption( "AllowSameBarExit", False );
SetTradeDelays( 1, 1, 1, 1 );
ED2 = 1251231;
dn = DateNum();
switch( Name() ) {
case "ETHI.au":
enddate1 = 1240701;
datecond = ( dn >= 1240301 AND dn < enddate1 );
exitdatecond = dn >= enddate1;
break;
case "SYA.au":
enddate1 = 1240628;
Reentry = 1240801;
enddate2 = 1241001;
datecond = ( dn >= 1240324 AND dn < enddate1 ) OR ( dn >= reentry AND dn < enddate2 );
exitdatecond1 = dn >= enddate1 AND dn < reentry;
exitdatecond2 = dn >= enddate2;
exitdatecond = exitdatecond1 OR exitdatecond2; break;
default:
datecond = true;
exitdatecond = false;
break;
}
Buy = datecond;
Sell = exitdatecond;
buy = ExRem( buy, sell );
highsinceBuy = HighestSince( Buy, High );
//=================================================================================
//Two-stage trailing stop ==> amended to looping stop
//=================================================================================
StopLevel= 4*ATR(14);
trailARRAY = Null;
trailstop = 0;
TrailStopSell = 0;
for( i = 1; i < BarCount; i++ )
{
if( trailstop == 0 AND Buy[ i ] )
{
trailstop = High[ i ] - stoplevel [i];
}
if( trailstop > 0 AND TrailStopSell [i] == 0)
{
trailstop = Max( High[ i ] - stoplevel [i], trailstop );
trailARRAY[ i ] = trailstop;
}
if( trailstop > 0 AND Close[ i ] < trailstop )
{
TrailStopSell[ i ] = 1;
SellPrice[ i ] = trailstop;
trailstop = 0;
}
}
//=================================================================================
//Sell condition
//=================================================================================
SellCond1=Ref(TrailStopSell,0);
SellCond2= exitdatecond;
Sell= SellCond1 OR SellCond2;
Sell = ExRem( Sell, Buy );
BuyPrice = Open;
SellPrice = Open;
BuyPrice = O;
SellPrice = O;
_SECTION_END();
//=================================================================================
//Code to the chart & plots the signals.
//=================================================================================
_SECTION_BEGIN( "Price" );
SetChartOptions( 0, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - " + FullName() + " - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " + WriteVal( V, 1.0 ) + " {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) ); // Chart settings
Plot( C, "Close", ParamColor( "Color", colorBlack ),
ParamStyle( "Style", styleNoTitle | styleBar, maskAll ) );
PlotShapes( Buy*shapehollowUpArrow, colorWhite, 0, Low, -20 );
PlotShapes( ( Sell > 0 ) * shapeDownArrow, Coloryellow, 0, High, -40 );
PlotShapes( Ref( Buy, -1 ) * shapeHollowSquare, colorWhite, 0, O, 0, 0 );
PlotShapes( Ref( Sell, -1 ) * shapeHollowCircle, colorYellow, 0, O, 0, 0 );
for( i = 1; i < BarCount; i++ )
{
if( Buy[i - 1] ) PlotText( "Buy\n@ " + O[i], i, L[i] * 0.9, colorWhite );
if( sell[i - 1] ) PlotText( "Sell\n@ " + o[ i ], i, H[ i ] * 1.1, colorYellow );
}
//=================================================================================
//Plot Trail Stop & High Since Buy
//=================================================================================
PlotHighSinceBuy = Flip(Buy , Sell );
Plot( IIf( PlotHighSinceBuy, highsinceBuy,Null), "highsinceBuy", colorBrightGreen, styleDashed );
PlotTrailStop = Flip(Buy , Sell );
Plot( IIf( PlotTrailStop, trailARRAY, Null), "Trailing Stop", ColorRed, styleLine );
_SECTION_END();
//-- Top Left Corner
GfxSetBkMode( 1 );
GfxSelectFont("COMIC SANS MS", 15, 400, False, False, 0);
GfxSetTextColor(colorWhite);
GfxTextOut( "TRAILSTOP ISSUE", 10, 30);
Thanks for any help