| Overall Statistics |
|
Total Orders 988 Average Win 0.18% Average Loss -0.21% Compounding Annual Return 5.685% Drawdown 15.200% Expectancy 0.236 Start Equity 100000 End Equity 127149.57 Net Profit 27.150% Sharpe Ratio -0.019 Sortino Ratio -0.021 Probabilistic Sharpe Ratio 7.809% Loss Rate 34% Win Rate 66% Profit-Loss Ratio 0.86 Alpha -0.028 Beta 0.572 Annual Standard Deviation 0.099 Annual Variance 0.01 Information Ratio -0.57 Tracking Error 0.082 Treynor Ratio -0.003 Total Fees $1063.08 Estimated Strategy Capacity $13000000.00 Lowest Capacity Asset EEMV V0WRDXSSH205 Portfolio Turnover 2.81% Drawdown Recovery 895 |
#region imports
from AlgorithmImports import *
#endregion
from clr import AddReference
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Algorithm.Framework")
from QuantConnect import Resolution, Extensions
from QuantConnect.Algorithm.Framework.Alphas import *
from QuantConnect.Algorithm.Framework.Portfolio import *
from itertools import groupby
from datetime import datetime, timedelta
class EqualWeightingPortfolioConstructionModel(PortfolioConstructionModel):
'''Provides an implementation of IPortfolioConstructionModel that gives equal weighting to all securities.
The target percent holdings of each security is 1/N where N is the number of securities.
For insights of direction InsightDirection.Up, long targets are returned and
for insights of direction InsightDirection.Down, short targets are returned.'''
def __init__(self, rebalance = Resolution.Daily, portfolioBias = PortfolioBias.LongShort):
'''Initialize a new instance of EqualWeightingPortfolioConstructionModel
Args:
rebalance: Rebalancing parameter. If it is a timedelta, date rules or Resolution, it will be converted into a function.
If None will be ignored.
The function returns the next expected rebalance time for a given algorithm UTC DateTime.
The function returns null if unknown, in which case the function will be called again in the
next loop. Returning current time will trigger rebalance.
portfolioBias: Specifies the bias of the portfolio (Short, Long/Short, Long)'''
self.portfolioBias = portfolioBias
# If the argument is an instance of Resolution or Timedelta
# Redefine rebalancingFunc
rebalancingFunc = rebalance
if isinstance(rebalance, int):
rebalance = Extensions.ToTimeSpan(rebalance)
if isinstance(rebalance, timedelta):
rebalancingFunc = lambda dt: dt + rebalance
if rebalancingFunc:
self.SetRebalancingFunc(rebalancingFunc)
def DetermineTargetPercent(self, activeInsights):
'''Will determine the target percent for each insight
Args:
activeInsights: The active insights to generate a target for'''
result = {}
# give equal weighting to each security
count = sum(x.Direction != InsightDirection.Flat and self.RespectPortfolioBias(x) for x in activeInsights)
percent = 0 if count == 0 else 1.0 / count
for insight in activeInsights:
result[insight] = (insight.Direction if self.RespectPortfolioBias(insight) else InsightDirection.Flat) * percent
return result
def RespectPortfolioBias(self, insight):
'''Method that will determine if a given insight respects the portfolio bias
Args:
insight: The insight to create a target for
'''
return self.portfolioBias == PortfolioBias.LongShort or insight.Direction == self.portfolioBias
# Your New Python File#region imports
from AlgorithmImports import *
#endregion
from clr import AddReference
AddReference("QuantConnect.Algorithm.Framework")
AddReference("QuantConnect.Indicators")
AddReference("QuantConnect.Common")
from QuantConnect import *
from QuantConnect.Indicators import *
from QuantConnect.Algorithm.Framework.Alphas import *
from datetime import timedelta
class HistoricalReturnsAlphaModel(AlphaModel):
'''Uses Historical returns to create insights.'''
def __init__(self, *args, **kwargs):
'''Initializes a new default instance of the HistoricalReturnsAlphaModel class.
Args:
lookback(int): Historical return lookback period
resolution: The resolution of historical data'''
self.lookback = kwargs['lookback'] if 'lookback' in kwargs else 1
self.resolution = kwargs['resolution'] if 'resolution' in kwargs else Resolution.Daily
self.predictionInterval = Time.Multiply(Extensions.ToTimeSpan(self.resolution), self.lookback)
self.symbolDataBySymbol = {}
def Update(self, algorithm, data):
'''Updates this alpha model with the latest data from the algorithm.
This is called each time the algorithm receives data for subscribed securities
Args:
algorithm: The algorithm instance
data: The new data available
Returns:
The new insights generated'''
insights = []
for symbol, symbolData in self.symbolDataBySymbol.items():
if symbolData.CanEmit:
direction = InsightDirection.Flat
magnitude = symbolData.Return
if magnitude > 0: direction = InsightDirection.Up
if magnitude < 0: direction = InsightDirection.Down
insights.append(Insight.Price(symbol, self.predictionInterval, direction, magnitude, None))
return insights
def OnSecuritiesChanged(self, algorithm, changes):
'''Event fired each time the we add/remove securities from the data feed
Args:
algorithm: The algorithm instance that experienced the change in securities
changes: The security additions and removals from the algorithm'''
# clean up data for removed securities
for removed in changes.RemovedSecurities:
symbolData = self.symbolDataBySymbol.pop(removed.Symbol, None)
if symbolData is not None:
symbolData.RemoveConsolidators(algorithm)
# initialize data for added securities
symbols = [ x.Symbol for x in changes.AddedSecurities ]
history = algorithm.History(symbols, self.lookback, self.resolution)
if history.empty: return
tickers = history.index.levels[0]
for ticker in tickers:
symbol = SymbolCache.GetSymbol(ticker)
if symbol not in self.symbolDataBySymbol:
symbolData = SymbolData(symbol, self.lookback)
self.symbolDataBySymbol[symbol] = symbolData
symbolData.RegisterIndicators(algorithm, self.resolution)
symbolData.WarmUpIndicators(history.loc[ticker])
class SymbolData:
'''Contains data specific to a symbol required by this model'''
def __init__(self, symbol, lookback):
self.Symbol = symbol
self.ROC = RateOfChange('{}.ROC({})'.format(symbol, lookback), lookback)
self.Consolidator = None
self.previous = 0
def RegisterIndicators(self, algorithm, resolution):
self.Consolidator = algorithm.ResolveConsolidator(self.Symbol, resolution)
algorithm.RegisterIndicator(self.Symbol, self.ROC, self.Consolidator)
def RemoveConsolidators(self, algorithm):
if self.Consolidator is not None:
algorithm.SubscriptionManager.RemoveConsolidator(self.Symbol, self.Consolidator)
def WarmUpIndicators(self, history):
for tuple in history.itertuples():
self.ROC.Update(tuple.Index, tuple.close)
@property
def Return(self):
return float(self.ROC.Current.Value)
@property
def CanEmit(self):
if self.previous == self.ROC.Samples:
return False
self.previous = self.ROC.Samples
return self.ROC.IsReady
def __str__(self, **kwargs):
return '{}: {:.2%}'.format(self.ROC.Name, (1 + self.Return)**252 - 1)#region imports
from AlgorithmImports import *
#endregion
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Algorithm.Framework")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Indicators")
from System import *
from QuantConnect import *
from QuantConnect.Indicators import *
from QuantConnect.Algorithm import *
from QuantConnect.Algorithm.Framework import *
from QuantConnect.Algorithm.Framework.Portfolio import *
from Portfolio.MinimumVariancePortfolioOptimizer import MinimumVariancePortfolioOptimizer
from datetime import timedelta
import numpy as np
import pandas as pd
### <summary>
### Provides an implementation of Mean-Variance portfolio optimization based on modern portfolio theory.
### The default model uses the MinimumVariancePortfolioOptimizer that accepts a 63-row matrix of 1-day returns.
### </summary>
class MeanVarianceOptimizationPortfolioConstructionModel(PortfolioConstructionModel):
def __init__(self,
rebalance = Resolution.Daily,
portfolioBias = PortfolioBias.LongShort,
lookback = 1,
period = 63,
resolution = Resolution.Daily,
targetReturn = 0.02,
optimizer = None):
"""Initialize the model
Args:
rebalance: Rebalancing parameter. If it is a timedelta, date rules or Resolution, it will be converted into a function.
If None will be ignored.
The function returns the next expected rebalance time for a given algorithm UTC DateTime.
The function returns null if unknown, in which case the function will be called again in the
next loop. Returning current time will trigger rebalance.
portfolioBias: Specifies the bias of the portfolio (Short, Long/Short, Long)
lookback(int): Historical return lookback period
period(int): The time interval of history price to calculate the weight
resolution: The resolution of the history price
optimizer(class): Method used to compute the portfolio weights"""
self.lookback = lookback
self.period = period
self.resolution = resolution
self.portfolioBias = portfolioBias
self.sign = lambda x: -1 if x < 0 else (1 if x > 0 else 0)
lower = 0 if portfolioBias == PortfolioBias.Long else -1
upper = 0 if portfolioBias == PortfolioBias.Short else 1
self.optimizer = MinimumVariancePortfolioOptimizer(lower, upper, targetReturn) if optimizer is None else optimizer
self.symbolDataBySymbol = {}
# If the argument is an instance of Resolution or Timedelta
# Redefine rebalancingFunc
rebalancingFunc = rebalance
if isinstance(rebalance, int):
rebalance = Extensions.ToTimeSpan(rebalance)
if isinstance(rebalance, timedelta):
rebalancingFunc = lambda dt: dt + rebalance
if rebalancingFunc:
self.SetRebalancingFunc(rebalancingFunc)
def ShouldCreateTargetForInsight(self, insight):
if len(PortfolioConstructionModel.FilterInvalidInsightMagnitude(self.Algorithm, [insight])) == 0:
return False
symbolData = self.symbolDataBySymbol.get(insight.Symbol)
if insight.Magnitude is None:
self.algorithm.SetRunTimeError(ArgumentNullException('MeanVarianceOptimizationPortfolioConstructionModel does not accept \'None\' as Insight.Magnitude. Please checkout the selected Alpha Model specifications.'))
return False
symbolData.Add(self.Algorithm.Time, insight.Magnitude)
return True
def DetermineTargetPercent(self, activeInsights):
"""
Will determine the target percent for each insight
Args:
Returns:
"""
targets = {}
symbols = [insight.Symbol for insight in activeInsights]
# Create a dictionary keyed by the symbols in the insights with an pandas.Series as value to create a data frame
returns = { str(symbol) : data.Return for symbol, data in self.symbolDataBySymbol.items() if symbol in symbols }
returns = pd.DataFrame(returns)
# The portfolio optimizer finds the optional weights for the given data
weights = self.optimizer.Optimize(returns)
weights = pd.Series(weights, index = returns.columns)
# Create portfolio targets from the specified insights
for insight in activeInsights:
weight = weights[str(insight.Symbol)]
# don't trust the optimizer
if self.portfolioBias != PortfolioBias.LongShort and self.sign(weight) != self.portfolioBias:
weight = 0
targets[insight] = weight
return targets
def OnSecuritiesChanged(self, algorithm, changes):
'''Event fired each time the we add/remove securities from the data feed
Args:
algorithm: The algorithm instance that experienced the change in securities
changes: The security additions and removals from the algorithm'''
# clean up data for removed securities
super().OnSecuritiesChanged(algorithm, changes)
for removed in changes.RemovedSecurities:
symbolData = self.symbolDataBySymbol.pop(removed.Symbol, None)
symbolData.Reset()
# initialize data for added securities
symbols = [ x.Symbol for x in changes.AddedSecurities ]
history = algorithm.History(symbols, self.lookback * self.period, self.resolution)
if history.empty: return
tickers = history.index.levels[0]
for ticker in tickers:
symbol = SymbolCache.GetSymbol(ticker)
if symbol not in self.symbolDataBySymbol:
symbolData = self.MeanVarianceSymbolData(symbol, self.lookback, self.period)
symbolData.WarmUpIndicators(history.loc[ticker])
self.symbolDataBySymbol[symbol] = symbolData
class MeanVarianceSymbolData:
'''Contains data specific to a symbol required by this model'''
def __init__(self, symbol, lookback, period):
self.symbol = symbol
self.roc = RateOfChange(f'{symbol}.ROC({lookback})', lookback)
self.roc.Updated += self.OnRateOfChangeUpdated
self.window = RollingWindow[IndicatorDataPoint](period)
def Reset(self):
self.roc.Updated -= self.OnRateOfChangeUpdated
self.roc.Reset()
self.window.Reset()
def WarmUpIndicators(self, history):
for tuple in history.itertuples():
self.roc.Update(tuple.Index, tuple.close)
def OnRateOfChangeUpdated(self, roc, value):
if roc.IsReady:
self.window.Add(value)
def Add(self, time, value):
item = IndicatorDataPoint(self.symbol, time, value)
self.window.Add(item)
@property
def Return(self):
return pd.Series(
data = [(1 + float(x.Value))**252 - 1 for x in self.window],
index = [x.EndTime for x in self.window])
@property
def IsReady(self):
return self.window.IsReady
def __str__(self, **kwargs):
return '{}: {:.2%}'.format(self.roc.Name, (1 + self.window[0])**252 - 1)#region imports
from AlgorithmImports import *
#endregion
from clr import AddReference
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Algorithm.Framework")
AddReference("QuantConnect.Indicators")
from QuantConnect import *
from QuantConnect.Indicators import *
from QuantConnect.Algorithm import *
from QuantConnect.Algorithm.Framework import *
from QuantConnect.Algorithm.Framework.Alphas import *
class EmaCrossAlphaModel(AlphaModel):
'''Alpha model that uses an EMA cross to create insights'''
def __init__(self,
fastPeriod = 12,
slowPeriod = 26,
resolution = Resolution.Daily):
'''Initializes a new instance of the EmaCrossAlphaModel class
Args:
fastPeriod: The fast EMA period
slowPeriod: The slow EMA period'''
self.fastPeriod = fastPeriod
self.slowPeriod = slowPeriod
self.resolution = resolution
self.predictionInterval = Time.Multiply(Extensions.ToTimeSpan(resolution), fastPeriod)
self.symbolDataBySymbol = {}
resolutionString = Extensions.GetEnumString(resolution, Resolution)
self.Name = '{}({},{},{})'.format(self.__class__.__name__, fastPeriod, slowPeriod, resolutionString)
def Update(self, algorithm, data):
'''Updates this alpha model with the latest data from the algorithm.
This is called each time the algorithm receives data for subscribed securities
Args:
algorithm: The algorithm instance
data: The new data available
Returns:
The new insights generated'''
insights = []
for symbol, symbolData in self.symbolDataBySymbol.items():
if symbolData.Fast.IsReady and symbolData.Slow.IsReady:
if symbolData.FastIsOverSlow:
if symbolData.Slow > symbolData.Fast:
insights.append(Insight.Price(symbolData.Symbol, self.predictionInterval, InsightDirection.Down))
elif symbolData.SlowIsOverFast:
if symbolData.Fast > symbolData.Slow:
insights.append(Insight.Price(symbolData.Symbol, self.predictionInterval, InsightDirection.Up))
symbolData.FastIsOverSlow = symbolData.Fast > symbolData.Slow
return insights
def OnSecuritiesChanged(self, algorithm, changes):
'''Event fired each time the we add/remove securities from the data feed
Args:
algorithm: The algorithm instance that experienced the change in securities
changes: The security additions and removals from the algorithm'''
for added in changes.AddedSecurities:
symbolData = self.symbolDataBySymbol.get(added.Symbol)
if symbolData is None:
# create fast/slow EMAs
symbolData = SymbolData(added)
symbolData.Fast = algorithm.EMA(added.Symbol, self.fastPeriod, self.resolution)
symbolData.Slow = algorithm.EMA(added.Symbol, self.slowPeriod, self.resolution)
self.symbolDataBySymbol[added.Symbol] = symbolData
else:
# a security that was already initialized was re-added, reset the indicators
symbolData.Fast.Reset()
symbolData.Slow.Reset()
class SymbolData:
'''Contains data specific to a symbol required by this model'''
def __init__(self, security):
self.Security = security
self.Symbol = security.Symbol
self.Fast = None
self.Slow = None
# True if the fast is above the slow, otherwise false.
# This is used to prevent emitting the same signal repeatedly
self.FastIsOverSlow = False
@property
def SlowIsOverFast(self):
return not self.FastIsOverSlow
# Your New Python File#region imports
from AlgorithmImports import *
#endregion
"""
BLACK-LITTERMAN PORTFOLIO CONSTRUCTION USING HISTORICAL RETURNS ONLY
This strategy demonstrates the QuantConnect Algorithm Framework using a
Black-Litterman portfolio construction model and a simple historical-return alpha
model.
The framework structure is:
1. Universe Selection:
The strategy uses a manual ETF universe. The universe includes U.S. equity
factor ETFs, international factor ETFs, SPY as the broad U.S. equity market
reference, and TLT as a bond diversifier.
2. Alpha Model:
The alpha model uses historical returns only. Once per month, it calculates
each ETF's trailing return over a fixed lookback window. If the trailing return
is positive, the model emits an Up insight. If the trailing return is negative
or zero, the model emits no long insight. This keeps the alpha model simple and
fully tied to realized historical price behavior.
The insight magnitude is based on the trailing historical return and is capped
so the Black-Litterman model does not receive extreme views. The confidence is
also derived from the strength of the trailing return. Stronger historical
returns receive higher confidence, while weaker positive returns receive lower
confidence.
3. Portfolio Construction:
The BlackLittermanOptimizationPortfolioConstructionModel uses the historical
return insights as investor views. It blends those views with equilibrium
market assumptions to create portfolio targets.
4. Execution:
The ImmediateExecutionModel submits orders as soon as the portfolio construction
model creates targets.
5. Risk Management:
A portfolio-level trailing drawdown risk model is included, but it is not overly
restrictive. The drawdown threshold is set at 25%.
The benchmark is 60% SPY and 40% TLT. This benchmark is appropriate because the
strategy invests across equity factor ETFs and a bond ETF.
"""
class MonthlyHistoricalReturnsAlphaModel(AlphaModel):
def __init__(
self,
lookback_days=63,
insight_duration_days=35,
minimum_return=0.00,
maximum_magnitude=0.08
):
self.lookback_days = lookback_days
self.insight_duration = timedelta(days=insight_duration_days)
self.minimum_return = minimum_return
self.maximum_magnitude = maximum_magnitude
self.symbols = []
self.last_emit_month = None
def Update(self, algorithm, data):
insights = []
if algorithm.IsWarmingUp:
return insights
current_month = (algorithm.Time.year, algorithm.Time.month)
# Emit insights only once per month.
# This keeps the Framework stable and avoids repeated insight churn.
if self.last_emit_month == current_month:
return insights
active_count = 0
total_positive_return = 0
for symbol in self.symbols:
if not algorithm.Securities.ContainsKey(symbol):
continue
security = algorithm.Securities[symbol]
if not security.HasData:
continue
history = algorithm.History(
symbol,
self.lookback_days + 1,
Resolution.Daily
)
if history.empty:
continue
closes = history["close"]
if len(closes) < self.lookback_days:
continue
start_price = closes.iloc[0]
end_price = closes.iloc[-1]
if start_price <= 0:
continue
trailing_return = end_price / start_price - 1
# Historical-return-only rule:
# Only positive trailing returns create positive views.
if trailing_return <= self.minimum_return:
continue
active_count += 1
total_positive_return += trailing_return
# Magnitude is the historical return, capped for stability.
magnitude = min(
trailing_return,
self.maximum_magnitude
)
# Confidence increases with the strength of the historical return.
# It is capped so the view does not fully dominate the optimizer.
confidence = 0.40 + 3.0 * min(trailing_return, 0.15)
confidence = min(confidence, 0.90)
insights.append(
Insight.Price(
symbol,
self.insight_duration,
InsightDirection.Up,
magnitude,
confidence
)
)
self.last_emit_month = current_month
algorithm.Plot(
"Alpha Diagnostics",
"Active Historical Return Views",
active_count
)
if active_count > 0:
algorithm.Plot(
"Alpha Diagnostics",
"Average Positive Return",
total_positive_return / active_count
)
algorithm.Debug(
"Historical return alpha emitted "
+ str(len(insights))
+ " insights on "
+ str(algorithm.Time.date())
)
return insights
def OnSecuritiesChanged(self, algorithm, changes):
for security in changes.AddedSecurities:
if security.Symbol not in self.symbols:
self.symbols.append(security.Symbol)
for security in changes.RemovedSecurities:
if security.Symbol in self.symbols:
self.symbols.remove(security.Symbol)
class EnergeticSkyBlueFrog(QCAlgorithm):
def Initialize(self):
# ------------------------------------------------------------
# 1. BACKTEST SETTINGS
# ------------------------------------------------------------
self.SetStartDate(2022, 1, 1)
self.SetEndDate(2026, 5, 5)
self.initial_cash = 100000
self.SetCash(self.initial_cash)
# ------------------------------------------------------------
# 2. UNIVERSE SELECTION
# ------------------------------------------------------------
self.UniverseSettings.Resolution = Resolution.Daily
tickers = [
# U.S. factor ETFs
"USMV",
"DGRO",
"QUAL",
"DVY",
"MTUM",
"VLUE",
# International factor ETFs
"EFAV",
"EEMV",
"IDV",
"IQLT",
# Market and bond references
"SPY",
"TLT"
]
symbols = [
Symbol.Create(ticker, SecurityType.Equity, Market.USA)
for ticker in tickers
]
self.SetUniverseSelection(
ManualUniverseSelectionModel(symbols)
)
# Warm up enough data for the 63-day historical return alpha.
self.SetWarmUp(80, Resolution.Daily)
# ------------------------------------------------------------
# 3. HISTORICAL-RETURNS-ONLY ALPHA MODEL
# ------------------------------------------------------------
self.AddAlpha(
MonthlyHistoricalReturnsAlphaModel(
lookback_days=63,
insight_duration_days=35,
minimum_return=0.00,
maximum_magnitude=0.08
)
)
# ------------------------------------------------------------
# 4. BLACK-LITTERMAN PORTFOLIO CONSTRUCTION
# ------------------------------------------------------------
self.SetPortfolioConstruction(
BlackLittermanOptimizationPortfolioConstructionModel()
)
# ------------------------------------------------------------
# 5. EXECUTION MODEL
# ------------------------------------------------------------
self.SetExecution(
ImmediateExecutionModel()
)
# ------------------------------------------------------------
# 6. RISK MANAGEMENT MODEL
# ------------------------------------------------------------
self.risk_drawdown_limit = 0.25
self.SetRiskManagement(
MaximumDrawdownPercentPortfolio(
self.risk_drawdown_limit,
isTrailing=True
)
)
# ------------------------------------------------------------
# 7. BENCHMARK
# ------------------------------------------------------------
self._benchmark_spy = self.AddEquity(
"SPY",
Resolution.Daily,
Market.USA
).Symbol
self._benchmark_tlt = self.AddEquity(
"TLT",
Resolution.Daily,
Market.USA
).Symbol
self.SetBenchmark(self._benchmark_spy)
self.initial_spy_price = None
self.initial_tlt_price = None
self.benchmark_spy_weight = 0.60
self.benchmark_tlt_weight = 0.40
# ------------------------------------------------------------
# 8. DIAGNOSTIC STATE VARIABLES
# ------------------------------------------------------------
self.strategy_peak = self.initial_cash
self.benchmark_peak = self.initial_cash
def OnData(self, data):
# ------------------------------------------------------------
# 1. CHECK BENCHMARK DATA
# ------------------------------------------------------------
if self._benchmark_spy not in data or data[self._benchmark_spy] is None:
return
if self._benchmark_tlt not in data or data[self._benchmark_tlt] is None:
return
spy_price = self.Securities[self._benchmark_spy].Price
tlt_price = self.Securities[self._benchmark_tlt].Price
if spy_price <= 0 or tlt_price <= 0:
return
if self.initial_spy_price is None:
self.initial_spy_price = spy_price
if self.initial_tlt_price is None:
self.initial_tlt_price = tlt_price
# ------------------------------------------------------------
# 2. CUSTOM BENCHMARK VALUE
# ------------------------------------------------------------
benchmark_value = (
self.initial_cash
* (
self.benchmark_spy_weight
* spy_price
/ self.initial_spy_price
+
self.benchmark_tlt_weight
* tlt_price
/ self.initial_tlt_price
)
)
# ------------------------------------------------------------
# 3. STRATEGY VS BENCHMARK
# ------------------------------------------------------------
self.Plot(
"Strategy Equity",
"Portfolio Value",
self.Portfolio.TotalPortfolioValue
)
self.Plot(
"Strategy Equity",
"Benchmark 60 pct SPY 40 pct TLT",
benchmark_value
)
# ------------------------------------------------------------
# 4. PORTFOLIO STATE
# ------------------------------------------------------------
invested_value = 0
active_holdings = 0
for holding in self.Portfolio.Values:
if holding.Invested:
invested_value += abs(holding.HoldingsValue)
active_holdings += 1
if self.Portfolio.TotalPortfolioValue > 0:
invested_weight = (
invested_value
/ self.Portfolio.TotalPortfolioValue
)
cash_weight = 1 - invested_weight
self.Plot(
"Portfolio State",
"Invested Weight",
invested_weight
)
self.Plot(
"Portfolio State",
"Cash Weight",
cash_weight
)
self.Plot(
"Portfolio Diagnostics",
"Active Holdings",
active_holdings
)
# ------------------------------------------------------------
# 5. DRAWDOWN DIAGNOSTICS
# ------------------------------------------------------------
self.strategy_peak = max(
self.strategy_peak,
self.Portfolio.TotalPortfolioValue
)
self.benchmark_peak = max(
self.benchmark_peak,
benchmark_value
)
strategy_drawdown = (
self.Portfolio.TotalPortfolioValue
/ self.strategy_peak
- 1
)
benchmark_drawdown = (
benchmark_value
/ self.benchmark_peak
- 1
)
self.Plot(
"Drawdown",
"Strategy Drawdown",
strategy_drawdown
)
self.Plot(
"Drawdown",
"Benchmark Drawdown",
benchmark_drawdown
)
# ------------------------------------------------------------
# 6. RISK LIMIT VISUALIZATION
# ------------------------------------------------------------
self.Plot(
"Risk Management",
"Drawdown Limit",
-self.risk_drawdown_limit
)
risk_triggered_marker = 0
if strategy_drawdown <= -self.risk_drawdown_limit:
risk_triggered_marker = 1
self.Plot(
"Risk Management",
"Risk Triggered",
risk_triggered_marker
)