riteshcp commited on
Commit
19108ab
·
verified ·
1 Parent(s): 4b273d3

Create old version

Browse files
Files changed (1) hide show
  1. old version +106 -0
old version ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import yfinance as yf
4
+ import numpy as np
5
+ import datetime
6
+
7
+ def fetch_stock_data(symbol, start_date, end_date, interval):
8
+ try:
9
+ valid_intervals = {
10
+ "1m": 8, # 1-minute data is available for up to 8 days
11
+ "2m": 60, # 2-minute data is available for up to 60 days
12
+ "5m": 60, # 5-minute data is available for up to 60 days
13
+ "15m": 60, # 15-minute data is available for up to 60 days
14
+ "30m": 60, # 30-minute data is available for up to 60 days
15
+ "60m": 730, # 60-minute data is available for up to 730 days (2 years)
16
+ "90m": 730, # 90-minute data is available for up to 730 days (2 years)
17
+ "1h": 730, # 1-hour data is available for up to 730 days (2 years)
18
+ "1d": 10000, # Daily data is available for up to maximum available
19
+ "5d": 10000, # 5-day data is available for up to maximum available
20
+ "1wk": 10000,# Weekly data is available for up to maximum available
21
+ "1mo": 10000,# Monthly data is available for up to maximum available
22
+ "3mo": 10000 # Quarterly data is available for up to maximum available
23
+ }
24
+
25
+ if interval not in valid_intervals:
26
+ st.error(f"Invalid interval: {interval}. Please choose from {list(valid_intervals.keys())}")
27
+ return
28
+
29
+ max_days = valid_intervals[interval]
30
+ start_date_pd = pd.to_datetime(start_date)
31
+ end_date_pd = pd.to_datetime(end_date)
32
+ max_start_date = end_date_pd - pd.to_timedelta(max_days, unit='d')
33
+
34
+ if start_date_pd < max_start_date:
35
+ st.warning(f"The selected start date exceeds the maximum allowed range for {interval} interval. Adjusting start date to {max_start_date.strftime('%Y-%m-%d')}")
36
+ start_date_pd = max_start_date
37
+
38
+ ticker = yf.Ticker(symbol)
39
+ data = ticker.history(start=start_date_pd.strftime('%Y-%m-%d'), end=end_date_pd.strftime('%Y-%m-%d'), interval=interval)
40
+ if not data.empty:
41
+ data.reset_index(inplace=True)
42
+ data.rename(columns={'index': 'Datetime'}, inplace=True)
43
+ # Calculate On-Balance Volume (OBV)
44
+ data['OBV'] = (np.sign(data['Close'].diff()) * data['Volume']).fillna(0).cumsum()
45
+ # Calculate Relative Strength Index (RSI)
46
+ delta = data['Close'].diff()
47
+ gain = (delta.where(delta > 0, 0)).rolling(window=14).mean()
48
+ loss = (-delta.where(delta < 0, 0)).rolling(window=14).mean()
49
+ rs = gain / loss
50
+ data['RSI'] = 100 - (100 / (1 + rs))
51
+ # Calculate Exponential Moving Average (EMA)
52
+ data['EMA'] = data['Close'].ewm(span=20, adjust=False).mean()
53
+ # Calculate Simple Moving Average (SMA)
54
+ data['SMA'] = data['Close'].rolling(window=20).mean()
55
+
56
+ st.write(f"Data for {symbol} fetched successfully with OBV, RSI, EMA, and SMA included.")
57
+ st.write(data)
58
+ else:
59
+ st.error(f"No data available for {symbol}. Please check the symbol or try a different date range.")
60
+ except Exception as e:
61
+ st.error(f"Error fetching data for {symbol}: {e}")
62
+
63
+ # Stock List
64
+ stock_list = [
65
+ "RELIANCE.NS", "TCS.NS", "INFY.NS", "HDFCBANK.NS", "ICICIBANK.NS",
66
+ "KOTAKBANK.NS", "SBIN.NS", "LT.NS", "HINDUNILVR.NS", "BHARTIARTL.NS"
67
+ ]
68
+
69
+ # Streamlit app layout
70
+ st.title("Share Market Data")
71
+ symbol = st.selectbox("Select Stock Symbol", options=stock_list)
72
+ interval = st.selectbox("Select Data Interval", options=["1m", "2m", "5m", "15m", "30m", "60m", "90m", "1h", "1d", "5d", "1wk", "1mo", "3mo"])
73
+
74
+ # Allow user to select a start date
75
+ start_date = st.date_input("Select Start Date", pd.to_datetime('2023-01-01'))
76
+ end_date = pd.to_datetime('today').strftime('%Y-%m-%d')
77
+
78
+ valid_intervals = {
79
+ "1m": 8, # 1-minute data is available for up to 8 days
80
+ "2m": 60, # 2-minute data is available for up to 60 days
81
+ "5m": 60, # 5-minute data is available for up to 60 days
82
+ "15m": 60, # 15-minute data is available for up to 60 days
83
+ "30m": 60, # 30-minute data is available for up to 60 days
84
+ "60m": 730, # 60-minute data is available for up to 730 days (2 years)
85
+ "90m": 730, # 90-minute data is available for up to 730 days (2 years)
86
+ "1h": 730, # 1-hour data is available for up to 730 days (2 years)
87
+ "1d": 10000, # Daily data is available for up to maximum available
88
+ "5d": 10000, # 5-day data is available for up to maximum available
89
+ "1wk": 10000,# Weekly data is available for up to maximum available
90
+ "1mo": 10000,# Monthly data is available for up to maximum available
91
+ "3mo": 10000 # Quarterly data is available for up to maximum available
92
+ }
93
+
94
+ max_days = valid_intervals[interval]
95
+ end_date_pd = pd.to_datetime('today')
96
+ max_start_date = end_date_pd - pd.to_timedelta(max_days, unit='d')
97
+
98
+ # Adjust the start date if it exceeds the limit
99
+ if pd.to_datetime(start_date) < max_start_date:
100
+ st.warning(f"The selected start date exceeds the maximum allowed range for {interval} interval. Adjusting start date to {max_start_date.strftime('%Y-%m-%d')}")
101
+ start_date = max_start_date
102
+
103
+ fetch_data = st.button("Fetch and Display Data")
104
+
105
+ if fetch_data and symbol:
106
+ fetch_stock_data(symbol, start_date.strftime('%Y-%m-%d'), end_date, interval)