Overall Statistics
Total Trades
72
Average Win
2.02%
Average Loss
-1.37%
Compounding Annual Return
58.430%
Drawdown
15.700%
Expectancy
0.652
Net Profit
35.838%
Sharpe Ratio
2.304
Loss Rate
33%
Win Rate
67%
Profit-Loss Ratio
1.48
Alpha
0.826
Beta
-17.767
Annual Standard Deviation
0.206
Annual Variance
0.043
Information Ratio
2.208
Tracking Error
0.207
Treynor Ratio
-0.027
Total Fees
$72.00
import numpy as np
from sklearn.ensemble import GradientBoostingRegressor

# Adapted from https://www.quantopian.com/posts/simple-machine-learning-example-mk-ii

class SimpleML(QCAlgorithm):
  
    def Initialize(self):
  
        self.SetStartDate(2018,1,1)
        self.SetEndDate(2018,9,1)
        self.SetCash(100000)
        self.AddEquity('AMZN', Resolution.Daily)
        
        self.model = GradientBoostingRegressor()
        
        self.lookback = 30
        self.history_range = 200
        
        self.X = []
        self.y = []
        
        self.Schedule.On(self.DateRules.WeekStart(), self.TimeRules.BeforeMarketClose('AMZN', minutes=10), Action(self.create_model))
        self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen('AMZN', minutes=1), Action(self.trade))

    def OnData(self, data):

        pass
            
    def create_model(self):
        
        recent_prices = self.History(['AMZN'], self.history_range)['close'].values
        price_changes = np.diff(recent_prices).tolist()
        
        for i in range(self.history_range-self.lookback-1):
            self.X.append(price_changes[i:i+self.lookback])
            self.y.append(price_changes[i+self.lookback])
            
        self.model.fit(self.X, self.y)
    
    def trade(self):
        
        if len(self.y) > self.lookback:
            
            recent_prices = self.History(['AMZN'], self.lookback+1)['close'].values
            price_changes = np.diff(recent_prices)
            
            prediction = self.model.predict(price_changes.reshape(1, -1))
            
            if prediction > 0:
                self.SetHoldings('AMZN', 1.0)
            else:
                self.SetHoldings('AMZN', 0)