コスト関数
def trading_cost_bps(participation_ratio=0.1, commission_bps=10):
c=commission_bps
p=participation_ratio
return (-1/(p/15+0.01)+200)/3.4-24+c
バックテスター
import pandas as pd
from datetime import date, datetime as dt
import random
def default_manager(d):
"""Manages each of process of back testing
"""
print('Start back testing...')
d.log=pd.DataFrame(0, index=d.dates, columns=d.universe)
for date in d.dates:
d.each_day(date)
print(d.log)
print('Backtest completed.')
class BackTestWorker(object):
"""Works as if a trader for each of the day
"""
def __init__(self, universe, rule, dates, manager=default_manager,
*args,**kwargs):
self.universe=universe
self.rule=rule
self.dates=dates
self.manager=manager
self.args=args
self.kwargs=kwargs
def each_day(self, date):
return self.rule(self, date)
def run(self):
self.manager(self)
def random_buyer(d, date):
if date==d.dates[0]:
d.log.loc[date,random.sample(d.universe,2)]=1
else:
pass
if __name__ == '__main__':
BackTestWorker(
['3382','7203','2651'],
random_buyer,
[date(2007, 12, 5), date(2007, 12, 6), date(2007, 12, 7)]
).run()