Here you find your exam questions. Please submit your work by commiting to github by 23:00, 7 Tuesday, latest. You can continue working on any unfinished questions and submit them until the class on Tuesday, the 14th.
Implement a program, that, computes a moving average of a sequence $x_t$, given as a list of numbers as a list. All the boundry values should be assumed to be zero.
$$ \mu_t = \frac{1}{L}\sum_{i=0}^{L-1} x_{t-i} $$Example:
For the list $[1,4,5,-1]$, moving average with window $L=3$ should output $[0.3333, 1.6666, 3.3333, 2.6666]$.
Then your program must plot the original sequence and the moving average. The plot should be as beautiful as possible. Demonstrate your program with a generated random walk of length $L=200$.
Read the following price data and implement a back testing program to evaluate the return for the following investment strategy:
Initially invest $p$ and $1-p$ percent of a total kapital $K$ to each stock respectively (microsoft and apple).
Every weekend, only using past averages of last two weeks, evaluate the following:
If both stocks lost in value, or both stocks gain in value, do nothing
Otherwise sell $r$ percent of the loosing stock with the opening price and invest the money immediately in the winning stock.
Plot the evolution of the capital and compare with the return of the static strategy where you rebalance every week to keep the $p$ - $1-p$ portfolio. Ignore any transaction fees.
In [1]:
import pandas as pd
import numpy as np
import datetime
msft = pd.read_csv("msft.csv", index_col=0, parse_dates=True)
aapl = pd.read_csv("aapl.csv", index_col=0, parse_dates=True)
In [11]:
msft['2012-01']
Out[11]:
In [6]:
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
plt.plot(aapl['2012']['Open'])
Out[6]:
In [ ]:
%matplotlib inline