Using scoreNoRotate

I am building a rotational trading system that trades on the first trading bar of the month. I am trying to backtest through past and current stocks of an index.

At the moment I am setting positionScore to scoreNoRotate to prevent trading of ALL symbols on the other bars of the month. However, this prevents the sale of delisted symbol on the last bar they trade if it is not the first bar of the month.

How do I close out of a trade on a symbol of a delisted symbol if I am using scoreNoRotate?

Thanks for any help. My example code is below:

///////////////////////////////////////////////////////////////
// INITIAL SETUP
///////////////////////////////////////////////////////////////
// Add include for Norgate Data AmiBroker functions
#include_once "Formulas\Norgate Data\Norgate Data Functions.afl"

// Set backtester to rotational trading mode
SetBacktestMode( backtestRotational );

// Variables
Max_Open_Positions = 4;
ROC_Length = 125;

// AmiBroker Settings
SetOption("InitialEquity", 100000 );
SetOption("MaxOpenPositions", Max_Open_Positions );
SetOption("WorstRankHeld", Max_Open_Positions);  // Sell positions lower than MaxOpenPositions
SetPositionSize( 100/Max_Open_Positions, spsPercentOfEquity );
SetOption("AllowPositionShrinking", True );  // Reduce position size if we don't hold enough cash
SetTradeDelays(1,1,1,1);  // Set all trading for the following bar

BuyPrice = Open;  // Set Buy prices to be Open price
SellPrice = Open;  // Set Sell prices to be Open price

///////////////////////////////////////////////////////////////
// CALCULATE INITIAL MOMENTUM SCORE FOR SYMBOL
///////////////////////////////////////////////////////////////
PositionScore = ROC(Close, ROC_Length);

///////////////////////////////////////////////////////////////
// ONLY TRADE SYMBOLS ON ACTION DAY (otherwise set PositionScore to scoreNoRotate)
///////////////////////////////////////////////////////////////
// Calculate if current bar is the first bar of month
Action_Day = Month() != Ref(Month(),-1);
PositionScore = IIf(Action_Day == True, PositionScore, scoreNoRotate);

///////////////////////////////////////////////////////////////
// REMOVE SYMBOLS THAT ARE NOT IN OUR TRADABLE UNIVERSE (by setting its PositionScore to 0.0)
///////////////////////////////////////////////////////////////
// Check if symbol was in historical index
ASX100 = "$XTO"; // Norgate Data Ticker code for ASX100
Symbol_In_Universe = NorgateIndexConstituentTimeSeries(ASX100); // This has a special funtion within NorgateData
// Don't trade symbols that are not in universe of tradable symbols (and exit trade if we were in it)
PositionScore = IIf(Symbol_In_Universe == False, 0.0, PositionScore);
   
///////////////////////////////////////////////////////////////
// CLOSE TRADES IN DELISTED SYMBOLS (by setting PositionScore to 0.0)
// https://www.amibroker.com/kb/2015/09/27/how-to-handle-delisted-symbols-in-rotational-test/
///////////////////////////////////////////////////////////////
bi = BarIndex();
lastbi = LastValue( bi ) - Status("BuyDelay");
exitLastBar = bi == lastbi;
PositionScore = IIf( exitLastBar == True, 0.0, PositionScore );

///////////////////////////////////////////////////////////////
// REMOVE SHORT TRADES (by setting PositionScore to 0.0)
///////////////////////////////////////////////////////////////
PositionScore = IIf (PositionScore < 0.0, 0.0, PositionScore);

If your Norgate Data subscription level caters for delisted stocks then change your current method to this one.

delistedSecurity = DateTimeDiff(DateTime(), GetFnData("DelistingDate")) >= 0;
PositionScore = IIf(delistedSecurity, 0, PositionScore);

Thanks TrendSurfer.

Does the code you provide always exit the delisted symbol on the symbols actual delisting date or does it always exit the delisted symbol on the next bar that PositionScore is not set to scoreNoRotate?

From what I understand of setting PositionScore to scoreNoRotate it is the later. If that is the case then where would the Close price for a delisted stock come from days after its delisting date? Is the final Close value repeated in a delisted symbols Close array after the delisting date?

Using 'Pad & Align' with the above delisted stock method, delisted stocks will exit on the next rotation bar at the last traded price.

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