From 346780c3f05cac8dee1f1c630184a6101241441d Mon Sep 17 00:00:00 2001 From: Frederic Fortier Date: Mon, 21 Aug 2017 00:16:34 -0400 Subject: [PATCH] Bug fixes --- catalyst/examples/buy_the_dip_live.py | 5 +++++ catalyst/exchange/exchange_order.py | 18 +----------------- catalyst/exchange/exchange_portfolio.py | 17 +++++++++-------- 3 files changed, 15 insertions(+), 25 deletions(-) diff --git a/catalyst/examples/buy_the_dip_live.py b/catalyst/examples/buy_the_dip_live.py index af8571aa..73d56385 100644 --- a/catalyst/examples/buy_the_dip_live.py +++ b/catalyst/examples/buy_the_dip_live.py @@ -75,6 +75,11 @@ def _handle_data(context, data): cost_basis = None if context.asset in context.portfolio.positions: position = context.portfolio.positions[context.asset] + + if position.amount >= context.TARGET_POSITIONS: + log.info('reached positions target: {}'.format(position.amount)) + return + cost_basis = position.cost_basis log.info( 'found {amount} positions with cost basis {cost_basis}'.format( diff --git a/catalyst/exchange/exchange_order.py b/catalyst/exchange/exchange_order.py index e2375ce3..5ebc667c 100644 --- a/catalyst/exchange/exchange_order.py +++ b/catalyst/exchange/exchange_order.py @@ -2,25 +2,9 @@ import math import catalyst.protocol as zp from catalyst.assets import Asset -from catalyst.finance.order import Order -from catalyst.utils.enum import enum +from catalyst.finance.order import Order, ORDER_STATUS from catalyst.utils.input_validation import expect_types -ORDER_STATUS = enum( - 'OPEN', - 'FILLED', - 'CANCELLED', - 'REJECTED', - 'HELD', -) - -SELL = 1 << 0 -BUY = 1 << 1 -STOP = 1 << 2 -LIMIT = 1 << 3 - -ORDER_FIELDS_TO_IGNORE = {'type', 'direction', '_status', 'asset'} - class ExchangeOrder(Order): @expect_types(asset=Asset) diff --git a/catalyst/exchange/exchange_portfolio.py b/catalyst/exchange/exchange_portfolio.py index 3f6e9899..1accc45f 100644 --- a/catalyst/exchange/exchange_portfolio.py +++ b/catalyst/exchange/exchange_portfolio.py @@ -1,5 +1,6 @@ import numpy as np from catalyst.protocol import Portfolio, Positions, Position +from catalyst.finance.order import BUY from logbook import Logger log = Logger('ExchangePortfolio') @@ -49,14 +50,14 @@ class ExchangePortfolio(Portfolio): self.capital_used += order.amount * order.executed_price - if order_position.cost_basis > 0: - # TODO: consider buy orders only - order_position.cost_basis = np.average( - [order_position.cost_basis, order.executed_price], - weights=[order_position.amount, order.amount] - ) - else: - order_position.cost_basis = order.executed_price + if order.amount > 0: + if order_position.cost_basis > 0: + order_position.cost_basis = np.average( + [order_position.cost_basis, order.executed_price], + weights=[order_position.amount, order.amount] + ) + else: + order_position.cost_basis = order.executed_price log.debug('updated portfolio with executed order')