Skip to content

Commit

Permalink
new filed for BookingPosition
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuizi7 committed Sep 6, 2018
1 parent 7ea4fbe commit abb4bb8
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions rqalpha/model/booking.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ def __init__(self, order_book_id, direction):
self._old_quantity = 0
self._today_quantity = 0

self._avg_price = 0

@property
def order_book_id(self):
"""
Expand Down Expand Up @@ -169,6 +171,13 @@ def today_quantity(self):
"""
return self._today_quantity

@property
def avg_price(self):
"""
[float] 平均开仓价格
"""
return self._avg_price

def apply_settlement(self):
self._old_quantity += self._today_quantity
self._today_quantity = 0
Expand All @@ -177,6 +186,15 @@ def apply_trade(self, trade):
position_effect = self._get_position_effect(trade.side, trade.position_effect)

if position_effect == POSITION_EFFECT.OPEN:
if self.quantity < 0:
if trade.last_quantity <= -1 * self.quantity:
self._avg_price = 0
else:
self._avg_price = trade.last_price
else:
self._avg_price = (
self.quantity * self.avg_price + trade.last_quantity * trade.last_price
) / (self.quantity + trade.last_quantity)
self._today_quantity += trade.last_quantity
elif position_effect == POSITION_EFFECT.CLOSE_TODAY:
self._today_quantity -= trade.last_quantity
Expand Down

0 comments on commit abb4bb8

Please sign in to comment.