Easy Language Meeting: Target Volatility Function and Automation

  • Step 1. Create a new function named TV
  • Step 2. Copy in the function code
  • Step 3. Call the function into a strategy or indicator with the inputs (ex… tv(.05, 21))

Function code

Inputs:
TargetVolatility(Numeric),
HVlookback(Numeric);
Vars:
Allocation(0),
HistoricalVolatility(0);
HistoricalVolatility = standarddev( (c - c[1]) / c[1], HVlookback, 1) * Squareroot(252) ;
if HistoricalVolatility > TargetVolatility then
Allocation = TargetVolatility / HistoricalVolatility
else
Allocation = 1.00 ;
TV = Allocation;

Indicator code

Vars:
TargetVol(0);
TargetVol = tv(.05,21);
Plot1(TargetVol,"cyan");

Strategy code (For analysis only)

{not production ready… closes the entire position every month}
Inputs:
TargetVol(.05),
LB(21),
FullSize(100000);
Vars:
currentmonth(0);
currentmonth = month(date);
buy (100000 * tv(targetvol,LB) / c) shares next bar market;
If currentmonth > currentmonth[1] then begin
sell next bar market;
end;