-
Notifications
You must be signed in to change notification settings - Fork 5
/
siberian
87 lines (70 loc) · 2.78 KB
/
siberian
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
from bittrex import Bittrex
bitty = Bittrex("None", "None")
from time import sleep
import json
def usdt_btc_l():
"""This function returns the long average price of BTC in USDT"""
return(avg_mkt_p("USDT-BTC", 100))
def usdt_btc_s():
"""This function returns the short average price of BTC in USDT"""
return(avg_mkt_p("USDT-BTC", 5))
def avg_mkt_p(mkt, count=20):
"""
This function returns the avg price of any market currency over the last (count) trades
Price will be in the preceding currency given in the mkt argument ("ETH-LTC" will return a price in ETH)
count: the number of most recent trades to average
mkt: string determining market ("USDT-BTC", "BTC-VTC")
"""
clist = bitty.get_market_history(mkt, count)['result']
plist = []
for c in clist:
plist.append(c['Price'])
total = 0
for p in plist:
total += p
return(total/len(plist))
def mkt_p_usd(mkt, count=20):
"""
This function returns the price in USD of any BTC-tradable currency over the last (count) trades
mkt: string determining market ("USDT-BTC", "BTC-VTC")
"""
avg = avg_mkt_p(mkt, count)
btcp = (avg_mkt_p("USDT-BTC", count))
return (avg * btcp)
def wrt_mkt_json(mkt, count=20):
"""This file is used to build a JSON with a long-term market history
It fetches the recent history and compares it against the file that already exists. If no file exists one is created
and written to. If it exists then the current data is evaluated to see if it is duplicate and then written depending
on that evaluation."""
try:
with open(mkt + "data.json", "r") as f:
write =0
mList = bitty.get_market_history(mkt, count)['result']
file_list = json.loads(f.readline())
for m in mList:
if m in file_list: #checking for duplicates
pass
else:
write = 1
file_list.append(m)
if write==1:
with open(mkt + "data.json", "w") as f:
json.dump(file_list, f) #writes new data, if any
return(mkt + " file updated.")
else:
return("No changes written to " + mkt + " file.")
except FileNotFoundError:
init_file(mkt, count)
def init_file(mkt, count=20):
"""Simply writes a JSON with the recent market history, will delete history if your file already exists"""
with open(mkt + "data.json", "w") as f:
json.dump(bitty.get_market_history(mkt, count)['result'], f)
print("Made initial", mkt, "data file.")
def main():
while True:
print(wrt_mkt_json("BTC-ETH", 20))
print(wrt_mkt_json("BTC-VTC", 20))
#wrt_mkt_json("ETH-VTC", 20)
sleep(10)
if __name__ == "__main__":
main()