riteshcp commited on
Commit
1229b10
·
verified ·
1 Parent(s): b15db34

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -12
app.py CHANGED
@@ -3,7 +3,14 @@ import pandas as pd
3
  import numpy as np
4
  import plotly.graph_objects as go
5
  import streamlit as st
 
 
 
 
 
6
  from datetime import datetime
 
 
7
 
8
  # Step 1: Define a function to fetch real-time market data up to the current date
9
  def fetch_data(ticker_symbol):
@@ -31,8 +38,8 @@ def calculate_indicators(df, lengthEMA=3, lengthRSI=14, momentumLength=3, trendL
31
  # Calculate Momentum
32
  df['Momentum'] = df['Close'].diff(momentumLength)
33
 
34
- # Composite SIGNAL Calculation with Adjusted Weight
35
- df['SignalComposite'] = (0.6 * df['SignalEMA']) + (0.2 * (df['RSI'] - 50) / 100) + (0.2 * (df['Momentum'] / df['Close'].rolling(window=lengthRSI).mean()))
36
 
37
  # Smooth the Composite SIGNAL with EMA
38
  df['SmoothedSignal'] = df['SignalComposite'].ewm(span=lengthEMA, adjust=False).mean()
@@ -40,20 +47,16 @@ def calculate_indicators(df, lengthEMA=3, lengthRSI=14, momentumLength=3, trendL
40
  # Trend Filter (SMA)
41
  df['TrendSMA'] = df['Close'].rolling(window=trendLength).mean()
42
 
43
- # Adjusted Thresholds for Buy and Sell Signals
44
- buyThreshold = 1.0
45
- sellThreshold = -1.0
46
- df['BuySignal'] = (df['SmoothedSignal'] > buyThreshold) & (df['Close'] > df['TrendSMA']) & (df['Close'] > df['MA50'])
47
- df['SellSignal'] = (df['SmoothedSignal'] < sellThreshold) & (df['Close'] < df['TrendSMA']) & (df['Close'] < df['MA50'])
48
-
49
- # Add a Cooldown Period for Signals
50
- df['BuySignal'] = df['BuySignal'] & ~df['BuySignal'].shift(1).fillna(False)
51
- df['SellSignal'] = df['SellSignal'] & ~df['SellSignal'].shift(1).fillna(False)
52
 
53
  return df
54
 
55
  # Step 3: Streamlit UI Setup for Stock Selection
56
- st.title("Simplified Indian Share Market Analysis")
57
  st.sidebar.header("Select Stock Ticker")
58
 
59
  # Add a stock selector input box
@@ -121,5 +124,33 @@ try:
121
 
122
  st.plotly_chart(fig)
123
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  except Exception as e:
125
  st.error(f"An error occurred: {str(e)}")
 
3
  import numpy as np
4
  import plotly.graph_objects as go
5
  import streamlit as st
6
+ import altair as alt
7
+ import matplotlib.pyplot as plt
8
+ import seaborn as sns
9
+ from bs4 import BeautifulSoup
10
+ import requests
11
  from datetime import datetime
12
+ from rich import print
13
+ import jsonschema
14
 
15
  # Step 1: Define a function to fetch real-time market data up to the current date
16
  def fetch_data(ticker_symbol):
 
38
  # Calculate Momentum
39
  df['Momentum'] = df['Close'].diff(momentumLength)
40
 
41
+ # Composite SIGNAL Calculation
42
+ df['SignalComposite'] = (0.5 * df['SignalEMA']) + (0.3 * (df['RSI'] - 50) / 100) + (0.2 * (df['Momentum'] / df['Close'].rolling(window=lengthRSI).mean()))
43
 
44
  # Smooth the Composite SIGNAL with EMA
45
  df['SmoothedSignal'] = df['SignalComposite'].ewm(span=lengthEMA, adjust=False).mean()
 
47
  # Trend Filter (SMA)
48
  df['TrendSMA'] = df['Close'].rolling(window=trendLength).mean()
49
 
50
+ # Buy and Sell Signals
51
+ buyThreshold = 0.75
52
+ sellThreshold = -0.75
53
+ df['BuySignal'] = (df['SmoothedSignal'] > buyThreshold) & (df['Close'] > df['TrendSMA'])
54
+ df['SellSignal'] = (df['SmoothedSignal'] < sellThreshold) & (df['Close'] < df['TrendSMA'])
 
 
 
 
55
 
56
  return df
57
 
58
  # Step 3: Streamlit UI Setup for Stock Selection
59
+ st.title("Advanced Indian Share Market Analysis")
60
  st.sidebar.header("Select Stock Ticker")
61
 
62
  # Add a stock selector input box
 
124
 
125
  st.plotly_chart(fig)
126
 
127
+ # Step 7: Altair Chart for Moving Averages
128
+ st.subheader(f"Moving Averages for {ticker_symbol}")
129
+ alt_chart = alt.Chart(nifty_data.reset_index()).mark_line().encode(
130
+ x='Date:T',
131
+ y=alt.Y('MA20:Q', title='Moving Average (20-day)'),
132
+ color=alt.value('orange')
133
+ ).properties(title="20-Day Moving Average")
134
+ st.altair_chart(alt_chart, use_container_width=True)
135
+
136
+ # Step 8: Market News Integration Using BeautifulSoup
137
+ st.sidebar.header("Latest Market News")
138
+ news_url = 'https://www.moneycontrol.com/news/'
139
+ response = requests.get(news_url)
140
+ soup = BeautifulSoup(response.content, 'html.parser')
141
+
142
+ headlines = [headline.text for headline in soup.find_all('h2')[:5]]
143
+ st.sidebar.subheader("Top 5 News Headlines")
144
+ for idx, headline in enumerate(headlines, 1):
145
+ st.sidebar.write(f"{idx}. {headline}")
146
+
147
+ # Step 9: Alerts Using Rich Library
148
+ st.sidebar.subheader("Set Alerts:")
149
+ alert_type = st.sidebar.selectbox('Alert Type', ['Price', 'RSI'])
150
+ alert_value = st.sidebar.number_input('Enter Alert Value')
151
+
152
+ if alert_value:
153
+ print(f"[bold cyan]Alert Set for {alert_type} at Value:[/bold cyan] {alert_value}")
154
+
155
  except Exception as e:
156
  st.error(f"An error occurred: {str(e)}")