541 lines
298 KiB
Plaintext
541 lines
298 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Premier Algorithme de Trading\n",
|
||
|
"## Trading de Paires\n",
|
||
|
"\n",
|
||
|
"Le trading de paires est une stratégie qui utilise deux actions qui sont fortement corrélées. Nous pouvons alors utiliser la différence de prix entre les deux actions comme signal si l'une d'entre elles sort de la corrélation avec l'autre. Il s'agit d'une stratégie plus ancienne qui est utilisée classiquement comme guide pour commencer le trading algorithmique. Il existe un guide complet fantastique et vous pouvez le trouvez sur Investopedia [ici](http://www.investopedia.com/university/guide-pairs-trading/)! **Je vous recommande vivement de lire l'article dans son intégralité avant de continuer, il est divertissant et instructif !**\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"Créons notre premier algorithme de Trading base ! Il s'agit d'un exercice d'utilisation de l'algorithme quantique, **PAS** d'une représentation réaliste de ce qu'est un bon algorithme ! N'utilisez jamais quelque chose d'aussi simple que cela dans le monde réel ! Il s'agit d'une version extrêmement simplifiée du Trading de paires, nous ne prendrons pas en compte des facteurs tels que la co-intégration !"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"import numpy as np\n",
|
||
|
"import pandas as pd\n",
|
||
|
"import matplotlib.pyplot as plt"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## United Airlines et American Airlines"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"start = '07-01-2015'\n",
|
||
|
"end = '07-01-2017'"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# en local\n",
|
||
|
"# united = quandl.get('WIKI/UAL',start_date=start,end_date=end)\n",
|
||
|
"# american = quandl.get('WIKI/AAL',start_date=start,end_date=end)\n",
|
||
|
"\n",
|
||
|
"united = get_pricing('UAL',start_date=start,end_date=end)\n",
|
||
|
"american = get_pricing('AAL',start_date=start,end_date=end)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<div>\n",
|
||
|
"<table border=\"1\" class=\"dataframe\">\n",
|
||
|
" <thead>\n",
|
||
|
" <tr style=\"text-align: right;\">\n",
|
||
|
" <th></th>\n",
|
||
|
" <th>open_price</th>\n",
|
||
|
" <th>high</th>\n",
|
||
|
" <th>low</th>\n",
|
||
|
" <th>close_price</th>\n",
|
||
|
" <th>volume</th>\n",
|
||
|
" <th>price</th>\n",
|
||
|
" </tr>\n",
|
||
|
" </thead>\n",
|
||
|
" <tbody>\n",
|
||
|
" <tr>\n",
|
||
|
" <th>2015-07-01 00:00:00+00:00</th>\n",
|
||
|
" <td>54.05</td>\n",
|
||
|
" <td>54.14</td>\n",
|
||
|
" <td>50.10</td>\n",
|
||
|
" <td>51.69</td>\n",
|
||
|
" <td>12129258.0</td>\n",
|
||
|
" <td>51.69</td>\n",
|
||
|
" </tr>\n",
|
||
|
" <tr>\n",
|
||
|
" <th>2015-07-02 00:00:00+00:00</th>\n",
|
||
|
" <td>51.88</td>\n",
|
||
|
" <td>52.51</td>\n",
|
||
|
" <td>50.98</td>\n",
|
||
|
" <td>51.48</td>\n",
|
||
|
" <td>4551677.0</td>\n",
|
||
|
" <td>51.48</td>\n",
|
||
|
" </tr>\n",
|
||
|
" <tr>\n",
|
||
|
" <th>2015-07-06 00:00:00+00:00</th>\n",
|
||
|
" <td>52.05</td>\n",
|
||
|
" <td>53.95</td>\n",
|
||
|
" <td>52.05</td>\n",
|
||
|
" <td>53.80</td>\n",
|
||
|
" <td>5458762.0</td>\n",
|
||
|
" <td>53.80</td>\n",
|
||
|
" </tr>\n",
|
||
|
" <tr>\n",
|
||
|
" <th>2015-07-07 00:00:00+00:00</th>\n",
|
||
|
" <td>53.99</td>\n",
|
||
|
" <td>54.49</td>\n",
|
||
|
" <td>52.54</td>\n",
|
||
|
" <td>54.25</td>\n",
|
||
|
" <td>5373987.0</td>\n",
|
||
|
" <td>54.25</td>\n",
|
||
|
" </tr>\n",
|
||
|
" <tr>\n",
|
||
|
" <th>2015-07-08 00:00:00+00:00</th>\n",
|
||
|
" <td>53.44</td>\n",
|
||
|
" <td>53.72</td>\n",
|
||
|
" <td>52.25</td>\n",
|
||
|
" <td>52.82</td>\n",
|
||
|
" <td>4050933.0</td>\n",
|
||
|
" <td>52.82</td>\n",
|
||
|
" </tr>\n",
|
||
|
" </tbody>\n",
|
||
|
"</table>\n",
|
||
|
"</div>"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
" open_price high low close_price volume \\\n",
|
||
|
"2015-07-01 00:00:00+00:00 54.05 54.14 50.10 51.69 12129258.0 \n",
|
||
|
"2015-07-02 00:00:00+00:00 51.88 52.51 50.98 51.48 4551677.0 \n",
|
||
|
"2015-07-06 00:00:00+00:00 52.05 53.95 52.05 53.80 5458762.0 \n",
|
||
|
"2015-07-07 00:00:00+00:00 53.99 54.49 52.54 54.25 5373987.0 \n",
|
||
|
"2015-07-08 00:00:00+00:00 53.44 53.72 52.25 52.82 4050933.0 \n",
|
||
|
"\n",
|
||
|
" price \n",
|
||
|
"2015-07-01 00:00:00+00:00 51.69 \n",
|
||
|
"2015-07-02 00:00:00+00:00 51.48 \n",
|
||
|
"2015-07-06 00:00:00+00:00 53.80 \n",
|
||
|
"2015-07-07 00:00:00+00:00 54.25 \n",
|
||
|
"2015-07-08 00:00:00+00:00 52.82 "
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 4,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"united.head()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<div>\n",
|
||
|
"<table border=\"1\" class=\"dataframe\">\n",
|
||
|
" <thead>\n",
|
||
|
" <tr style=\"text-align: right;\">\n",
|
||
|
" <th></th>\n",
|
||
|
" <th>open_price</th>\n",
|
||
|
" <th>high</th>\n",
|
||
|
" <th>low</th>\n",
|
||
|
" <th>close_price</th>\n",
|
||
|
" <th>volume</th>\n",
|
||
|
" <th>price</th>\n",
|
||
|
" </tr>\n",
|
||
|
" </thead>\n",
|
||
|
" <tbody>\n",
|
||
|
" <tr>\n",
|
||
|
" <th>2015-07-01 00:00:00+00:00</th>\n",
|
||
|
" <td>39.846</td>\n",
|
||
|
" <td>40.091</td>\n",
|
||
|
" <td>37.052</td>\n",
|
||
|
" <td>38.023</td>\n",
|
||
|
" <td>23439886.0</td>\n",
|
||
|
" <td>38.023</td>\n",
|
||
|
" </tr>\n",
|
||
|
" <tr>\n",
|
||
|
" <th>2015-07-02 00:00:00+00:00</th>\n",
|
||
|
" <td>38.248</td>\n",
|
||
|
" <td>39.018</td>\n",
|
||
|
" <td>37.640</td>\n",
|
||
|
" <td>38.253</td>\n",
|
||
|
" <td>10226556.0</td>\n",
|
||
|
" <td>38.253</td>\n",
|
||
|
" </tr>\n",
|
||
|
" <tr>\n",
|
||
|
" <th>2015-07-06 00:00:00+00:00</th>\n",
|
||
|
" <td>38.317</td>\n",
|
||
|
" <td>39.209</td>\n",
|
||
|
" <td>38.307</td>\n",
|
||
|
" <td>38.934</td>\n",
|
||
|
" <td>8025562.0</td>\n",
|
||
|
" <td>38.934</td>\n",
|
||
|
" </tr>\n",
|
||
|
" <tr>\n",
|
||
|
" <th>2015-07-07 00:00:00+00:00</th>\n",
|
||
|
" <td>39.140</td>\n",
|
||
|
" <td>39.934</td>\n",
|
||
|
" <td>38.483</td>\n",
|
||
|
" <td>39.865</td>\n",
|
||
|
" <td>9341995.0</td>\n",
|
||
|
" <td>39.865</td>\n",
|
||
|
" </tr>\n",
|
||
|
" <tr>\n",
|
||
|
" <th>2015-07-08 00:00:00+00:00</th>\n",
|
||
|
" <td>39.503</td>\n",
|
||
|
" <td>39.513</td>\n",
|
||
|
" <td>38.366</td>\n",
|
||
|
" <td>38.542</td>\n",
|
||
|
" <td>9976525.0</td>\n",
|
||
|
" <td>38.542</td>\n",
|
||
|
" </tr>\n",
|
||
|
" </tbody>\n",
|
||
|
"</table>\n",
|
||
|
"</div>"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
" open_price high low close_price \\\n",
|
||
|
"2015-07-01 00:00:00+00:00 39.846 40.091 37.052 38.023 \n",
|
||
|
"2015-07-02 00:00:00+00:00 38.248 39.018 37.640 38.253 \n",
|
||
|
"2015-07-06 00:00:00+00:00 38.317 39.209 38.307 38.934 \n",
|
||
|
"2015-07-07 00:00:00+00:00 39.140 39.934 38.483 39.865 \n",
|
||
|
"2015-07-08 00:00:00+00:00 39.503 39.513 38.366 38.542 \n",
|
||
|
"\n",
|
||
|
" volume price \n",
|
||
|
"2015-07-01 00:00:00+00:00 23439886.0 38.023 \n",
|
||
|
"2015-07-02 00:00:00+00:00 10226556.0 38.253 \n",
|
||
|
"2015-07-06 00:00:00+00:00 8025562.0 38.934 \n",
|
||
|
"2015-07-07 00:00:00+00:00 9341995.0 39.865 \n",
|
||
|
"2015-07-08 00:00:00+00:00 9976525.0 38.542 "
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 5,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"american.head()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"<matplotlib.legend.Legend at 0x7f5c657cdd68>"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 6,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAtEAAAHKCAYAAADFMBfCAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XlglPW1//H3TCaZyb4nkBAgLGEJ+yIqAq5otVatWm0R\n9V7b3t7itdVfN1vRarWt2t6qV21rLVr32qqoqLiCsshiWGQxECCBEEL2PTOTmcz8/pjMJEMSSMhk\nEobP658mzzzzPCdP1J45Od/zNbjdbjciIiIiItJjxoEOQERERETkVKMkWkRERESkl5REi4iIiIj0\nkpJoEREREZFeUhItIiIiItJLSqJFRERERHrJFMybud1u7rnnHvbu3UtERAT33nsvkZGR/PSnP8Xt\ndpOamspDDz1EeHh4MMMSEREREemVoCbRH3/8MY2NjbzyyisUFxfzwAMPkJiYyOLFi1m4cCEPP/ww\nr732Gtdff30wwxIRERER6ZWgtnMUFRUxZcoUALKysigpKWHz5s2cd955AFxwwQWsX78+mCGJiIiI\niPRaUJPonJwc1qxZg8vl4sCBAxw+fJiSkhJf+0ZqaioVFRXBDElEREREpNeC2s4xf/58tm7dyg03\n3MC4ceMYNWoUBQUFvtfdbjcGg+GE18nLy+vPMEVEREREfGbOnNnpWFCTaIAf/ehHvq8vuugi0tPT\naWlpISIigrKyMlJTU3t0na5+GDk5eXl5ep4BpmcaOHqWgaXnGVh6noGnZxoYeo6B013xNqjtHPn5\n+fzyl78E4LPPPiM3N5ezzjqLlStXAvD+++8zb968YIYkIiIiItJrQa1Ejxs3DrfbzfXXX09cXBy/\n//3vaW1t5ec//zmvvvoqGRkZXHXVVcEMSURERESk14KaRBsMBn73u991Or5s2bJghiEiIiISMG63\nG7vdPtBhdGKz2QY6hFOO2Wzu0fo80I6FIiIiIn1it9sHXRKdm5s70CGccnr7ewz6wkIRERGRUGM2\nm7FYLAMdhgSRKtEiIiIiIr2kJFpEREREpJeURIuIiIiI9JKSaBEREZEQ8fbbbzNp0iRqa2sDet2n\nnnqK7du3B/SaXkuXLu004njJkiWdznv88cd58cUXyc/P5/HHH++XWHpDCwtFREREQsQ777zD8OHD\nef/997nuuusCdt3vf//7AbtWR06nk9WrV2M2myksLCQ7OxuAJ554otv3jB8/nvHjx/dLPL2hJFpE\nREQkBNTV1bFjxw5++9vf8vTTT/uS6MWLFzNnzhzWr1+P0Wjkyiuv5I033iAsLIx//OMfNDc3c+ed\nd9LQ0IDT6WTp0qXk5ORw8cUXs2DBApKSkjh48CCXXHIJc+fO5ec//zlHjhzBYrHw4IMPEhUVxU9+\n8hOsVis2m4277rqLyZMns3DhQq677jpWrVqFw+HgmWeeISoqyi9m7w7W48eP55133uHWW28F4Mwz\nz2TDhg0sXryYnJwcDAYDCQkJAGzatIkXXniBxx57jIULF3LhhReyZcsW4uLieOqpp2hqaury53nq\nqaf46KOPMBqNnH/++X3+YKAkWkRERCSAlr29i3XbSwJ6zblTM/nPy48/+/m9997j/PPPZ968eSxd\nupTy8nLS0tIASE9P56WXXuLb3/429fX1vPjii9xwww3s2bOHTz75hPnz53PNNdewf/9+HnjgAZYt\nW4bD4WDBggXMnTuXO++8E4A33niDtLQ0/vjHP/Luu+/yySefcNZZZ3HttddywQUXsGHDBv72t7/x\n2GOP4XQ6GTNmDLfccgt33HEHn3/+ORdccIFfzCtWrOCyyy5jwoQJ3Hrrrb4kuuOGJzk5OVx33XV+\nLRze14uLi7nqqqv42c9+xvXXX09+fn63P88zzzzDunXrMBqNvPLKK33+nSiJFhEREQkBK1asYMmS\nJRiNRhYuXMh7773HTTfdBMDkyZMBSE1NZcKECQAkJSXR2NjI1q1bqam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\nKIkWERERCWElJSX84he/ICsri/z8fHJzc/nNb37DnXfeycUXX8zLL7/Mjh07ePLJJ7npppu48847\naWhowOl0snTpUnJycnjzzTf5+9//zsiRI3G73YwbN67TfT777DNyc3MZP34877zzDrfeeisAZ555\nJhs2bGDx4sXk5ORgMBhISEgAPBX0F154gccee4yFCxdy4YUXsmXLFuLi4njqqadoamrqMp6nnnqK\njz76CKPRyPnnn+/7QBBMSqJFREREAuj5ba+xoXhLQK95ZtYMFk+7+sQndmPXrl08+uijJCYmsmDB\nAhobGwFP9fqWW27hxRdf5Ic//CFPPvkk8+fP55prrmH//v088MADLFu2jEceeYTXX3+d2NhYvvnN\nb3Z5jxUrVnDZZZcxYcIEbr31Vl8S3bFCnpOTw3XXXefXwuF9vbi4mKuuuoqf/exnXH/99eTn5/PJ\nJ590Gc8zzzzDunXrMBqNvPLKKyf9XPpCSbSIiIhIiPImqCNGjCApKQmAtLQ0Ghoaujx/69at1NTU\n8OabbwLQ0tJCTU0NMTExJCYmAjBjxoxO77Naraxfv57777+fqKgoIiIiyM/P79SqMWXKlG5jjYmJ\nYezYsQCkp6fT2NjYZTwAl1xyCTfddBOXX345X//613v8PAJJSbSIiIhIAC2ednWfqsYnIykpifr6\ner9j1dXVviQ2LCzM7zW3293ldcLDw1m6dClTp071u05HLper0/s+/PBDXC4XixYtwu12U1tby4oV\nKzol0eHh4d3+DB1jdLvduFwuIiIiOsUDcM8991BYWMi7777L4sWLee211zAagzsvQ9M5RERERE5x\nI0eOpKysjOLiYsCT+G7cuNFXNe6YNB+bQBuNRux2OwBTp07lww8/BGDfvn08++yzJCYm0tTURGNj\nIw6Hg61bt3a6/4oVK3j
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x7f5c66da4278>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# american['Adj. Close'].plot(label='American Airlines',figsize=(12,8))\n",
|
||
|
"# united['Adj. Close'].plot(label='United Airlines')\n",
|
||
|
"american['close_price'].plot(label='American Airlines',figsize=(12,8))\n",
|
||
|
"united['close_price'].plot(label='United Airlines')\n",
|
||
|
"plt.legend()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Dispersion et Corrélation"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 7,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"array([[ 1. , 0.91653673],\n",
|
||
|
" [ 0.91653673, 1. ]])"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 7,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# np.corrcoef(american['Adj. Close'],united['Adj. Close'])\n",
|
||
|
"np.corrcoef(american['close_price'],united['close_price'])"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 8,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"<matplotlib.legend.Legend at 0x7f5c600bf400>"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 8,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAtEAAAHKCAYAAADFMBfCAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3XdgW+XVP/Dv1Z625b1XnGFnx9kJBAh7FxraQpPSQqHt\nr7ylbweU0gFvJ22h8AItLaG8zNJQygw0QCFxErKcvew43nvJ1t7398fVvZq2JVuWZPt8/sGWr6XH\nQpHOPfc85zAsy7IghBBCCCGEREy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hBBCSMJQEE1Igoy3\nnAMAlHIJlXMQQgghCURBNCEJwm8sTImynAOgIJoQQghJNAqiCUkQE18TPY5MtEImgc3uAsuysV4W\nIYQQQiJAQTQhCSKUc4ynJlohgYcF7E53rJdFCCGEkAhQEE1IghgsDihkYkgl0U8eVMolAACbnYJo\nQgghJBEoiCYkQUwWx7hKOQBAKeOCaKqLJoQQQhKDgmhCEsRic0HlzShHS6mgIJoQQghJJAqiCUkQ\nu9MNhWycQbScgmhCCCEkkeIaRB84cABr167Fzp07hdvOnj2LL37xi7j11lvx0EMPxXM5hCSM28PC\n6fJALou+Hhqg0d+EEEJIosUtiG5ra8Pzzz+P5cuXB9z+q1/9Cj/5yU/wyiuvQK/Xo6amJl5LIiRh\n7A4u+B1vEK2iTDQhhBCSUHELorOzs/HUU09BrVYLtzmdTnR0dGD+/PkAgI0bN2Lv3r3xWhIhCWN3\ncF015NLxBdF8TTSN/ia
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x7f5c65da57b8>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# spread = american['Adj. Close'] - united['Adj. Close']\n",
|
||
|
"spread = american['close_price'] - united['close_price']\n",
|
||
|
"spread.plot(label='Spread',figsize=(12,8))\n",
|
||
|
"plt.axhline(spread.mean(),c='r')\n",
|
||
|
"plt.legend()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### Normaliser avec un z-score"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 9,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def zscore(stocks):\n",
|
||
|
" return (stocks - stocks.mean()) / np.std(stocks)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 10,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "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\nmT9/Pl988QU///nP+eS
|
||
|
"text/plain": [
|
||
|
"<matplotlib.figure.Figure at 0x7f5c64b152e8>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"zscore(spread).plot(figsize=(14,8))\n",
|
||
|
"plt.axhline(zscore(spread).mean(), color='black')\n",
|
||
|
"plt.axhline(1.0, c='r', ls='--')\n",
|
||
|
"plt.axhline(-1.0, c='g', ls='--')\n",
|
||
|
"plt.legend(['Spread z-score', 'Mean', '+1', '-1']);"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### Rolling Z-Score\n",
|
||
|
"\n",
|
||
|
"Notre écart est actuellement American-United. Décidons de la façon de calculer ce spread sur une base continue pour notre utilisation en Quantopian"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 11,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAtEAAAHKCAYAAADFMBfCAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsvXeYJFd57/+t0Gly3KRN0iqBtFqkRUIiSRdEsAw2NsHI\nGNlckjHca7jXFwtMsC8YDJgfFwwyBgnbIoMMGJkgVhFJoLAjrcJK2pU2and2Z3Zy6Fjh90fVqTpd\nXdVdVV1dXT3zfp5Hz45marrPVFfVec/3fN/3FXRd10EQBEEQBEEQhG/Edg+AIAiCIAiCIDoNCqIJ\ngiAIgiAIIiAURBMEQRAEQRBEQCiIJgiCIAiCIIiAUBBNEARBEARBEAGhIJogCIIgCIIgApKoILpU\nKuHKK6/ET37yk3YPhSAIgiAIgiA8SVQQfd1112FwcLDdwyAIgiAIgiCIuiQmiD548CAOHjyIyy+/\nvN1DIQiCIAiCIIi6JCaI/sxnPoNrr7223cMgCIIgCIIgiIbI7R4AAPzkJz/BhRdeiNNOOw0A0KgT\n+djYWBzDIgiCIAiCIAjs3Lmz5nuJCKLvuusuHDt2DHfccQdOnjyJTCaDdevW4bLLLvP8Hbc/hgjH\n2NgYnc+IoXMaHXQuo4XOZ7TQ+YweOqfRQOcxOrzE20QE0V/4whesr7/85S9j48aNdQNogiAIgiAI\ngmgnifFEEwRBEARBEESnkAglmud973tfu4dAEARBEARBEHUhJZogCIIgCIIgAkJBNEEQBEEQBEEE\nhIJogiAIgiAIgggIBdEEQRAEQRAEERAKogmCIAiCIAgiIBREEwR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plotlib.figure.Figure at 0x7f5c6acb6a20>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Moyenne mobile sur 1 jour de l'écart de prix\n",
|
||
|
"spread_mavg1 = spread.rolling(1).mean()\n",
|
||
|
"\n",
|
||
|
"# Moyenne mobile sur 30 jours de l'écart de prix\n",
|
||
|
"spread_mavg30 = spread.rolling(30).mean()\n",
|
||
|
"\n",
|
||
|
"# Prendre un écart-type mobile de 30 jours\n",
|
||
|
"std_30 = spread.rolling(30).std()\n",
|
||
|
"\n",
|
||
|
"# Calculer le z score pour chaque jour\n",
|
||
|
"zscore_30_1 = (spread_mavg1 - spread_mavg30)/std_30\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"zscore_30_1.plot(figsize=(12,8),label='Rolling 30 day Z score')\n",
|
||
|
"plt.axhline(0, color='black')\n",
|
||
|
"plt.axhline(1.0, color='red', linestyle='--');"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Implémentation de la Stratégie sur l'IDE Quantopian\n",
|
||
|
"\n",
|
||
|
"#### AVERTISSEMENT : VOUS NE DEVEZ PAS FAIRE DE TRADING AVEC ÇA !"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 12,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"import numpy as np\n",
|
||
|
" \n",
|
||
|
"def initialize(context):\n",
|
||
|
" \"\"\"\n",
|
||
|
" Appelé une fois au début de l'algorithme.\n",
|
||
|
" \"\"\" \n",
|
||
|
" \n",
|
||
|
" # Chaque jour, nous vérifions le status de la paire\n",
|
||
|
" schedule_function(check_pairs, date_rules.every_day(), time_rules.market_close(minutes=60))\n",
|
||
|
" \n",
|
||
|
" # Nos 2 compagnies aériennes\n",
|
||
|
" context.aa = sid(45971) #aal\n",
|
||
|
" context.ual = sid(28051) #ual \n",
|
||
|
" \n",
|
||
|
" # Flags pour nous dire si on est dans une position de Trading\n",
|
||
|
" context.long_on_spread = False\n",
|
||
|
" context.shorting_spread = False\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"def check_pairs(context, data):\n",
|
||
|
" \n",
|
||
|
" # Par commodité\n",
|
||
|
" aa = context.aa\n",
|
||
|
" ual = context.ual\n",
|
||
|
" \n",
|
||
|
" # Obtenir l'historique des prix\n",
|
||
|
" prices = data.history([aa, ual], \"price\", 30, '1d')\n",
|
||
|
" \n",
|
||
|
" \n",
|
||
|
" # Nécessité d'utiliser .iloc[-1 :] pour obtenir un dataframe au lieu de séries\n",
|
||
|
" short_prices = prices.iloc[-1:]\n",
|
||
|
" \n",
|
||
|
" # Obtenir la moyenne mobile longue sur 30 jours\n",
|
||
|
" mavg_30 = np.mean(prices[aa] - prices[ual])\n",
|
||
|
" \n",
|
||
|
" # Obtenez l'écart-type' de la fenêtre de 30 jours\n",
|
||
|
" std_30 = np.std(prices[aa] - prices[ual])\n",
|
||
|
" \n",
|
||
|
" # Obtenir la moyenne mobile courte sur 30 jours\n",
|
||
|
" mavg_1 = np.mean(short_prices[aa] - short_prices[ual])\n",
|
||
|
" \n",
|
||
|
" # Calculer le z-score\n",
|
||
|
" if std_30 > 0:\n",
|
||
|
" zscore = (mavg_1 - mavg_30)/std_30\n",
|
||
|
" \n",
|
||
|
" # Nos 2 cas d'entrée\n",
|
||
|
" if zscore > 0.5 and not context.shorting_spread:\n",
|
||
|
" # spread = aa - ual\n",
|
||
|
" order_target_percent(aa, -0.5) # short top\n",
|
||
|
" order_target_percent(ual, 0.5) # long bottom\n",
|
||
|
" context.shorting_spread = True\n",
|
||
|
" context.long_on_spread = False\n",
|
||
|
" \n",
|
||
|
" elif zscore < -0.5 and not context.long_on_spread:\n",
|
||
|
" # spread = aa - ual\n",
|
||
|
" order_target_percent(aa, 0.5) # long top\n",
|
||
|
" order_target_percent(ual, -0.5) # short bottom\n",
|
||
|
" context.shorting_spread = False\n",
|
||
|
" context.long_on_spread = True\n",
|
||
|
" \n",
|
||
|
" # Notre cas de sortie\n",
|
||
|
" elif abs(zscore) < 0.1:\n",
|
||
|
" order_target_percent(aa, 0)\n",
|
||
|
" order_target_percent(ual, 0)\n",
|
||
|
" context.shorting_spread = False\n",
|
||
|
" context.long_on_spread = False\n",
|
||
|
" \n",
|
||
|
" record('zscore', zscore)"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3.5",
|
||
|
"language": "python",
|
||
|
"name": "py35"
|
||
|
},
|
||
|
"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.7.5"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 1
|
||
|
}
|