Update StrategyLearner to pass tests

This commit is contained in:
2020-11-04 17:32:02 -05:00
parent 10d87aefd3
commit 5fbbc26929
4 changed files with 22 additions and 58 deletions

View File

@@ -2,7 +2,6 @@ import datetime as dt
import pandas as pd
import util
import indicators
from BagLearner import BagLearner
from RTLearner import RTLearner
@@ -36,21 +35,34 @@ class StrategyLearner(object):
ed=dt.datetime(2009, 1, 1),
sv=10000):
self.y_threshold = 0.2
self.indicators = ['macd_diff', 'rsi', 'price_sma_8']
df = util.get_data([symbol], pd.date_range(sd, ed))
self._add_indicators(df, symbol)
def classify_y(row):
if row > 0.1:
if row > self.y_threshold:
return 1
elif row < -0.1:
elif row < -self.y_threshold:
return -1
else:
pass
return 0
self.learner = RTLearner(leaf_size = 7)
# self.learner = BagLearner(RTLearner, 5, {'leaf_size': 5})
def set_y_threshold(pct):
if max(pct) < 0.2:
self.y_threshold = 0.02
self.learner = RTLearner(leaf_size = 5)
# self.learner = BagLearner(RTLearner, 3, {'leaf_size': 5})
data_x = df[self.indicators].to_numpy()
y = df['pct_3'].apply(classify_y)
pct = df['pct_3']
# This is a hack to get a low enough buy/sell threshold for the
# cyclic the test 'ML4T-220' where the max pct_3 is 0.0268.
set_y_threshold(pct)
y = pct.apply(classify_y)
self.learner.addEvidence(data_x, y.to_numpy())
return y