• Australian (ASX) Stock Market Forum

Hello and welcome to Aussie Stock Forums!

To gain full access you must register. Registration is free and takes only a few seconds to complete.

Already a member? Log in here.

Stale Exit - Amibroker

Discussion in 'Software and Data' started by Warr87, Oct 26, 2019.

sentifi.com

Aussie Stock Forum Sentifi Top themes and market attention on:

  1. Warr87

    Warr87

    Posts:
    60
    Likes Received:
    80
    Joined:
    Jun 28, 2019
    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.
     
  2. Sir Burr

    Sir Burr

    Posts:
    528
    Likes Received:
    118
    Joined:
    Feb 15, 2006
  3. Gringotts Bank

    Gringotts Bank

    Posts:
    6,163
    Likes Received:
    577
    Joined:
    Jun 30, 2007
    Something like: "Sell if 20 days pass and there's been no break above the highest high in the last 50 days".


    a = HHV(H,50);
    b = Cross(C,Ref(a,-1));
    Sell = Sum(b,20)==0;
     
    IFocus and qldfrog like this.
  4. Warr87

    Warr87

    Posts:
    60
    Likes Received:
    80
    Joined:
    Jun 28, 2019
    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.
     
  5. trash

    trash

    Posts:
    412
    Likes Received:
    16
    Joined:
    Mar 31, 2012
    You would have to apply looping since you need to store bar at entry.
     
    Skate likes this.
  6. Grep

    Grep

    Posts:
    19
    Likes Received:
    0
    Joined:
    Jun 22, 2009
    Try this.
     
  7. trash

    trash

    Posts:
    412
    Likes Received:
    16
    Joined:
    Mar 31, 2012
    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.
     
  8. Grep

    Grep

    Posts:
    19
    Likes Received:
    0
    Joined:
    Jun 22, 2009
    It was people like you that made me give up on this forum years ago.
    It seems like nothing has changed.

    Fvck you too ....
     
  9. trash

    trash

    Posts:
    412
    Likes Received:
    16
    Joined:
    Mar 31, 2012
    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.
     
    Nina4 likes this.
  10. Warr87

    Warr87

    Posts:
    60
    Likes Received:
    80
    Joined:
    Jun 28, 2019
    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.
     
  11. trash

    trash

    Posts:
    412
    Likes Received:
    16
    Joined:
    Mar 31, 2012
    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.

    It is not difficult at all and not rocket science. ;)
     
    Skate likes this.
  12. Warr87

    Warr87

    Posts:
    60
    Likes Received:
    80
    Joined:
    Jun 28, 2019
    I'm a simple man ;):laugh:
     
    CNHTractor likes this.
  13. Warr87

    Warr87

    Posts:
    60
    Likes Received:
    80
    Joined:
    Jun 28, 2019
    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:

    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.
     
  14. BoNeZ

    BoNeZ

    Posts:
    39
    Likes Received:
    35
    Joined:
    Nov 11, 2013
    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 ) ;
     
  15. Warr87

    Warr87

    Posts:
    60
    Likes Received:
    80
    Joined:
    Jun 28, 2019
    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.
     
  16. rnr

    rnr

    Posts:
    442
    Likes Received:
    155
    Joined:
    Jul 1, 2007
    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
     
  17. Warr87

    Warr87

    Posts:
    60
    Likes Received:
    80
    Joined:
    Jun 28, 2019
    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.
     
  18. rnr

    rnr

    Posts:
    442
    Likes Received:
    155
    Joined:
    Jul 1, 2007
    @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
     
  19. Warr87

    Warr87

    Posts:
    60
    Likes Received:
    80
    Joined:
    Jun 28, 2019
    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.
     
  20. trash

    trash

    Posts:
    412
    Likes Received:
    16
    Joined:
    Mar 31, 2012
    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 */ );
    
     
    Warr87 and debtfree like this.
Loading...

Share This Page