Australian (ASX) Stock Market Forum

Amibroker - multiple conditions on trailing stop

Joined
13 January 2013
Posts
258
Reactions
50
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" );
RestorePriceArrays(True);
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
BMT_Up==1;

// 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 );
 
Hi rb --

With so many conditions and variable exit prices, writing the trailing exit using looping code will be much clearer, and will give you the tools to plot the exit price.

Regards,
Howard
 
Howard - I have constructed the exit with a for loop (code I ripped from the AB website). Is this what you mean? When I try to add in my other ' if ' conditions it goes a bit hay wire for me. I did a bit of programming at uni a few years back and for and while loops were something I never quite got a grasp on.
I did figure out the bollinger band exit code last night and that is outside the loop given it is a global exit parameter.
 
Hi RB --

I recommend a single loop through all the bars. At each index, compute the distance of all the exit prices. Used a stop-type execution to sell the long position.

A program fragment that uses a stop-type exit would be:

...
...
exitPrice = xxx;
Sell = 0;
...
for (i = 1;i<BarCount;i++)
{
if(l <= exitPrice)
{
Sell = 1;
SellPrice = exitPrice;
}
}


There is a lot more to the program, but this should help.

Howard
 
Below is my attempt to set up loop that looks at each bar.

MinStopLevel = 0.7;
TightStopLevel = 0.9;

trailARRAY = Null;
TrailStop = 0;

for( i = 1; i < BarCount; i++ )
{

if( TrailStop == 0 AND Buy[ i ] )
{
MinStop = C[ i ] * MinStopLevel;
TrailStop = MinStop;

if( TrailStop > 0 AND Buy[ i ] BMT_Up == 1 )
{
MinStop = C[ i ] * MinStopLevel;
VolatilityStop = BBandBot( C[ i ], range = 20, width = 2 );
TrailStop = Max( MinStop, VolatilityStop );

if( TrailStop > 0 AND Buy[ i ] AND BMT_Down == 1 )
{
TightStop = C[ i ] * TightStopLevel;
VolatilityStop = BBandBot( C[ i ], range = 20, width = 2 );
TrailStop = Max( TightStop, VolatilityStop );

}
else Buy[ i ] = 0;

if( TrailStop > 0 AND C[ i ] < TrailStop )
{
Sell[ i ] = 1;
SellPrice[ i ] = trailstop;
TrailStop = 0;
}

if( TrailStop > 0 )
{
MinStop = C[ i ] * MinStopLevel;
TrailStop = Max( MinStop, TrailStop );
trailARRAY[ i ] = TrailStop;
}

}

Plot( trailARRAY, "Trailing Stop Level", colorRed );



Obyiously this doesn't work and I'm totally lost. The last If statement doesn't even take into consideration the volatility stop I want.
 
To simplify things a little, I have decided to drop the volatility stop and just keep the index filter switching.

TrailARRAY = Null;
TrailStop = 0;

for( i = 1; i < BarCount; i++ )
{
if( TrailStop == 0 AND Buy[ i ] )
{
StopLevel = IIf( BMT[ i ] == 1, 0.7 * C[ i ], 0.9 * C[ i ] );
TrailStop = StopLevel;
}
else Buy[ i ] = 0;

if( TrailStop > 0 AND C[ i ] < TrailStop )
{
Sell[ i ] = 1;
SellPrice[ i ] = TrailStop;
TrailStop = 0;
}

if( TrailStop > 0 )
{
TrailStop = Max( StopLevel, TrailStop );
TrailARRAY[ i ] = TrailStop;
}
}


This code still isnt working, but atleast doesn't throw me an error. It seems to have a stop at one level that doesnt move, can anyone help?
 
Hi rb...

Make sure you are checking the value of the TrailingStop on the previous bar to the Close of the current bar otherwise the Close could never be below the TrailingStop!
 
Top