'How to make my "Binance CCXT" Code Run's Fast? [closed]
How to make my "Binance CCXT" Code Run's Fast?
bars = ex.fetch_ohlcv('ZIL/USDT', timeframe=timeframeEMA, limit=500)
data = pd.DataFrame(bars[:-1], columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
data['timestamp'] = pd.to_datetime(data['timestamp'], unit='ms')
data['7_EMA'] = data['close'].ewm(span = 5, adjust= False).mean()
data['76_EMA'] = data['close'].ewm(span = 40, adjust= False).mean()
data['Signal'] = 0.0
data['Signal'] = np.where(data['7_EMA'] > data['76_EMA'],1.0, 0.0 )
data['Position'] = data['Signal'].diff()
data['ZIL/USDT'] = data['Position']
EMA = data[data['ZIL/USDT'] !=0]
FMA = EMA[EMA['ZIL/USDT'] != -1]
FFMA = FMA[FMA['timestamp'] == timestamp]
coin = 'ZILUSDT'
coinA = TA_Handler(
symbol=coin,
screener="crypto",
exchange="BINANCE",
interval=timeframe,
)
I Want to make my code run fast
I am having multiple lines like this
I have to take the data of multiple coins
At the moment, it is taking 3 minutes to run, but I want this in 10 or 5seconds.
Solution 1:[1]
I've tried your code but I can't get it to work as there are variables missing so can't really help you if you don't give the full code or a Minimal, Reproducible Example.
In any case I found out that CCXT free version uses REST API while the CCXT Pro version uses websockets which makes a huge difference in latency which is probably why you are experiencing slowness when running multiple queries.
I would suggest you ditch CCXT and query the Binance API directly using websockets and asyncio which will give you the fast speed you are looking for.
It might take you a bit of time to learn but it will be worthwhile in the end.
Below is a basic example using websockets and asyncio with the Deribit exchange (sorry I don't use Binance and don't have time to implement the Binance API right now).
While each exchange has its specificities this should help you get started.
import asyncio
import websockets
import json
dmsg = {"jsonrpc": "2.0", "method": "public/get_order_book", "id": 1,
"params": {"instrument_name": "BTC-PERPETUAL"}, "depth": "5"}
async def call_dapi():
global response
async with websockets.connect('wss://test.deribit.com/ws/api/v2') as websocket:
await websocket.send(json.dumps(dmsg))
response = await websocket.recv()
response = json.loads(response)
print(response)
asyncio.get_event_loop().run_until_complete(call_dapi())
Feel free to ask a new question with your new code if you struggle to get it working with websockets and asyncio.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
