Help Coding a Stale EXIT

I have a weekly strategy with a looping Trailing stop that is dependent on an Index Filter.

When the Index Filter is UP my stoploss is 63%

When the Index Filter is DOWN my stoploss shortens to 26%

As my stoploss is generous it has a tendency to keep the trade open for extended periods of time – so if the stock trends sideway for a lengthy period I'm trying to have an exit generated – if or when the trend goes stale.

My code so far..

``````/////Trailing Stop
StopLevelUP = 1 - Param( "Trailing Stop %", 63, 1, 100, 0.1 ) / 100; //63% Trailing Stop Index is Up
StopATRPeriod = Param("Chandelier ATR period", 14, 3, 50 );
TSUp = H * StopLevelUP; //Trailing Stop Index UP

StopLevelDown = 1 - Param( "Trailing Stop Index Down %", 26, 1, 100, 0.1 ) / 100; //26% Trailing Stop Index is Down
TSDown = H * StopLevelDown; //Trailing Stop Index DOWN

TS = IIf( IndexFilterDown, TSDown, TSUp );

InitialStop = Ref( C, -1 ) * StopLevelUP OR StopATRPeriod;
LIS = Ref( C, -1 ) - InitialStop;
``````

I'm having difficulty coding a stale exit - meaning if the stock stops trending over a few months it would be better to exit that position & buy another position.

I’ve included a StopATRPeriod trying to achieve a stale exit – a workaround to achieve a stale exit, but it’s not satisfactory.

Searching for a ‘stale exit’ has not been fruitful – maybe it’s the terminology I’m using that’s wrong that displays no results, can anyone help with a ‘stale exit’ code or hyperlink me to reference page.

Meaning: if the stock stops trending after nNumber of weeks – EXIT.

Skate.

@skate you could add either a SELL order or a third stop level. But indirectly you are asking one of the oldest questions in trading/investing, when is a trend no longer a trend.

For simplicity you could use ROC(20) < zero for either a Sell order or to tighten your stop to an even smaller amount (for example 5%). Of course any other measure of "trend" could be substituted for the Rate-of-Change indicator.

The 20 week's is just an example but if a stock is lower today than it was 20 weeks ago I certainly would be comfortable saying it is not in an uptrend. But it all depends on your time horizon.

2 Likes

A "stale exit" could also be described as a sideways channel. To quantify a sideways channel it needs an upper and lower limit. The stoploss condition sets the line for the bottom of the channel. For a trend to continue it needs a series of higher highs. If there are no higher highs AND your stoploss isn't triggered then the stock is in a sideways channel.

One solution to this is to sell if no higher high has been made after "x" number of bars. (This would be your upper limit of the channel). The value of "x" would be determined by the timeframe. eg Sell if no higher high has been made after 100 bars etc.

There are a number of functions you could use, eg. BarsSince , HHVBars depending on how you want to approach it.

1 Like

Hi @skate,

Stale Stops are interesting/difficult to code as you don't won't downward price action to trigger a Stale Stop - you have your normal stops for that. Also you don't wont a Stale Stop to trigger too early nor too late. Here is a code that you can play with and see if it suits your idea of 'going sideways for too long'. But bare in mind that like everything in trading sometimes it works well and sometimes it doesn't!

``````// MS Stale Stop (more suited to weekly systems with wide stops)

NumBars = HHVBars(Close, 50);

MS_StaleStop = NumBars >= 33 AND ROC(Close, 1) < 0 AND ROC(Close, 20) < 0;

PlotShapes(IIf(MS_StaleStop, shapeHollowDownArrow, shapeNone), colorRed, 0, High, -22);
``````
4 Likes

Trying to achieve
I'm trying to toggle between a percentage stop or a Stale Exit whilst using a Looping Stop.

@portfoliobuilder gave me a stale exit code that worked well when using the applystop exit but I'm finding it difficult to apply it to my preferred Looping Stop.

MY CODE so far - it shows I need help.

``````////////////////////////////////////////////////////////////////////////////////////
/////Toggle StopMode
////////////////////////////////////////////////////////////////////////////////////
StopMode = ParamToggle( "Stop Mode", "Fixed|Stale" );
Stale = Param( "ROC", 20, 1, 1000, 1 );

// Percentage or Stale Exit
if( StopMode == 0 ) // fixed percent trailing stop
{
IndexUp = 1 - Param( "Trailing Stop Index Up %", 63, 1, 100, 0.1 ) / 100;
IndexDown = 1 - Param( "Trailing Stop Index Down %", 26, 1, 100, 0.1 ) / 100;
}
else // Stale Exit Stop stop
{
StaleEX = ROC( C, Stale ) < 0;
}

InitialStop = Ref( C, -1 ) * StopMode;
LIS = Ref( C, -1 ) - InitialStop;
IS 	= C * StopMode;

////////////////////////////////////////////////////////////////////////////////////
/////Looping
////////////////////////////////////////////////////////////////////////////////////
Sell = 0;
trailARRAY = Null;
trailstop = 0;

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

if( trailstop == 0 AND Buy[ i ] )
{
trailstop = High[ i ] * stopMode;
}

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

if( trailstop > 0 )
{
trailstop = Max( High[ i ] * stopMode, trailstop );
trailARRAY[ i ] = trailstop;
}

}

``````

@Bodhi @TrendSurfer both gave great suggestions that I appreciated.

Skate

Hi @skate,

Are you sure you want to have the option of just having a Stale Exit on its own? What if price drops quickly and steeply - the Stale Exit will be too slow to react?

I was hoping to have a Looping Stop & an additional Stale Stop.

My looping stop is a Trailing Stop percentage depending on whether the index filter is ON or OFF.

Having such generous stops keeps the position open for many, many months sometimes the position stays inactive just going sideways - thus the Stale Exit.

Skate

Hi @skate,

So you need to get rid of this:

Have your final Sell variable something like:

``````Sell = percentageStopHit Or StaleStopHit;
``````
1 Like

Also @skate,

@TrendSurfer I appreciate your help it been a struggle.

The Looping Stop is currently:

``````Sell = 0; // fixed percentage Trailing Stop
``````

The problem is how to add an additional sell condition to my Looping Stop

Aiming to have two SELL Conditions:
Cond1 = fixed percentage Trailing Stop
———— OR ————-
Cond2 = Stale Stop

My exit should be the first of either Cond1 OR Cond2 (if that makes sense)

@skate try:

StaleStopHit: Non loop (like you already have).

PercentageStopHit: In your current trailing loop change Sell variable name (eg, to PercentageStopHit).