-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr_dash.py
More file actions
102 lines (88 loc) · 3.9 KB
/
str_dash.py
File metadata and controls
102 lines (88 loc) · 3.9 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
import requests, pandas as pd, streamlit as st
from Trader_made import free_api_key
from datetime import date, timedelta
today = date.today()- timedelta(days=1)
endpoints = st.sidebar.selectbox('End_points',options=['timeseries','historical'])
base_url = f"https://marketdata.tradermade.com/api/v1/{endpoints}?api_key={free_api_key}"
#ISO currency
cur_s = pd.read_csv("countryCurrISO.csv")
cur_s=list(cur_s)
#From API :( !!
#cur_list_url = f"https://marketdata.tradermade.com/api/v1/historical_currencies_list?api_key={free_api_key}"
#cur_list = requests.get(cur_list_url).json()
#cur_s = list(cur_list.values())[0]
st.title(":blue[Currency Exchange Rates]", anchor=None)
st.write("""
## Historical currency rates
source: TraderMade
""")
if endpoints== 'timeseries':
currency = st.sidebar.selectbox('currency',options=cur_s)
interval = st.sidebar.selectbox('Inteval',options=['daily','hourly'])
if interval=='daily':
delta_date = timedelta(days = 365)
else:
delta_date = timedelta(days = 30)
start_date =st.sidebar.date_input("Start date", today - delta_date,
max_value=today,
min_value=date(2010,1,1),
format="YYYY-MM-DD")
default_end = (lambda x, y: y if x >= y else x)(start_date+delta_date, today)
end_date = st.sidebar.date_input("End date",default_end,
max_value=default_end,
min_value=start_date,
format="YYYY-MM-DD")
tail_url = f"¤cy={currency}&format=records&start_date={start_date}&end_date={end_date}&interval={interval}&period=1"
url = base_url+tail_url
#get data
data = requests.get(url).json()
#data from csv file, comment the above line and uncoment these to use csv
#data = {
# "base_currency": "USD",
# "end_date": "2024-06-18",
# "endpoint": "timeseries",
# "quote_currency": "INR"}
#df = pd.read_csv("2024-06-18T08-04_export.csv",index_col=0)
#heading
st.header(f"""
:green[Timeseries.]
""",help="close: closing rate, high: highest rate, low: lowest rate, open: opening rate")
try:
#write info
st.write(f"""
#### Base Currency(ISO): {data["base_currency"]} | Quote_currency(ISO): {data["quote_currency"]}
start :{start_date} | end :{end_date}\n
For daily interval : Data available for one year period. | For hour interval: Data for one month period.
""")
#get dataframe of quotes
df = pd.DataFrame(data["quotes"])
#st.dataframe to display df.
st.dataframe(df,use_container_width = True)
st.header(f"""Value of {data["quote_currency"]} against {data["base_currency"]}""",
help="Scroll in the chart to zoom in, click and drag to move with in the chart")
st.line_chart(data=df, x='date', y='close', color='#ADD8E6', use_container_width=True)
except:
#write data from url if the api returns with any erros ar different data.
st.write(data)
data = None
else:
#sidebar
currency = st.sidebar.selectbox('currency',options=cur_s)
data_date =st.sidebar.date_input("Date", today,
max_value=today,
min_value=date(2016,1,1),
format="YYYY-MM-DD")
#url for data
tail_url = f"¤cy={currency}&date={data_date}"
url = base_url+tail_url
#get data
data = requests.get(url).json()
#heading
st.header(f" :green[Historical]",help=" ")
#read and print data
try:
df = pd.DataFrame(data["quotes"])
st.dataframe(df,use_container_width = True)
except:
st.write(data)
data = None