{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Trading Strategies\n", "\n", "Assume that we have a Brownian motion model for stock price:\n", "$$\n", "dS = \\mu S_t + \\sigma S_t dW.\n", "$$\n", "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 \n", "$$\n", "W_t = \\Delta_t S_t + B_t\n", "$$\n", "\t\n", "\n", "Here in this notebook we can test and run trading strategies on simulated stock data.\n", " - Total time $t\\in[0,1]$\n", " - One hundred steps in each run\n", " - Use the model for stock price as above\n", " - We restart each simulation run with the same parameters\n", " \n", "The algorithm is as follows:\n", " - Initialise values $S_0=1$, $W_0=20$, $B_0=10$, $\\Delta_0=10$\n", " - For i=0 to i=99\n", " - $S_{i+1}=S_{i} + dS$ where $dS$ is a random number\n", " - $\\Delta_{i+1}=\\Delta_{i} + T(S,W,B,\\Delta)$\n", " - $B_{i+1}=B_{i} - T(S,W,B,\\Delta)S_{i+1}$\n", " - $W_{i+1}= \\Delta_{i+1} S_{i+1} + B_{i+1}$\n", " - Output $W_{100}$, the final value of wealth\n", "\n", "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.\n", " \n", "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?\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from MathFinanceWorkshop import MFW_Simulation\n", "import numpy as np\n", "Model = MFW_Simulation()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First lets consider a simple strategy, where\n", "$$\n", "T(S,W,B,\\Delta) = 1.\n", "$$\n", "The code for this would be written:\n", "~~~\n", "def tradingStrategy(St,Wt,Bt,delta_t):\n", " return 1\n", "~~~\n", "Now lets see how it works some simulated data. We can run a few simulations by issuing the command:\n", "~~~\n", "Model.plotSimulation( number of simulations, mu , sigma )\n", "~~~\n", "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$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mu = 0.1\n", "sigma = 0.2\n", "Model.plotSimulation(10,mu,sigma)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want to see what happens over a large number of simulations, we can plot out some statistics using the following" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Model.statsSimulation(1000,mu,sigma)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tasks\n", "\n", "1. Some of the strategies end up with more or less than 20 shares. Can you explain why this number is sometimes different?\n", "2. How does this strategy perform for different values if you choose different values of $\\mu$ and $\\sigma$?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating Your Own Strategy\n", "\n", "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 \n", "~~~\n", "def tradingStrategy(St,Wt,Bt,delta_t):\n", " # minus one here means that we will reduce delta\n", " return -1\n", "~~~\n", "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:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# \"Selling\" is the name of class\n", "class Selling(MFW_Simulation):\n", " # we redefine the trading strategy in here\n", " def tradingStrategy(self,St,Wt,Bt,delta_t):\n", " # inside here is where you should make edits\n", " # this example is another simple strategy, which is to sell as many assets as possible.\n", " return -1\n", "Model = Selling()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Model.plotSimulation(10,0.1,0.2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Tasks\n", "\n", "1. Run the \"statsSimulation\" on this new version of \"Model\". How does this strategy compare to the previous strategy?\n", "2. Try out the strategy below, what parameters _alpha_ and _beta_ give the best returns for a given $\\mu$ and $\\sigma$? " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# In \"BuyLowSellHigh\", we check the value of stock at the current time \n", "# compared to the value at the start, buy if St < alpha * S_0 sell if St > beta * S_0\n", "class BuyLowSellHigh(MFW_Simulation):\n", " # we redefine the trading strategy in here\n", " def tradingStrategy(self,St,Wt,Bt,delta_t):\n", " alpha=0.95\n", " beta=1.05\n", " # we can get current price of stock using notation St[-1]\n", " Scurrent = St[-1]\n", " # initial price of stock is S[0]\n", " Sinitial = St[0]\n", " # check if stock price is low\n", " if Scurrent < alpha * Sinitial:\n", " return 1\n", " elif Scurrent > beta * Sinitial:\n", " return -1\n", " else:\n", " return 0\n", "\n", "NewModel = BuyLowSellHigh()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.7" } }, "nbformat": 4, "nbformat_minor": 2 }