-
Notifications
You must be signed in to change notification settings - Fork 4
/
dydxtrades.py
109 lines (103 loc) · 4.17 KB
/
dydxtrades.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import json
import logging
import os
import sys
import time
from datetime import datetime
from logging import handlers
from random import randint
from websocket import create_connection
def openconnection():
global ws
global api_data
# ws = create_connection("wss://api.stage.dydx.exchange/v3/ws")
ws = create_connection("wss://api.dydx.exchange/v3/ws")
api_data = {
"type": "subscribe",
"channel": "v3_trades",
"id": market
}
ws.send(json.dumps(api_data))
api_data = ws.recv()
api_data = json.loads(api_data)
print(api_data)
api_data = ws.recv()
api_data = json.loads(api_data)
print(api_data)
def checkwidth(elementname, elementsize):
global maxwidthtradeprice
global maxwidthtradesize
if elementname == 'tradeprice' and elementsize > maxwidthtradeprice:
fp = open(ramdiskpath+'/'+market+'/maxwidth'+elementname, "w")
fp.write(str(elementsize)+'\n')
fp.close()
maxwidthtradeprice = elementsize
elif elementname == 'tradesize' and elementsize > maxwidthtradesize:
fp = open(ramdiskpath+'/'+market+'/maxwidth'+elementname, "w")
fp.write(str(elementsize)+'\n')
fp.close()
maxwidthtradesize = elementsize
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S")+' dydxtrades.py')
logger = logging.getLogger("Rotating Log")
logger.setLevel(logging.INFO)
if sys.platform == "linux" or sys.platform == "linux2":
# linux
ramdiskpath = '/mnt/ramdisk'
elif sys.platform == "darwin":
# OS X
ramdiskpath = '/Volumes/RAMDisk'
if len(sys.argv) < 2:
market = 'BTC-USD'
else:
market = sys.argv[1]
handler = logging.handlers.RotatingFileHandler(ramdiskpath+'/dydxtrades'+market+'.log',
maxBytes = 2097152,
backupCount = 4
)
logger.addHandler(handler)
if os.path.isdir(ramdiskpath) == False:
print('Error: Ramdisk', ramdiskpath, 'not mounted')
sys.exit()
if os.path.ismount(ramdiskpath) == False:
print('Warning:', ramdiskpath, 'is not a mount point')
if os.path.isdir(ramdiskpath+'/'+market) == False:
os.system('mkdir -p '+ramdiskpath+'/'+market)
maxwidthtradeprice = 0
maxwidthtradesize = 0
openconnection()
while True:
try:
trades = api_data['contents']['trades'][0]
tradecreatedat = trades['createdAt']
tradeliquidation = trades['liquidation']
tradeprice = trades['price']
tradeside = trades['side']
tradesize = trades['size']
if tradeliquidation == True:
liquidationstring = 'L'
fp = open(ramdiskpath+'/'+market+'/liquidations', "a")
fp.write(tradecreatedat+' '+tradeprice+' '+tradeside+' ('+tradesize+')L\n')
fp.close()
else:
liquidationstring = ''
fp = open(ramdiskpath+'/'+market+'/lasttrade', "w")
fp.write(tradecreatedat+' '+tradeprice+' '+tradeside+' ('+tradesize+')'+liquidationstring+'\n')
fp.close()
logger.info(datetime.now().strftime("%Y-%m-%d %H:%M:%S")+' '+tradecreatedat+' '+tradeprice+' '+tradeside.ljust(4)+' ('+tradesize+')'+liquidationstring)
checkwidth('tradeprice', len(tradeprice))
checkwidth('tradesize', len(tradesize))
api_data = ws.recv()
api_data = json.loads(api_data)
except KeyboardInterrupt:
ws.close()
sys.exit(0)
except Exception as error:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "WebSocket message failed (%s)" % error)
ws.close()
time.sleep(1)
try:
openconnection()
except Exception as error:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "WebSocket message failed (%s)" % error)
ws.close()
time.sleep(randint(1,10))