python-pour-finance/10-Plateforme-Quantopian/.ipynb_checkpoints/01-Bases-Quantopian-Researc...

702 lines
70 KiB
Plaintext
Raw Normal View History

2023-08-21 15:12:19 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 01-Les bases de Research avec Quantopian\n",
"\n",
"**N'oubliez pas que ce notebook ne fonctionnera que sur Quantopian ! Créez un compte et téléversez (upload) le fichier de ce notebook. Ces commandes et fonctions ne fonctionneront que sur la plateforme de trading Quantopian !**\n",
"\n",
"Notez qu'une grande partie du texte écrit dans ce notebook en Markdown provient directement des documents et tutoriels Quantopian, regardez les bien aussi, ils sont bien faits !\n",
"\n",
"## Research\n",
"\n",
"Le format notebook nous permet de rassembler facilement les informations sur les différents titres au sein de la plateforme Quantopian. Il faut garder à l'esprit que cela est différent de la plateforme de codage de base de Quantopian, qui se concentre sur la mise en œuvre effective et le backtesting des stratégies de trading."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"# PAS BESOIN D'UTILISER LA COMMANDE MAGIQUE SUR QUANTOPIAN !"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Obtenir des informations\n",
"\n",
"Passons en revue quelques fonctions clés :\n",
"\n",
"* get_pricing()\n",
"* symbols()\n",
"* local_csv()\n",
"* get_backtest()\n",
"* get_fundamentals()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## get_pricing()\n",
"\n",
"La fonction `get_pricing` permet d'accéder à 12 ans de données sur le prix des actions américaines : les mêmes données que celles utilisées par le backtester Quantopian.\n",
"\n",
"`get_pricing` retourne un <b>objet Pandas</b>. Il peut s'agir d'un dataframe ou d'une série, selon les valeurs d'entrée. "
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"mcdon = get_pricing('MCD',\n",
" start_date='2017-01-01', \n",
" end_date = '2017-02-01', \n",
" frequency='minute')"
]
},
{
"cell_type": "code",
"execution_count": 18,
"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>2017-01-03 14:31:00+00:00</th>\n",
" <td>121.86</td>\n",
" <td>121.86</td>\n",
" <td>121.57</td>\n",
" <td>121.63</td>\n",
" <td>123665.0</td>\n",
" <td>121.63</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2017-01-03 14:32:00+00:00</th>\n",
" <td>121.73</td>\n",
" <td>121.92</td>\n",
" <td>121.41</td>\n",
" <td>121.45</td>\n",
" <td>13536.0</td>\n",
" <td>121.45</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2017-01-03 14:33:00+00:00</th>\n",
" <td>121.45</td>\n",
" <td>121.79</td>\n",
" <td>121.45</td>\n",
" <td>121.52</td>\n",
" <td>17562.0</td>\n",
" <td>121.52</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2017-01-03 14:34:00+00:00</th>\n",
" <td>121.45</td>\n",
" <td>121.80</td>\n",
" <td>121.44</td>\n",
" <td>121.75</td>\n",
" <td>12072.0</td>\n",
" <td>121.75</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2017-01-03 14:35:00+00:00</th>\n",
" <td>121.74</td>\n",
" <td>122.00</td>\n",
" <td>121.66</td>\n",
" <td>121.99</td>\n",
" <td>8100.0</td>\n",
" <td>121.99</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" open_price high low close_price volume \\\n",
"2017-01-03 14:31:00+00:00 121.86 121.86 121.57 121.63 123665.0 \n",
"2017-01-03 14:32:00+00:00 121.73 121.92 121.41 121.45 13536.0 \n",
"2017-01-03 14:33:00+00:00 121.45 121.79 121.45 121.52 17562.0 \n",
"2017-01-03 14:34:00+00:00 121.45 121.80 121.44 121.75 12072.0 \n",
"2017-01-03 14:35:00+00:00 121.74 122.00 121.66 121.99 8100.0 \n",
"\n",
" price \n",
"2017-01-03 14:31:00+00:00 121.63 \n",
"2017-01-03 14:32:00+00:00 121.45 \n",
"2017-01-03 14:33:00+00:00 121.52 \n",
"2017-01-03 14:34:00+00:00 121.75 \n",
"2017-01-03 14:35:00+00:00 121.99 "
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mcdon.head()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'pandas.core.frame.DataFrame'>\n",
"DatetimeIndex: 8190 entries, 2017-01-03 14:31:00+00:00 to 2017-02-01 21:00:00+00:00\n",
"Data columns (total 6 columns):\n",
"open_price 8188 non-null float64\n",
"high 8188 non-null float64\n",
"low 8188 non-null float64\n",
"close_price 8188 non-null float64\n",
"volume 8190 non-null float64\n",
"price 8190 non-null float64\n",
"dtypes: float64(6)\n",
"memory usage: 447.9 KB\n"
]
}
],
"source": [
"mcdon.info()"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"# On ne peut revenir que 12 ans en arrière\n",
"# ce qui est vraiment tout ce dont vous avez besoin pour l'algo de trading', \n",
"# remonter plus loin est probablement plus du bruit que du signal.\n",
"\n",
"mcdon = get_pricing('MCD',\n",
" start_date='2005-01-01', \n",
" end_date = '2017-01-01', \n",
" frequency='daily')"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.axes._subplots.AxesSubplot at 0x7f98b62cdc90>"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAz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seFcydRcLH16f0eBzOPP/44+vfvjyVLlgAAYmJiRJmYoqIipKamtnqftLQ0T7tAfiIjI4PvAfE9\nIAHfBQL4HpAF3wNnO88eBGAJLlz9bDYc/BVALWZNHdvqfBkAUIeX4bvdF3DmYgUKyuoQFRaA39w0\nFftX/gSjSYfRqWOw/dRBAJW4cnoqtmXkAifPYtDgIRjev+Wyye4yGk0o++I7UdukCWOFKmwtBWMe\nlWbesGEDlEolHnjgAaEtJSUFx44dQ21tLerq6nDo0CG+fEREREREXlTfYBC2XY2EulRSi7BglVuB\nDACMGBCJvyxKw7jhsQAgVBHLKawBAGzZdxHbD+YhWKNEQkyQfZiZl+bMGIwmLH5mM+ocPheAFstJ\nO2o1M5OZmYnly5ejvLwcMpkMH3/8MUwmE1QqFRYvXgyJRIKkpCQ89dRTWLZsGe666y5IpVI8+OCD\nCAoK8uxTERERERGRk/oGvbBtMJqhcJjArzeYUFxej6H9Ilxd2qJFVw5DYlwwZo5NFLW/+tlhAEBo\nkBISiQQymbUAgJfmzFwqqUVNvXi+zIy0BLevbzWYSUlJwcaNG926WXp6OtLT091+OBERERERuc8x\ng6E3GKFwWEemvkEPkxkIDWr7XJYAlRxzJ/YT9ieMiMPe4/bqZvfeMAoAhGpm3srMFJXXO7VNGNHL\n7es9GmZGREREREQdr15
"text/plain": [
"<matplotlib.figure.Figure at 0x7f98c634d790>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"mcdon['close_price'].plot()"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.axes._subplots.AxesSubplot at 0x7f98c85a6a10>"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAX4AAAEDCAYAAAAyZm/jAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAGTNJREFUeJzt3XtQVPfdx/EPuwLRBS+LQj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G1dYLChqwfGaR+NMzTRGelSgIgg+AHAMAQ/ABiGPn6gj5i+AQMNwQ/0EdM3YKAh\n+IEQYPoGDCT08QOAYQh+ADAMwQ8AhiH4AcAwBD8AGIbgBwDDBBT8Z86c0fz583XkyBFJUl5enr7/\n/e9r5cqVWrlypf70pz9JkoqKipSdna1ly5bp2LFj4asaABA0v+P4m5ubtWvXLs2cObPL8k2bNmnW\nrFld1tu/f78KCws1ZMgQZWdny+Vyafjw4aGvGgAQNL9n/PHx8XrjjTc0evToW65XUVGh1NRUORwO\nxcfHKz09XWVlZSErFAAQGn6D32azKS4u7qblhw8f1pNPPqnc3FxdunRJNTU1cjr/M82t0+lUdXV1\naKsFAPRZUFM2LF68WCNHjtSUKVOUn5+vffv2KS0trcs6lmUFtC+32x1MCegB7XlrlZWVYb8Pj8ej\n+vr6sN/PQMOxGT2CCv4ZM2Z0/P7AAw9ox44dWrhwoT766KOO5T6f76YXg+5kZGQEUwK64Xa7aU8/\nEhMTpfcvhPU+pk6dyiRtN+DYDK2+vogGNZxzw4YN+sc//iFJKi0t1aRJk5SamiqPx6OGhgY1Njaq\nvLycfzSiRmtrq7744guuswsogDP+iooKbd++XRcvXpTdbldBQYE2bNigvLw8ORwOORwOvfzyy4qP\nj1dubq5ycnJks9m0fv1
"text/plain": [
"<matplotlib.figure.Figure at 0x7f98b6d440d0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"mcdon['close_price'].pct_change(1).hist(bins=100,figsize=(6,4))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## symbols()\n",
"\n",
"By default `symbols` returns the security object for a ticker symbol. Specify a ticker symbol, or list of symbols, as a string and get a list of security objects back. \n",
"\n",
"- Use `symbol_reference_date` to identify which date you want the symbol back for a particular ticker symbol. \n",
"- Specify how you would like missing results to be handled with `handle_missing`\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"mcdon_eq_info = symbols('MCD')"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<type 'zipline.assets._assets.Equity'>"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(mcdon_eq_info)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"end_date\n",
"2017-07-19 00:00:00+00:00\n",
"\n",
"\n",
"exchange\n",
"NYSE\n",
"\n",
"\n",
"auto_close_date\n",
"2017-07-24 00:00:00+00:00\n",
"\n",
"\n",
"symbol\n",
"MCD\n",
"\n",
"\n",
"first_traded\n",
"None\n",
"\n",
"\n",
"asset_name\n",
"MCDONALDS CORP\n",
"\n",
"\n",
"exchange_full\n",
"NEW YORK STOCK EXCHANGE\n",
"\n",
"\n",
"sid\n",
"4707\n",
"\n",
"\n",
"start_date\n",
"2002-01-01 00:00:00+00:00\n",
"\n",
"\n"
]
}
],
"source": [
"for key in mcdon_eq_info.to_dict():\n",
" print(key)\n",
" print(mcdon_eq_info.to_dict()[key])\n",
" print('\\n')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## get_fundamentals()\n",
"\n",
"La fonction `get_fundamentals` fournit un accès programmatique à la base de données fondamentale Quantopian. Basé sur les données fournies par Morningstar, `get_fundamentals` fournit plus de 600 mesures d'entreprises datant de 2002 (pour correspondre aux données de tarification de Quantopian). \n",
"\n",
"Les données utilisées par cette fonction de recherche sont les mêmes que celles utilisées par la fonction `get_fundamentals` utilisée dans l'IDE Quantopian. C'est décrit dans les documents d'aide Quantopian: http://www.quantopian.com/help/fundamentals.\n"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Il faut faire ça d'abord dans le notebook:\n",
"fundamentals = init_fundamentals()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The get_fundamentals() function takes in a SQLAlchemy query which can be quite complicated and strange looking at first. Basically it allows you to filter by a variety of fundamentals (things like Market Cap, P/E Ratio, or even city of HQ). Check out the link above for all the things you can filter by!\n",
"\n",
"Let's walk through a few query examples.\n",
"\n",
"First call fundamentals and use tab to check out the various options:"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (<ipython-input-55-bbf6938c8013>, line 1)",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-55-bbf6938c8013>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m fundamentals. # call tab here as in the video!\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"fundamentals. # call tab here as in the video!"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
"# Market Cap\n",
"my_query = query(fundamentals.valuation.market_cap)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [],
"source": [
"my_funds = get_fundamentals(my_query,'2017-01-01')"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'pandas.core.frame.DataFrame'>\n",
"Index: 1 entries, market_cap to market_cap\n",
"Columns: 5551 entries, Equity(21 [AAME]) to Equity(50554 [HEBT])\n",
"dtypes: float64(5551)\n",
"memory usage: 43.4+ KB\n"
]
}
],
"source": [
"my_funds.info()"
]
},
{
"cell_type": "code",
"execution_count": 64,
"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>Equity(21 [AAME])</th>\n",
" <th>Equity(24 [AAPL])</th>\n",
" <th>Equity(25 [ARNC_PR])</th>\n",
" <th>Equity(31 [ABAX])</th>\n",
" <th>Equity(39 [DDC])</th>\n",
" <th>Equity(41 [ARCB])</th>\n",
" <th>Equity(52 [ABM])</th>\n",
" <th>Equity(53 [ABMD])</th>\n",
" <th>Equity(62 [ABT])</th>\n",
" <th>Equity(64 [ABX])</th>\n",
" <th>...</th>\n",
" <th>Equity(50533 [CNDT])</th>\n",
" <th>Equity(50534 [HGV])</th>\n",
" <th>Equity(50535 [PK])</th>\n",
" <th>Equity(50537 [WRD])</th>\n",
" <th>Equity(50539 [TIG])</th>\n",
" <th>Equity(50540 [TRVG])</th>\n",
" <th>Equity(50541 [KEY_PRI])</th>\n",
" <th>Equity(50544 [KEG])</th>\n",
" <th>Equity(50547 [YTRA])</th>\n",
" <th>Equity(50554 [HEBT])</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>market_cap</th>\n",
" <td>80256300.0</td>\n",
" <td>6.224410e+11</td>\n",
" <td>NaN</td>\n",
" <td>1.190750e+09</td>\n",
" <td>808964000.0</td>\n",
" <td>722555000.0</td>\n",
" <td>2.265560e+09</td>\n",
" <td>4.927210e+09</td>\n",
" <td>5.640420e+10</td>\n",
" <td>1.910140e+10</td>\n",
" <td>...</td>\n",
" <td>3.029760e+09</td>\n",
" <td>2.559150e+09</td>\n",
" <td>5.890500e+09</td>\n",
" <td>1.734880e+09</td>\n",
" <td>2.265930e+09</td>\n",
" <td>2.741480e+09</td>\n",
" <td>NaN</td>\n",
" <td>26639500.0</td>\n",
" <td>329764000.0</td>\n",
" <td>91800000.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>1 rows × 5551 columns</p>\n",
"</div>"
],
"text/plain": [
" Equity(21 [AAME]) Equity(24 [AAPL]) Equity(25 [ARNC_PR]) \\\n",
"market_cap 80256300.0 6.224410e+11 NaN \n",
"\n",
" Equity(31 [ABAX]) Equity(39 [DDC]) Equity(41 [ARCB]) \\\n",
"market_cap 1.190750e+09 808964000.0 722555000.0 \n",
"\n",
" Equity(52 [ABM]) Equity(53 [ABMD]) Equity(62 [ABT]) \\\n",
"market_cap 2.265560e+09 4.927210e+09 5.640420e+10 \n",
"\n",
" Equity(64 [ABX]) ... Equity(50533 [CNDT]) \\\n",
"market_cap 1.910140e+10 ... 3.029760e+09 \n",
"\n",
" Equity(50534 [HGV]) Equity(50535 [PK]) Equity(50537 [WRD]) \\\n",
"market_cap 2.559150e+09 5.890500e+09 1.734880e+09 \n",
"\n",
" Equity(50539 [TIG]) Equity(50540 [TRVG]) \\\n",
"market_cap 2.265930e+09 2.741480e+09 \n",
"\n",
" Equity(50541 [KEY_PRI]) Equity(50544 [KEG]) \\\n",
"market_cap NaN 26639500.0 \n",
"\n",
" Equity(50547 [YTRA]) Equity(50554 [HEBT]) \n",
"market_cap 329764000.0 91800000.0 \n",
"\n",
"[1 rows x 5551 columns]"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Basically just returns the market cap of everything\n",
"# for 2017-01-01\n",
"my_funds.head()"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [],
"source": [
"# What you usualy do is filter by other qualities after the query!\n",
"\n",
"# Only get companies worth 500 billion or more (that's a lot of dough!)\n",
"big_companies = (query(fundamentals.valuation.market_cap).\n",
" filter(fundamentals.valuation.market_cap > 500000000000) )"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"my_big_funds = get_fundamentals(big_companies,'2017-07-19')"
]
},
{
"cell_type": "code",
"execution_count": 76,
"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>Equity(24 [AAPL])</th>\n",
" <th>Equity(5061 [MSFT])</th>\n",
" <th>Equity(26578 [GOOG_L])</th>\n",
" <th>Equity(46631 [GOOG])</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>market_cap</th>\n",
" <td>7.824930e+11</td>\n",
" <td>5.659140e+11</td>\n",
" <td>6.752440e+11</td>\n",
" <td>6.752440e+11</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Equity(24 [AAPL]) Equity(5061 [MSFT]) Equity(26578 [GOOG_L]) \\\n",
"market_cap 7.824930e+11 5.659140e+11 6.752440e+11 \n",
"\n",
" Equity(46631 [GOOG]) \n",
"market_cap 6.752440e+11 "
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# On \n",
"my_big_funds"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"782000000000.0"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"7.82 * 10**11"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "get_fundamentals() takes at least 2 arguments (0 given)",
"output_type": "error",
"traceback": [
"\u001b[0;31m\u001b[0m",
"\u001b[0;31mTypeError\u001b[0mTraceback (most recent call last)",
"\u001b[0;32m<ipython-input-50-68e9d331adfd>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mget_fundamentals\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: get_fundamentals() takes at least 2 arguments (0 given)"
]
}
],
"source": [
"get_fundamentals()"
]
}
],
"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.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}