-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
244 lines (178 loc) · 8.15 KB
/
main.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
242
243
244
import pandas
import requests
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import smtplib
# your api here
API_KEY = "your api key"
API_Nomics = "your api here"
def getCryptoRates(currencyName='INR', assets='BTC,ETH,XRP'):
url = 'https://api.nomics.com/v1/currencies/ticker'
payload = {'key': API_Nomics, 'convert': currencyName, 'ids': assets, 'interval': '1d'}
response = requests.get(url, params=payload)
data = response.json()
crypto, price, time = [], [], []
for asset in data:
crypto.append(asset['currency'])
price.append(asset['price'])
time.append(asset['price_timestamp'])
raw_data = {
'assets': crypto,
'rates': price,
'timestamp': time
}
df_cryptoRates = pd.DataFrame(raw_data)
return df_cryptoRates
def setAlert(dataframe, asset, alert_price):
user_email_func = input('Enter email where you want to be prompted:')
cryptoValue = float(dataframe[dataframe['assets'] == asset]['rates'].item())
price_info = f'{asset} : {cryptoValue}, Target: {alert_price}'
if cryptoValue >= alert_price:
print(price_info + '<< Target value reached')
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
print(user_email_func)
server.login('your mail id', 'password for windows app here')
subject_mail = f'Target value of {asset} has been achieved'
body_mail = price_info
msg = f"Subject: {subject_mail}\n\n{body_mail}"
server.sendmail('your mail id', user_email_func, msg)
print('Hey the email has been sent')
server.quit()
else:
print(price_info)
def supportedCurrencies():
url = f'https://api.coingecko.com/api/v3/coins'
responseCrypto = requests.get(url)
data = responseCrypto.json()
cryptoNames = []
for assets in data:
cryptoNames.append(assets['id'])
return cryptoNames
def cryptoChart(cryptoList, coinName_chart='bitcoin', currencyToCheck='inr', daysToTrack='30', interval='daily'):
if coinName_chart in cryptoList:
url = f'https://api.coingecko.com/api/v3/coins/{coinName_chart}/market_chart'
payload = {'vs_currency': currencyToCheck, 'days': daysToTrack, 'interval': interval}
resource = requests.get(url, payload)
data = resource.json()
timeStampList, priceList = [], []
for price in data['prices']:
timeStampList.append(datetime.datetime.fromtimestamp(price[0] / 1000))
priceList.append(price[1])
raw_data_map = {
'timestamp': timeStampList,
'price': priceList
}
df_chart = pandas.DataFrame(raw_data_map)
return df_chart
else:
print('The crypto you entered is not supported check out our supported crypto list:')
print(cryptoList)
def getYearlyrates(amount, currency, converted_currency, days):
today_date = datetime.datetime.now()
# Api takes only days so converting into days
date_yearAgo = (today_date - datetime.timedelta(days=1 * days))
url = f'https://api.exchangerate.host/timeseries'
payload = {'base': currency, 'amount': amount, 'start_date': date_yearAgo.date(), 'end_date': today_date.date()}
response_currency = requests.get(url, params=payload)
data = response_currency.json()
currency_hist = {}
rates_history = []
for item in data['rates']:
current_date = item
currency_rate = data['rates'][item][converted_currency]
currency_hist[current_date] = [currency_rate]
rates_history.append(currency_rate)
pd_data = pd.DataFrame(currency_hist).transpose()
pd_data.columns = ['Rate']
pd.set_option('display.max_rows', None)
print(pd_data)
plt.plot(rates_history)
plt.ylabel(f'{amount} {currency} to {converted_currency}')
plt.xlabel('Days')
plt.title(f'Current exchange rate for {amount} {currency} to {converted_currency} is {rates_history[-1]}')
plt.show()
def get_crypto_data(currencyFun="USD", crypto="BTC", invert='true'):
url = f'https://rest.coinapi.io/v1/exchangerate/{currencyFun}?invert={invert}'
headers = {'X-CoinAPI-Key': API_KEY}
response = requests.get(url, headers=headers)
data = response.json()
assets_names, assets_rates = [], []
for asset in data['rates']:
asset_id = asset['asset_id_quote']
rate = asset['rate']
assets_names.append(asset_id)
assets_rates.append(rate)
raw_data = {
'assets': assets_names,
'rates': assets_rates
}
df_coin = pd.DataFrame(raw_data)
pd.set_option('display.max_rows', None)
value = df_coin.loc[df_coin.assets == crypto, 'rates'].tolist()[0]
print(df_coin)
return value
# Execution starts here
want_to_continue = 1
while want_to_continue == 1:
whatToDO = int(
input('\nPlease Enter digit for what you want to do: \n 1->Currency Conversions \n 2->Crypto Price track \n '
'3->Exit'))
if whatToDO == 1:
graphChahiye = int(
input('Please enter what you want to do with currencies: \n 1-> Check for conversions \n 2->Track its '
'changes '))
if graphChahiye == 1:
CurrencyFrom = input('Enter the abbr of the currency to be converted:')
CurrencyTo = input('Enter the abbr of currency to be checked:')
money = input('Enter the amount to be converted:')
getYearlyrates(money, CurrencyFrom, CurrencyTo, 1)
elif graphChahiye == 2:
CurrencyFrom = input('Enter the abbr of the currency to be converted:')
CurrencyTo = input('Enter the abbr of currency to be checked:')
daysTocheck = int(input('Enter the days to be checked(Max 1year):'))
getYearlyrates(1, CurrencyFrom, CurrencyTo, daysTocheck)
else:
print("\nEnter a valid response")
elif whatToDO == 2:
invert_checker = int(input(
'Enter What has to be done \n1-> Crypto -> Currency \n2-> Currency -> Crypto \n3-> Track Crypto changes '
'\n4-> Set alerts'))
if invert_checker == 1:
Crypto = input('Enter the crypto currency abbreviation:')
currency_entered = input('Enter the currency abbreviation:')
valueToPrint = get_crypto_data(currency_entered, Crypto)
print(f'\n The current price of 1 {Crypto} is {valueToPrint} in {currency_entered}')
elif invert_checker == 2:
Crypto = input('Enter the crypto currency abbreviation:')
currency_entered = input('Enter the currency abbreviation:')
valueToPrint = get_crypto_data(currency_entered, Crypto, 'false')
print(f'\n The current price of 1 {currency_entered} is {valueToPrint} in {Crypto}')
elif invert_checker == 3:
cryptoOptionsAvailable = supportedCurrencies()
print('here are the list of currencies that you can track:', cryptoOptionsAvailable)
Crypto = input('Enter the crypto currency (type them same as shown above):')
currency_entered = input('Enter the currency abbreviation (in small):')
daysTocheck = input('Enter the days to be checked (enter max for all data):')
printThisPlease = cryptoChart(cryptoOptionsAvailable, Crypto, currency_entered, daysTocheck)
print(printThisPlease)
printThisPlease.plot(y='price', x='timestamp')
plt.ylabel(f'{Crypto} to {currency_entered}')
plt.xlabel('Days')
plt.title(f'exchange graph for {Crypto} to {currency_entered}')
plt.show()
elif invert_checker == 4:
coinName = input('Enter the crypto abbreviation you want to set alert for:')
currencyNameEntered = input('Enter the currency abbreviation you want to check:')
alertPrice = float(input('Enter the price above which you want to be alerted:'))
df = getCryptoRates(currencyNameEntered, assets=coinName)
setAlert(df, coinName, alertPrice)
else:
print('\nEnter a valid response')
elif whatToDO == 3:
break
else:
print("\nEnter a valid response")