Assume that we have a Brownian motion model for stock price: $$ dS = \mu S_t + \sigma S_t dW. $$ We try to model a trading strategy by tracking the number $\Delta_t$ of stocks we hold at time $t$, and the amount of money in our bank account $B_t$ at time $t$. We can define our wealth at time $t$ to be $$ W_t = \Delta_t S_t + B_t $$
Here in this notebook we can test and run trading strategies on simulated stock data.
The algorithm is as follows:
We are interested in the value of $W$ at the end of the simulation, as this indicates our winnings. If $W_{100}>20$ then this means we have made a profit on our initial investment.
Try to think about what is a good result -- do you expect the strategy to always win or just win on average? What does it mean if there is a large variation in returns?
from MathFinanceWorkshop import MFW_Simulation
import numpy as np
Model = MFW_Simulation()
First lets consider a simple strategy, where $$ T(S,W,B,\Delta) = 1. $$ The code for this would be written:
def tradingStrategy(St,Wt,Bt,delta_t):
return 1
Now lets see how it works some simulated data. We can run a few simulations by issuing the command:
Model.plotSimulation( number of simulations, mu , sigma )
putting in whatever numbers we want. For instance we run it in the next cell with 10 simulations, $\mu=0.1$, and $\sigma=0.2$.
mu = 0.1
sigma = 0.2
Model.plotSimulation(10,mu,sigma)
If you want to see what happens over a large number of simulations, we can plot out some statistics using the following
Model.statsSimulation(1000,mu,sigma)
Obviously this is a very simple stragegy, and we would like to try out some different strategies. The next most simple strategy to create is one that just sells everything. In code this would be
def tradingStrategy(St,Wt,Bt,delta_t):
# minus one here means that we will reduce delta
return -1
but we want to overwrite the strategy in "MFW_Simulation". To do this we create a new class (you don't need to understand this), inheriting functionality from "MFW_Simulation". The code will look like:
# "Selling" is the name of class
class Selling(MFW_Simulation):
# we redefine the trading strategy in here
def tradingStrategy(self,St,Wt,Bt,delta_t):
# inside here is where you should make edits
# this example is another simple strategy, which is to sell as many assets as possible.
return -1
Model = Selling()
The final line names this model as "ModelSelling", so can run the plot and stats function to see what this looks like. First we run the plot function.
Model.plotSimulation(10,0.1,0.2)
# In "BuyLowSellHigh", we check the value of stock at the current time
# compared to the value at the start, buy if St < alpha * S_0 sell if St > beta * S_0
class BuyLowSellHigh(MFW_Simulation):
# we redefine the trading strategy in here
def tradingStrategy(self,St,Wt,Bt,delta_t):
alpha=0.95
beta=1.05
# we can get current price of stock using notation St[-1]
Scurrent = St[-1]
# initial price of stock is S[0]
Sinitial = St[0]
# check if stock price is low
if Scurrent < alpha * Sinitial:
return 1
elif Scurrent > beta * Sinitial:
return -1
else:
return 0
NewModel = BuyLowSellHigh()