-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiAlerts.py
More file actions
145 lines (104 loc) · 4.62 KB
/
apiAlerts.py
File metadata and controls
145 lines (104 loc) · 4.62 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
'''
Author: Jonathan Cucci
'''
import requests, json
import sys
import datetime
from statistics import *
import statistics
def output(msg, variable = ""):
print(str(datetime.datetime.now()) + msg + str(variable))
def price_dev(sym):
output(" - AlertingTool - INFO - *** Running priceDev check on: ", sym)
output(" - AlertingTool - INFO - Getting Price API Data for: ", sym)
price_info = (requests.get("https://api.gemini.com/v2/ticker/" + sym).json())
last_price = float(price_info['close'])
output(" - AlertingTool - INFO - Last Price: ", last_price)
float_changes = [float(i) for i in price_info['changes']]
std_dev = statistics.stdev(float_changes)
average = mean(float_changes)
diff = average - last_price
output(" - AlertingTool - INFO - Standard Deviation: ", std_dev)
output(" - AlertingTool - INFO - Average: ", average)
output(" - AlertingTool - INFO - Price diff: ", diff)
if abs(diff) > std_dev:
output(" - AlertingTool - ERROR - ****** Price Deviation")
print("\n")
def price_change(sym):
x = float(input("Enter your percent change value: "))
output(" - AlertingTool - INFO - *** Running priceChange check on: ", sym)
output(" - AlertingTool - INFO - Getting Price API Data for: ", sym)
price_info = (requests.get("https://api.gemini.com/v2/ticker/" + sym).json())
opening_price = float(price_info['open'])
closing_price = float(price_info['close'])
low = (1-(x/100)) * opening_price
high = (1+(x/100)) * opening_price
output(" - AlertingTool - INFO - Opening Price: ", opening_price)
output(" - AlertingTool - INFO - Current Price: ", closing_price)
output(" - AlertingTool - INFO - Percent Difference: ", (str(x) + "%"))
output(" - AlertingTool - INFO - Low Threshold: ", str(low))
output(" - AlertingTool - INFO - High Threshold: ", str(high))
if closing_price < low or closing_price > high:
output(" - AlertingTool - ERROR - ****** Price Change outside Percent Threshold")
print("\n")
def volume_deviation(sym):
x = float(input("Enter your percent change value: "))
output(" - AlertingTool - INFO - *** Running volumeDeviation check on: ", sym)
output(" - AlertingTool - INFO - Getting Volume API Data for: ", sym)
base = (requests.get("https://api.gemini.com/v1/symbols/details/" + sym).json())['base_currency']
volume = float((requests.get("https://api.gemini.com/v1/pubticker/" + sym).json())['volume'][base])
output(" - AlertingTool - INFO - Base Currency: ", base)
output(" - AlertingTool - INFO - Volume in Market: ", volume)
trade_info = (requests.get("https://api.gemini.com/v1/trades/" + sym).json())[0]
amount_traded = float(trade_info['amount'])
output(" - AlertingTool - INFO - Most Recent Trade Volume: ", amount_traded)
percent_of_volume = (amount_traded / volume) * 100
output(" - AlertingTool - INFO - Percent of Volume Purchase: ", (str(percent_of_volume) + "%"))
output(" - AlertingTool - INFO - Percent Threshold: ", (str(x) + "%"))
if percent_of_volume > x:
output(" - AlertingTool - ERROR - ****** Volume purchased outside Percent Threshold")
print("\n")
output(" - AlertingTool - INFO - Parsing args")
args = sys.argv
currencies = requests.get("https://api.gemini.com/v1/symbols").json()
functions = ["pricedev", "pricechange", "voldev", "all"]
if "-h" in args:
print("\n")
print("List of functions")
print(functions)
print("\n")
print("List of currencies")
print(currencies)
exit()
try:
symbol = args[args.index('-c') + 1]
except ValueError:
print("Please enter a '-c' with proper CURRENCY from below.")
print(currencies)
quit()
if symbol not in currencies:
print(currencies)
raise "Currency input not in list of currencies"
try:
command = args[args.index('-t') + 1].lower()
except ValueError:
print("Please enter a '-t' with proper FUNCTION from below.")
print(functions)
quit()
if command not in functions:
print(functions)
raise "Function input not in list of functions"
output(" - AlertingTool - INFO - Running Check: ", command)
print("\n")
base_url = "https://api.gemini.com/v1"
symbol_details = (requests.get(base_url + "/symbols/details/" + symbol)).json()
if command == "pricedev":
price_dev(symbol)
elif command == "pricechange":
price_change(symbol)
elif command == "voldev":
volume_deviation(symbol)
elif command == "all":
price_dev(symbol)
price_change(symbol)
volume_deviation(symbol)