-
Notifications
You must be signed in to change notification settings - Fork 4
/
dydxv3markets.py
135 lines (129 loc) · 5.65 KB
/
dydxv3markets.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
# 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_markets"}
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 maxwidthindexPrice
global maxwidthnextFundingAt
global maxwidthnextFundingRate
global maxwidthopenInterest
global maxwidthoraclePrice
global maxwidthpriceChange24H
global maxwidthtrades24H
global maxwidthvolume24H
if elementname == 'indexPrice' and elementsize > maxwidthindexPrice:
fp = open(ramdiskpath+'/maxwidth'+elementname, "w")
fp.write(str(elementsize)+'\n')
fp.close()
maxwidthindexPrice = elementsize
elif elementname == 'nextFundingAt' and elementsize > maxwidthnextFundingAt:
fp = open(ramdiskpath+'/maxwidth'+elementname, "w")
fp.write(str(elementsize)+'\n')
fp.close()
maxwidthnextFundingAt = elementsize
elif elementname == 'nextFundingRate' and elementsize > maxwidthnextFundingRate:
fp = open(ramdiskpath+'/maxwidth'+elementname, "w")
fp.write(str(elementsize)+'\n')
fp.close()
maxwidthnextFundingRate = elementsize
elif elementname == 'openInterest' and elementsize > maxwidthopenInterest:
fp = open(ramdiskpath+'/maxwidth'+elementname, "w")
fp.write(str(elementsize)+'\n')
fp.close()
maxwidthopenInterest = elementsize
elif elementname == 'oraclePrice' and elementsize > maxwidthoraclePrice:
fp = open(ramdiskpath+'/maxwidth'+elementname, "w")
fp.write(str(elementsize)+'\n')
fp.close()
maxwidthoraclePrice = elementsize
elif elementname == 'priceChange24H' and elementsize > maxwidthpriceChange24H:
fp = open(ramdiskpath+'/maxwidth'+elementname, "w")
fp.write(str(elementsize)+'\n')
fp.close()
maxwidthpriceChange24H = elementsize
elif elementname == 'trades24H' and elementsize > maxwidthtrades24H:
fp = open(ramdiskpath+'/maxwidth'+elementname, "w")
fp.write(str(elementsize)+'\n')
fp.close()
maxwidthtrades24H = elementsize
elif elementname == 'volume24H' and elementsize > maxwidthvolume24H:
fp = open(ramdiskpath+'/maxwidth'+elementname, "w")
fp.write(str(elementsize)+'\n')
fp.close()
maxwidthvolume24H = elementsize
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S")+' dydxv3markets.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'
handler = logging.handlers.RotatingFileHandler(ramdiskpath+'/dydxv3markets.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')
maxwidthindexPrice = 0
maxwidthnextFundingAt = 0
maxwidthnextFundingRate = 0
maxwidthopenInterest = 0
maxwidthoraclePrice = 0
maxwidthpriceChange24H = 0
maxwidthtrades24H = 0
maxwidthvolume24H = 0
openconnection()
while True:
try:
api_data = ws.recv()
api_data = json.loads(api_data)
logger.info("{'timestamp': '"+datetime.now().strftime("%Y-%m-%d %H:%M:%S")+"'}")
logger.info(api_data)
for item in api_data['contents'].items():
market = item[0]
marketdata = item[1]
if os.path.isdir(ramdiskpath+'/'+market) == False:
os.system('mkdir -p '+ramdiskpath+'/'+market)
for marketdata in marketdata.items():
marketdataelement = marketdata[0]
marketdatavalue = marketdata[1]
fp = open(ramdiskpath+'/'+market+'/'+marketdataelement, "w")
fp.write(marketdatavalue+' '+datetime.now().strftime("%Y-%m-%d %H:%M:%S")+'\n')
fp.close()
checkwidth(marketdataelement, len(marketdatavalue))
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))