{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### If you can see into the future, that is.\n",
    "\n",
    "My previous class in Stochastic Calculus covered a lot of interesting topics, and the important one for today\n",
    "is the [Gambler's Ruin][1] problem. If you're interested in some of the theory behind it, also make sure to check out\n",
    "[random walks][2]. The important bit is that we studied the [Martingale Betting Strategy][3], which describes for us\n",
    "a **guaranteed way** to <span style='font-size: x-small'>eventually</span> make money.\n",
    "\n",
    "The strategy goes like this: You are going to toss a fair coin with a friend. If you guess heads or tails correctly, you get back double the money you bet. If you guess incorrectly, you lose money. How should you bet?\n",
    "\n",
    "The correct answer is that you should double your bet each time you lose. Then when you finally win, you'll be guaranteed to make back everything you lost and then &#36;1 extra! Consider the scenario:\n",
    "\n",
    "1. You bet &#36;1, and guess incorrectly. You're 1 dollar in the hole.\n",
    "2. You bet &#36;2, and guess incorrectly. You're 3 dollars in the hole now.\n",
    "3. You bet &#36;4, and guess incorrectly. You're 7 dollars in the hole.\n",
    "4. You bet &#36;8, and guess correctly! You now get back those 8 dollars you bet, plus 8 extra for winning, for a **total profit of one dollar**!\n",
    "\n",
    "Mathematically, we can prove that as long as you have unlimited money to bet, you are guaranteed to make money.\n",
    "\n",
    "# Applying the Martingale Strategy\n",
    "\n",
    "But we're all realistic people, and once you start talking about \"unlimited money\" eyebrows should be raised. Even still, this is an interesting strategy to investigate, and I want to apply it to the stock market. As long as we can guarantee there's a single day in which the stock goes up, we should be able to make money right? The question is just how much we have to invest to guarantee this.\n",
    "\n",
    "Now it's time for the math. We'll use the following definitions:\n",
    "\n",
    "- $o_i$ = the share price at the opening of day $i$\n",
    "- $c_i$ = the share price at the close of day $i$\n",
    "- $d_i$ = the amount of money we want to invest at the beginning of day $i$\n",
    "\n",
    "With those definitions in place, I'd like to present the formula that is **guaranteed to make you money**. I call it *Bradlee's Investment Formula*:\n",
    "\n",
    "$c_n \\sum_{i=1}^n \\frac{d_i}{o_i} > \\sum_{i=1}^{n} d_i$\n",
    "\n",
    "It might not look like much, but if you can manage to make it so that this formula holds true, you will be guaranteed to make money. The intuition behind the formula is this: The closing share price times the number of shares you have purchased ends up greater than the amount of money you invested.\n",
    "\n",
    "That is, on day $n$, <span style='font-size: x-small'>if you know what the closing price will be</span> you can set up the amount of money you invest that day to **guarantee you make money**. I'll even teach you to figure out how much money that is! Take a look:\n",
    "\n",
    "$\n",
    "\\begin{align}\n",
    "c_n \\sum_{i=1}^{n-1} \\frac{d_i}{o_i} + \\frac{c_nd_n}{o_n} &> \\sum_{i=1}^{n-1}d_i + d_n\\\\\n",
    "\\frac{c_nd_n}{o_n} - d_n &> \\sum_{i=1}^{n-1}(d_i - \\frac{c_nd_i}{o_i})\\\\\n",
    "d_n (\\frac{c_n - o_n}{o_n}) &> \\sum_{i=1}^{n-1} d_i(1 - \\frac{c_n}{o_i})\\\\\n",
    "d_n &> \\frac{o_n}{c_n - o_n} \\sum_{i=1}^{n-1} d_i(1 - \\frac{1}{o_i})\n",
    "\\end{align}$\n",
    "\n",
    "If you invest exactly $d_n$ that day, you'll break even. But if you can make sure the money you invest is greater than that quantity on the right <span style='font-size: x-small'>(which requires that you have a crystal ball tell you the stock's closing price)</span> you are **guaranteed to make money!**\n",
    "\n",
    "# Interesting Implications\n",
    "\n",
    "On a more serious note though, the formula above tells us a couple of interesting things:\n",
    "\n",
    "1. It's impossible to make money without the closing price at some point being greater than the opening price (or vice-versa if you are short selling) - there is no amount of money you can invest that will turn things in your favor.\n",
    "2. Close prices of the past aren't important if you're concerned about the bottom line. While chart technicians use price history to make judgment calls, in the end, the closing price on anything other than the last day is irrelevant.\n",
    "3. It's possible to make money as long as there is a single day where the closing price is greater than the opening price! You might have to invest a lot to do so, but it's possible.\n",
    "4. You must make a prediction about where the stock will close at if you want to know how much to invest. That is, we can set up our investment for the day to make money if the stock goes up 1%, but if it only goes up .5% we'll still lose money.\n",
    "5. It's possible the winning move is to scale back your position. Consider the scenario:\n",
    "    - You invest money and the stock closes down the day .5%\n",
    "    - You invest tomorrow expecting the stock to go up 1%\n",
    "    - The winning investment to break even (assuming a 1% increase) is to scale back the position, since the shares you purchased at the beginning would then be profitable\n",
    "\n",
    "# Running the simulation\n",
    "\n",
    "So now that we've defined our investment formula,we need to tweak a couple things in order to make an investment strategy we can actually work with. There are two issues we need to address:\n",
    "\n",
    "1. The formula only tells us how much to invest if we want to break even ($d_n$). If we actually want to turn a profit, we need to invest more than that, which we will refer to as the **bias**.\n",
    "2. The formula assumes we know what the closing price will be on any given day. If we don't know this, we can still invest assuming the stock price will close at a level we choose. If the price doesn't meet this objective, we try again tomorrow! This predetermined closing price will be referred to as the **expectation**.\n",
    "\n",
    "Now that we've defined our *bias* and *expectation*, we can actually build a strategy we can simulate. Much like the martingale strategy told you to bet twice your previous bet in order to make money, we've designed a system that tells us how much to bet in order to make money as well.\n",
    "\n",
    "Now, let's get to the code!\n",
    "\n",
    "[1]: https://en.wikipedia.org/wiki/Gambler's_ruin\n",
    "[2]: https://en.wikipedia.org/wiki/Random_walk\n",
    "[3]: https://en.wikipedia.org/wiki/Martingale_%28betting_system%29"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "using Quandl\n",
    "api_key = \"\"\n",
    "daily_investment = function(current_open, current_close, purchase_history, open_history)\n",
    "    # We're not going to safeguard against divide by 0 - that's the user's responsibility\n",
    "    t1 = current_close / current_open - 1\n",
    "    t2 = sum(purchase_history - purchase_history*current_close ./ open_history)\n",
    "    return t2 / t1\n",
    "end;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And let's code a way to run simulations quickly:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "is_profitable = function(current_price, purchase_history, open_history)\n",
    "    shares = sum(purchase_history ./ open_history)\n",
    "    return current_price*shares > sum(purchase_history)\n",
    "end\n",
    "\n",
    "simulate = function(name, start, init, expected, bias)\n",
    "    ticker_info = quandlget(name, from=start, api_key=api_key)\n",
    "    open_vals = ticker_info[\"Open\"].values\n",
    "    close_vals = ticker_info[\"Close\"].values\n",
    "    invested = [init]\n",
    "    \n",
    "    # The simulation stops once we've made a profit\n",
    "    day = 1\n",
    "    profitable = is_profitable(close_vals[day], invested, open_vals[1:length(invested)]) ||\n",
    "        is_profitable(open_vals[day+1], invested, open_vals[1:length(invested)])\n",
    "    while !profitable\n",
    "        expected_close = open_vals[day+1] * expected\n",
    "        todays_purchase = daily_investment(open_vals[day+1], expected_close, invested, open_vals[1:day])\n",
    "        invested = [invested; todays_purchase + bias]\n",
    "        # expected_profit = expected_close * sum(invested ./ open_vals[1:length(invested)]) - sum(invested)\n",
    "        day += 1\n",
    "        profitable = is_profitable(close_vals[day], invested, open_vals[1:length(invested)]) ||\n",
    "            is_profitable(open_vals[day+1], invested, open_vals[1:length(invested)])\n",
    "    end\n",
    "    \n",
    "    shares = sum(invested ./ open_vals[1:length(invested)])\n",
    "    max_profit = max(close_vals[day], open_vals[day+1])\n",
    "    profit = shares * max_profit - sum(invested)\n",
    "    return (invested, profit)\n",
    "end\n",
    "\n",
    "sim_summary = function(investments, profit)\n",
    "    leverages = [sum(investments[1:i]) for i=1:length(investments)]\n",
    "    max_leverage = maximum(leverages) / investments[1]\n",
    "    println(\"Max leverage: $(max_leverage)\")\n",
    "    println(\"Days invested: $(length(investments))\")\n",
    "    println(\"Profit: $profit\")\n",
    "end;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, let's get some data and run a simulation! Our first test:\n",
    "\n",
    "- We'll invest 100 dollars in LMT, and expect that the stock will close up 1% every day. We'll invest $d_n$ + 10 dollars every day that we haven't turned a profit, and end the simulation once we've made a profit."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Max leverage: 5.590373200042106\n",
      "Days invested: 5\n",
      "Profit: 0.6894803101560001\n"
     ]
    }
   ],
   "source": [
    "investments, profit = simulate(\"YAHOO/LMT\", Date(2015, 11, 29), 100, 1.01, 10)\n",
    "sim_summary(investments, profit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The result: We need to invest 5.6x our initial position over a period of 5 days to make approximately .69&#162;\n",
    "\n",
    "- Now let's try the same thing, but we'll assume the stock closes up 2% instead."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Max leverage: 1.854949900247809\n",
      "Days invested: 25\n",
      "Profit: 0.08304813163696423\n"
     ]
    }
   ],
   "source": [
    "investments, profit = simulate(\"YAHOO/LMT\", Date(2015, 11, 29), 100, 1.02, 10)\n",
    "sim_summary(investments, profit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this example, we only get up to a 1.85x leveraged position, but it takes 25 days to turn a profit of 8&#162;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Summary\n",
    "\n",
    "We've defined an investment strategy that can tell us how much to invest when we know what the closing position of a stock will be. We can tweak the strategy to actually make money, but plenty of work needs to be done so that we can optimize the money invested.\n",
    "\n",
    "In the next post I'm going to post more information about some backtests and strategy tests on this strategy (unless of course this experiment actually produces a significant profit potential, and then I'm keeping it for myself)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Side note and disclaimer\n",
    "\n",
    "The claims made in this presentation about being able to guarantee making money are intended as a joke and do not constitute investment advice of any sort."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.4.2",
   "language": "julia",
   "name": "julia-0.4"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.4.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
