Does my stock trading strategy work?

I recently realized that I have a certain habit when it comes to stock trading even though I used to tell myself that “every trading strategy is useless because the stock market is just random”. If I buy a share I usually set the limit just a little below the actual price while if i sell I place the limit above. The question we want to answer today is if such a trading strategy is reasonable and if there is an optimal limit to maximize the expected profit.

More formally, let S(t) be the stock price at time t and let l be a limit set for a certain order, then the order will be fulfilled if S(t) = l for some t. In the following we will investigate how to set a best price if an order should be processed within a given time \tau

(1)   \begin{eqnarray*}l^* = E (max_{0\leq s \leq \tau} S(s) ).\end{eqnarray*}


To answer the questions we have deduct a model based on some assumptions. A popular start is to think about a stock price as a brownian motion to capture the random behavior. Remember, we already covered the brownian motion in a previous post. However the properties of a Brownian motion do not match the experience of a stock price entirely. Usually a stock price has to be non negative, we can also assume that it follows some kind of trend and the increment size will change based on the price. More formally ,we assume for the exchange rate S(t) that

  • S(t) is the logarithm of a randomly varying Brownian Motion W_t
  • a percentage drift \mu
  • a percentage volatility \sigma

This then gives

(2)   \begin{eqnarray*}S(t) = S(0) \exp \left(\left(\mu - \frac{1}{2}\sigma^2\right)t + \sigma W_t \right) \end{eqnarray*}

where W_t is a standard brownian motion and S(0) the initial value. By definition (3) is a geometric brownian motion since it solves the stochastic differential equation dS_t = \mu S_t dt + \sigma S_tdW_t. Assuming a geometric brownian motion is also the foundation of the famous black-scholes-model. Thus S_t is log normally distributed with

(3)   \begin{eqnarray*}E(S(t)) = S(0) \exp(\mu t), \; Var(S(t))= S_0^2 exp (2 \mu t) (exp(\sigma^2 t)-1).\end{eqnarray*}


To answer our initial question we use a commonly known result based on the Girsanov theorem:

Proposition 1 (proof as Proposition 3.2)
Let W_t^{\mu} = (W_t + \mu t) be a brownian motion with drift \mu, let t \in \mathbb{R}^+ M_t^{\mu} := (sup_{s \in [0,t]} W_s^{\mu})>0, x \in \mathbb{R} then

(4)   \begin{eqnarray*}\mathbb{P}(M_t^{\mu} \geq x) = e^{2 \mu x}  \Phi\left( \frac{-x-\mu t}{\sqrt{t}} \right) +  \Phi\left( \frac{-x+\mu t}{\sqrt{t}} \right)\end{eqnarray*}


here \Phi() is CDF of the standard normal distribution.

When we apply (4) to the geometric brownian motion with S^{sup}_\tau :=  (sup_{t \in [0,\tau]} S(t)) we get

(5)   \begin{eqnarray*}\mathbb{P}(S^{sup}_\tau \leq l) = \mathbb{P}\left(M^{\lambda}_\tau \leq ln\left(\frac{l}{S_0}\right)\right) \end{eqnarray*}


where \lambda= \frac{\mu - 0.5\sigma^2}{\sigma^2}. M^{\lambda}_\tau is thus a brownian motion with drift \lambda, in addition using a scaling with t=\sigma^2 \tau of the underlying brownian motion, this lead to

(6)   \begin{eqnarray*}\mathbb{P}(S^{sup}_\tau \geq l) = \frac{l}{S_0}^{2 \lambda} \Phi \left(\frac{ln(\frac{l}{S_0}) -\sigma^2 \lambda \tau}{\sigma \sqrt{\tau}}}\right) + \Phi \left(\frac{ln(\frac{l}{S_0}) +\sigma^2 \lambda \tau}{\sigma \sqrt{\tau}}}\right)\end{eqnarray*}


Accordingly, by symmetrie we get for S^{inf}_\tau :=  (inf_{t \in [0,\tau]} S(t))

(7)   \begin{eqnarray*}\mathbb{P}(S^{inf}_\tau \leq l) = \frac{l}{S_0}^{2 \lambda} \Phi \left(\frac{ln(\frac{l}{S_0}) +\sigma^2 \lambda \tau}{\sigma \sqrt{\tau}}}\right) + \Phi \left(\frac{ln(\frac{l}{S_0}) -\sigma^2 \lambda \tau}{\sigma \sqrt{\tau}}}\right)\end{eqnarray*}


The equations above thus tell us how likely it is to sell/buy a stock within a time interval of length \tau. To answer our initial question (1) we derive the expectation, to get the best buy and sell limits, with

(8)   \begin{eqnarray*}l_{buy, \tau}^* = \int_0^{\infty} 1- \mathbb{P}(S^{inf}_\tau \leq l) dl, \; l_{sell, \tau}^* = \int_{0}^{\infty} \mathbb{P}(S^{sup}_\tau \geq l) dl.\end{eqnarray*}

Estimating \sigma and \mu

Since \mu and \sigma are unknown we have to estimate them from observations. Let \Delta = ln(S(t+\Delta t))-ln(S(t))=  \left( \mu -  \frac{\sigma^2}{2} \right)\Delta t + \sigma W_{\Delta t}, which obviously follows a normal distribution with mean \mu_\Delta = (\mu - 0.5\sigma^2)\Delta t and variance \sigma_\Delta^2 = \sigma^2 \Delta t. Thus \sigma^2 = \frac{\sigma_\Delta^2}{\Delta t} and \mu= \frac{\mu_\Delta + 0.5 \sigma_\Delta^2}{\Delta t}.
Stock prices are usually not observed as a continuous process, but instead oftentimes on a daily or minutely basis. In fact stock prices are never continuous because price fluctuations are measured using ticks. This is one reason, among others, why a brownian motion can only be considered as an approximation of the true behaviour of a stock price. If we assume N discrete observations \Delta_1, \dots, \Delta_N with starting point t_0 and endpoint t_N with an equidistant time gap of \Delta t  = \Delta t_i = t_{i}-t_{i-1} = \frac{t_N-t_0}{N}, i=1,\dots,N we can derive unbiased estimators

(9)   \begin{eqnarray*}\hat{\mu}= \frac{\hat{\mu}_\Delta + 0.5 \hat{\sigma}_\Delta^2}{\Delta t}, \;\hat{\mu}_\Delta = \sum_{i=1}^N \frac{\Delta_i}{N} \rightarrow_p \left( \mu - \frac{\sigma^2}{2} \right)\Delta t\end{eqnarray*}


while

(10)   \begin{eqnarray*}\hat{\sigma}^2 = \frac{\hat{\sigma}_\Delta^2}{\Delta t}, \; \hat{\sigma}^2_\Delta = \sum_{i=1}^N \frac{(\Delta_i-\hat{\mu}_\Delta)^2}{N-1} \rightarrow_p \sigma^2 \Delta t\end{eqnarray*}


further we can verify that the estimators \hat{\mu}, \hat{\sigma}^2 are consistent with

(11)   \begin{eqnarray*}Var(\hat{\sigma}^2)= \frac{Var(\hat{\sigma}_\Delta^2)}{\Delta t^2}=\frac{E(\hat{\sigma}_\Delta^4)-  E(\hat{\sigma}_\Delta^2)^2}{\Delta t^2}= \frac{2 \sigma^4}{N}\end{eqnarray*}


(12)   \begin{eqnarray*}Var(\hat{\mu})= \frac{4 Var(\hat{\mu}_\Delta)+ Var(\hat{\sigma}^2_\Delta)}{4\Delta t^2} = \frac{2\sigma^2 + \sigma^4 \Delta t}{2N\Delta t} \end{eqnarray*}

Real Data Example

We use the above described method write a small python script that will estimate \mu and \sigma for a particular stock and then derive the optimal buy or sell limit dependent on \tau.

Try it yourself with my online notebook:

Figure 1: A picture of the python program. Beside the estimates for \mu and \sigma as well as \hat{l }_{buy, \tau}^*, \hat{l }_{sell, \tau}^* also is \mathbb{P}(S^{inf}_\tau \leq \hat{l }_{buy, \tau}^*), \mathbb{P}(S^{inf}_\tau \leq \hat{l }_{sell, \tau}^*) shown. The blue line shows the share price of the DAX between 1988 and today.The orange line is the trend S(0) exp(\hat{\mu} - 0.5 \hat{\sigma}^2)t.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.