ENH: Added commission model PerDollar

Commissions will be calculated based on total dollar traded.
This commit is contained in:
stanh
2013-09-27 10:59:46 +08:00
committed by Thomas Wiecki
parent 395ce67c53
commit 1f1be35734
3 changed files with 31 additions and 4 deletions
+2 -2
View File
@@ -38,7 +38,7 @@ from zipline.finance.slippage import (
SlippageModel,
transact_partial
)
from zipline.finance.commission import PerShare, PerTrade
from zipline.finance.commission import PerShare, PerTrade, PerDollar
from zipline.finance.blotter import Blotter
from zipline.finance.constants import ANNUALIZER
from zipline.finance import trading
@@ -419,7 +419,7 @@ class TradingAlgorithm(object):
self.slippage = slippage
def set_commission(self, commission):
if not isinstance(commission, (PerShare, PerTrade)):
if not isinstance(commission, (PerShare, PerTrade, PerDollar)):
raise UnsupportedCommissionModel()
if self.initialized:
+2 -2
View File
@@ -63,8 +63,8 @@ method.
class UnsupportedCommissionModel(ZiplineError):
"""
Raised if a user script calls the override_commission magic
with a commission object that isn't a PerShare or
PerTrade commission
with a commission object that isn't a PerShare, PerTrade or
PerDollar commission
"""
msg = """
You attempted to override commission with an unsupported class. \
+27
View File
@@ -66,3 +66,30 @@ class PerTrade(object):
return 0.0, 0.0
return abs(self.cost / transaction.amount), self.cost
class PerDollar(object):
"""
Calculates a commission for a transaction based on a per
dollar cost.
"""
def __init__(self, cost=0.0015):
"""
Cost parameter is the cost of a trade per-dollar. 0.0015
on $1 million means $1,500 commission (=1,000,000 x 0.0015)
"""
self.cost = float(cost)
def __repr__(self):
return "{class_name}(cost={cost})".format(
class_name=self.__class__.__name__,
cost=self.cost)
def calculate(self, transaction):
"""
returns a tuple of:
(per share commission, total transaction commission)
"""
cost_per_share = transaction.price * self.cost
return cost_per_share, abs(transaction.amount) * cost_per_share