Files
catalyst/zipline/finance/commission.py
T
Eddie Hebert 23ff65ad32 MAINT: Convert per share cost to float during init.
The cost value should always be a float.
The per share calculation shouldn't be changed dramatically by being
a float, (except for potential rounding errors), but change to so
that PerShare and PerTrade are aligned.
 # Please enter the commit message for your changes. Lines starting
2013-04-09 12:04:08 -04:00

64 lines
1.9 KiB
Python

#
# Copyright 2012 Quantopian, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
class PerShare(object):
"""
Calculates a commission for a transaction based on a per
share cost.
"""
def __init__(self, cost=0.03):
"""
Cost parameter is the cost of a trade per-share. $0.03
means three cents per share, which is a very conservative
(quite high) for per share costs.
"""
self.cost = float(cost)
def calculate(self, transaction):
"""
returns a tuple of:
(per share commission, total transaction commission)
"""
return self.cost, abs(transaction.amount * self.cost)
class PerTrade(object):
"""
Calculates a commission for a transaction based on a per
trade cost.
"""
def __init__(self, cost=5.0):
"""
Cost parameter is the cost of a trade, regardless of
share count. $5.00 per trade is fairly typical of
discount brokers.
"""
# Cost needs to be floating point so that calculation using division
# logic does not floor to an integer.
self.cost = float(cost)
def calculate(self, transaction):
"""
returns a tuple of:
(per share commission, total transaction commission)
"""
if transaction.amount == 0:
return 0.0, 0.0
return abs(self.cost / transaction.amount), self.cost