Hi there,
I've been trying to work this problem out for over a week, but I haven't found a solution. I'm currently trying to create a buy condition based on my current symbol being in a strong sector of its market at any point in time.
I've been using the StaticVarGenerateRanks to perform the ranking and this is my code so far below to retrieve the top sectors, and then check to see if the current symbol's sector is in that group of top sectors.
symlist = "DSBM,DSNC,DSCY,DSFN,DSHC,DSIN,DSTL";
ThisSymbolsSector = "DSCY"; // Hardcoded for this example
StrongSector = Null;
// delete static variables
StaticVarRemove( "ItemScore*" );
// fill input static arrays
for ( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
{
SetForeign( sym );
Value = ROC( C, 10 );
RestorePriceArrays();
StaticVarSet( "ItemScore" + sym, Value );
}
// perform ranking
TopSectorCount = 3;
StaticVarGenerateRanks( "rank", "ItemScore", 0, 1224 ); // normal rank mode
StaticVarGenerateRanks( "top", "ItemScore", TopSectorCount, 1224 ); // top-N mode
StaticVarGenerateRanks( "bot", "ItemScore", -3, 1224 ); // bottom-N mode
// read ranking
for ( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
{
Plot( StaticVarGet( "RankItemScore" + sym ), sym, colorCustom10 + i );
}
sdt = SelectedValue( DateTime() );
// plot
Title = "{{NAME}} -{{DATE}} - {{VALUES}} TOP: " + StaticVarGetRankedSymbols( "top", "ItemScore", sdt ) +
" BOT: " + StaticVarGetRankedSymbols( "bot", "ItemScore", sdt ) ;
// Get top ranked sectors
TopSectors = StaticVarGetRankedSymbols( "top", "ValuesToSort", sdt );
for ( i = 0; i < TopSectorCount; i++)
{
_TRACE(StrExtract(TopSectors, i) + DateTimeFormat("%Y%m%d", sdt) + "\n");
TopSingleSector = StrExtract(TopSectors, i);
TopSingleSector = StrToUpper(TopSingleSector);
if (ThisSymbolsSector == TopSingleSector)
{
//printf(TopSingleSector + " is a strong sector "+ DateTimeFormat("%Y%m%d", sdt) + "\n");
StrongSector = True;
}
}
Buy = StrongSector & C>MA(C,52);
Sell = C<MA(C,52);
As per the documentation, I've found that sdt = SelectedValue(DateTime())
returns the last date of my analysis range, rather than the date of the current bar during backtesting. So its only checking the ranking on the last date, rather than the current bar being backtested.
It is possible to get the sdt variable to contain the date of the current bar that its being processed while undergoing backtesting? I've tried ValueWhen, but that retrieves an array, whilst StaticVarGetRankedSymbols requests a number for a date.
I've had a look at the portfolio backtester reference, but it seems that with the backtester I can only get date of the current bar once a signal occurs, whereas I'm looking to get the current date of the bar to generate a signal. I hope that all makes sense.
Any help would be much appreciated.
Thanks