-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataStream.py
More file actions
36 lines (26 loc) · 889 Bytes
/
DataStream.py
File metadata and controls
36 lines (26 loc) · 889 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import pandas as pd
import asyncio
from binance import AsyncClient, BinanceSocketManager
import sqlalchemy
pair = 'BTCUSDT'
engine = sqlalchemy.create_engine('sqlite:///'+pair+'stream.db')
async def main():
client = await AsyncClient.create()
bsm = BinanceSocketManager(client)
socket = bsm.trade_socket(pair)
while True:
await socket.__aenter__()
msg = await socket.recv()
df = createframe(msg)
df.to_sql(pair, engine, if_exists='append', index=False)
print(df)
def createframe(msg):
df = pd.DataFrame([msg])
df = df.loc[:, ['s', 'E', 'p']]
df.columns = ['symbol', 'Time', 'price']
df.price = df.price.astype(float)
df.Time = pd.to_datetime(df.Time, unit='ms')
return df
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())