mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-29 11:18:20 +08:00
REF: Move transaction class to own module.
This commit is contained in:
@@ -16,9 +16,9 @@ from zipline.finance.risk.period import RiskMetricsPeriod
|
||||
from zipline.finance.risk.report import RiskReport
|
||||
from zipline.finance.slippage import (
|
||||
FixedSlippage,
|
||||
Transaction,
|
||||
VolumeShareSlippage
|
||||
)
|
||||
from zipline.finance.transaction import Transaction
|
||||
from zipline.protocol import Account
|
||||
from zipline.protocol import Portfolio
|
||||
from zipline.protocol import Position as ProtocolPosition
|
||||
|
||||
@@ -36,7 +36,7 @@ from six.moves import range, zip
|
||||
import zipline.utils.factory as factory
|
||||
import zipline.finance.performance as perf
|
||||
from zipline.finance.performance import position_tracker
|
||||
from zipline.finance.slippage import Transaction, create_transaction
|
||||
from zipline.finance.transaction import Transaction, create_transaction
|
||||
import zipline.utils.math_utils as zp_math
|
||||
|
||||
from zipline.gens.composites import date_sorted_sources
|
||||
|
||||
@@ -28,7 +28,7 @@ except ImportError:
|
||||
from six import iteritems, itervalues
|
||||
|
||||
from zipline.protocol import Event, DATASOURCE_TYPE
|
||||
from zipline.finance.slippage import Transaction
|
||||
from zipline.finance.transaction import Transaction
|
||||
from zipline.utils.serialization_utils import (
|
||||
VERSION_LABEL
|
||||
)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#
|
||||
# Copyright 2014 Quantopian, Inc.
|
||||
# Copyright 2015 Quantopian, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
@@ -23,7 +23,7 @@ from functools import partial
|
||||
|
||||
from six import with_metaclass
|
||||
|
||||
from zipline.protocol import DATASOURCE_TYPE
|
||||
from zipline.finance.transaction import create_transaction
|
||||
from zipline.utils.serialization_utils import (
|
||||
VERSION_LABEL
|
||||
)
|
||||
@@ -52,66 +52,6 @@ def transact_partial(slippage, commission):
|
||||
return partial(transact_stub, slippage, commission)
|
||||
|
||||
|
||||
class Transaction(object):
|
||||
|
||||
def __init__(self, sid, amount, dt, price, order_id, commission=None):
|
||||
self.sid = sid
|
||||
self.amount = amount
|
||||
self.dt = dt
|
||||
self.price = price
|
||||
self.order_id = order_id
|
||||
self.commission = commission
|
||||
self.type = DATASOURCE_TYPE.TRANSACTION
|
||||
|
||||
def __getitem__(self, name):
|
||||
return self.__dict__[name]
|
||||
|
||||
def to_dict(self):
|
||||
py = copy(self.__dict__)
|
||||
del py['type']
|
||||
return py
|
||||
|
||||
def __getstate__(self):
|
||||
|
||||
state_dict = copy(self.__dict__)
|
||||
|
||||
STATE_VERSION = 1
|
||||
state_dict[VERSION_LABEL] = STATE_VERSION
|
||||
|
||||
return state_dict
|
||||
|
||||
def __setstate__(self, state):
|
||||
|
||||
OLDEST_SUPPORTED_STATE = 1
|
||||
version = state.pop(VERSION_LABEL)
|
||||
|
||||
if version < OLDEST_SUPPORTED_STATE:
|
||||
raise BaseException("Transaction saved state is too old.")
|
||||
|
||||
self.__dict__.update(state)
|
||||
|
||||
|
||||
def create_transaction(event, order, price, amount):
|
||||
|
||||
# floor the amount to protect against non-whole number orders
|
||||
# TODO: Investigate whether we can add a robust check in blotter
|
||||
# and/or tradesimulation, as well.
|
||||
amount_magnitude = int(abs(amount))
|
||||
|
||||
if amount_magnitude < 1:
|
||||
raise Exception("Transaction magnitude must be at least 1.")
|
||||
|
||||
transaction = Transaction(
|
||||
sid=event.sid,
|
||||
amount=int(amount),
|
||||
dt=event.dt,
|
||||
price=price,
|
||||
order_id=order.id
|
||||
)
|
||||
|
||||
return transaction
|
||||
|
||||
|
||||
class LiquidityExceeded(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
#
|
||||
# Copyright 2015 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.
|
||||
from __future__ import division
|
||||
|
||||
from copy import copy
|
||||
|
||||
from zipline.protocol import DATASOURCE_TYPE
|
||||
from zipline.utils.serialization_utils import (
|
||||
VERSION_LABEL
|
||||
)
|
||||
|
||||
|
||||
class Transaction(object):
|
||||
|
||||
def __init__(self, sid, amount, dt, price, order_id, commission=None):
|
||||
self.sid = sid
|
||||
self.amount = amount
|
||||
self.dt = dt
|
||||
self.price = price
|
||||
self.order_id = order_id
|
||||
self.commission = commission
|
||||
self.type = DATASOURCE_TYPE.TRANSACTION
|
||||
|
||||
def __getitem__(self, name):
|
||||
return self.__dict__[name]
|
||||
|
||||
def to_dict(self):
|
||||
py = copy(self.__dict__)
|
||||
del py['type']
|
||||
return py
|
||||
|
||||
def __getstate__(self):
|
||||
|
||||
state_dict = copy(self.__dict__)
|
||||
|
||||
STATE_VERSION = 1
|
||||
state_dict[VERSION_LABEL] = STATE_VERSION
|
||||
|
||||
return state_dict
|
||||
|
||||
def __setstate__(self, state):
|
||||
|
||||
OLDEST_SUPPORTED_STATE = 1
|
||||
version = state.pop(VERSION_LABEL)
|
||||
|
||||
if version < OLDEST_SUPPORTED_STATE:
|
||||
raise BaseException("Transaction saved state is too old.")
|
||||
|
||||
self.__dict__.update(state)
|
||||
|
||||
|
||||
def create_transaction(event, order, price, amount):
|
||||
|
||||
# floor the amount to protect against non-whole number orders
|
||||
# TODO: Investigate whether we can add a robust check in blotter
|
||||
# and/or tradesimulation, as well.
|
||||
amount_magnitude = int(abs(amount))
|
||||
|
||||
if amount_magnitude < 1:
|
||||
raise Exception("Transaction magnitude must be at least 1.")
|
||||
|
||||
transaction = Transaction(
|
||||
sid=event.sid,
|
||||
amount=int(amount),
|
||||
dt=event.dt,
|
||||
price=price,
|
||||
order_id=order.id
|
||||
)
|
||||
|
||||
return transaction
|
||||
Reference in New Issue
Block a user