|
import yfinance as yf |
|
import pandas as pd |
|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
import seaborn as sns |
|
import streamlit as st |
|
|
|
|
|
def fetch_data(ticker_symbol): |
|
ticker = yf.Ticker(ticker_symbol) |
|
data = ticker.history(period="1y") |
|
return data |
|
|
|
|
|
def calculate_indicators(df, lengthEMA=3, lengthRSI=14, momentumLength=3, trendLength=50): |
|
|
|
df['MA20'] = df['Close'].rolling(window=20).mean() |
|
df['MA50'] = df['Close'].rolling(window=50).mean() |
|
|
|
|
|
buy_signal = df['Close'] - df['Open'] |
|
df['SignalEMA'] = df['Close'].ewm(span=lengthEMA, adjust=False).mean() |
|
|
|
|
|
delta = df['Close'].diff(1) |
|
gain = (delta.where(delta > 0, 0)).rolling(window=lengthRSI).mean() |
|
loss = (-delta.where(delta < 0, 0)).rolling(window=lengthRSI).mean() |
|
rs = gain / loss |
|
df['RSI'] = 100 - (100 / (1 + rs)) |
|
|
|
|
|
df['Momentum'] = df['Close'].diff(momentumLength) |
|
|
|
|
|
df['SignalComposite'] = (0.5 * df['SignalEMA']) + (0.3 * (df['RSI'] - 50) / 100) + (0.2 * (df['Momentum'] / df['Close'].rolling(window=lengthRSI).mean())) |
|
|
|
|
|
df['SmoothedSignal'] = df['SignalComposite'].ewm(span=lengthEMA, adjust=False).mean() |
|
|
|
|
|
df['TrendSMA'] = df['Close'].rolling(window=trendLength).mean() |
|
|
|
|
|
buyThreshold = 0.75 |
|
sellThreshold = -0.75 |
|
df['BuySignal'] = (df['SmoothedSignal'] > buyThreshold) & (df['Close'] > df['TrendSMA']) |
|
df['SellSignal'] = (df['SmoothedSignal'] < sellThreshold) & (df['Close'] < df['TrendSMA']) |
|
|
|
return df |
|
|
|
|
|
st.title("Indian Share Market Analysis") |
|
|
|
|
|
st.sidebar.header("Select Stock Ticker") |
|
ticker_symbol = st.sidebar.text_input("Enter Stock Ticker (e.g., RELIANCE.NS, ^NSEI)", "^NSEI") |
|
|
|
|
|
nifty_data = fetch_data(ticker_symbol) |
|
nifty_data = calculate_indicators(nifty_data) |
|
|
|
|
|
st.subheader(f"Data Overview for {ticker_symbol}") |
|
st.write(nifty_data.head()) |
|
|
|
|
|
st.subheader(f"Close Price for {ticker_symbol}") |
|
fig, ax = plt.subplots(figsize=(12, 6)) |
|
sns.lineplot(x=nifty_data.index, y=nifty_data['Close']) |
|
st.pyplot(fig) |
|
|
|
st.subheader(f"Moving Averages, EMA, and RSI for {ticker_symbol}") |
|
fig, ax = plt.subplots(figsize=(12, 6)) |
|
sns.lineplot(x=nifty_data.index, y=nifty_data['MA20'], label='MA20') |
|
sns.lineplot(x=nifty_data.index, y=nifty_data['MA50'], label='MA50') |
|
sns.lineplot(x=nifty_data.index, y=nifty_data['RSI'], label='RSI') |
|
plt.axhline(y=70, color='red', linestyle='--', label='Overbought (70)') |
|
plt.axhline(y=30, color='green', linestyle='--', label='Oversold (30)') |
|
plt.legend() |
|
plt.title("Moving Averages and RSI") |
|
st.pyplot(fig) |
|
|
|
|
|
st.subheader(f"Buy and Sell Signals for {ticker_symbol}") |
|
fig, ax = plt.subplots(figsize=(12, 6)) |
|
sns.lineplot(x=nifty_data.index, y=nifty_data['Close'], label='Close Price', color='blue') |
|
sns.lineplot(x=nifty_data.index, y=nifty_data['TrendSMA'], label='Trend SMA', color='gray') |
|
|
|
|
|
buy_signals = nifty_data[nifty_data['BuySignal']] |
|
plt.scatter(buy_signals.index, buy_signals['Close'], marker='^', color='green', label='Buy Signal', s=100) |
|
|
|
|
|
sell_signals = nifty_data[nifty_data['SellSignal']] |
|
plt.scatter(sell_signals.index, sell_signals['Close'], marker='v', color='red', label='Sell Signal', s=100) |
|
|
|
plt.legend() |
|
plt.title("Buy and Sell Signals with Trend Filter") |
|
st.pyplot(fig) |
|
|
|
|
|
st.sidebar.subheader("Select Timeframe:") |
|
timeframes = ['15m', '60m', '1d', '5d', '1mo', '3mo', '6mo', '1y', '2y', '5y', '10y', 'ytd', 'max'] |
|
selected_timeframe = st.sidebar.selectbox('Timeframe', timeframes) |
|
|
|
st.sidebar.subheader("Set Alerts:") |
|
alert_type = st.sidebar.selectbox('Alert Type', ['Price', 'RSI']) |
|
alert_value = st.sidebar.number_input('Enter Alert Value') |
|
|
|
|
|
|
|
|