def setUp(self): ''' called at the beginning of each test ''' self.log_handler = logbook.TestHandler() self.log_handler.push_thread() self.tickers = ['google', 'apple', 'starbucks'] self.fields = ['close', 'volume'] self.start = dt.datetime(2006, 1, 1, tzinfo = pytz.utc) self.end = dt.datetime(2010, 12, 31, tzinfo = pytz.utc) self.offset = pd.datetools.BMonthEnd() self.agent = DataAgent()
def setUp(self): ''' called at the beginning of each test ''' self.tickers = ['starbucks'] self.fields = ['low', 'adj_close', 'volume'] self.start = dt.datetime(2004, 12, 1, tzinfo=pytz.utc) self.end = dt.datetime(2012, 12, 31, tzinfo=pytz.utc) self.offset = pd.datetools.BMonthEnd(4) self.timestamp = pd.date_range(self.start, self.end, freq=self.offset) self.agent = DataAgent() self.agent.connectTo(['remote', 'database'], db_name='stocks.db', lvl='critical')
'''-------------------------------------- Notes Zipline uses a benchmark data very interesting (see utils/factory.py and data/benchmark.py) Modifying it to take in parameter an action and to process associated market ? only s&p500 at the moment ''' if __name__ == '__main__': ''' ----------------------------------------------------------------- Data -----''' # Thiz tzinfo is annoying ! default to provide if forgotten, or the one sent #through the init of dataagent. See also zipline.loader_utils.py timestamp = date_range(dt.datetime(2004,12,31, tzinfo=pytz.utc), \ dt.datetime(2005,12,31, tzinfo=pytz.utc), freq='M') ticker = 'starbucks' #TODO implement a how method wich give the same results as in table #put in some DataAgent properties ? db_agent = DataAgent('stocks.db') data = db_agent.getQuotes([ticker], ['adj_close'], index=timestamp, \ save=False, reverse=True) #ts = Series(data[ticker]['adj_close'], name=ticker) ts = data['adj_close'][ticker] #ts = Series([31.18, 27.0, 25.91, 25.83, 24.76, 27.4, 25.83, 26.27, \ #24.51, 25.05, 28.28, 30.45, 30.51], index=timestamp) ''' ----------------------------------------------------------------------------------''' print print('-' * 60) #1 Simple monthly return between the end of December 2004 and the end of January 2005 compute = Quantitative(lvl='error') monthly_ret = compute.returns(ts, start=dt.datetime(2004, 12, 31),
sys.path.append(str(os.environ['QTRADE'])) from pyTrade.data.DataAgent import DataAgent from pyTrade.calculus.engine import Backtester from pyTrade.utils import LogSubsystem import pandas as pd import pytz from zipline.data.benchmarks import * from pyTrade.ai.genetic import Genetic, GeneticAlgorithm start = pd.datetime(2008, 6, 20, 0, 0, 0, 0, pytz.utc) end = pd.datetime(2010, 4, 1, 0, 0, 0, 0, pytz.utc) delta = pd.datetools.timedelta(days=1) dataobj = DataAgent('stocks.db') data_tmp = dataobj.getQuotes(['google'], ['open'], start=start, end=end, delta=delta, reverse=True) data = data_tmp['open'] def runBacktest(genes): '''-------------------------------------------- Parameters -----''' '''chromosome[short_window, long_window, buy_on_event, sell_on_event] ''' print('chromosome: {}'.format(genes)) short_window = genes['short_window'] long_window = round(short_window * genes['ma_rate'] / 2) buy_on_event = genes['buy_n']