shreyashnadage commited on
Commit
0a8e5bb
1 Parent(s): 560ffaf

Commit for huggingface space

Browse files
Files changed (3) hide show
  1. PortfolioAnalytics.py +26 -0
  2. Stock_Static_Data.py +258 -0
  3. main.py +49 -7
PortfolioAnalytics.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import yfinance as yf
2
+ import pandas as pd
3
+ from pypfopt.hierarchical_portfolio import HRPOpt
4
+
5
+
6
+ def get_data_for_HRP(asset_list, n=10):
7
+ df = yf.download(' '.join(asset_list), period='5y', interval='1d')['Adj Close']
8
+ df = df.dropna(axis=1, how='all')
9
+ df = df.pct_change().dropna()
10
+ df = df[df.columns[:n]]
11
+ return df
12
+
13
+ def get_HRP_weights(asset_list):
14
+ prices_data = get_data_for_HRP(asset_list)
15
+
16
+ hrp = HRPOpt(prices_data)
17
+ res = hrp.optimize()
18
+ df = pd.DataFrame({'Stock': [key for key in res.keys()], 'Weights': [round(res[key], 2) for key in res.keys()]})
19
+ return df, hrp
20
+
21
+ def get_portfolio_performance(hrp):
22
+ five_yr_benchmk = yf.download('^NSEI', period='5y', interval='1d')[['Adj Close']]\
23
+ .pct_change().dropna().mean()['Adj Close']
24
+ exp_ret, vol, sharpe = hrp.portfolio_performance(five_yr_benchmk, frequency=252)
25
+ return exp_ret, vol, sharpe
26
+
Stock_Static_Data.py ADDED
@@ -0,0 +1,258 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ nse_stock_sectors = {
2
+ 'RELIANCE': 'Energy',
3
+ 'HDFCBANK': 'Financial Services',
4
+ 'INFY': 'IT',
5
+ 'ICICIBANK': 'Financial Services',
6
+ 'TCS': 'IT',
7
+ 'HINDUNILVR': 'Consumer Goods',
8
+ 'KOTAKBANK': 'Financial Services',
9
+ 'HDFC': 'Financial Services',
10
+ 'BAJFINANCE': 'Financial Services',
11
+ 'AXISBANK': 'Financial Services',
12
+ 'ASIANPAINT': 'Consumer Goods',
13
+ 'ITC': 'Consumer Goods',
14
+ 'LT': 'Infrastructure',
15
+ 'HCLTECH': 'IT',
16
+ 'SBI': 'Financial Services',
17
+ 'BAJAJFINSV': 'Financial Services',
18
+ 'TECHM': 'IT',
19
+ 'NTPC': 'Energy',
20
+ 'BANKBARODA': 'Financial Services',
21
+ 'IOC': 'Energy',
22
+ 'NESTLEIND': 'Consumer Goods',
23
+ 'ONGC': 'Energy',
24
+ 'BPCL': 'Energy',
25
+ 'POWERGRID': 'Infrastructure',
26
+ 'SBIN': 'Financial Services',
27
+ 'BHARTIARTL': 'Telecom',
28
+ 'MARUTI': 'Automobile',
29
+ 'ULTRACEMCO': 'Cement',
30
+ 'BAJAJ-AUTO': 'Automobile',
31
+ 'WIPRO': 'IT',
32
+ 'JSWSTEEL': 'Metals',
33
+ 'TATASTEEL': 'Metals',
34
+ 'HINDZINC': 'Metals',
35
+ 'SUNPHARMA': 'Pharmaceuticals',
36
+ 'TATAMOTORS': 'Automobile',
37
+ 'DMART': 'Retail',
38
+ 'BRITANNIA': 'Consumer Goods',
39
+ 'TITAN': 'Consumer Goods',
40
+ 'GAIL': 'Energy',
41
+ 'DRREDDY': 'Pharmaceuticals',
42
+ 'HINDPETRO': 'Energy',
43
+ 'CIPLA': 'Pharmaceuticals',
44
+ 'BHARTIINFR': 'Telecom',
45
+ 'EICHERMOT': 'Automobile',
46
+ 'M&M': 'Automobile',
47
+ 'ULTRACEMC': 'Cement',
48
+ 'INDUSINDBK': 'Financial Services',
49
+ 'HEROMOTOCO': 'Automobile',
50
+ 'SHREECEM': 'Cement',
51
+ 'GRASIM': 'Cement',
52
+ 'ONGCORP': 'Energy',
53
+ 'ADANIPORTS': 'Infrastructure',
54
+ 'BAJAJHLDNG': 'Financial Services',
55
+ 'DIVISLAB': 'Pharmaceuticals',
56
+ 'TATAPOWER': 'Energy',
57
+ 'ZEEL': 'Media',
58
+ 'NTPC_': 'Energy',
59
+ 'ICICIPRULI': 'Financial Services',
60
+ 'MRF': 'Automobile',
61
+ 'MOTHERSUMI': 'Automobile',
62
+ 'COALINDIA': 'Mining',
63
+ 'SBILIFE': 'Financial Services',
64
+ 'PIDILITIND': 'Consumer Goods',
65
+ 'TATACONSUM': 'Consumer Goods',
66
+ 'SBICARD': 'Financial Services',
67
+ 'DABUR': 'Consumer Goods',
68
+ 'PNB': 'Financial Services',
69
+ 'LUPIN': 'Pharmaceuticals',
70
+ 'UBL': 'Consumer Goods',
71
+ 'BHEL': 'Infrastructure',
72
+ 'ACC': 'Cement',
73
+ 'HINDALCO': 'Metals',
74
+ 'TORNTPHARM': 'Pharmaceuticals',
75
+ 'BOSCHLTD': 'Automobile',
76
+ 'DLF': 'Real Estate',
77
+ 'AMBUJACEM': 'Cement',
78
+ 'SAIL': 'Metals',
79
+ 'MUTHOOTFIN': 'Financial Services',
80
+ 'MCDOWELL-N': 'Consumer Goods',
81
+ 'PFC': 'Financial Services',
82
+ 'BEL': 'Defense',
83
+ 'BANDHANBNK': 'Financial Services',
84
+ 'PEL': 'Conglomerate',
85
+ 'TORNTPOWER': 'Energy',
86
+ 'HAVELLS': 'Consumer Goods',
87
+ 'FEDERALBNK': 'Financial Services',
88
+ 'BERGEPAINT': 'Consumer Goods',
89
+ 'RBLBANK': 'Financial Services',
90
+ 'INDIGO': 'Airlines',
91
+ 'RAMCOCEM': 'Cement',
92
+ 'EXIDEIND': 'Automobile',
93
+ 'CHOLAFIN': 'Financial Services',
94
+ 'ICICIGI': 'Financial Services',
95
+ 'BANKINDIA': 'Financial Services',
96
+ 'ADANIGREEN': 'Energy',
97
+ 'HDFCLIFE': 'Financial Services',
98
+ 'APOLLOHOSP': 'Healthcare',
99
+ 'AUROPHARMA': 'Pharmaceuticals',
100
+ 'IGL': 'Energy',
101
+ 'TVSMOTOR': 'Automobile',
102
+ 'GODREJCP': 'Consumer Goods',
103
+ 'MGL': 'Energy',
104
+ 'BATAINDIA': 'Retail',
105
+ 'M&MFIN': 'Financial Services',
106
+ 'NIACL': 'Financial Services',
107
+ 'ADANIENT': 'Conglomerate',
108
+ 'JINDALSTEL': 'Metals',
109
+ 'BANKNIFTY': 'Index',
110
+ 'COLPAL': 'Consumer Goods',
111
+ 'UBL_': 'Consumer Goods',
112
+ 'INDIANB': 'Financial Services',
113
+ 'BANKBAROD': 'Financial Services',
114
+ 'ASHOKLEY': 'Automobile',
115
+ 'SRTRANSFIN': 'Financial Services',
116
+ 'ACC_': 'Cement',
117
+ 'SIEMENS': 'Infrastructure',
118
+ 'HDFCAMC': 'Financial Services',
119
+ 'AMARAJABAT': 'Automobile',
120
+ 'BSE': 'Index',
121
+ 'MGL_': 'Energy',
122
+ 'BAJAJHLDNG_': 'Financial Services',
123
+ 'AMBUJACEM_': 'Cement',
124
+ 'BPCL_': 'Energy',
125
+ 'IDFCFIRSTB': 'Financial Services',
126
+ 'IDEA': 'Telecom',
127
+ 'PFIZER': 'Pharmaceuticals',
128
+ 'BANDHANBNK_': 'Financial Services',
129
+ 'HCLTECH_': 'IT',
130
+ 'MINDTREE': 'IT',
131
+ 'HDFCBANK_': 'Financial Services',
132
+ 'ASHOKLEY_': 'Automobile',
133
+ 'PNBHOUSING': 'Real Estate',
134
+ 'GRASIM_': 'Cement',
135
+ 'M&MFIN_': 'Financial Services',
136
+ 'PVR': 'Entertainment',
137
+ 'RPOWER': 'Energy',
138
+ 'TVTODAY': 'Media',
139
+ 'APLLTD': 'Pharmaceuticals',
140
+ 'IDBI': 'Financial Services',
141
+ 'IRCTC': 'Travel',
142
+ 'JINDALSTEL_': 'Metals',
143
+ 'L&TFH': 'Financial Services',
144
+ 'NIITTECH': 'IT',
145
+ 'INDIGO_': 'Airlines',
146
+ 'BANKINDIA_': 'Financial Services',
147
+ 'MINDACORP': 'Retail',
148
+ 'FEDERALBNK_': 'Financial Services',
149
+ 'GLENMARK': 'Pharmaceuticals',
150
+ 'TV18BRDCST': 'Media',
151
+ 'UJJIVAN': 'Financial Services',
152
+ 'CENTRALBK': 'Financial Services',
153
+ 'NCC': 'Infrastructure',
154
+ 'HDFCLIFE_': 'Financial Services',
155
+ 'SYNGENE': 'Pharmaceuticals',
156
+ 'BALKRISIND': 'Automobile',
157
+ 'CHOLAFIN_': 'Financial Services',
158
+ 'COFORGE': 'IT',
159
+ 'CRISIL': 'Financial Services',
160
+ 'DEEPAKNTR': 'Consumer Goods',
161
+ 'JUBLFOOD': 'Retail',
162
+ 'PHOENIXLTD': 'Real Estate',
163
+ 'EQUITAS': 'Financial Services',
164
+ 'LTI': 'IT',
165
+ 'RBLBANK_': 'Financial Services',
166
+ 'CANBK': 'Financial Services',
167
+ 'MOTILALOFS': 'Financial Services',
168
+ 'PNCINFRA': 'Infrastructure',
169
+ 'SUNTV': 'Media',
170
+ 'AMBER': 'Consumer Goods',
171
+ 'BLUESTARCO': 'Consumer Goods',
172
+ 'CUB': 'Financial Services',
173
+ 'GREENPLY': 'Consumer Goods',
174
+ 'KOLTEPATIL': 'Real Estate',
175
+ 'L&TFH_': 'Financial Services',
176
+ 'METROPOLIS': 'Healthcare',
177
+ 'RCOM': 'Telecom',
178
+ 'SYMPHONY': 'Consumer Goods',
179
+ 'VBL': 'Consumer Goods',
180
+ 'VENKEYS': 'Consumer Goods',
181
+ 'COROMANDEL': 'Fertilizers',
182
+ 'GODREJAGRO': 'Consumer Goods',
183
+ 'HAL': 'Defense',
184
+ 'JINDALSAW': 'Metals',
185
+ 'KEC': 'Infrastructure',
186
+ 'MINDACORP_': 'Retail',
187
+ 'NMDC': 'Metals',
188
+ 'PGHL': 'Consumer Goods',
189
+ 'PRAJIND': 'Infrastructure',
190
+ 'RITES': 'Infrastructure',
191
+ 'BANKBAROD_': 'Financial Services',
192
+ 'BATAINDIA_': 'Retail',
193
+ 'BEML': 'Infrastructure',
194
+ 'CAREERP': 'IT',
195
+ 'CENTURYTEX': 'Cement',
196
+ 'DCBBANK': 'Financial Services',
197
+ 'DHFL': 'Financial Services',
198
+ 'FSL': 'Financial Services',
199
+ 'FUTURECON': 'Retail',
200
+ 'GEPIL': 'Pharmaceuticals',
201
+ 'GESHIP': 'Shipping',
202
+ 'GUJALKALI': 'Fertilizers',
203
+ 'HATHWAY': 'Media',
204
+ 'HERCULES': 'Pharmaceuticals',
205
+ 'HEXAWARE': 'IT',
206
+ 'IDFC': 'Financial Services',
207
+ 'IDFCFIRSTB_': 'Financial Services',
208
+ 'IFCI': 'Financial Services',
209
+ 'IIFL': 'Financial Services',
210
+ 'INDIANB_': 'Financial Services',
211
+ 'IRCON': 'Infrastructure',
212
+ 'JETAIRWAYS': 'Airlines',
213
+ 'LUPIN': 'Pharmaceuticals',
214
+ 'M&M': 'Automobile',
215
+ 'MAHLOG': 'Automobile',
216
+ 'MANAPPURAM': 'Financial Services',
217
+ 'MAXFIN': 'Financial Services',
218
+ 'MCX': 'Financial Services',
219
+ 'MOTHERSUMI': 'Automobile',
220
+ 'NATCOPHARM': 'Pharmaceuticals',
221
+ 'NATIONALUM': 'Metals',
222
+ 'NH': 'Hospitality',
223
+ 'NLCINDIA': 'Power',
224
+ 'NTPC': 'Power',
225
+ 'PCJEWELLER': 'Retail',
226
+ 'PNBHOUSING': 'Financial Services',
227
+ 'PVR': 'Media',
228
+ 'RAYMOND': 'Textiles',
229
+ 'RELCAPITAL': 'Financial Services',
230
+ 'RELIANCE': 'Oil & Gas',
231
+ 'RIIL': 'Infrastructure',
232
+ 'SADBHAV': 'Infrastructure',
233
+ 'SAIL': 'Metals',
234
+ 'SCHAEFFLER': 'Automobile',
235
+ 'SFL': 'Retail',
236
+ 'SOUTHBANK': 'Financial Services',
237
+ 'SPARC': 'Pharmaceuticals',
238
+ 'SUZLON': 'Power',
239
+ 'SYNDIBANK': 'Financial Services',
240
+ 'TATAELXSI': 'IT',
241
+ 'TATAMOTORS': 'Automobile',
242
+ 'TECHM': 'IT',
243
+ 'THERMAX': 'Engineering',
244
+ 'TITAN': 'Retail',
245
+ 'TRIDENT': 'Textiles',
246
+ 'UFO': 'Media',
247
+ 'UPL': 'Chemicals',
248
+ 'VAKRANGEE': 'IT',
249
+ 'WOCKPHARMA': 'Pharmaceuticals',
250
+ 'YESBANK': 'Financial Services'
251
+ }
252
+
253
+ def get_sector_list():
254
+ return list(set(nse_stock_sectors.values()))
255
+
256
+ def get_stock_list_by_sector(sector='IT'):
257
+ return [key.strip()+'.NS' for key, val in nse_stock_sectors.items() if val == sector]
258
+
main.py CHANGED
@@ -2,7 +2,7 @@ import streamlit as st
2
  from nsetools import Nse
3
  from nsepy import get_history
4
  from datetime import date, timedelta
5
-
6
  import CS_pattern_rankings
7
  from PatternRecognition import *
8
  from VixAnalysis import *
@@ -15,6 +15,8 @@ import pandas as pd
15
  import yfinance as yf
16
  from plotly.subplots import make_subplots
17
  from NewsAnalyzer import *
 
 
18
 
19
  buy_sell_color_dict = {"Buy": "green", "Sell": "red", "Hold": "yellow"}
20
 
@@ -27,7 +29,7 @@ del all_stock_names['NAME OF COMPANY']
27
  st.set_page_config(page_title='Jarvis', page_icon=':money_with_wings:', layout='wide', initial_sidebar_state='auto')
28
  pd.options.plotting.backend = "plotly"
29
  st.title('Jarvis')
30
- stock_tab, mf_tab, intraday_tab = st.tabs(['Stocks', 'Mutual Funds', 'Intraday'])
31
 
32
  main_sidebar = st.sidebar
33
  with main_sidebar:
@@ -219,6 +221,50 @@ with stock_tab:
219
  st.subheader(f"Live sentiment for {ticker_yf.split('.')[0]}:")
220
  st.markdown(f"**{sentiment}**")
221
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
  with intraday_tab:
223
 
224
  stock_selector_container_intraday = st.container()
@@ -308,9 +354,5 @@ with intraday_tab:
308
 
309
  time.sleep(5)
310
 
311
- with portfolio_tab:
312
 
313
- # with mf_tab:
314
- # test_tab1, test_tab2 = st.tabs(['tab1', 'tab2'])
315
- # with test_tab1:
316
- # st.write("Coming soon...")
 
2
  from nsetools import Nse
3
  from nsepy import get_history
4
  from datetime import date, timedelta
5
+ import plotly.express as px
6
  import CS_pattern_rankings
7
  from PatternRecognition import *
8
  from VixAnalysis import *
 
15
  import yfinance as yf
16
  from plotly.subplots import make_subplots
17
  from NewsAnalyzer import *
18
+ from Stock_Static_Data import *
19
+ from PortfolioAnalytics import *
20
 
21
  buy_sell_color_dict = {"Buy": "green", "Sell": "red", "Hold": "yellow"}
22
 
 
29
  st.set_page_config(page_title='Jarvis', page_icon=':money_with_wings:', layout='wide', initial_sidebar_state='auto')
30
  pd.options.plotting.backend = "plotly"
31
  st.title('Jarvis')
32
+ stock_tab, portfolio_tab, intraday_tab = st.tabs(['Stocks', 'Portfolio', 'Intraday'])
33
 
34
  main_sidebar = st.sidebar
35
  with main_sidebar:
 
221
  st.subheader(f"Live sentiment for {ticker_yf.split('.')[0]}:")
222
  st.markdown(f"**{sentiment}**")
223
 
224
+ with portfolio_tab:
225
+ portfolio_main_container = st.container()
226
+ with portfolio_main_container:
227
+ create_portfolio_for_me, diy_portfolio = st.columns(2)
228
+ with create_portfolio_for_me:
229
+ sector_list = get_sector_list()
230
+ sector_list.append('Surprise me!')
231
+ select_portfolio_sector = st.selectbox(
232
+ "Choose your sector of preference?",
233
+ tuple(sector_list))
234
+
235
+ if st.button('Build Portfolio'):
236
+ with st.spinner(text='Creating your portfolio! Please wait!'):
237
+ asset_list = get_stock_list_by_sector(select_portfolio_sector)
238
+ portfolio_df, hrp = get_HRP_weights(asset_list)
239
+ fig = px.pie(portfolio_df, values='Weights', names='Stock', title=f'Portfolio allocation for'
240
+ f' {select_portfolio_sector} sector')
241
+ st.plotly_chart(fig)
242
+ exp_ret, vol, sharpe = get_portfolio_performance(hrp)
243
+ st.write(f'Expected Returns {exp_ret:.0%}')
244
+ st.write(f'Annualized Volatility {vol:.0%}')
245
+ st.write(f'Sharpe Ratio {sharpe:.0%}')
246
+ else:
247
+ pass
248
+
249
+ with diy_portfolio:
250
+ asset_list_diy = st.multiselect(
251
+ 'Choose stocks you want to add to your portfolio',
252
+ list(nse_stock_sectors.keys()),
253
+ list(nse_stock_sectors.keys())[:3])
254
+ asset_list_diy = [i.strip()+'.NS' for i in asset_list_diy]
255
+ if st.button('Get Weights'):
256
+ with st.spinner(text='Creating your portfolio! Please wait!'):
257
+ portfolio_df, hrp = get_HRP_weights(asset_list_diy)
258
+ fig = px.pie(portfolio_df, values='Weights', names='Stock', title=f'Portfolio allocation for'
259
+ f' selected stocks')
260
+ st.plotly_chart(fig)
261
+ exp_ret, vol, sharpe = get_portfolio_performance(hrp)
262
+ st.write(f'Expected Returns {exp_ret:.0%}')
263
+ st.write(f'Annualized Volatility {vol:.0%}')
264
+ st.write(f'Sharpe Ratio {sharpe:.0%}')
265
+ else:
266
+ pass
267
+
268
  with intraday_tab:
269
 
270
  stock_selector_container_intraday = st.container()
 
354
 
355
  time.sleep(5)
356
 
 
357
 
358
+