Assistance with Loop and SetPositionSize

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 );

When posting the formula, please make sure that you use Code Tags (using </> code button) as explained here: How to use this site.

Using code button

Code tags are required so formulas can be properly displayed and copied without errors.

I didn't have that option for some reason on the earlier submission, but here it is now as I do with the reply dialogue.

// See Carver, Smart Portfolios pgs 96-99

//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 );

What non-sense! Of course you did!

In fact, I did not. For whatever reason the </> icon was not available. But thank you for your concern. The animation to the right as I type this is pretty difficult to miss, don't you think?

You might not see the icon because due to weird design choice Discourse authors recently decided to move </> icon into cogwheel submenu IF you used touch screen device to post So it was there, but available under "cogwheel" menu.
I have fixed it as per this Discourse Meta thread.