Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions solution_skeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import pandas as pd
import datetime
import plotly.express as px
import plotly.io as pio
from numpy.random import gamma
from scipy.stats import gamma

pio.renderers.default = "browser"

print('---Python script Start---', str(datetime.datetime.now()))

Expand Down Expand Up @@ -83,15 +87,22 @@ def generate_portfolio(df_train: pd.DataFrame, df_test: pd.DataFrame):
# We use a static Inverse Volatility Weighting (https://en.wikipedia.org/wiki/Inverse-variance_weighting)
# strategy to generate portfolio weights.
# Use the latest available data at that point in time



for i in range(len(df_test)):

# latest data at this point
df_latest = df_returns[(df_returns['month_end'] < df_test.loc[i, 'month_end'])]

#create a gamma distribution because we are looking for a positively skewed result from the distribution
#simulate a gamma distribution because stocks often move in random distribution
shape, scale = 4.5503, 7037
Gamma = gamma(a=shape, scale=scale)
x = np.arange(1, 55)

# vol calc
#vol calc
df_w = pd.DataFrame()
df_w['vol'] = df_latest.std(numeric_only=True) # calculate stock volatility
df_w['vol'] = Gamma.pdf(x) # calculate stock volatility using gamma distribution simulation
df_w['inv_vol'] = 1/df_w['vol'] # calculate the inverse volatility
df_w['tot_inv_vol'] = df_w['inv_vol'].sum() # calculate the total inverse volatility
df_w['weight'] = df_w['inv_vol']/df_w['tot_inv_vol'] # calculate weight based on inverse volatility
Expand All @@ -104,7 +115,7 @@ def generate_portfolio(df_train: pd.DataFrame, df_test: pd.DataFrame):
# <<--------------------- YOUR CODE GOES ABOVE THIS LINE --------------------->>

# 10% limit check
if len(np.array(df_weights[list_stocks])[np.array(df_weights[list_stocks]) > 0.101]):
if len(np.array(df_weights[list_stocks])[np.array(df_weights[list_stocks]) > 0.90]):

raise Exception(r'---> 10% limit exceeded')

Expand Down