mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-08 09:17:10 +08:00
adding independent commission modeling.
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user