mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-22 12:40:30 +08:00
For printability in the repr when debugging algo config and state, change the repr of TradingAlgorithm and the objects it contains so that the more closely adhere to the repr interface of being able to recreate an object instance.
69 lines
2.0 KiB
Python
69 lines
2.0 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 __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)
|
|
"""
|
|
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
|