Skip to content

Commit

Permalink
fix: update pnl when reducing positions
Browse files Browse the repository at this point in the history
  • Loading branch information
kieran-mackle committed Sep 9, 2022
1 parent 85c10ff commit b457ee6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion autotrader/autoplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1989,7 +1989,7 @@ def _plot_trade_history(
list(shorts_trades.data_index.values),
list(shorts_trades.fill_price.values),
"inverted_triangle",
"lightgreen",
"orangered",
"Short trades",
linked_fig,
)
Expand Down
16 changes: 11 additions & 5 deletions autotrader/brokers/trading.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,7 @@ def __init__(self, **kwargs):
self.HCF = 1

self.avg_price = None
self._prev_avg_price = None

self.notional = 0

Expand Down Expand Up @@ -796,17 +797,22 @@ def _update_with_fill(self, trade: Trade):
self.net_position += trade.size * trade.direction

# Update average entry price
self._prev_avg_price = self.avg_price
if self.net_position * net_position_before_fill > 0:
# Position has not flipped
if abs(self.net_position) > abs(net_position_before_fill):
# Position has increased
self.avg_price = (
self.avg_price * abs(self.net_position)
+ trade.fill_price * trade.size
) / (abs(self.net_position) + trade.size)
self.avg_price = round(
(
self.avg_price * abs(self.net_position)
+ trade.fill_price * trade.size
)
/ (abs(self.net_position) + trade.size),
self.price_precision,
)
else:
# Position has flipped
self.avg_price = trade.fill_price
self.avg_price = round(trade.fill_price, self.price_precision)

# Update last price and last time
self.last_price = trade.last_price
Expand Down
24 changes: 23 additions & 1 deletion autotrader/brokers/virtual/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,9 +911,29 @@ def _modify_position(self, trade: Trade, reduce_only: bool):
self._positions[trade.instrument]._update_with_fill(
trade=trade,
)
new_net_position = self._positions[trade.instrument].net_position

# Check if position has been reduced
if np.sign(new_net_position - starting_net_position) != np.sign(
starting_net_position
):
# Update account with position pnl
units_reduced = min(
abs(new_net_position - starting_net_position),
abs(starting_net_position),
)
reduction_direction = np.sign(starting_net_position - new_net_position)
pnl = (
reduction_direction
* units_reduced
* (
trade.fill_price
- self._positions[trade.instrument]._prev_avg_price
)
)
self._adjust_balance(pnl)

# Check if position is zero
new_net_position = self._positions[trade.instrument].net_position
if round(new_net_position, price_precision) == 0:
# Move to closed positions
popped_position = self._positions.pop(trade.instrument)
Expand All @@ -929,6 +949,8 @@ def _modify_position(self, trade: Trade, reduce_only: bool):
):
# Position has swapped sides
a = 10
# TODO - cancel any old SL or TP orders (will need to keep
# track of them as created in fill_order)

else:
# Create new position
Expand Down
6 changes: 5 additions & 1 deletion tests/macd_strategy_continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ def generate_exit_levels(self, signal, i):
end="1/3/2022",
)
at.virtual_account_config(
initial_balance=1000, leverage=30, spread=0.5 * 1e-4, commission=0.005
# initial_balance=1000, leverage=30, spread=0.5 * 1e-4, commission=0.005
initial_balance=1000,
leverage=30,
spread=0,
commission=0.0,
)
at.run()

Expand Down

0 comments on commit b457ee6

Please sign in to comment.