Australian (ASX) Stock Market Forum

Stale Exit - Amibroker

Joined
28 June 2019
Posts
1,078
Reactions
2,535
I am trying to code a stale exit. My code thus far is:

Code:
Stale = 20;
StaleStop = ROC(C, Stale) < 0;
Sell = SellingCross OR StaleStop;

I still have a trailing stop that will trigger as well.

My current issue is that I am having my new positions exited too prematurely. I would prefer that my system will check after 20 bar's if the ROC is < 0, whereas at the moment the statement ROC(C, 20) is checked periodically so it sells after only a few bars which makes the trailing stop almost pointless.
 
Thanks @Gringotts Bank, I will give that a go. There are obviously multiple approaches to a stale exit which is why I thought it be good to put this up here for others benefits too.

I will give that code a go.

@Sir Burr, that is where I got my original code from but as I said, it doesn't wait until the end of 20 days to check if there is a change. WHat was happen was after approx 5 bars my trades were closing out and instead of getting 50 trades over my sample period I was getting 350 trades. It was also negating the point of a stale exit.
 
Try this.

This won't work properly if you have state signals (Buy, Buy, Buy, Buy....).
Please listen what I "said"... You need to iterate.
Period.

It is basically an n-bar stop just with addition of further check when n-bar after entry is reached.
 
This won't work properly if you have state signals (Buy, Buy, Buy, Buy....).
Please listen what I "said"... You need to iterate.
Period.

It is basically an n-bar stop just with addition of further check when n-bar after entry is reached.

It was people like you that made me give up on this forum years ago.
It seems like nothing has changed.

Fvck you too ....
 
All I wrote is the fact that the code you posted won't work properly and why it won't work properly.

Seriously, grow up. Your last post (especially your last line) shows that it is you seemingly having some serious problem with your psyche. What a shame.
 
Try this.

That seemed to work. Though I need to verify. What I do get from backtests is that my trailing stop is still hit but my system will still exit from Bar 19+.

2018 was still a bad year for my system .... -13%, otherwise good results for the past 3 years (net profit of 127%). Still toggling things like position sizing. But I'm glad I got some progress on the stale exit.

I think the idea of a looping that @trash mentioned will be useful but it was hurting my head, haha.
 
What I do get from backtests is that my trailing stop is still hit but my system will still exit from Bar 19+.

As I said it won't work (properly) that way for each and every case.

It is like n-bar stop (but just with additional condition).
Why do you think AmiBroker software has ApplyStop function (with stopTypeNBar) ?
But since that one only has simple nbar stop you have to create custom one yourself using looping.

I was actually about to post code here but after Grep's last post I will not.
That being said there are examples on AB forum.

I think the idea of a looping that @trash mentioned will be useful but it was hurting my head, haha

It is not difficult at all and not rocket science. ;)
 
I am back as I tried to give this another go.

I was going over my work again and figured that BarsSince could work, and then I found this:

BarsSince(Buy) is the right answer as long as your buy signals are not repetitive .

So you can use it if your Buy is in SIGNAL form (such as returned by Cross() function)

Copy
Buy = Cross(Close, MA( C, 10 )); // SIGNAL form can be used with BarsSince
but you can not use it if your Buy signal is in STATE form like this:

Copy
Buy = Close > MA( C, 10 ); // STATE form can not be used with BarsSInce

In state form you need to keep track of when you are “in-trade”.
In simple cases one can do this with Flip() and/or ExRem(), but a general-purpose solution is presented in the Knowledge Base
http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/

My buy condition is in a STATE form. Any idea on how to build the loop and count bars if the buy condition is "Buy = Close > MA( C, 10 )". I'm going around in circles right now knowing that the solution is simple but I'm just not getting it.
 
I was going over my work again and figured that BarsSince could work

Maybe use the SumSince function to find the number of consecutive buy signals you have had then use the BarsSince function to find the number of bars since the first buy signal.

Signals = SumSince( Sell, Buy ) ; // Count the buy signals since the last sell signal
BarsSinceBuy = BarsSince( Signals == 0 ) ;
 
Maybe use the SumSince function to find the number of consecutive buy signals you have had then use the BarsSince function to find the number of bars since the first buy signal.

Signals = SumSince( Sell, Buy ) ; // Count the buy signals since the last sell signal
BarsSinceBuy = BarsSince( Signals == 0 ) ;
I'm not sure that would work as well. I need to have a variable that will hold the value for the number of bars since buying. This is why, as @trash pointed to, it should be an n-bar stop/count.

I saw the example in the link from the AB forum but I'm lost how to modify it for my buy statement.
 
Hi Warr87,

See if this solves the problem.

As you haven't posted your primary exit code you will need to replace ????? below.


Buy = Close > MA( C, 10 );

Warr87Exit = ?????;
StaleExit = ROC(C,20) < 0;

Sell = 0;
BarsInTrade = 0;
for( i = 0; i < BarCount; i++ )
{
if( BarsInTrade > 0 )
BarsInTrade++;
else if( Buy[ i ] ) BarsInTrade = 1;

if( BarsInTrade < 10 AND Warr87Exit[ i ] )
{
Sell[ i ] = 1;
BarsInTrade = 0;
}

if( BarsInTrade >= 10 AND StaleExit[ i ] )
{
Sell[ i ] = 1;
BarsInTrade = 0;
}
}


Cheers, Rob
 
Unfortunately it didn't seem to work. I even changed a few values in there and I got the exact same results.

And sorry for not giving my sell conditions. they are:

Code:
Sell = StaleStop OR SellingCross;

Where SellingCross is a simple EMA cross.
 
Unfortunately it didn't seem to work. I even changed a few values in there and I got the exact same results.

And sorry for not giving my sell conditions. they are:

Code:
Sell = StaleStop OR SellingCross;

Where SellingCross is a simple EMA cross.

@Warr87
Did you run the code as posted below after adjusting Warr87Exit to the EMA cross.

I believe Trash has saying that this code (Sell = StaleStop OR SellingCross) should not be used in this format.

Cheers, Rob
 
hey @rnr . I did adjust the Warr87Exit for my variable name. And I wasn't too sure about the sell statement. with the loop counter I was thinking that the loop counts the number of bars since buy condition was met, and each time it passes through the loop it gives a variable (x) that number. Once that number reaches 20 it then runs the ROC statement to see if there has been change, if not then stale exit condition has been met and then a variable is assigned as true (y = 1). Then when it comes to "Sell = SellingCross or StaleStop;", if the boolean statement of StaleStop has been met as true it triggers a sell condition.

At least that's how my logic was going. in my head it makes sense but my programming skills aren't up to it haha.
 
There is new information given by AB developer himself today as someone was asking similar question today. If you have AB version 6.30 and above then you may use ApplyStop new "valid from-to" settings. See here
https://forum.amibroker.com/t/time-stop-or-n-bar-stop-with-extra-condition/15776/3

So below 6.30 you have to iterate and if you have AB >= 6.30 then all you need is ApplyStop.

The example there in AB forum uses below 3% ROC since entry.
In your first post here you want to exit if ROC is below zero at nbar after entry.

So try this one (if you have AB 6.30). Change amount from 3 to low number close to zero.
I haven't tried it.

Code:
Version(6.30);// code requires min. AB 6.30

Buy = MA(C, 20);
BuyPrice = C;

Sell = SellingCross;// regular Sell signal, choose you own one
SellPrice = Close;// for regular Sell

Stale = 20;// nbars after entry
amount = 1e-9; // very low number close to zero see -> StaleStop = ROC(C, Stale) < 0;

// For AB versions >= 6.30
// https://forum.amibroker.com/t/time-stop-or-n-bar-stop-with-extra-condition/15776/3
// this max loss is valid only on n-th bar
ApplyStop( stopTypeLoss, stopModePercent, amount, True, False, 0, Stale /* valid from */, Stale /* valid to */ );
 
Top