-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpyTicker2.py
More file actions
161 lines (146 loc) · 5.33 KB
/
pyTicker2.py
File metadata and controls
161 lines (146 loc) · 5.33 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# import python libs
import sys
import re
import argparse
import time
import json
import io
import asyncio
from datetime import datetime
# import additional libs
from prettytable import PrettyTable
import pygame
# import pyTicker libs
import exchange
import util
import data
from slacker import Slacker
parser = argparse.ArgumentParser(description="cryptocurrency ticker comparer suite")
parser.add_argument('input', nargs='?', help="disable getting ticker from poloniex")
args = parser.parse_args()
def load_config(file):
default = {
'refresh':1,
'slack_alarm':80,
'server':False
}
with io.open(file, 'r') as fp:
j = json.load(fp)
default.update(j)
return default
def load_exchange(name):
return {'coinone':exchange.Coinone(),
'binance':exchange.Binance(),
'gate.io':exchange.GateIO(),
'upbit':exchange.Upbit(),
'cpdax':exchange.Cpdax(),
'kucoin':exchange.Kucoin(),
#'gopax':exchange.Gopax(),
'coinrail':exchange.Coinrail(),
'coinnest':exchange.Coinnest(),
'bittrex':exchange.Bittrex(),
'poloniex':exchange.Poloniex(),
'bithumb':exchange.Bithumb(),
'bitfinex':exchange.Bitfinex(),
#'liqui':exchange.Liqui(),
'hitbtc':exchange.Hitbtc(),
'cexio':exchange.CexIO()}[name]
def parse_config(cfg):
EX = dict(map(lambda name:(name,load_exchange(name)),cfg['exchanges']))
C = cfg['currencies']
nu = cfg['norm_unit']
TU = cfg['target_units']
SU = cfg['show_units']
RP = list(map(lambda pair:(EX[pair[0]], EX[pair[1]]), cfg['RNPR_pairs']))
SA = cfg['slack_alarm']
return EX, C, nu, TU, SU, RP, SA
def async_run(EX, C):
loop = asyncio.get_event_loop()
tasks = list(map(lambda ex:asyncio.ensure_future(ex[1].async_run(C)), list(EX.items())))
loop.run_until_complete(asyncio.wait(tasks))
def run(EX, C):
for kv in EX.items():
kv[1].run(C)
def make_header(C):
return ["Index"] + C
def get_type_u(u):
return {"KRW":data.KRWLast,"BTC":data.BTCLast,"ETH":data.ETHLast,"USDT":data.USDTLast}[u]
def get_u_last(ex,u):
if u == "KRW":
return ex.KRW_last
elif u == "BTC":
return ex.BTC_last
elif u == "ETH":
return ex.ETH_last
elif u == "USDT":
return ex.USDT_last
def make_exchange_body(name,ex,C,SU,TU,nu):
rows = []
U = ["KRW","BTC","ETH","USDT"]
for u in U:
if u in SU:
currencyOpt = u == "KRW" or u == "USDT"
if(issubclass(type(ex), get_type_u(u))):
rows.append(["%s (%s)"%(name,u)] + util.make_row(C,get_u_last(ex,u), currencyOpt=currencyOpt))
if(u != nu and issubclass(type(ex), get_type_u(u))):
currencyOpt = nu == "KRW" or nu == "USDT"
if u in TU:
NP = make_NP(ex=ex, tu=u, nu=nu)
rows.append(["%s (%s->%s)"%(name, u, nu)] + util.make_row(C,NP,roundOpt=8,currencyOpt=currencyOpt))
return rows
def make_NP(ex, tu, nu):
if issubclass(type(ex), get_type_u(tu)):
u_last = get_u_last(ex,tu)
nu_last = 1
if(tu != nu):
nu_last = u_last[nu]
return util.get_normalized_price(u_last,nu_last)
else:
return None
def make_NRPR_body(EX, RP, C, TU, nu, SA): # EX:Exchange, RP : RNPR pairs, C : Currencies, TU : Target Unit, nu : norm unit, SA : slack alarm
rows = []
for tu in TU:
for rp in RP:
if(rp[0] == rp[1] and tu == nu):
continue
NP_0 = make_NP(ex=rp[0],tu=tu,nu=nu)
NP_1 = make_NP(ex=rp[1],tu=nu,nu=nu)
if NP_0 is None or NP_1 is None:
continue
elif issubclass(type(rp[0]), get_type_u(tu)) == False:
continue
diff = util.get_diff_last(NP_0, NP_1)
rows.append(["%s(%s)/%s(%s) (+)"%(rp[0].symbol, tu, rp[1].symbol, nu)] + util.make_row(C, {k:v for (k,v) in diff.items() if v > 100}, 4))
rows.append(["%s(%s)/%s(%s) (-)"%(rp[0].symbol, tu, rp[1].symbol, nu)] + util.make_row(C, {k:v for (k,v) in diff.items() if v <= 100}, 4))
return rows
def print_table(EX,C,SU,TU,nu,RP, SA):
t = PrettyTable(make_header(C))
for kv in EX.items():
for row in make_exchange_body(name=kv[0],ex=kv[1],C=C,SU=SU,nu=nu,TU=TU):
t.add_row(row)
for row in make_NRPR_body(EX=EX,RP=RP,C=C,TU=TU,nu=nu, SA=SA):
t.add_row(row)
t.align = "l"
util.clear()
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
print(t)
def dump_json(EX):
with io.open('dump.json', 'w') as out:
json.dump(list(map(lambda ex:ex.to_json(), EX.values())), out)
if __name__ == "__main__":
if(args.input == None):
print("Please provide config json file.\n\re.g.) python pyTicker2.py default.json")
else:
cfg = load_config(args.input)
EX, C, nu, TU, SU, RP, SA = parse_config(cfg)
while(True):
try:
#async_run(EX, C)
run(EX,C)
if(cfg['server']):
dump_json(EX)
else:
print_table(EX=EX,C=C,SU=SU,TU=TU,nu=nu,RP=RP, SA = SA)
except Exception as e:
print("failed with error code: {}".format(e))
time.sleep(cfg['refresh'])