Overall Statistics
Total Trades
706
Average Win
1.22%
Average Loss
-0.35%
Compounding Annual Return
1.855%
Drawdown
16.800%
Expectancy
0.272
Net Profit
10.643%
Sharpe Ratio
0.217
Probabilistic Sharpe Ratio
3.646%
Loss Rate
71%
Win Rate
29%
Profit-Loss Ratio
3.44
Alpha
-0.013
Beta
0.293
Annual Standard Deviation
0.12
Annual Variance
0.014
Information Ratio
-0.63
Tracking Error
0.17
Treynor Ratio
0.089
Total Fees
$0.00
class DynamicNadionCompensator(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2015, 1, 15)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash
        
        self.SetSecurityInitializer(self.CustomSecurityInitializer)
        
        self.AddEquity('SPY', Resolution.Hour)
        
        self.sma = self.SMA('SPY', 50, Resolution.Hour)
        self.SetWarmup(50)
        
        self.month_counter = 0
        self.Schedule.On(self.DateRules.MonthEnd('SPY'), self.TimeRules.BeforeMarketClose('SPY'), self.Yearly)
        
        self.prev_cumulative_profit = 0
        
        
    def OnData(self, data):
        if self.IsWarmingUp:
            return
        
        if data.ContainsKey('SPY') and data['SPY'] is not None:
            if data['SPY'].Close > self.sma.Current.Value:
                self.SetHoldings('SPY', 1.0)
            else:
                self.Liquidate('SPY')
  
    def Yearly(self):
        self.month_counter += 1
        if self.month_counter % 12 == 0:
            self.Plot('Total Profit', self.Portfolio.TotalProfit)
            self.Plot('Separate Chart', 'Total Holdings', self.Portfolio.TotalHoldingsValue)
            
        # alternatively
        if self.Time.month == 11:  #
            self.Debug('Month End')
            self.IncorporateFees()
            
    def IncorporateFees(self):
        fees = .002 * self.Portfolio.TotalHoldingsValue
        
        current_year_profit = self.Portfolio.TotalProfit - self.prev_cumulative_profit
        self.prev_cumulative_profit = self.Portfolio.TotalProfit
        
        if current_year_profit > 0:
            fees += current_year_profit
            
        self.Plot('Custom', 'Fees', fees)
        
        cash_after_fees = self.Portfolio.Cash - fees
        
        if cash_after_fees < 0:
            self.MarketOrder('SPY', -int(cash_after_fees / self.Securities['SPY'].Price + 1))
            
        self.Portfolio.SetCash(cash_after_fees)
        
    def CustomSecurityInitializer(self, security):
        security.SetFeeModel(CustomFeeModel())
        
class CustomFeeModel:
    def GetOrderFee(self, parameters):
        return OrderFee(CashAmount(0, 'USD'))