Intraday Profit Targets, Trend Filter Exit on Next Day Open

Hi All,

New to programming and AFL. I have been scouring the AmiBroker user guide as well as looking for solutions on the forum but am unable to figure out the proper code to execute what I am looking to do.

My goal is to buy the lower extreme of a bollinger band, say a close beyond the 2 SD extreme. I then want to use an intraday scale out for 50% of the position at the bollinger band moving average, the second scale out would be intraday as well for 50% of the remainder of the position at an upper extreme of the bollinger band, and then an intraday trailing stop for the remaining 25% of the position. I would prefer as well to have the trailing stop only become active after the second target has been hit, NOT just have a 10% trail stop on the entire position from the start.

I have two trend filters in place, a broader index trend filter, as well as a longer term MA for the select equity as a trend filter. If either of those trend filters reverse, I would like the entire position to exit on the following day's open.

The only scale out example I am finding is from the AmiBroker User Guide (Portfolio-level back testing) which uses percentages for the scale out, rather than the specific profit targets I mentioned above. I have copied that code and am trying to alter it but I don't fully understand the logic nor how to alter it to fit what I am looking to do.

Additionally, I am running into the issue of not sure how to set the profit targets to execute intraday, while using the following day's open as the time of execution for the two trend filters.

If anyone can please point me in the right direction, that would be incredible!


SetOption("MaxOpenPositions", 1);
SetPositionSize(100,spsPercentOfEquity);
SetTradeDelays(1,1,0,0);

// Check market trend
SetForeign("$NDX");
IndexDownTrend = C < MA(C,200);
RestorePriceArrays();

// Check instrument trend
TrendMA = MA(C, 100);
TrendUp = TrendMA > Ref(TrendMA, -1);
TrendDown = TrendMA < Ref(TrendMA, -1);

// Entry Signal
BBandPeriod = 20;
BBandStdDev = 2;
LowerExtreme = BBandBot(C, BBandPeriod, BBandStdDev);
EntrySignal = C < LowerExtreme;

Buy = TrendUp AND EntrySignal AND NOT IndexDownTrend;

// ******************** Copy and pasted code from User Guide Starts Here, which I have attempted to alter ******************

Sell = 0;

// the system will exit
// 50% of position if FIRST PROFIT TARGET stop is hit
// 50% of position is SECOND PROFIT TARGET stop is hit
// 100% of position if TRAILING STOP is hit

FirstProfitTarget = MA(C, BBandPeriod); 
SecondProfitTarget = BBandTop(C, BBandPeriod, 2);
TrailingStop = 10; // In percent

priceatbuy=0;
highsincebuy = 0;

exit = 0;

for( i = 0; i < BarCount; i++ )
{
  if( priceatbuy == 0 AND Buy[ i ] )
   {
      priceatbuy = BuyPrice[ i ];
   }

  if( priceatbuy > 0 )
   {
      highsincebuy = Max( High[ i ], highsincebuy );

     if( exit == 0 AND
         High[ i ] >= FirstProfitTarget )
      {
        // first profit target hit - scale-out
        exit = 1;
        Buy[ i ] = sigScaleOut;
      }

     if( exit == 1 AND
         High[ i ] >= SecondProfitTarget )
      {
        // second profit target hit - exit
        exit = 2;
        SellPrice[ i ] = Max( Open[ i ], SecondProfitTarget);
      }

     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; // reset price
        highsincebuy = 0;
      }
   }
}

SetPositionSize( 100, spsPercentOfEquity );
SetPositionSize( 100, spsPercentOfPosition * ( Buy == sigScaleOut ) ); // scale out 50% of position

An immediate fix for the last line is:

// first argument should be 50, not 100 as in your formula, to scale 50% out
SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) ); // scale out 50% of position
1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.