subhash494 commited on
Commit
e76457a
1 Parent(s): cfc298f

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +82 -0
  2. maximum_dd.py +26 -0
  3. returns_gen.py +25 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from io import StringIO
3
+ import pandas as pd
4
+
5
+
6
+ from returns_gen import returns_set
7
+ #from plot_simulations import plot_sims,histogram
8
+ from maximum_dd import max_dd
9
+ import numpy as np
10
+ st. set_page_config(page_title='Monte Carlo Simulator ',layout="centered")
11
+
12
+ st.header('Monte Carlo Simulator for 10000 iterations')
13
+
14
+ from matplotlib import pyplot as plt
15
+
16
+
17
+ def plot_sims(sims_list):
18
+ global generated_plots
19
+
20
+ # sims_list=sims_list[:10]
21
+ fig, ax = plt.subplots()
22
+ for sl in sims_list:
23
+ ax.plot(sl)
24
+ number_of_sims=len(sims_list)
25
+ ax.set_title(f'Monte Carlo Paths of Nifty returns for {number_of_sims} simulations')
26
+ ax.set_ylabel('returns %')
27
+ ax.set_xlabel('count')
28
+ st.pyplot(fig)
29
+
30
+ def histogram(lis):
31
+ lis.sort()
32
+ print(min(lis),max(lis))
33
+ print(int(min(lis)),int(max(lis)))
34
+
35
+ bin_width=0.5
36
+ max_val=int(max(lis))+1
37
+ min_val=int(min(lis))-1
38
+ bins=[min_val+k*bin_width for k in range(int((max_val-min_val)/bin_width))]
39
+ fig,ax=plt.subplots()
40
+ ax.hist(lis,bins=bins,align='left')
41
+ #ax.xlim(max_val,min_val)
42
+ ax.axis(xmin=min_val,xmax=max_val)
43
+ ax.set_title('Drawdown from Monte Carlo iterations')
44
+ ax.set_ylabel('count')
45
+ ax.set_xlabel('Maxx DD %')
46
+ st.pyplot(fig)
47
+
48
+ def clean_returns(ret_lis):
49
+ return [float(str(k).replace(',','').replace('%','').replace(' ','')) for k in returns_list]
50
+
51
+ uploaded_file=st.file_uploader('DataFile', type=['csv'])
52
+ if uploaded_file:
53
+ stringio = StringIO(uploaded_file.getvalue().decode("utf-8"))
54
+ df=pd.read_csv(stringio)
55
+
56
+ df_cols=df.columns
57
+
58
+ default_col=0
59
+ for i in range(len(df_cols)):
60
+ dcol=df_cols[i]
61
+ if 'return' in dcol.lower():
62
+ default_col=i
63
+ break
64
+ returns_column=st.selectbox('returns column', df_cols,index=default_col)
65
+
66
+
67
+ returns_list=df[returns_column].tolist()
68
+ returns_list=clean_returns(returns_list)
69
+
70
+
71
+ returns=returns_set(returns_list)
72
+
73
+
74
+ cum_rets_dd=[max_dd(k) for k in returns]
75
+ cum_rets=[k[0] for k in cum_rets_dd]
76
+ dd_list=[k[1] for k in cum_rets_dd]
77
+ percentile_95=np.percentile(dd_list,95)
78
+ st.write(f'95 percentile Max DD is {percentile_95} %')
79
+ plot_sims(cum_rets)
80
+ histogram(dd_list)
81
+
82
+
maximum_dd.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ given a list of returns, returns the maximum drawdown of the returns
3
+
4
+ drawdown is calculated by taking the highest cumulative return so far, and calculating the difference of the current cumulative return from
5
+ the peak
6
+
7
+ the minimum of the drawdowns is max drawdown
8
+ """
9
+
10
+ def max_dd(returns_list):
11
+ #returns are in the form of % ex: 1.5%, 2% etc. without the % sign
12
+ cumulative_returns=[]
13
+ drawdown=[]
14
+ cum_ret=0
15
+ for i in returns_list:
16
+ cum_ret=cum_ret+i
17
+ cumulative_returns.append(cum_ret)
18
+ dd=cum_ret-max(cumulative_returns)
19
+
20
+ drawdown.append(dd)
21
+ return cumulative_returns,min(drawdown)
22
+
23
+ if __name__=='__main__':
24
+ test_dd=max_dd([1.1,1.1,-2,-7,25,34,-4,-3,-3,-5,1.2,-6])
25
+ print(test_dd)
26
+
returns_gen.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+
3
+ def dummy_returns(n=67):
4
+ #a return can vary between 100% and -100% i.e 2 and -1
5
+ #we will generate a random number between -100 and 200 and divide by hundred to get the dummy %return
6
+ return [random.randint(-100,200)/100 for _ in range(n)]
7
+
8
+ def randomise_returns(returns_list):
9
+ return random.sample(returns_list,len(returns_list))
10
+
11
+ def returns_set(original_returns_list,n=10000):
12
+ collected_returns=[]
13
+ while len(collected_returns)<n:
14
+ new_list=randomise_returns(original_returns_list)
15
+ if new_list not in collected_returns:
16
+ collected_returns.append(new_list)
17
+ return collected_returns
18
+
19
+ def dummy_sims():
20
+ return returns_set(dummy_returns())
21
+ if __name__=='__main__':
22
+ dum_rets=dummy_returns()
23
+ paths=returns_set(dum_rets)
24
+ for p in paths:
25
+ print(p,sum(p))