Skip to content

Commit e5767d4

Browse files
committed
Merge branch 'hotfix/1.7.1_samples'
2 parents 79a62eb + de23729 commit e5767d4

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ $ pip install basana[charts] talipp pandas statsmodels
2626

2727
### Backtest a pairs trading strategy
2828

29-
1. Download and unzip [samples](https://github.com/gbeced/basana/releases/download/1.7/samples.zip).
29+
1. Download and unzip [samples](https://github.com/gbeced/basana/releases/download/1.7.1/samples.zip).
3030

3131
2. Download historical data for backtesting
3232

samples/backtesting/position_manager.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,20 @@ async def get_position_info(self, pair: bs.Pair) -> Optional[PositionInfo]:
111111
return pos_info
112112

113113
async def check_loss(self):
114-
pairs = [pos_info.pair for pos_info in self._positions.values() if pos_info.current != 0]
115-
# For every pair get position information along with bid and ask prices.
116-
coros = [self.get_position_info(pair) for pair in pairs]
117-
coros.extend(self._exchange.get_bid_ask(pair) for pair in pairs)
118-
res = await asyncio.gather(*coros)
119-
midpoint = int(len(res) / 2)
120-
all_pos_info = res[0:midpoint]
121-
all_bid_ask = res[midpoint:]
122-
123-
# Log each position an check PnL.
124-
for pos_info, (bid, ask) in zip(all_pos_info, all_bid_ask):
114+
# Refresh positions that have an open order.
115+
refresh_pairs = [pair for pair, pos_info in self._positions.items() if pos_info.order_open]
116+
coros = [self.get_position_info(pair) for pair in refresh_pairs]
117+
if coros:
118+
await asyncio.gather(*coros)
119+
120+
# Check unrealized PnL for all non-neutral positions.
121+
non_neutral = [pos_info for pos_info in self._positions.values() if pos_info.current != Decimal(0)]
122+
if not non_neutral:
123+
return
124+
125+
# Get bid and ask prices and calculate unrealized PnL.
126+
bids_and_asks = await asyncio.gather(*[self._exchange.get_bid_ask(pos_info.pair) for pos_info in non_neutral])
127+
for pos_info, (bid, ask) in zip(non_neutral, bids_and_asks):
125128
pnl_pct = pos_info.calculate_unrealized_pnl_pct(bid, ask)
126129
logging.info(StructuredMessage(
127130
f"Position for {pos_info.pair}", current=pos_info.current, target=pos_info.target,

samples/binance/position_manager.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,20 @@ async def get_position_info(self, pair: bs.Pair) -> Optional[PositionInfo]:
137137
return pos_info
138138

139139
async def check_loss(self):
140-
pairs = [pos_info.pair for pos_info in self._positions.values() if pos_info.current != 0]
141-
# For every pair get position information along with bid and ask prices.
142-
coros = [self.get_position_info(pair) for pair in pairs]
143-
coros.extend(self._exchange.get_bid_ask(pair) for pair in pairs)
144-
res = await asyncio.gather(*coros)
145-
midpoint = int(len(res) / 2)
146-
all_pos_info = res[0:midpoint]
147-
all_bid_ask = res[midpoint:]
148-
149-
# Log each position an check PnL.
150-
for pos_info, (bid, ask) in zip(all_pos_info, all_bid_ask):
140+
# Refresh positions that have an open order.
141+
refresh_pairs = [pair for pair, pos_info in self._positions.items() if pos_info.order_open]
142+
coros = [self.get_position_info(pair) for pair in refresh_pairs]
143+
if coros:
144+
await asyncio.gather(*coros)
145+
146+
# Check unrealized PnL for all non-neutral positions.
147+
non_neutral = [pos_info for pos_info in self._positions.values() if pos_info.current != Decimal(0)]
148+
if not non_neutral:
149+
return
150+
151+
# Get bid and ask prices and calculate unrealized PnL.
152+
bids_and_asks = await asyncio.gather(*[self._exchange.get_bid_ask(pos_info.pair) for pos_info in non_neutral])
153+
for pos_info, (bid, ask) in zip(non_neutral, bids_and_asks):
151154
pnl_pct = pos_info.calculate_unrealized_pnl_pct(bid, ask)
152155
logging.info(StructuredMessage(
153156
f"Position for {pos_info.pair}", current=pos_info.current, target=pos_info.target,

0 commit comments

Comments
 (0)