Overall Statistics
Total Orders
2632
Average Win
0.11%
Average Loss
-0.08%
Compounding Annual Return
9.923%
Drawdown
28.600%
Expectancy
0.128
Start Equity
100000
End Equity
129008.42
Net Profit
29.008%
Sharpe Ratio
0.243
Sortino Ratio
0.322
Probabilistic Sharpe Ratio
12.720%
Loss Rate
53%
Win Rate
47%
Profit-Loss Ratio
1.38
Alpha
0.051
Beta
-0.093
Annual Standard Deviation
0.203
Annual Variance
0.041
Information Ratio
0.121
Tracking Error
0.26
Treynor Ratio
-0.531
Total Fees
$4473.07
Estimated Strategy Capacity
$39000000.00
Lowest Capacity Asset
DROOY R735QTJ8XC9X
Portfolio Turnover
7.71%
# region imports
from AlgorithmImports import *
# endregion

class FatRedMosquito(QCAlgorithm):

    def initialize(self):
        self.set_start_date(2022, 1, 1)
        self.set_cash(100000)
        self.universe_settings.resolution = Resolution.DAILY

        # Gold miners ETF constituents
        symbol = Symbol.create("GDX", SecurityType.EQUITY, Market.USA)
        universe = WeightETFConstituentsUniverseSelectionModel(symbol, self.universe_settings)
        self.add_universe_selection(universe)
        # Alpha specifically for gold miners
        gold = self.add_equity("GLD", Resolution.DAILY).symbol
        self.add_alpha(GoldMinerAlphaModel(gold, universe))

        self.set_portfolio_construction(InsightWeightingPortfolioConstructionModel(Expiry.END_OF_DAY))


class WeightETFConstituentsUniverseSelectionModel(ETFConstituentsUniverseSelectionModel):
    def __init__(self, symbol, settings):
        super().__init__(symbol, settings, self.etf_constituents_filter)

    def etf_constituents_filter(self, constituents):
        self.weights = {x.symbol: x.weight for x in constituents if x.weight}
        return list(self.weights.keys())

    
class GoldMinerAlphaModel(AlphaModel):
    def __init__(self, gold, universe):
        '''Instantiate a new instance of GoldMinerAlphaModel
        Parameter
        ---------
        - gold: Symbol
            Symbol of gold asset
        - universe: Universe
            A gold miner security universe reference that adapt GoldMinerAlphaModel
        '''
        self.gold = gold
        self.universe = universe

    def update(self, algorithm, data):
        insights = []

        if data.bars.contains_key(self.gold):
            gold = algorithm.securities[self.gold]
            gold_ema50 = gold.ema50.current.value

            if data.bars[self.gold].close > gold_ema50:
                insights.extend([
                    Insight.Price(symbol, timedelta(1), InsightDirection.UP, weight=weight) for symbol, weight in self.universe.weights.items()
                ])
            else:
                insights.extend([
                    Insight.Price(symbol, timedelta(1), InsightDirection.DOWN, weight=weight) for symbol, weight in self.universe.weights.items()
                ])

        return insights

    def on_securities_changed(self, algorithm, changes):
        for added in changes.added_securities:
            if added.symbol == self.gold:
                added.ema50 = algorithm.EMA(added.symbol, 50, Resolution.DAILY)
                algorithm.warm_up_indicator(added.symbol, added.ema50)