Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 09325b2

Browse files
updated for latest python-binance (0.7.10)
1 parent 02c759a commit 09325b2

File tree

5 files changed

+46
-57
lines changed

5 files changed

+46
-57
lines changed

‎pair_trading_percent.py‎

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import os
22
from time import sleep
33

4-
from binance.client import Client
5-
from binance.exceptions import BinanceAPIException, BinanceOrderException
6-
from binance.websockets import BinanceSocketManager
7-
from twisted.internet import reactor
84
import pandas as pd
5+
from binance import ThreadedWebsocketManager
6+
from binance.client import Client
97

108
# init
119
api_key = os.environ.get('binance_api')
@@ -19,13 +17,14 @@ def btc_pairs_trade(msg):
1917
if msg['e'] != 'error':
2018
price['BTCUSDT'].loc[len(price['BTCUSDT'])] = [pd.Timestamp.now(), float(msg['c'])]
2119
else:
22-
price['error']:True
20+
price['error']=True
2321

2422

2523
# init and start the WebSocket
26-
bsm = BinanceSocketManager(client)
27-
conn_key = bsm.start_symbol_ticker_socket('BTCUSDT', btc_pairs_trade)
24+
bsm = ThreadedWebsocketManager()
2825
bsm.start()
26+
bsm.start_symbol_ticker_socket(symbol='BTCUSDT', callback=btc_pairs_trade)
27+
2928

3029
## main
3130
while len(price['BTCUSDT']) == 0:
@@ -38,7 +37,8 @@ def btc_pairs_trade(msg):
3837
# error check to make sure WebSocket is working
3938
if price['error']:
4039
# stop and restart socket
41-
bsm.stop_socket(conn_key)
40+
bsm.stop()
41+
sleep(2)
4242
bsm.start()
4343
price['error'] = False
4444
else:
@@ -52,26 +52,18 @@ def btc_pairs_trade(msg):
5252
try:
5353
order = client.futures_create_order(symbol='ETHUSDT', side='SELL', type='MARKET', quantity=100)
5454
break
55-
except BinanceAPIException as e:
56-
# error handling goes here
57-
print(e)
58-
except BinanceOrderException as e:
59-
# error handling goes here
55+
except Exception as e:
6056
print(e)
6157

6258
elif df.price.iloc[-1] > min_price * 1.05:
6359
try:
6460
order = client.futures_create_order(symbol='ETHUSDT', side='BUY', type='MARKET', quantity=100)
6561
break
66-
except BinanceAPIException as e:
67-
# error handling goes here
68-
print(e)
69-
except BinanceOrderException as e:
70-
# error handling goes here
62+
except Exception as e:
7163
print(e)
64+
7265
sleep(0.1)
7366

7467

7568
# properly stop and terminate WebSocket
76-
bsm.stop_socket(conn_key)
77-
reactor.stop()
69+
bsm.stop()

‎pair_trading_price.py‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import os
22
from time import sleep
33

4+
import pandas as pd
5+
from binance import ThreadedWebsocketManager
46
from binance.client import Client
5-
from binance.exceptions import BinanceAPIException, BinanceOrderException
6-
from binance.websockets import BinanceSocketManager
7-
from twisted.internet import reactor
87

98
# init
109
api_key = os.environ.get('binance_api')
@@ -18,16 +17,16 @@ def btc_pairs_trade(msg):
1817
if msg['e'] != 'error':
1918
price['BTCUSDT'] = float(msg['c'])
2019
else:
21-
price['error']:True
20+
price['error']=True
2221

2322

2423
# init and start the WebSocket
25-
bsm = BinanceSocketManager(client)
26-
conn_key=bsm.start_symbol_ticker_socket('BTCUSDT', btc_pairs_trade)
24+
bsm = ThreadedWebsocketManager()
25+
bsm.start_symbol_ticker_socket(symbol='BTCUSDT', callback=btc_pairs_trade)
2726
bsm.start()
2827

29-
## main
3028

29+
## main
3130
while not price['BTCUSDT']:
3231
# wait for WebSocket to start streaming data
3332
sleep(0.1)
@@ -36,22 +35,19 @@ def btc_pairs_trade(msg):
3635
# error check to make sure WebSocket is working
3736
if price['error']:
3837
# stop and restart socket
39-
bsm.stop_socket(conn_key)
38+
bsm.stop()
39+
sleep(2)
4040
bsm.start()
4141
price['error'] = False
4242
else:
4343
if price['BTCUSDT'] > 10000:
4444
try:
4545
order = client.order_market_buy(symbol='ETHUSDT', quantity=100)
4646
break
47-
except BinanceAPIException as e:
48-
# error handling goes here
49-
print(e)
50-
except BinanceOrderException as e:
51-
# error handling goes here
47+
except Exception as e:
5248
print(e)
49+
5350
sleep(0.1)
5451

5552

56-
bsm.stop_socket(conn_key)
57-
reactor.stop()
53+
bsm.stop()

‎readme.rst‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Binance Python API – A Step-by-Step Guide - AlgoTrading101 Blog
44

55
This is the code used in `Binance Python API – A Step-by-Step Guide <https://algotrading101.com/learn/binance-python-api/>`_ published on the Algotrading101 Blog
66

7+
** The code and article were updated on June 28, 2021 to work with the latest version of Python-Binance (v0.7.10)
78
-----------------
89
Table of Contents
910
-----------------
@@ -31,12 +32,10 @@ Table of Contents
3132
Requirements
3233
------------
3334

34-
* `python <https://www.python.org>`_ >= 2.7, 3.4+
35-
* `python_binance <https://github.com/sammchardy/python-binance>`_ (tested to work with >= 0.7.5 )
35+
* `python <https://www.python.org>`_ >= 3.4+
36+
* `python_binance <https://github.com/sammchardy/python-binance>`_ (tested to work with >= 0.7.10 )
3637
* `bta_lib <https://github.com/mementum/bta-lib>`_ (tested to work with >= 1.0.0 )
3738
* `pandas <https://github.com/pandas-dev/pandas>`_ (tested to work with >= 1.0.3 )
38-
* Twisted (tested to work with >= 20.3.0 )
39-
* binance (tested to work with >= 0.3 )
4039

4140
-----------
4241
Author Info

‎requirements.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1+
python_binance==0.7.10
12
bta_lib==1.0.0
23
pandas==1.0.3
3-
python_binance==0.7.5

‎websocket_stream.py‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
1-
import os
21
from time import sleep
32

4-
from binance.client import Client
5-
from binance.websockets import BinanceSocketManager
6-
from twisted.internet import reactor
3+
from binance import ThreadedWebsocketManager
74

8-
# init
9-
api_key = os.environ.get('binance_api')
10-
api_secret = os.environ.get('binance_secret')
115

12-
client = Client(api_key, api_secret)
136
btc_price = {'error':False}
147

158
def btc_trade_history(msg):
169
''' define how to process incoming WebSocket messages '''
1710
if msg['e'] != 'error':
1811
print(msg['c'])
12+
1913
btc_price['last'] = msg['c']
2014
btc_price['bid'] = msg['b']
2115
btc_price['last'] = msg['a']
16+
btc_price['error'] = False
2217
else:
2318
btc_price['error'] = True
2419

2520

2621
# init and start the WebSocket
27-
bsm = BinanceSocketManager(client)
28-
conn_key = bsm.start_symbol_ticker_socket('BTCUSDT', btc_trade_history)
22+
bsm = ThreadedWebsocketManager()
2923
bsm.start()
3024

25+
# subscribe to a stream
26+
bsm.start_symbol_ticker_socket(callback=btc_trade_history, symbol='BTCUSDT')
27+
3128
# put script to sleep to allow WebSocket to run for a while
3229
# this is just for example purposes
33-
sleep(30)
30+
sleep(2)
3431

35-
# stop websocket
36-
bsm.stop_socket(conn_key)
32+
# add a second stream
33+
bsm.start_symbol_ticker_socket(callback=btc_trade_history, symbol='ETHUSDT')
34+
35+
# put script to sleep to allow WebSocket to run for a while
36+
# this is just for example purposes
37+
sleep(2)
3738

38-
# properly terminate WebSocket
39-
reactor.stop()
39+
# stop websocket
40+
bsm.stop()
4041

41-
# print out all the available WebSocket methods and details
42-
#help(BinanceSocketManager)
42+
sleep(2)
43+
# display more info about the various websocket streams
44+
help(ThreadedWebsocketManager)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /