-
Notifications
You must be signed in to change notification settings - Fork 2
/
bcnaracle.py
241 lines (212 loc) · 8.46 KB
/
bcnaracle.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# -*- coding: utf-8 -*-
from time import sleep, strftime
import numpy as np
from termcolor import colored
from requests import Request, Session
import json
## Get the config variables from bcnaracle_config.py
from bcnaracle_config import UPDATE_INTERVAL_SECONDS, PATH, JSON_FIAT_INPUT_FILE, CSV_OUTPUT_FILE, CSV_LOG_OUTPUT_FILE, JSON_FIAT_OUTPUT_BCNA_FILE, X_CMC_PRO_API_KEY, OHCL_URL, OHCL_FILE
## Get data from Price Feeds
def getCMC (last_average): # Function to get the info
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest' # Coinmarketcap API url
parameters = { 'slug': 'bitcanna', 'convert': 'USD' } # API parameters to pass in for retrieving specific cryptocurrency data
prepared_header = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': X_CMC_PRO_API_KEY
}
try:
session = Session()
session.headers.update(prepared_header)
response = session.get(url, params=parameters)
except:
conn_error = 'An error occurred getting the price for ' + url
print("\n"+conn_error)
log_this(conn_error)
price = last_average
else:
try:
info = json.loads(response.text)
except:
price = last_average
print('Error getting the CMC price. Bad JSON decoding.')
else:
if "data" in info:
price = info["data"]["4263"]["quote"]["USD"]["price"]
else:
print('Error getting the CMC price. Check the JSON response')
print(info)
log_this(str(info))
price = last_average
return price
def getCG (last_average):
url = 'https://api.coingecko.com/api/v3/simple/price?ids=bitcanna&vs_currencies=usd'
parameters = { 'ids': 'bitcanna', 'vs_currencies': 'usd' } # API parameters to pass in for retrieving specific cryptocurrency data
headers = {
'Accepts': 'application/json'
}
try:
session = Session()
session.headers.update(headers)
response = session.get(url, params=parameters)
except:
conn_error = 'An error occurred getting the price for ' + url
print("\n"+conn_error)
log_this(conn_error)
price = last_average
else:
try:
info = json.loads(response.text)
except: #JSON.JSONDecodeError:
price = last_average
else:
if "bitcanna" in info:
price = info["bitcanna"]["usd"]
else:
print('Error getting the CoinGecko price. Check the JSON response')
print(info)
log_this(str(info))
price = last_average
return price
def getOsmo (last_average):
url = 'https://api-osmosis.imperator.co/tokens/v2/price/bcna'
parameters = { 'ids': 'bitcanna', 'vs_currencies': 'usd' } # API parameters to pass in for retrieving specific cryptocurrency data
headers = {
'Accepts': 'application/json'
}
try:
session = Session()
session.headers.update(headers)
response = session.get(url, params=parameters)
except:
conn_error = 'An error occurred getting the price for ' + url
print("\n"+conn_error)
log_this(conn_error)
price = last_average
else:
try:
info = json.loads(response.text)
except: #json.JSONDecodeError:
price = last_average
else:
if "price" in info:
price = info["price"]
else:
print('Error getting the Osmosis price. Check the JSON response')
print(info)
log_this(str(info))
price = last_average
return price
def oracleit(a,b,c):
prices = [a,b,c]
AVERAGE = np.mean(prices, dtype=np.float64)
print(f"Current values: \n{a}\n{b}\n{c}\n")
print(f"Temporal Average value updated to: {AVERAGE} USD\n\n")
standard_deviation = np.std(prices, dtype=np.float64)
if standard_deviation > AVERAGE * 0.02: #0.1
print (colored('Standard Deviation of All values is ' + str(standard_deviation), 'red'))
print ("How far is from AVERAGE each one:")
denominator = 0
fixed_prices = []
for price in prices:
if abs(price - AVERAGE) > AVERAGE * 0.3: #0.3
print(colored(str(price - AVERAGE), 'red'))
# Not in the Median :[
log = str(a) + "," + str(b)+ "," + str(c) + "," + str(DEFINITIVE_AVERAGE) + "\n"
log_this(log)
else:
print(colored(str(price - AVERAGE), 'green'))
fixed_prices.append(price)
++denominator
print ('Sources to be counted: ', fixed_prices)
DEFINITIVE_AVERAGE = np.mean(fixed_prices, dtype=np.float64)
print (colored('Definitive AVERAGE ' + str(DEFINITIVE_AVERAGE), 'green'))
else:
print (colored('Standard Deviation of All values is ' + str(standard_deviation), 'green'))
DEFINITIVE_AVERAGE = AVERAGE
print (colored('Definitive AVERAGE ' + str(DEFINITIVE_AVERAGE), 'green'))
# Save to CSV file for history log
export_dot = (strftime("%d-%m-%Y-%H:%M") + "," + str(a) + "," + str(b)+ "," + str(c) + "," + str(DEFINITIVE_AVERAGE) + "\n")
file_dot = open(PATH + CSV_OUTPUT_FILE, "a")
file_dot.write (export_dot)
print (export_dot)
return DEFINITIVE_AVERAGE #return values a, b, c to repeat them if Source of data fails.
def log_this(log_info):
string_to_log = strftime('%d-%m-%Y-%H:%M') + ',"' + log_info + '"\n'
file_log = open(PATH + CSV_LOG_OUTPUT_FILE, "a")
file_log.write (string_to_log)
def fiat_price(bcna_price):
calculated_prices = {}
try:
with open(PATH + JSON_FIAT_INPUT_FILE, 'r') as json_data:
info = json.load(json_data)
except:
open_error = 'An error occurred opening the file ' + PATH + JSON_FIAT_INPUT_FILE
log_this(open_error)
calculated_prices = 0
else:
if info["success"]:
currencies = info["rates"]
for key in currencies:
calculated_prices[str(key).lower()] = float(currencies[key] * bcna_price)
else:
print ('Data gathering not successful')
calculated_prices = 0
return calculated_prices
def write_fiat_json_to_file(fiat_json):
if fiat_json != 0:
final_dump = {}
final_dump["bitcanna"] = fiat_json
with open(PATH + JSON_FIAT_OUTPUT_BCNA_FILE, "w") as write_file:
json.dump(final_dump, write_file, indent=4)
print ('Data with FIAT prices store at: ' + JSON_FIAT_OUTPUT_BCNA_FILE)
else:
write_error = 'An error occurred with FIAT prices, check the file ' + PATH + JSON_FIAT_INPUT_FILE
print(write_error)
log_this(write_error)
def read_last_saved_price():
read_price = 0
try:
with open(PATH + JSON_FIAT_OUTPUT_BCNA_FILE, 'r') as json_data:
info = json.load(json_data)
except:
open_error = 'An error occurred opening the file ' + PATH + JSON_FIAT_INPUT_FILE
log_this(open_error)
# we can try to get the data from CoinMarketCap
read_price = getCMC(0.016)
else:
if "bitcanna" in info:
read_price = info["bitcanna"]["usd"]
else:
error_msg = 'Error getting the price from file ' + PATH + JSON_FIAT_INPUT_FILE
print(error_msg)
log_this(error_msg)
read_price = getCMC(0.016)
return read_price
def process_ohcl():
# Make the GET
headers = {
'Accepts': 'application/json'
}
session = Session()
session.headers.update(headers)
response = session.get(OHCL_URL)
if response.status_code == 200:
# If ok
datos_ohlc = response.json()
# Store in a 4 char formated JSON file
with open(PATH + OHCL_FILE, 'w') as archivo_json:
json.dump(datos_ohlc, archivo_json, indent=4)
print(f"OHCL data saved at: {PATH + OHCL_FILE}")
else:
error = f"Error fetching OHCL data from CoinGecko: {response.status_code}"
log_this(error)
print(error)
def main(price):
while True:
price = oracleit(getOsmo(price), getCMC(price), getCG(price)) #check that functions returns ! zero
print (f"Last AVERAGE price: {price}")
write_fiat_json_to_file(fiat_price(price))
process_ohcl()
sleep(UPDATE_INTERVAL_SECONDS)
if __name__ == "__main__":
main(read_last_saved_price())