Skip to content

Commit

Permalink
fix RuntimeError: dictionary changed size during iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
51bitquant committed Oct 11, 2021
1 parent afcd660 commit 3549375
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 6 additions & 2 deletions trader/binance_future_trader.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def start(self):
return

symbols = self.positions.positions.keys()

deleted_positions = []
for s in symbols:
pos_data = self.positions.positions.get(s)
pos = pos_data.get('pos')
Expand All @@ -221,7 +221,8 @@ def start(self):
value = pos * bid_price
if value < self.symbols_dict.get(s, {}).get('min_notional', 0):
print(f"{s} notional value is small, delete the position data.")
del self.positions.positions[s] # delete the position data if the position notional is very small.
deleted_positions.append(s)
# del self.positions.positions[s] # delete the position data if the position notional is very small.
else:
avg_price = pos_data.get('avg_price')
self.positions.update_profit_max_price(s, bid_price)
Expand Down Expand Up @@ -288,6 +289,9 @@ def start(self):
else:
print(f"{s}: bid_price: {bid_price}, ask_price: {bid_price}")

for s in deleted_positions:
del self.positions.positions[s] # delete the position data if the position notional is very small.

pos_symbols = self.positions.positions.keys() # the position's symbols, if there is {"symbol": postiondata}, you get the symbols here.
pos_count = len(pos_symbols) # position count

Expand Down
7 changes: 6 additions & 1 deletion trader/binance_spot_trader.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ def start(self):

symbols = self.positions.positions.keys()

deleted_positions = []
for s in symbols:
pos_data = self.positions.positions.get(s)
pos = pos_data.get('pos')
Expand All @@ -230,7 +231,8 @@ def start(self):
value = pos * bid_price
if value < self.symbols_dict.get(s, {}).get('min_notional', 0):
print(f"{s} notional value is small, delete the position data.")
del self.positions.positions[s] # delete the position data if the position notional is very small.
deleted_positions.append(s) #
# del self.positions.positions[s] # delete the position data if the position notional is very small.
else:
avg_price = pos_data.get('avg_price')
self.positions.update_profit_max_price(s, bid_price)
Expand Down Expand Up @@ -297,6 +299,9 @@ def start(self):
else:
print(f"{s}: bid_price: {bid_price}, ask_price: {bid_price}")

for s in deleted_positions:
del self.positions.positions[s] # delete the position data if the position notional is very small.

pos_symbols = self.positions.positions.keys() # 有仓位的交易对信息.
pos_count = len(pos_symbols) # 仓位的个数.

Expand Down

0 comments on commit 3549375

Please sign in to comment.