python marketsim.py 1000000 orders.csv values.csv使用sys模块的argv即可。
import pandas as pd import numpy as np import math import copy import QSTK.qstkutil.qsdateutil as du import datetime as dt import QSTK.qstkutil.DataAccess as da import QSTK.qstkutil.tsutil as tsu import QSTK.qstkstudy.EventProfiler as ep #get order #sys.argv to get comman parameter na_data = np.loadtxt('orders2.csv',dtype=np.str,delimiter=',') #dtype={'names':('year','month','day','equity','buorsell','count'), 'formats':('i4','i4','i4','S5','S5','i4')}, na_dates=np.int_(na_data[:,0:3]) order=na_data[:,3:6] ls_symbols=set(order[:,0]) #get equity price dt_start = dt.datetime(2011, 1, 1) dt_end = dt.datetime(2011, 12, 31) ldt_timestamps = du.getNYSEdays(dt_start, dt_end, dt.timedelta(hours=16)) dataobj = da.DataAccess('Yahoo') #why close? #close for Adjusted Close ;actual_close for actual close ls_keys = 'close'#['open', 'high', 'low', 'close', 'volume', 'actual_close'] ldf_data = dataobj.get_data(ldt_timestamps, ls_symbols, ls_keys) #calc portfolio currentCash=1000000 currentEquity=dict() byOrSellDict={'Buy':1,'Sell':-1} #dateInd=0 #currentDate=dt.datetime(na_dates[dateInd,0],na_dates[dateInd,1],na_dates[dateInd,2])+dt.timedelta(hours=16) #orders=[dt.datetime(na_dates[dateInd,0],na_dates[dateInd,1],na_dates[dateInd,2])+dt.timedelta(hours=16), # [order[dateInd,0],order[dateInd,1],int(order[dateInd,2])] for dateInd in range(na_data.shape[0])] orders={} for dateInd in range(na_data.shape[0]): tmpDate=dt.datetime(na_dates[dateInd,0],na_dates[dateInd,1],na_dates[dateInd,2])+dt.timedelta(hours=16) if tmpDate in orders.keys(): orders[tmpDate].append([order[dateInd,0],order[dateInd,1],int(order[dateInd,2])]) else:orders[tmpDate]=[[order[dateInd,0],order[dateInd,1],int(order[dateInd,2])]] for i in ldt_timestamps: if i in orders.keys(): for singleOrder in orders[i]: equity=singleOrder[0] byOrSell=singleOrder[1] count=singleOrder[2] if equity in currentEquity.keys(): currentEquity[equity]+=count*byOrSellDict[byOrSell] else:currentEquity[equity]=count*byOrSellDict[byOrSell] currentCash+=-ldf_data[equity][i]*count*byOrSellDict[byOrSell] print '----------------------',i,equity,byOrSell,count print currentEquity #dateInd+=1 #currentDate=dt.datetime(na_dates[dateInd,0],na_dates[dateInd,1],na_dates[dateInd,2])+dt.timedelta(hours=16) #calc portfolia value portfValue=currentCash for tmpEqui in currentEquity.keys(): portfValue+=ldf_data[tmpEqui][i]*currentEquity[tmpEqui] print i,portfValue