Overall Statistics
Total Orders
96
Average Win
3.02%
Average Loss
-2.93%
Compounding Annual Return
-0.351%
Drawdown
45.800%
Expectancy
-0.110
Start Equity
100000
End Equity
98255.56
Net Profit
-1.744%
Sharpe Ratio
-0.037
Sortino Ratio
-0.05
Probabilistic Sharpe Ratio
0.855%
Loss Rate
56%
Win Rate
44%
Profit-Loss Ratio
1.03
Alpha
-0.09
Beta
1.157
Annual Standard Deviation
0.249
Annual Variance
0.062
Information Ratio
-0.416
Tracking Error
0.189
Treynor Ratio
-0.008
Total Fees
$146.09
Estimated Strategy Capacity
$2900000.00
Lowest Capacity Asset
SSNT WJTX1OBQ409X
Portfolio Turnover
0.48%
Drawdown Recovery
84
#region imports
from AlgorithmImports import *
#endregion


class PriceEarningsAnamoly(QCAlgorithm):

    def initialize(self):
        self.set_start_date(self.end_date - timedelta(5*365))        
        self.set_cash(100000)
        self.set_benchmark("SPY")
        self.settings.seed_initial_prices = True
        # Define some parameters.
        self._liquidity_filter_size = 200
        self._universe_size = 10
        # Add a universe of US Equities.
        self._date_rule = self.date_rules.year_start('SPY')
        self.universe_settings.schedule.on(self._date_rule)
        self._universe = self.add_universe(self._select_assets)
        # Add a warm-up period so the algorithm trades on deployment
        # instead of waiting for the new year.
        self.set_warm_up(timedelta(400))
        
    def _select_assets(self, fundamentals):       
        # Select the most liquid stocks.
        filtered = [f for f in fundamentals if f.price > 5 and f.has_fundamental_data]
        filtered = sorted(filtered, key=lambda x: x.dollar_volume)[-self._liquidity_filter_size:]
        # Select the subset of stocks with the lowest PE ratios.
        filtered = [f for f in filtered if f.valuation_ratios.pe_ratio > 0]
        filtered = sorted(filtered, key=lambda x: x.valuation_ratios.pe_ratio)[:self._universe_size]
        return [f.symbol for f in filtered]
    
    def on_warmup_finished(self):
        # Add a Scheduled Event to rebalance the portfolio yearly.
        self.schedule.on(self._date_rule, self.time_rules.at(8, 0), self._rebalance)
        # Rebalance today too.
        self._rebalance()
    
    def _rebalance(self):
        # Form an equal-weighted portfolio.
        targets = [PortfolioTarget(symbol, 1/self._universe_size) for symbol in self._universe.selected]
        self.set_holdings(targets, True)