Using stock historical data, train a supervised learning algorithm with any combination of financial indicators. Rapidly backtest your model for accuracy and simulate investment portfolio performance.
pip install clairvoyant
During the testing period, the model signals to buy or sell based on its prediction for price movement the following day. By putting your trading algorithm aside and testing for signal accuracy alone, you can rapidly build and test more reliable models.
from clairvoyant import Backtest
from pandas import read_csv
# Testing performance on a single stock
variables = ["SSO", "SSC"] # Financial indicators of choice
trainStart = '2013-03-01' # Start of training period
trainEnd = '2015-07-15' # End of training period
testStart = '2015-07-16' # Start of testing period
testEnd = '2016-09-17' # End of training period
buyThreshold = 0.65 # Confidence threshold for predicting buy (default = 0.65)
sellThreshold = 0.65 # Confidence threshold for predicting sell (default = 0.65)
C = 1 # Penalty parameter (default = 1)
gamma = 10 # Kernel coefficient (default = 10)
continuedTraining = False # Continue training during testing period? (default = false)
backtest = Backtest(variables, trainStart, trainEnd, testStart, testEnd)
data = read_csv("Stocks/SBUX.csv") # Read in data
data = data.round(3) # Round all values
backtest.stocks.append("SBUX") # Inform the model which stock is being tested
for i in range(0,10): # Run the model 10-15 times
backtest.runModel(data)
# Testing performance across multiple stocks
stocks = ["AAPL", "ADBE", "AMGN", "AMZN",
"BIIB", "EBAY", "GILD", "GRPN",
"INTC", "JBLU", "MSFT", "NFLX",
"SBUX", "TSLA", "VRTX", "YHOO"]
for stock in stocks:
data = read_csv('Stocks/%s.csv' % stock)
data = data.round(3)
backtest.stocks.append(stock)
for i in range(0,10):
backtest.runModel(data)
backtest.displayConditions()
backtest.displayStats()
Conditions X1: SSO X2: SSC Buy Threshold: 65.0% Sell Threshold: 65.0% C: 1 gamma: 10 Continued Training: False
Stats Stock(s): AAPL | Training: 03/01/2013-07/15/2015 Testing: 07/16/2015-07/15/2016 ADBE | Training: 03/01/2013-07/15/2015 Testing: 07/16/2015-07/15/2016 AMGN | Training: 03/01/2013-07/15/2015 Testing: 07/16/2015-07/15/2016 AMZN | Training: 03/01/2013-07/15/2015 Testing: 07/16/2015-07/15/2016 BIIB | Training: 03/01/2013-07/15/2015 Testing: 07/16/2015-07/15/2016 EBAY | Training: 03/01/2013-07/15/2015 Testing: 07/16/2015-07/15/2016 GILD | Training: 03/01/2013-07/15/2015 Testing: 07/16/2015-07/15/2016 GRPN | Training: 03/01/2013-07/15/2015 Testing: 07/16/2015-07/15/2016 INTC | Training: 03/01/2013-07/15/2015 Testing: 07/16/2015-07/15/2016 JBLU | Training: 03/01/2013-07/15/2015 Testing: 07/16/2015-07/15/2016 MSFT | Training: 03/01/2013-07/15/2015 Testing: 07/16/2015-07/15/2016 NFLX | Training: 03/01/2013-07/15/2015 Testing: 07/16/2015-07/15/2016 SBUX | Training: 03/01/2013-07/15/2015 Testing: 07/16/2015-07/15/2016 TSLA | Training: 03/01/2013-07/15/2015 Testing: 07/16/2015-07/15/2016 VRTX | Training: 03/01/2013-07/15/2015 Testing: 07/16/2015-07/15/2016 YHOO | Training: 03/01/2013-07/15/2015 Testing: 07/16/2015-07/15/2016
Total Buys: 39 Buy Accuracy: 62.86% Total Sells: 20 Sell Accuracy: 70.41%
from clairvoyant import Portfolio
from pandas import read_csv
variables = ["SSO", "SC"] # Financial indicators of choice
trainStart = '2013-03-01' # Start of training period
trainEnd = '2015-07-15' # End of training period
testStart = '2015-07-16' # Start of testing period
testEnd = '2016-07-17' # End of training period
buyThreshold = 0.65 # Confidence threshold for predicting buy (default = 0.65)
sellThreshold = 0.65 # Confidence threshold for predicting sell (default = 0.65)
C = 1 # Penalty parameter (default = 1)
gamma = 10 # Kernel coefficient (default = 10)
continuedTraining = False # Continue training during testing period? (default = false)
startingBalance = 1000000 # Starting balance of portfolio
# User defined trading logic (see below)
def buyLogic(self, confidence, data, testDay)
def sellLogic(self, confidence, data, testDay)
def nextDayLogic(self, prediction, nextDayPerformance, data, testDay)
portfolio = Portfolio(variables, trainStart, trainEnd, testStart, testEnd)
data = read_csv("Stocks/SBUX.csv")
data = data.round(3)
for i in range(0,5):
portfolio.runModel(data, startingBalance, buyLogic, sellLogic, nextDayLogic)
portfolio.displayLastRun()
portfolio.displayAllRuns()
variables = ["SSO"] # 1 feature
variables = ["SSO", "SSC"] # 2 features
variables = ["SSO", "SSC", "RSI"] # 3 features
variables = ["SSO", "SSC", "RSI", ... , Xn] # n features
backtest.visualizeModel()