11 Oct 2023

-Working on a simple equal weighted monthly rebalancing strategy
-Picked 3 ETF as illustration
-Aim to use target volatity to limit the weight
-Script:
function run()
{
if(is(INITRUN))
set(LOGFILE);
setf(SaveMode,SV_STATS);

//Basic setting
StartDate= 20020101;
EndDate = 20231001;
setf(PlotMode, PL_FINE);
BarPeriod = 1440;
Capital = 3000;

var Weights[3],Strengths[3],target_vol[3],vol[3];
var pool = Capital + ProfitClosed;
string Stock,Stocks[3];
assetList("AssetsFix");
while(Stock = loop("VTI","GLD","TLT"))
{
if(Init) assetHistory(Stock,FROM_YAHOO);
asset(Stock);
Stocks[Itor1] = Stock;
vol[Itor1]=Volatility(series(priceClose(0)),60);
}


if(tom()==tdm()) // at any month
{
int i;
for(i = 0; i<3; i++) {
asset(Stocks[i]);
Weights[i] = 0.333;
target_vol[i] = 0.15;
int NewShares = (target_vol[i]/vol[i])*Weights[i]*pool/priceClose(0) - LotsPool;
if(NewShares > 0)
enterLong(NewShares);
else if(NewShares < 0)
exitLong("",-NewShares);
}
}

}

Challenge:
1. For the execution, not all stocks are entered and rebalanced each month, identifying the bug

Community feel free to drop your comments for suggestion if you worked on simple rebalancing strategy before and share your expereinces laugh