-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarket_scrapers.py
130 lines (101 loc) · 4.35 KB
/
market_scrapers.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
import sys
import requests
import yaml
import json
import ccxt
import logging as log
from decimal import Decimal
from pprint import pprint
from qtrade_client.api import QtradeAPI
COIN = Decimal('.00000001')
class APIScraper:
def __init__(self, **kwargs):
self.__dict__.update(**kwargs)
def scrape_ticker(self): # dummy function, meant to be overridden
pass
class QTradeScraper(APIScraper):
def __init__(self, **kwargs):
self.api = QtradeAPI("https://api.qtrade.io",
key=open("lpbot_hmac.txt", "r").read().strip())
super().__init__(**kwargs)
def scrape_ticker(self):
tickers = {}
for market, qmarket in self.markets.items():
res = self.api.get("/v1/ticker/{}".format(market))
log.debug("Ticker %s from %s was acquired successfully",
market, self.exchange_name)
bid = Decimal(res["bid"]).quantize(COIN)
log.debug("Bid price is %s", bid)
last = Decimal(res["last"]).quantize(COIN)
log.debug("Last price is %s", last)
ask = Decimal(res["ask"]).quantize(COIN)
log.debug("Ask price is %s", ask)
tickers[qmarket] = {"bid": bid, "last": last, "ask": ask}
return tickers
class BittrexScraper(APIScraper):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def scrape_ticker(self):
tickers = {}
for market, qmarket in self.markets.items():
res = json.loads(requests.get(
'https://api.bittrex.com/api/v1.1/public/getticker?market=' + market).content)
if not res['success']:
log.warning("Could not acquire ticker %s from %s",
market, self.exchange_name)
return
log.debug("Ticker %s from %s was acquired successfully",
market, self.exchange_name)
bid = Decimal(res["result"]["Bid"]).quantize(COIN)
log.debug("Bid price is %s", bid)
last = Decimal(res["result"]["Last"]).quantize(COIN)
log.debug("Last price is %s", last)
ask = Decimal(res["result"]["Ask"]).quantize(COIN)
log.debug("Ask price is %s", ask)
tickers[qmarket] = {"bid": bid, "last": last, "ask": ask}
return tickers
class CCXTScraper(APIScraper):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def scrape_ticker(self):
tickers = {}
for market, qmarket in self.markets.items():
bid_total = Decimal('0')
last_total = Decimal('0')
ask_total = Decimal('0')
for ex_id in self.exchanges:
res = ex_class = getattr(ccxt, ex_id)
ex = ex_class({
'apiKey': '',
'secret': '',
'timeout': 30000,
'enableRateLimit': True,
})
res = ex.fetchTicker(market)
log.debug("Ticker %s from %s was acquired successfully",
market, ex_id)
bid_total += Decimal(res['bid'])
last_total += Decimal(res['last'])
ask_total += Decimal(res['ask'])
bid_total = (bid_total/len(self.exchanges)).quantize(COIN)
last_total = (last_total/len(self.exchanges)).quantize(COIN)
ask_total = (ask_total/len(self.exchanges)).quantize(COIN)
tickers[qmarket] = {"bid": bid_total, "last": last_total, "ask": ask_total}
return tickers
if __name__ == "__main__":
log_level = log.INFO
root = log.getLogger()
root.setLevel(log_level)
handler = log.StreamHandler(sys.stdout)
handler.setLevel(log_level)
formatter = log.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root.addHandler(handler)
config = yaml.load(open('config.yml'))['market_data_collector']
pprint(BittrexScraper(exchange_name='bittrex', markets=config[
'scrapers']['bittrex']['markets']).scrape_ticker())
pprint(QTradeScraper(exchange_name='qtrade', markets=config[
'scrapers']['qtrade']['markets']).scrape_ticker())
pprint(CCXTScraper(markets=config['scrapers']['ccxt']['markets'],
exchanges=config['scrapers']['ccxt']['exchanges']).scrape_ticker())