Hi,
I'm hoping for a bit of assistance. The below code is functioning how I would like it to and produces everything as expected in an exploration. However, I would also like to use the values generated in the loops below to set position sizing for the watchlist symbols. The only value I am receiving is the last value...here is the line of code I can't quite get right.
SetPositionSize( IIf( Name() == symbol, CashWeighting * 100, 0 ), spsPercentOfEquity );
//Set Annualized Target Volatility
TargetVolatility = Param( "Target Volatility", 16, 1, 100, .25 );
Days = Param( "Total Market Days for Calculations", 252, 0, 2520, 21 );
//Get symbol from a watchlist to calculate values
WatchlistName = ParamStr( "WatchlistName", "WorkingWatchlist" );
WatchlistNumber = CategoryFind( WatchlistName, categoryWatchlist );
SymList = CategoryGetSymbols( categoryWatchlist, WatchlistNumber );
//Iterate through the list of symbols
//initialize loop variable
SumVolRatios = 0;
for( i = 0; ( symbol = StrExtract( SymList, i ) ) != ""; i++ )
{
// body of the loop
HistVol = StDev( ln( Foreign( symbol, "C" ) / Ref( Foreign( symbol, "C" ), -1 ) ), days ) * 100 * sqrt( 252 ); //calc historical volatility for each symbol
VolRatio = TargetVolatility / HistVol; //calculate volatility ratio
SumVolRatios += VolRatio;
Filter = 1;
AddColumn( HistVol, symbol + "HistVol", 1.2 ); //display historical volitility for each symbol
AddColumn( VolRatio , symbol + "VolRatio", 1.2 ); //display volitility ratio for each symbol
}
//display values
NormalizationFactor = 1 / SumVolRatios; //normalization factor is used to equalize position volatility for a cash weighting
AddColumn( SumVolRatios , "SumVolRatios", 1.4 ); //display summation of volatility ratios
AddColumn( NormalizationFactor, "NormalizationFactor", 1.4 ); //display normalization factor
//initialize loop variable
SumCashWeightings = 0;
for( i = 0; ( symbol = StrExtract( SymList, i ) ) != ""; i++ )
{
// body of the loop to reuse previous elements
HistVol = StDev( ln( Foreign( symbol, "C" ) / Ref( Foreign( symbol, "C" ), -1 ) ), days ) * 100 * sqrt( 252 );
VolRatio = TargetVolatility / HistVol;
CashWeighting = VolRatio * NormalizationFactor; //main purpose of loop - calculate the cash weightings after normalization factor is applied
SumCashWeightings += CashWeighting;
Filter = 1;
AddColumn( CashWeighting * 100 , symbol + "CashWeight", 1.2 );
SetPositionSize( IIf( Name() == symbol, CashWeighting * 100, 0 ), spsPercentOfEquity );
}
//display value
AddColumn( SumCashWeightings * 100, "SumCashWeightings", 1.2 );
Buy = C > 0;
Sell = Month() != Ref( Month(), -1 );