From f29c83e941a685fdc237772cf71b0cd6b37f4ac1 Mon Sep 17 00:00:00 2001 From: fawce Date: Sun, 26 Feb 2012 20:08:10 -0500 Subject: [PATCH] renamed factory to singular --- zipline/test/factory.py | 107 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 zipline/test/factory.py diff --git a/zipline/test/factory.py b/zipline/test/factory.py new file mode 100644 index 00000000..60945e79 --- /dev/null +++ b/zipline/test/factory.py @@ -0,0 +1,107 @@ +import datetime +import pytz +from algorithm.quantoenv import * +from algorithm.quantomodels import * +from algorithm.hostedalgorithm import * +from algorithm.risk import * + +def createReturns(daycount, start): + i = 0 + test_range = [] + current = start.replace(tzinfo=pytz.utc) + one_day = datetime.timedelta(days = 1) + while i < daycount: + i += 1 + r = daily_return(current, random.random()) + test_range.append(r) + current = current + one_day + return [ x for x in test_range if(trading_calendar.is_trading_day(x.date)) ] + +def createReturnsFromRange(start, end): + current = start.replace(tzinfo=pytz.utc) + end = end.replace(tzinfo=pytz.utc) + one_day = datetime.timedelta(days = 1) + test_range = [] + i = 0 + while current <= end: + current = current + one_day + if(not trading_calendar.is_trading_day(current)): + continue + r = daily_return(current, random.random()) + i += 1 + test_range.append(r) + + return test_range +def createReturnsFromList(returns, start): + current = start.replace(tzinfo=pytz.utc) + one_day = datetime.timedelta(days = 1) + test_range = [] + i = 0 + while len(test_range) < len(returns): + if(trading_calendar.is_trading_day(current)): + r = daily_return(current, returns[i]) + i += 1 + test_range.append(r) + current = current + one_day + return test_range + + +def createAlgo(filename): + algo = Algorithm() + algo.code = getCodeFromFile(filename) + algo.title = filename + algo._id = pymongo.objectid.ObjectId() + hostedAlgo = HostedAlgorithm(algo) + return hostedAlgo + +def getCodeFromFile(filename): + rVal = None + with open('./test/algo_samples/' + filename, 'r') as f: + rVal = f.read() + return rVal + + +def createTrade(sid, price, amount, datetime): + row = {} + row['sid'] = sid + row['dt'] = datetime + row['price'] = price + row['volume'] = amount + row['exchange_code'] = "fake exchange" + db = getTickDB() + db.equity.trades.minute.insert(row,safe=True) + dw = DocWrap() + dw.store = row + return dw + +def createTradeHistory(sid, priceList, amtList, startTime, interval): + i = 0 + trades = [] + current = startTime + while i < len(priceList): + if(trading_calendar.is_trading_day(current)): + trades.append(createTrade(sid, priceList[i], amtList[i], current)) + current = current + interval + i += 1 + else: + current = current + datetime.timedelta(days=1) + + return trades + +def createTxn(sid, price, amount, datetime, btrid=None): + txn = Transaction(sid=sid, amount=amount, dt = datetime, + price=price, transaction_cost=-1*price*amount) + return txn + +def createTxnHistory(sid, priceList, amtList, startTime, interval): + i = 0 + txns = [] + current = startTime + while i < len(priceList): + if(trading_calendar.is_trading_day(current)): + txns.append(createTxn(sid,priceList[i],amtList[i], current)) + current = current + interval + i += 1 + else: + current = current + datetime.timedelta(days=1) + return txns \ No newline at end of file