Files
ML4T/strategy_evaluation/ManualStrategy.py

59 lines
2.1 KiB
Python

import datetime as dt
import pandas as pd
import util as ut
class ManualStrategy:
def __init__(self, verbose=False, impact=0.0, commission=0.0):
self.verbose = verbose
self.impact = impact
self.commission = commission
# this method should create a QLearner, and train it for trading
def addEvidence(self, symbol="IBM",
sd=dt.datetime(2008, 1, 1),
ed=dt.datetime(2009, 1, 1),
sv=10000):
# add your code to do learning here
# example usage of the old backward compatible util function
syms = [symbol]
dates = pd.date_range(sd, ed)
prices_all = ut.get_data(syms, dates) # automatically adds SPY
prices = prices_all[syms] # only portfolio symbols
# prices_SPY = prices_all['SPY'] # only SPY, for comparison later
if self.verbose:
print(prices)
# example use with new colname
# automatically adds SPY
volume_all = ut.get_data(syms, dates, colname="Volume")
volume = volume_all[syms] # only portfolio symbols
# volume_SPY = volume_all['SPY'] # only SPY, for comparison later
if self.verbose:
print(volume)
# this method should use the existing policy and test it against new data
def testPolicy(self, symbol="IBM",
sd=dt.datetime(2009, 1, 1),
ed=dt.datetime(2010, 1, 1),
sv=10000):
dates = pd.date_range(sd, ed)
prices = ut.get_data([symbol], dates) # automatically adds SPY
orders = pd.DataFrame(index=prices.index)
orders["Symbol"] = symbol
orders["Order"] = ""
orders["Shares"] = 0
# here we build a fake set of trades
orders.iloc[0] = [symbol, "BUY", 1000]
orders.iloc[40] = [symbol, "SELL", 1000]
orders.iloc[41] = [symbol, "BUY", 1000]
orders.iloc[60] = [symbol, "SELL", 2000]
orders.iloc[61] = [symbol, "BUY", 2000]
orders.iloc[-1] = [symbol, "SELL", 1000]
orders = orders[orders["Shares"] != 0]
return orders