Skip to content

Instantly share code, notes, and snippets.

# -*- coding: utf-8 -*-
from datetime import datetime
import json
import numpy as np
import pandas as pd
from pandas_datareader import data as wb
from scipy.stats import norm, gmean
START_DATE = '2007-1-1' # Date from which we want to get the financial year.
YEARS = 40 # Years until retirement
DAYS_IN_YEAR = 253 # Average number of trading days in the year
PORTFOLIO_VALUE = 1000
def get_portfolio():
with open('portfolio.json', 'r') as portfolio_file:
portfolio_json = portfolio_file.read()
return json.loads(portfolio_json)['portfolio']
portfolio_data = get_portfolio()
portfolio = pd.DataFrame(portfolio_data).assign(returns = [get_data(stock) for stock in portfolio_data])
def get_data(stock):
data = pd.DataFrame()
if stock['type'] == 'index':
data = wb.DataReader(stock['ticker'], 'stooq', start=START_DATE)['Close']
return get_simulation(data)
data = wb.DataReader(stock['ticker'], 'yahoo', start=START_DATE)['Adj Close']
return get_simulation(data)
{
"portfolio": [{
"ticker": "^FTM",
"name": "FTSE 250",
"type": "index",
"weight": 0.1
},
{
"ticker": "VEMAX",
def get_expected_portfolio_return(porfolio):
return np.sum(portfolio['weight'] * porfolio['returns'])
expected_portfolio_return = get_expected_portfolio_return(portfolio)
final_value = expected_portfolio_return * PORTFOLIO_VALUE
print('Estimated value of Portfolio in {} : £{:,.2f} \nExpected Portfolio Return: {:,.2f}%').format(datetime.now().year + YEARS, final_value, expected_portfolio_return)
print('Estimated Income £{:,.2f}').format(final_value * 0.04)
def get_simulation(data):
# Get the logarithmic returns of the % change of prices from one trading day to the next.
log_returns = np.log(1 + data.pct_change())
# Get the mean of these returns
u = log_returns.mean()
# Get the variance of these returns
var = log_returns.var()
# Get the change in the average value of these values
drift = u - (0.5 * var)
# Get the standard deviation
@ShaneLee
ShaneLee / pom.xml
Last active March 5, 2022 16:13
Maven checkstyle
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${checkstyle-maven-plugin.version}</version>
<configuration>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>${checkstyle.version}</version>
</dependency>
import subprocess, urllib, random
class NoBlocks(Exception): pass
def getblocks():
r = urllib.urlopen("http://{?REDACTED?}/grab").read()
if '<html' in r.lower(): raise NoBlocks
return r.split()
import sys
if len(sys.argv) > 1: