-
Notifications
You must be signed in to change notification settings - Fork 0
/
Crypto-Tracker.py
138 lines (117 loc) · 4.21 KB
/
Crypto-Tracker.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
import time
import requests
from collections import deque
from datetime import datetime
from datetime import timedelta
import pytz
import sys
import subprocess
coin=sys.argv[1]
UTC=pytz.utc
IST=pytz.timezone('America/Mexico_City')
start_time = int(time.time())
price_history=deque()
percdiff_history=deque()
price_now=0
price_last=0
price_started = None
pda_old=0
sleep_time=10
MAX=10 #max items in the list
def percdiff(price1,price2):
result=round(((price1/price2)*100)-100,2)
return result
def stringify_deque(dlist):
return ','.join(map(str,(list(deque(dlist)))))
def writeFile(msg,filename):
subprocess.check_output("echo \""+msg+"\"> "+filename,shell=True)
def appendFile(msg,filename):
subprocess.check_output("echo \""+msg+"\">> "+filename,shell=True)
def sendEmail(filename):
subprocess.check_output("cat "+filename+" | mail -a 'From: newfinstech' -s '::newfinstech:: - NEW SIGNAL' [email protected];",shell=True)
def logging():
if len(price_history) > 2:
if price_history[0] < price_history[1] and price_history[1] < price_history[2]:
print("price going down")
elif price_history[0] > price_history[1] and price_history[1] > price_history[2]:
print("price going up")
def getPrice(coin):
# DEFINE VARIABLES
global price_now
global price_last
global log_file
global price_started
global pda_old
# MESSAGES
MESSAGE_PRICEHISTORY = coin.upper()+"\nLast Price: "+str(price_last)+"\nPrice History: "+stringify_deque(price_history)+"\nPercDiff History: "+stringify_deque(percdiff_history)+"\n"
# LOGGING
log_file = open(coin+".log","a")
old_stdout = sys.stdout
sys.stdout = log_file
# TIME LOGIC
timestamp = datetime.now(IST).strftime("%m/%d/%Y %H:%M:%S")
current_time = int(time.time())
elapsed = round((current_time - start_time)/10)*10
# API REQUEST
request = requests.get('https://api.coingecko.com/api/v3/coins/'+coin)
json = request.json()
price_now = json['market_data']['current_price']['usd']
if price_started is None:
price_started = price_now
#print("Price Started!",price_started)
if len(price_history) > 0:
print("Price History:",stringify_deque(price_history))
if len(price_history) > 1:
print(stringify_deque(percdiff_history))
if len(price_history) >= 2:
pd1 = percdiff(price_history[0],price_history[1])
pd2 = percdiff(price_history[0],price_history[len(price_history)-1])
pda = percdiff(price_history[0],price_started)
print("Percent Difference Current:",pd1,"Relative:",pd2,"AllTime:",pda)
# SEND SIGNALS FOR PRICE ACTION MOVEMENTS
signal_pda=pda_old - pda
if signal_pda > 3:
pda_old=pda
appendFile(MESSAGE_PRICEHISTORY,coin+".price")
sendEmail(coin+".price")
elif signal_pda < -3:
pda_old=pda
appendFile(MESSAGE_PRICEHISTORY,coin+".price")
sendEmail(coin+".price")
# END SIGNALS
if len(price_history) > MAX:
price_history.pop()
percdiff_history.pop()
# PRINT LOGS
print("")
print("Tracking..",coin.upper(),"elapsed: ",elapsed)
print(timestamp)
if elapsed != 0:
if elapsed % 1800 == 0:
print("every 30 minutes")
elif (elapsed % 600) == 0:
print("every 10 minutes")
elif (elapsed % 300) == 0:
print("every 5 minutes")
elif (elapsed % 60) == 0:
print("every 1 minute")
elif (elapsed % 30) == 0:
print("every 30 seconds")
print("Current Price:",price_now)
# PRICE AND PERCDIFF ARRAY
if price_now != price_last:
if price_last != 0:
percdiff_history.appendleft(percdiff(price_now,price_last))
print("Last Price:",price_last)
else:
percdiff_history.appendleft("Start")
price_history.appendleft(price_now)
# WAIT
time.sleep(sleep_time)
price_last = price_now
logging()
sys.stdout = old_stdout
log_file.close()
# RECURSION
while True:
getPrice(coin)