I'm hoping someone can offer me some help to code up a trailing stop with a few conditions. I have had very limited experience with programming and am struggling with this one, the rest of my code is working well, i.e. buy signal etc. I am trying to build a longer term trading system based on end of day data. I'm also very new to trading so some of my parameters may seem a little whacky, these will be changed at a later time when I have more experience. I'll explain what I want to do.
I am using an overall market trend filter; when the MA is rising, I will take the buy signals. When the MA is falling, I will not take the buy signals. This part I have working.
Now for the stop:
1. After making a buy, I want to set a trailing stop at 70% of the previous days close price.
2. If the overall market trend filter turns down, I want to shift the stop up to 90% of the previous days close price.
3. If the overall market trend filter turns back up, I want to shift the stop back to 70% of the previous days close price. The condition here is I want to let the 90% * Close stop to stay at that value until it equals the 70% stop, then the 70% stop takes over again. So, TrailStop = (max(C*0.9 when the overall market trend turned up, C*0.70).
4. Set a stop over the whole trade based on a lower bollinger band (parameters yet to be determined).
5. Obviously, when the closing price crosses below any of these stops sell the next day.
I would like to plot a line representing the trailing stop excluding the bollinger band. I want to plot the bollinger band separately.
I have the system working but only with the 70% * Close trailing stop. I am baffled as to how to write the code for the remainder of the stop conditions. Any help would be really appreciated.
Code as it stands:
BMT_Period = 200;
// Broader Market Trend
BMT_Data = Foreign( "^AORD", "C" );
BMT_Up = BMT_Data > MA( BMT_Data, BMT_Period );
BMT_Down = BMT_Data < MA( BMT_Data, BMT_Period );
// Buy Signals
BSig_Close = HHV( Ref( C, -1 ), 260 );
Buy = C > BSig_Close AND
// Sell Signals
MinStopLevel = 0.7;
TighterStopLevel = 0.9;
Sell = 0;
trailARRAY = Null;
TrailStop = 0;
for( i = 1; i < BarCount; i++ )
if( TrailStop == 0 AND Buy[ i ] )
TrailStop = C[ i ] * MinStopLevel;
else Buy[ i ] = 0;
if( TrailStop > 0 AND C[ i ] < TrailStop )
Sell[ i ] = 1;
SellPrice[ i ] = O[ i ];
TrailStop = 0;
if( TrailStop > 0 )
TrailStop = Max( C[ i ] * MinStopLevel, TrailStop );
trailARRAY[ i ] = TrailStop;
Plot( trailARRAY,"Trailing Stop Level", colorRed );
Buy = ExRem ( Buy,Sell );
Sell = ExRem ( Sell,Buy );
// Graph Plotting
Plot( 1, "", IIf( BMT_Up, colorGreen, IIf( BMT_Down, colorRed, colorDarkGrey )), styleOwnScale | styleArea | styleNoLabel, -0.5, 100 );
Plot( BSig_Close, "Buy", colorGreen, styleDashed );
BBL = BBandBot( C, range = 30, width = 3 );
Plot( BBL, "BB Stop", colorBlue, styleDashed );
PlotShapes( IIf( Buy==1, shapeUpArrow, shapeNone ), colorGreen, 0, Low, Offset=-15 );
PlotShapes( IIf( Sell==1, shapeDownArrow, shapeNone ), colorRed, 0, Low, Offset=-15 );