mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-25 13:10:33 +08:00
Bug fixes
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user