Hi all,
the code below does not take the highest ranked symbols in my backtest and I do not know how to fix this issue. Do you have any ideas or comments?
In my code I work on portfolio level with multiple buy signal on the same day. The same buy signal exists on multiple days for the same symbol. With positionscore the ten best ranked symbols based on ROC should be included. Here is an example with the Nasdaq100:
The first ten symbols that were entered on 2.1.20 are the highest ranked securities.
Trace results (in Excel):
On 3.3.20 was a new entry in KLAC:
The trace shows that eight symbols have a higher ranking:
Question: Why were the higher ranked symbols ignored? REGN should be opened.
I read that in portfolio mode SetBacktestMode( backtestRegularraw ) should be used. So I run the next test. The first ten positions are the same. The first entry was on 27.2. in GILD:
Again I have higher ranked symbols (without an open position) in the trace:
Again REGN has a higher positionscore.
I did a single backtest with GILD and REGN. REGN would open on 2.1. and GILD on 9.1.:
Is this a valid assumption: On 2.1.20 were the first ten open positions created. REGN was not opened because of the low ROC. But my code does not “reset” REGN and so it is ignored on 27.2.20. GILD did not have a buysignal on 2.1.20 and the signal on 9.1.20 was ignored because ten position were open. So how do I “reset” REGN in my code?
Code:
SetBacktestMode( backtestRegularraw ); // backtestRegularRaw
#include_once "Formulas\Norgate Data\Norgate Data Functions.afl";
/* Symbols: */
index = "$NDX";
indexFilter = NorgateIndexConstituentTimeSeries( index );
//======SETTINGS====================
SetOption( "PortfolioReportMode",0); // 0 - trade list 1 - detailed log 2 - summary
SetOption("GenerateReport", 1 ); // 0 suppress generation of report 1 force generation of full report
OnSecondLastBarOfDelistedSecurity = !IsNull( GetFnData( "DelistingDate" ) ) AND( BarIndex() == ( LastValue( BarIndex() ) - 1 ) OR DateTime() >= GetFnData( "DelistingDate" ) ) ;
OnLastTwoBarsOfDelistedSecurity = !IsNull( GetFnData( "DelistingDate" ) ) AND( BarIndex() >= ( LastValue( BarIndex() ) - 1 ) OR DateTime() >= GetFnData( "DelistingDate" ) );
SetOption( "InitialEquity", 100000 ); RoundLotSize = 0; // SetTradeDelays( 0, 1, 1, 1 );
SetOption( "AccountMargin",0);
maxopenpos = 10 ;
SetOption( "MaxOpenPositions", maxopenpos );
SetPositionSize( 100 / (maxopenpos), spsPercentOfEquity );
SetOption( "CommissionMode" , 3 ); SetOption( "CommissionAmount" , 0.01 );
SetOption("holdminbars",10); SetOption("MinShares",1);//
//======ENTRY====================
longterm = 200 ;
shrtterm = 10 ;
medterm = 100 ;
symbol_Signal = C > ma( C, shrtterm ) AND C > ma( C, longterm ) ;
PositionScore = IIf(Ref(ROC( Close, medterm ),-1)>0 ,1000 + Ref(ROC( Close, medterm ),-1),0) ;
buysignal = symbol_Signal ;
Buy = indexFilter AND NOT OnLastTwoBarsOfDelistedSecurity AND Ref( buysignal, -1 ) ;
//======EXIT====================
Sell = DateTime() == GetFnData( "DelistingDate" ) /* sell if stock is de-listed */ OR !indexFilter OR OnSecondLastBarOfDelistedSecurity ;
ApplyStop(stopTypeLoss,stopModePercent,20);
//======TRACE====================
_TRACE("; "+Name() + ";;;; ;" + Date() + " ;" + "Positionscore =; "+PositionScore + " ;Buy: ;" + Buy);
Thanks,
Peter