adding independent commission modeling.

This commit is contained in:
fawce
2012-09-27 23:09:14 -04:00
parent 4220fbd365
commit e6b3e8e7b1
2 changed files with 32 additions and 8 deletions
+19
View File
@@ -0,0 +1,19 @@
class PerShareCommission(object):
def __init__(self, cost=0.03):
self.cost = cost
def calculate(self, transaction):
return self.cost, abs(transaction.amount * self.cost)
class PerTradeCommission(object):
def __init__(self, cost=5.0):
self.cost = cost
def calculate(self, transaction):
if transaction.amount == 0:
return 0.0, 0.0
return abs(self.cost / transaction.amount), self.cost
+13 -8
View File
@@ -1,24 +1,31 @@
import pytz
import math
from datetime import timedelta
import zipline.protocol as zp
def create_transaction(sid, amount, price, dt, direction, commission):
from zipline.finance.commissions import PerShareCommission
def create_transaction(sid, amount, price, dt, commission):
txn = {'sid' : sid,
'amount' : int(amount),
'dt' : dt,
'price' : price,
'commission' : commission * amount * direction
}
return zp.ndict(txn)
transaction = zp.ndict(txn)
per_share, total_commission = commission.calculate(transaction)
transaction.price = transaction.price + per_share
transaction.commission = total_commission
return transaction
class VolumeShareSlippage(object):
def __init__(self,
volume_limit=.25,
price_impact=0.1,
commission=0.03):
commission=PerShareCommission()):
self.volume_limit = volume_limit
self.price_impact = price_impact
self.commission = commission
@@ -83,13 +90,12 @@ class VolumeShareSlippage(object):
simulated_amount,
event.price + simulated_impact,
dt.replace(tzinfo = pytz.utc),
direction,
self.commission
)
class FixedSlippage(object):
def __init__(self, spread=0.0, commission=0.0):
def __init__(self, spread=0.0, commission=PerShareCommission()):
"""
Use the fixed slippage model, which will just add/subtract a specified spread
spread/2 will be added on buys and subtracted on sells per share
@@ -119,7 +125,6 @@ class FixedSlippage(object):
amount,
event.price + (self.spread/2.0 * direction),
event.dt,
direction,
self.commission
)