shreyashnadage commited on
Commit
696af78
0 Parent(s):

Initial commit

Browse files
.idea/.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
.idea/ProjectRam.iml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="PYTHON_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$" />
5
+ <orderEntry type="jdk" jdkName="Python 3.8 (tradeplatform)" jdkType="Python SDK" />
6
+ <orderEntry type="sourceFolder" forTests="false" />
7
+ </component>
8
+ </module>
.idea/inspectionProfiles/profiles_settings.xml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ <component name="InspectionProjectProfileManager">
2
+ <settings>
3
+ <option name="USE_PROJECT_PROFILE" value="false" />
4
+ <version value="1.0" />
5
+ </settings>
6
+ </component>
.idea/misc.xml ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (tradeplatform)" project-jdk-type="Python SDK" />
4
+ </project>
.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/ProjectRam.iml" filepath="$PROJECT_DIR$/.idea/ProjectRam.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
CS_pattern_rankings.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Note - 1
2
+ # Only some patterns have bull and bear versions.
3
+ # However, to make the process unified and for codability purposes
4
+ # all patterns are labeled with "_Bull" and "_Bear" tags.
5
+ # Both versions of the single patterns are given same performance rank,
6
+ # since they will always return only 1 version.
7
+
8
+ # Note - 2
9
+ # Following TA-Lib patterns are excluded from the analysis since the corresponding ranking not found:
10
+ # CounterAttack, Longline, Shortline, Stalledpattern, Kickingbylength
11
+
12
+
13
+ candle_rankings = {
14
+ "CDL3LINESTRIKE_Bull": 1,
15
+ "CDL3LINESTRIKE_Bear": 2,
16
+ "CDL3BLACKCROWS_Bull": 3,
17
+ "CDL3BLACKCROWS_Bear": 3,
18
+ "CDLEVENINGSTAR_Bull": 4,
19
+ "CDLEVENINGSTAR_Bear": 4,
20
+ "CDLTASUKIGAP_Bull": 5,
21
+ "CDLTASUKIGAP_Bear": 5,
22
+ "CDLINVERTEDHAMMER_Bull": 6,
23
+ "CDLINVERTEDHAMMER_Bear": 6,
24
+ "CDLMATCHINGLOW_Bull": 7,
25
+ "CDLMATCHINGLOW_Bear": 7,
26
+ "CDLABANDONEDBABY_Bull": 8,
27
+ "CDLABANDONEDBABY_Bear": 8,
28
+ "CDLBREAKAWAY_Bull": 10,
29
+ "CDLBREAKAWAY_Bear": 10,
30
+ "CDLMORNINGSTAR_Bull": 12,
31
+ "CDLMORNINGSTAR_Bear": 12,
32
+ "CDLPIERCING_Bull": 13,
33
+ "CDLPIERCING_Bear": 13,
34
+ "CDLSTICKSANDWICH_Bull": 14,
35
+ "CDLSTICKSANDWICH_Bear": 14,
36
+ "CDLTHRUSTING_Bull": 15,
37
+ "CDLTHRUSTING_Bear": 15,
38
+ "CDLINNECK_Bull": 17,
39
+ "CDLINNECK_Bear": 17,
40
+ "CDL3INSIDE_Bull": 20,
41
+ "CDL3INSIDE_Bear": 56,
42
+ "CDLHOMINGPIGEON_Bull": 21,
43
+ "CDLHOMINGPIGEON_Bear": 21,
44
+ "CDLDARKCLOUDCOVER_Bull": 22,
45
+ "CDLDARKCLOUDCOVER_Bear": 22,
46
+ "CDLIDENTICAL3CROWS_Bull": 24,
47
+ "CDLIDENTICAL3CROWS_Bear": 24,
48
+ "CDLMORNINGDOJISTAR_Bull": 25,
49
+ "CDLMORNINGDOJISTAR_Bear": 25,
50
+ "CDLXSIDEGAP3METHODS_Bull": 27,
51
+ "CDLXSIDEGAP3METHODS_Bear": 26,
52
+ "CDLTRISTAR_Bull": 28,
53
+ "CDLTRISTAR_Bear": 76,
54
+ "CDLGAPSIDESIDEWHITE_Bull": 46,
55
+ "CDLGAPSIDESIDEWHITE_Bear": 29,
56
+ "CDLEVENINGDOJISTAR_Bull": 30,
57
+ "CDLEVENINGDOJISTAR_Bear": 30,
58
+ "CDL3WHITESOLDIERS_Bull": 32,
59
+ "CDL3WHITESOLDIERS_Bear": 32,
60
+ "CDLONNECK_Bull": 33,
61
+ "CDLONNECK_Bear": 33,
62
+ "CDL3OUTSIDE_Bull": 34,
63
+ "CDL3OUTSIDE_Bear": 39,
64
+ "CDLRICKSHAWMAN_Bull": 35,
65
+ "CDLRICKSHAWMAN_Bear": 35,
66
+ "CDLSEPARATINGLINES_Bull": 36,
67
+ "CDLSEPARATINGLINES_Bear": 40,
68
+ "CDLLONGLEGGEDDOJI_Bull": 37,
69
+ "CDLLONGLEGGEDDOJI_Bear": 37,
70
+ "CDLHARAMI_Bull": 38,
71
+ "CDLHARAMI_Bear": 72,
72
+ "CDLLADDERBOTTOM_Bull": 41,
73
+ "CDLLADDERBOTTOM_Bear": 41,
74
+ "CDLCLOSINGMARUBOZU_Bull": 70,
75
+ "CDLCLOSINGMARUBOZU_Bear": 43,
76
+ "CDLTAKURI_Bull": 47,
77
+ "CDLTAKURI_Bear": 47,
78
+ "CDLDOJISTAR_Bull": 49,
79
+ "CDLDOJISTAR_Bear": 51,
80
+ "CDLHARAMICROSS_Bull": 50,
81
+ "CDLHARAMICROSS_Bear": 80,
82
+ "CDLADVANCEBLOCK_Bull": 54,
83
+ "CDLADVANCEBLOCK_Bear": 54,
84
+ "CDLSHOOTINGSTAR_Bull": 55,
85
+ "CDLSHOOTINGSTAR_Bear": 55,
86
+ "CDLMARUBOZU_Bull": 71,
87
+ "CDLMARUBOZU_Bear": 57,
88
+ "CDLUNIQUE3RIVER_Bull": 60,
89
+ "CDLUNIQUE3RIVER_Bear": 60,
90
+ "CDL2CROWS_Bull": 61,
91
+ "CDL2CROWS_Bear": 61,
92
+ "CDLBELTHOLD_Bull": 62,
93
+ "CDLBELTHOLD_Bear": 63,
94
+ "CDLHAMMER_Bull": 65,
95
+ "CDLHAMMER_Bear": 65,
96
+ "CDLHIGHWAVE_Bull": 67,
97
+ "CDLHIGHWAVE_Bear": 67,
98
+ "CDLSPINNINGTOP_Bull": 69,
99
+ "CDLSPINNINGTOP_Bear": 73,
100
+ "CDLUPSIDEGAP2CROWS_Bull": 74,
101
+ "CDLUPSIDEGAP2CROWS_Bear": 74,
102
+ "CDLGRAVESTONEDOJI_Bull": 77,
103
+ "CDLGRAVESTONEDOJI_Bear": 77,
104
+ "CDLHIKKAKEMOD_Bull": 82,
105
+ "CDLHIKKAKEMOD_Bear": 81,
106
+ "CDLHIKKAKE_Bull": 85,
107
+ "CDLHIKKAKE_Bear": 83,
108
+ "CDLENGULFING_Bull": 84,
109
+ "CDLENGULFING_Bear": 91,
110
+ "CDLMATHOLD_Bull": 86,
111
+ "CDLMATHOLD_Bear": 86,
112
+ "CDLHANGINGMAN_Bull": 87,
113
+ "CDLHANGINGMAN_Bear": 87,
114
+ "CDLRISEFALL3METHODS_Bull": 94,
115
+ "CDLRISEFALL3METHODS_Bear": 89,
116
+ "CDLKICKING_Bull": 96,
117
+ "CDLKICKING_Bear": 102,
118
+ "CDLDRAGONFLYDOJI_Bull": 98,
119
+ "CDLDRAGONFLYDOJI_Bear": 98,
120
+ "CDLCONCEALBABYSWALL_Bull": 101,
121
+ "CDLCONCEALBABYSWALL_Bear": 101,
122
+ "CDL3STARSINSOUTH_Bull": 103,
123
+ "CDL3STARSINSOUTH_Bear": 103,
124
+ "CDLDOJI_Bull": 104,
125
+ "CDLDOJI_Bear": 104
126
+ }
PatternRecognition.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import talib
2
+ import pandas as pd
3
+
4
+ def GetPatternForData(stock_data_df):
5
+ candle_name_list = talib.get_function_groups()['Pattern Recognition']
6
+ tech_analysis_df = stock_data_df.iloc[-10:].copy()
7
+ op_df = tech_analysis_df.Open
8
+ hi_df = tech_analysis_df.High
9
+ lo_df = tech_analysis_df.Low
10
+ cl_df = tech_analysis_df.Close
11
+
12
+ for candle in candle_name_list:
13
+ tech_analysis_df[candle] = getattr(talib, candle)(op_df, hi_df, lo_df, cl_df)
14
+
15
+ result = pd.DataFrame(tech_analysis_df[['Date']+candle_name_list].sum(),columns=['Count'])
16
+ filtered_results = result[result.Count!=0]
17
+
18
+ if filtered_results.empty:
19
+ return None,tech_analysis_df
20
+ else:
21
+ return filtered_results[filtered_results.Count == filtered_results.Count.max()].index[0],tech_analysis_df
22
+
23
+ return None,tech_analysis_df
VixAnalysis.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from hmmlearn import hmm
2
+ import plotly.graph_objects as go
3
+ import streamlit as st
4
+
5
+ original_map = {'max': 2, 'mid': 1, 'low': 0}
6
+
7
+ @st.cache
8
+ def GetMood(indval):
9
+ if indval <= 1/3:
10
+ return "Greed"
11
+ elif 1/3 < indval < 2/3:
12
+ return "Holding Steady"
13
+ else:
14
+ return "Fear"
15
+
16
+ @st.cache
17
+ def ProcessVixData(vix_df):
18
+ X = vix_df[['Close']].copy()
19
+ model = hmm.GaussianHMM(n_components=3, covariance_type="full", n_iter=100)
20
+ model.fit(X)
21
+ res = X.copy()
22
+ res['Class'] = model.predict(X)
23
+ resmap = res.groupby('Class').agg({'Close': 'mean'}).sort_values(by='Close')
24
+ resmap = dict(zip(resmap.index, ['low', 'mid', 'max']))
25
+ res['Class_name'] = res.Class.apply(lambda x:resmap[x])
26
+ res['Class_proc'] = res.Class_name.apply(lambda x:original_map[x])
27
+ res['Norm_Class'] = (res['Class_proc']-res['Class_proc'].min())/(res['Class_proc'].max()-res['Class_proc'].min())
28
+ return res
29
+
30
+ @st.cache
31
+ def GetIndicatorChart(res):
32
+ value = res.Norm_Class[-30:].mean()
33
+ fig = go.Figure(go.Indicator(
34
+ domain={'x': [0, 1], 'y': [0, 1]},
35
+ value=value,
36
+ mode="gauge+number",
37
+ title={'text': f"VIX Analysis\nMood : {GetMood(value)}"},
38
+ gauge={'axis': {'range': [None, res.Norm_Class.max()]},
39
+ 'steps': [
40
+ {'range': [0, 1/3], 'color': "green"},
41
+ {'range': [1/3, 2/3], 'color': "gray"},
42
+ {'range': [2/3, 1], 'color': "red"}],
43
+ 'threshold': {'line': {'color': "black", 'width': 4}, 'thickness': 0.75, 'value': value}}))
44
+ return fig
45
+
46
+
47
+
main.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from nsetools import Nse
3
+ from nsepy import get_history
4
+ from datetime import date, timedelta
5
+ from PatternRecognition import GetPatternForData
6
+ from VixAnalysis import *
7
+
8
+ import yfinance as yf
9
+
10
+ today = date.today()
11
+ nse = Nse()
12
+ all_stock_codes = nse.get_stock_codes()
13
+ all_stock_names = dict(zip(nse.get_stock_codes().values(),nse.get_stock_codes().keys()))
14
+ del all_stock_names['NAME OF COMPANY']
15
+
16
+ st.set_page_config(page_title='Jarvis', page_icon='N', layout='wide', initial_sidebar_state='auto')
17
+ st.title('Jarvis')
18
+ stock_tab, mf_tab = st.tabs(['Stocks', 'Mutual Funds'])
19
+
20
+ with stock_tab:
21
+ stock_selector_col, tenure_selector_start_col, tenure_selector_end_col = st.columns(3)
22
+ with stock_selector_col:
23
+ selected_stock = st.selectbox('Please select a stock for analysis',
24
+ list(all_stock_names.keys()),help='Choose stock to analyze')
25
+ ticker_yf = all_stock_names[selected_stock]+".NS"
26
+
27
+ with tenure_selector_start_col:
28
+ start_date = st.date_input("Start Date", today-timedelta(40))
29
+
30
+ with tenure_selector_end_col:
31
+ end_date = st.date_input("End Date", today)
32
+
33
+ if (end_date<start_date):
34
+ st.error('Start date should be before End date')
35
+
36
+ time_delta = end_date-start_date
37
+ if time_delta.days < 35:
38
+ st.error('Please extend the range of dates. We need atleast 30 days of data for analysis.')
39
+
40
+ with st.spinner(text=f'Please Wait...Loading data for {selected_stock}'):
41
+ tk = yf.Ticker(ticker_yf)
42
+ stock_data_df = tk.history(start=start_date, end=end_date, interval="1d").reset_index()
43
+
44
+ chart_title_col, download_button_col = st.columns(2)
45
+ with chart_title_col:
46
+ st.subheader(f'OHLC Chart for {selected_stock}')
47
+
48
+ with download_button_col:
49
+ @st.cache
50
+ def convert_df(df):
51
+ # IMPORTANT: Cache the conversion to prevent computation on every rerun
52
+ return df.to_csv().encode('utf-8')
53
+
54
+ csv = convert_df(stock_data_df)
55
+ st.download_button(
56
+ label="Download data as CSV",
57
+ data=csv,
58
+ file_name=f'{selected_stock}.csv',
59
+ mime='text/csv',
60
+ )
61
+
62
+ fig_main_cs = go.Figure(data=[go.Candlestick(x=stock_data_df['Date'],
63
+ open=stock_data_df['Open'],
64
+ high=stock_data_df['High'],
65
+ low=stock_data_df['Low'],
66
+ close=stock_data_df['Close'])])
67
+ st.plotly_chart(fig_main_cs, use_container_width=True)
68
+
69
+ st.subheader('Candlestick Matched pattern:')
70
+ pattern_expander_col, cs_gauge_col = st.columns((1,3))
71
+
72
+ matched_pattern, tech_df = GetPatternForData(stock_data_df=stock_data_df)
73
+ if matched_pattern is not None:
74
+ with pattern_expander_col:
75
+ with st.expander(matched_pattern):
76
+ st.write("Will add description here")
77
+
78
+ else:
79
+ with pattern_expander_col:
80
+ with st.expander("No Pattern Matched"):
81
+ st.write("It seems there were no pattern formations for last 10 days.")
82
+
83
+ with st.sidebar:
84
+ vix_container = st.container()
85
+ with vix_container:
86
+ st.subheader("VIX Analysis")
87
+ with st.spinner(text='Loading VIX Analysis...'):
88
+ vix_df = get_history(symbol="India VIX", start=today-timedelta(30), end=today, index=True)
89
+ vix_analysis_df = ProcessVixData(vix_df)
90
+ value = vix_analysis_df.Norm_Class[-30:].mean()
91
+ st.metric("Current Market Mood", GetMood(value), '{:.2f}'.format(value))
92
+ mood_dict = {'Greed': 'Markets are in bullish state. Good time to book profits.',
93
+ 'Holding Steady': 'Markets are indecisive. its good to hold onto existing portfolio.',
94
+ 'Fear': "Markets are in bearish state. Good time to pick good stocks for cheap prices."}
95
+
96
+ with st.expander(GetMood(value)):
97
+ st.write(mood_dict[GetMood(value)])
98
+
99
+
test.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from nsetools import Nse
3
+ from datetime import date, timedelta
4
+ import plotly.graph_objects as go
5
+ import pandas as pd
6
+ import yfinance as yf
7
+
8
+
9
+ with st.spinner(text=f'Please Wait...Loading data for Reliance'):
10
+ tk = yf.Ticker('RELIANCE.NS')
11
+ stock_data_df = tk.history(start='2022-11-01', end='2022-12-01', interval="1d").reset_index()
12
+ st.success('Done!')
13
+
14
+ fig = go.Figure(data=[go.Candlestick(x=stock_data_df['Date'],
15
+ open=stock_data_df['Open'],
16
+ high=stock_data_df['High'],
17
+ low=stock_data_df['Low'],
18
+ close=stock_data_df['Close'])])
19
+ st.plotly_chart(fig)