Exit 1 trade on multiple targets, stop loss

Hello Amibroker team,

Can you please point me to some examples, where I can see a code for multiple exits for single trade?

This is what I would like to code somehow:
For every open trade immediately to define 1x stop loss and 3x target profit following the rules.
First calculate SL at some level (could be x points price difference from entry price to keep it simple). Based on this set first Target for 1/3 position at RRR 1:1, another 1/3 of position at RRR 2:1 and the rest at 3:1. Additionally, 1st TP is hit, move SL to BreakEven, later if 2nd TP is hit, move SL up to price where was 1st TP.

I'm not asking you to write a code for me, but would be great to see some similar codes for exists with similar idea. I did a search, but I'm a bit lost here.
https://www.amibroker.com/guide/h_pyramid.html

Many thanks,
Tom

You might want to search the forum and KB with the term "Trailing stop(s)".

Please refer Quantitative Trading System by Howard Bandy, Figure 7.10 ScaleOut afl code. You will get step by step explanation also in the book.

2 Likes

hi,

I ran Howard's scale-out code and it appears to only sell at the second profit target. Could someone advise why this is happening please? Altering "FirstProfitTarget" makes no difference to the backtest.

// ScaleOut.afl
buy = cross( ma( c, 5 ), ma( c, 25 ) );
sell = 0;
// Targets are in percentages.
FirstProfitTarget = 3;
SecondProfitTarget = 10;
TrailingStop = 30;

// Scalars to keep track of prices while in trade.
PriceAtBuy = 0;
HighSinceBuy = 0;
Exit = 0;

// Loop through all the bars.
for( i = 0; i < BarCount; i++ )

{
    if( PriceAtBuy == 0 AND Buy[i] == 1 )
    {
        PriceAtBuy = BuyPrice[i];
    }

    else

        if( PriceatBuy > 0 )

        {

            HighSinceBuy = Max( High[i], HighSinceBuy );

            if( exit == 0 AND
                    High[i] >= ( 1 + FirstProfitTarget * .01 ) * PriceAtBuy )

            {
                //first profit target hit - scale-out
                Exit = 1;
                Buy[i] = sigScaleout;
                BuyPrice[i] = ( 1 + FirstProfitTarget * 0.01 ) * PriceatBuy;

            }

            if( Exit == 1 AND
                    High[i] >= ( 1 + SecondProfitTarget * 0.01 ) * PriceAtBuy )

            {

                // second profit target hit - exit
                Exit = 2;
                SellPrice[i] = Max( Open[i],
                    ( 1 + SecondProfitTarget * 0.01 ) * PriceAtBuy );
            }

            if( Low[i] <= ( 1 - TrailingStop * 0.01 ) * HighSinceBuy )
            {
                //trailing stop hit - exit
                Exit = 3;
                SellPrice[i] = Min( Open[i], ( 1 - TrailingStop * 0.01 ) * HighSinceBuy );
            }

            if( Exit >= 2 )

            {
                Buy[i] = 0;
                Sell[i] = Exit + 1; // mark appropriate exit code
                Exit = 0;
                PriceatBuy = 0;
                HighSinceBuy = 0;
            }
        }
}

SetPositionSize( 50, spsPercentofequity );
// scale out 50% of position
SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleout ) );

Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

Well, that's interesting. The code in the AB guide works, so I'll use that instead. Virtually identical anyway.