-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptoStream.py
More file actions
executable file
·44 lines (34 loc) · 1.23 KB
/
CryptoStream.py
File metadata and controls
executable file
·44 lines (34 loc) · 1.23 KB
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
37
38
39
40
41
42
43
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) Dave Beusing <david.beusing@gmail.com>
#
import os
import asyncio
from utils import Credentials, fetch_NonLeveragedTradePairs, build_Frame
from binance import Client, AsyncClient, BinanceSocketManager
from sqlalchemy import create_engine, engine
from config import Config
credentials = Credentials( 'key/binance.key' )
client = Client( credentials.key, credentials.secret )
# Cleanup old data
if os.path.exists( Config.Database ):
os.remove( Config.Database )
engine = create_engine( f'sqlite:///{Config.Database}' )
#prepare multistream list
tp = fetch_NonLeveragedTradePairs( client )
tp = [ i.lower() + '@trade' for i in tp ]
async def main():
asyncClient = await AsyncClient.create()
bsm = BinanceSocketManager( asyncClient )
ms = bsm.multiplex_socket( tp )
async with ms as tscm:
while True:
response = await tscm.recv()
if response:
frame = build_Frame( response, isMultiStream=True )
frame.to_sql( frame.Symbol[0], engine, if_exists='append', index=False )
await asyncClient.close_connection()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete( main() )