Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Branch 1 #5

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
21 changes: 9 additions & 12 deletions problem1/problem1_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@
from backtester.constants import *
from backtester.features.feature import Feature
from backtester.logger import *
from backtester.versions import versions
import pandas as pd
import numpy as np
import sys
from sklearn import linear_model
from sklearn import metrics as sm
from problem1_trading_params import MyTradingParams
try:
from urllib2 import urlopen
except ImportError:
from urllib.request import urlopen
import collections

## Make your changes to the functions below.
## SPECIFY the symbols you are modeling for in getSymbolsToTrade() below
Expand All @@ -25,16 +31,13 @@
## Don't change any other function
## The toolbox does the rest for you, from downloading and loading data to running backtest


class MyTradingFunctions():


def __init__(self): #Put any global variables here
self.lookback = 1200 ## max number of historical datapoints you want at any given time
self.targetVariable = 'Y'
if datetime.today() < datetime(2018, 7, 3):
self.dataSetId = 'QQ3DataSample'
else:
self.dataSetId = 'QQ3DataDownSampled'
self.dataSetId = ''
self.params = {}

# for example you can import and store an ML model from scikit learn in this dict
Expand Down Expand Up @@ -74,7 +77,6 @@ def getSymbolsToTrade(self):
'''

def getInstrumentFeatureConfigDicts(self):

##############################################################################
### TODO 2a: FILL THIS FUNCTION TO CREATE DESIRED FEATURES for each symbol. ###
### USE TEMPLATE BELOW AS EXAMPLE ###
Expand Down Expand Up @@ -104,7 +106,6 @@ def getMarketFeatureConfigDicts(self):
### TODO 2b: FILL THIS FUNCTION TO CREATE features that use multiple symbols ###
### USE TEMPLATE BELOW AS EXAMPLE ###
###############################################################################

# customFeatureDict = {'featureKey': 'custom_mrkt_feature',
# 'featureId': 'my_custom_mrkt_feature',
# 'params': {'param1': 'value1'}}
Expand Down Expand Up @@ -155,7 +156,6 @@ def getPrediction(self, time, updateNum, instrumentManager,predictions):
ma1 = lookbackInstrumentFeatures.getFeatureDf('ma_5') #DF with rows=timestamp and columns=stockIDS
ma2 = lookbackInstrumentFeatures.getFeatureDf('ma_10') #DF with rows=timestamp and columns=stockIDS
factor2Values = (ma1/ma2) #DF with rows=timestamp and columns=stockIDS

# Now looping over all stocks:
for s in self.getSymbolsToTrade():
#Creating a dataframe to hold features for this stock
Expand Down Expand Up @@ -194,7 +194,6 @@ def getPrediction(self, time, updateNum, instrumentManager,predictions):
threshold = 0.8
predictions[s] = 1 if y_predict>threshold else 0.5
predictions[s] = 0 if y_predict<(1-threshold) else 0.5

return predictions

###########################################
Expand Down Expand Up @@ -243,7 +242,6 @@ def getCustomFeatures(self):
def computeForInstrument(cls, updateNum, time, featureParams, featureKey, instrumentManager):
# Custom parameter which can be used as input to computation of this feature
param1Value = featureParams['param1']

# A holder for the all the instrument features
lookbackInstrumentFeatures = instrumentManager.getLookbackInstrumentFeatures()

Expand All @@ -254,14 +252,13 @@ def computeForInstrument(cls, updateNum, time, featureParams, featureKey, instru
# The last row of the previous dataframe gives the last calculated value for that feature (basis in this case)
# This returns a series with symbols/instrumentIds as the index.
currentValue = lookbackInstrumentValue.iloc[-1]

if param1Value == 'value1':
return currentValue * 0.1
else:
return currentValue * 0.5


if __name__ == "__main__":
versions()
if updateCheck():
print('Your version of the auquan toolbox package is old. Please update by running the following command:')
print('pip install -U auquan_toolbox')
Expand Down
10 changes: 9 additions & 1 deletion problem1/problem1_trading_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
import sys
from sklearn import linear_model
from sklearn import metrics as sm
try:
from urllib2 import urlopen
except ImportError:
from urllib.request import urlopen

## Make your changes to the functions below.
## SPECIFY the symbols you are modeling for in getSymbolsToTrade() below
Expand All @@ -32,7 +36,11 @@ class MyTradingParams(TradingSystemParameters):
'''
def __init__(self, tradingFunctions):
self.__tradingFunctions = tradingFunctions
self.__dataSetId = self.__tradingFunctions.getDataSetId()

url = "https://raw.githubusercontent.com/Auquan/data_set_id/master/DataSetId.txt"
response = urlopen(url)
self.__dataSetId = response.read().decode('utf8').rstrip() + self.__tradingFunctions.getDataSetId()

self.__instrumentIds = self.__tradingFunctions.getSymbolsToTrade()
self.__priceKey = 'F5'
self.__additionalInstrumentFeatureConfigDicts = []
Expand Down