-
-
Save kevincruzai/7456a9c8067cdf596de1c521bb25cccc to your computer and use it in GitHub Desktop.
Revisions
-
yhilpisch revised this gist
Jan 28, 2018 . 3 changed files with 411 additions and 3375 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -40,7 +40,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -57,7 +57,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -68,47 +68,25 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Sandra.first_name # <2>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Sandra.position # <2>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -119,27 +97,16 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Sandra.position # <4>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -150,20 +117,9 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Sandra.position" ] @@ -184,7 +140,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -195,120 +151,54 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(n) # <2>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n.numerator # <3>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n.bit_length() # <4>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n + n # <5>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2 * n # <6>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n.__sizeof__() # <7>" ] @@ -322,7 +212,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -333,47 +223,25 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(l) # <2>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l[0] # <3>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -384,80 +252,36 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l + l # <5>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2 * l # <6>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sum(l) # <7>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l.__sizeof__() # <8>" ] @@ -471,7 +295,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -482,7 +306,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -493,212 +317,90 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a # <2>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(a) # <3>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.nbytes # <1>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.sum() # <2>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.cumsum(axis=0) # <3>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a + a # <4>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2 * a # <5>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sum(a) # <6>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.sum(a) # <7>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.__sizeof__() # <8>" ] @@ -712,7 +414,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -723,7 +425,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -734,354 +436,72 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(df) # <3>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.columns # <1>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.sum() # <2>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.cumsum() # <3>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df + df # <4>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2 * df # <5>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.sum(df) # <6>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.__sizeof__() # <7>" ] @@ -1099,7 +519,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1111,7 +531,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1122,67 +542,34 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(fi) # <4>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fi # <4>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fi.__str__() # <5>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1193,27 +580,16 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fi.price # <6>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1228,27 +604,16 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "FinancialInstrument.author # <1>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1259,47 +624,25 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "aapl.symbol # <5>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "aapl.author # <6>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1310,27 +653,16 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "aapl.price # <7>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1345,7 +677,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1356,27 +688,16 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fi.get_price() # <6>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1387,47 +708,25 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fi.get_price() # <6>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fi.price # <8>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1445,7 +744,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1456,68 +755,34 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fi.get_price() # <2>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fi.__price # <3>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fi._FinancialInstrument__price # <4>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1528,7 +793,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1539,7 +804,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1560,7 +825,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1571,67 +836,34 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pp.get_position_size()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pp.get_position_value() # <3>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pp.position.get_price() # <4>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1642,20 +874,9 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pp.get_position_value() # <6>" ] @@ -1673,7 +894,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1688,7 +909,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { @@ -1702,31 +923,20 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v # <3>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1739,7 +949,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { @@ -1753,168 +963,83 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v # <1>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(v) # <1>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "abs(-2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "int(False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "int(True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bool(10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bool(0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bool(-1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1931,7 +1056,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { @@ -1945,55 +1070,33 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "abs(v)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "bool(v)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { @@ -2007,113 +1110,56 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v # <3>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "abs(v)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "bool(v)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v + v" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2 * v" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -2134,7 +1180,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { @@ -2148,76 +1194,42 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v + Vector(2, 3, 4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v * 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "len(v)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -2236,7 +1248,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { @@ -2250,101 +1262,55 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "len(v)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v[-2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v[3]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -2358,7 +1324,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { @@ -2372,51 +1338,31 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(3): # <1>\n", " print(v[i]) # <1>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "for coordinate in v: # <2>\n", " print(coordinate) # <2>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -167,7 +167,7 @@ "# event-based view on data = going bar by bar \"through time\"\n", "for bar in range(10):\n", " print(bar, fd.data['AAPL.O'].iloc[bar])\n", " time.sleep(1)" ] }, { @@ -179,7 +179,7 @@ "# event-based view on data = going bar by bar \"through time\"\n", "for bar in range(10):\n", " print(bar, str(fd.data['AAPL.O'].index[bar])[:10], fd.data['AAPL.O'].iloc[bar])\n", " time.sleep(.5)" ] }, { @@ -234,7 +234,16 @@ "metadata": {}, "outputs": [], "source": [ "amount / price # --> vectorized backtesting" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "units = math.floor(amount / price) # --> event-based backtesting\n", "units" ] }, @@ -248,6 +257,15 @@ "cost" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "amount - cost # cash left" ] }, { "cell_type": "code", "execution_count": null, @@ -259,8 +277,8 @@ "class BacktestingBase(FinancialData):\n", " def __init__(self, symbol, amount, verbose=True):\n", " super(BacktestingBase, self).__init__(symbol)\n", " self.amount = amount # current cash balance\n", " self.initial_amount = amount # initial invest/cash\n", " self.verbose = verbose\n", " self.units = 0\n", " self.trades = 0\n", @@ -273,12 +291,12 @@ " def print_balance(self, bar):\n", " date, price = self.get_date_price(bar)\n", " print('%s | current cash balance is %8.2f' % (date, self.amount))\n", " \n", " def place_buy_order(self, bar, units=None, amount=None):\n", " date, price = self.get_date_price(bar)\n", " if amount is not None:\n", " units = math.floor(amount / price)\n", " self.amount -= units * price # here tc can be included\n", " self.units += units\n", " self.trades += 1\n", " if self.verbose is True:\n", @@ -295,7 +313,7 @@ " if self.verbose is True:\n", " print('%s | selling %3d units for %8.2f' % (date, units, price))\n", " self.print_balance(bar)\n", " \n", " def close_out(self, bar):\n", " date, price = self.get_date_price(bar)\n", " self.amount += self.units * price\n", @@ -314,9 +332,7 @@ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bb = BacktestingBase('AAPL.O', 10000)" @@ -337,7 +353,7 @@ "metadata": {}, "outputs": [], "source": [ "bb.get_date_price(177)" ] }, { @@ -346,7 +362,7 @@ "metadata": {}, "outputs": [], "source": [ "bb.print_balance(210)" ] }, { @@ -446,6 +462,7 @@ " self.units = 0\n", " self.trades = 0\n", " self.position = 0\n", " self.amount = self.initial_amount\n", " self.results = self.data.copy()\n", " self.results['SMA1'] = self.results[self.symbol].rolling(SMA1).mean()\n", " self.results['SMA2'] = self.results[self.symbol].rolling(SMA2).mean()\n", @@ -455,8 +472,8 @@ " if self.position == 0:\n", " if self.results['SMA1'].iloc[bar] > self.results['SMA2'].iloc[bar]:\n", " # self.place_buy_order(bar, units=100)\n", " self.place_buy_order(bar, amount=self.amount * 0.8)\n", " # self.place_buy_order(bar, amount=5000)\n", " date, price = self.get_date_price(bar)\n", " self.entry_cost = self.units * price\n", " # place whatever logic reflects your strategy\n", @@ -471,10 +488,11 @@ " else:\n", " date, price = self.get_date_price(bar)\n", " current_position_value = self.units * price\n", " if (current_position_value - self.entry_cost) / self.entry_cost <= -0.05:\n", " self.place_sell_order(bar, units=self.units)\n", " self.position = -2 # position indicating a previous stop\n", " self.entry_cost = 0\n", " self.trades += 1\n", " self.wait_days = 10\n", " if self.verbose:\n", " print('Closing out due to stop loss.')\n", @@ -490,9 +508,7 @@ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sma = LongOnlyBacktest('AAPL.O', 10000, True)" @@ -591,6 +607,7 @@ " self.trades = 0\n", " self.position = 0\n", " self.entry_value = 0\n", " self.amount = self.initial_amount\n", " self.results = self.data.copy()\n", " self.results['SMA1'] = self.results[self.symbol].rolling(SMA1).mean()\n", " self.results['SMA2'] = self.results[self.symbol].rolling(SMA2).mean()\n", @@ -607,9 +624,10 @@ " \n", " if self.position in [0, -1, -2]:\n", " if self.results['SMA1'].iloc[bar] > self.results['SMA2'].iloc[bar]:\n", " if self.position == -1:\n", " self.place_buy_order(bar, amount=-self.units)\n", " # self.place_buy_order(bar, amount=5000)\n", " self.place_buy_order(bar, amount=self.amount * 0.8)\n", " date, price = self.get_date_price(bar)\n", " self.entry_value = self.units * price\n", " self.position = 1\n", @@ -623,13 +641,12 @@ " \n", " elif self.position in [0, 1, 2]:\n", " if self.results['SMA1'].iloc[bar] < self.results['SMA2'].iloc[bar]:\n", " if self.position == 1:\n", " self.place_sell_order(bar, amount=self.units)\n", " # self.place_sell_order(bar, amount=5000)\n", " self.place_sell_order(bar, amount=self.amount * 0.8)\n", " self.entry_value = self.units * price\n", " self.position = -1\n", " elif self.entry_value != 0:\n", " if (current_position_value - self.entry_value) / self.entry_value <= -0.075:\n", " self.place_sell_order(bar, units=self.units)\n", @@ -649,7 +666,7 @@ }, "outputs": [], "source": [ "sma = LongShortBacktest('AAPL.O', 10000, False)" ] }, { -
yhilpisch revised this gist
Jan 27, 2018 . 5 changed files with 822 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,716 @@ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "<img src=\"http://hilpisch.com/tpq_logo.png\" width=\"350px\" align=\"right\">" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# EPAT Session 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Executive Program in Algorithmic Trading**\n", "\n", "**_Event-based Backtesting_**\n", "\n", "Dr. Yves J. Hilpisch | The Python Quants GmbH | http://tpq.io\n", "\n", "<img src=\"http://hilpisch.com/images/tpq_bootcamp.png\" width=\"350px\" align=\"left\">" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic Imports" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "from pylab import plt\n", "plt.style.use('ggplot')\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Financial Data Class" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class FinancialData(object):\n", " def __init__(self, symbol):\n", " self.symbol = symbol\n", " self.prepare_data()\n", " \n", " def prepare_data(self):\n", " self.raw = pd.read_csv('http://hilpisch.com/tr_eikon_eod_data.csv',\n", " index_col=0, parse_dates=True)\n", " self.data = pd.DataFrame(self.raw[self.symbol])\n", " self.data['Returns'] = np.log(self.data / self.data.shift(1))\n", " \n", " def plot_data(self, cols=None):\n", " if cols is None:\n", " cols = [self.symbol]\n", " self.data[cols].plot(figsize=(10, 6), title=self.symbol)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fd = FinancialData('AAPL.O')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fd.data.info()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fd.data.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fd.plot_data()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Event-based View on Data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# vectorized data handling = complete data set in a single step\n", "# fd.data['AAPL.O'].plot(figsize=(10, 6));\n", "fd.plot_data()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for bar in range(10):\n", " print(bar)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import time" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# event-based view on data = going bar by bar \"through time\"\n", "for bar in range(10):\n", " print(bar, fd.data['AAPL.O'].iloc[bar])\n", " time.sleep(.2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# event-based view on data = going bar by bar \"through time\"\n", "for bar in range(10):\n", " print(bar, str(fd.data['AAPL.O'].index[bar])[:10], fd.data['AAPL.O'].iloc[bar])\n", " time.sleep(.2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Backtesting Base Class" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are going to implement a **base class** for event-based backtesting with:\n", "\n", "* `__init__`\n", "* `prepare_data` (`FinancialBase`)\n", "* `plot_data` (`FinancialBase`)\n", "* `get_date_price`\n", "* `print_balance`\n", "* `place_buy_order`\n", "* `place_sell_order`\n", "* `close_out`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import math" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "amount = 5000\n", "price = 27.85" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "units = math.floor(amount / price)\n", "units" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cost = units * price\n", "cost" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class BacktestingBase(FinancialData):\n", " def __init__(self, symbol, amount, verbose=True):\n", " super(BacktestingBase, self).__init__(symbol)\n", " self.amount = amount\n", " self.initial_amount = amount\n", " self.verbose = verbose\n", " self.units = 0\n", " self.trades = 0\n", " \n", " def get_date_price(self, bar):\n", " date = str(self.data[self.symbol].index[bar])[:10]\n", " price = self.data[self.symbol].iloc[bar]\n", " return date, price\n", " \n", " def print_balance(self, bar):\n", " date, price = self.get_date_price(bar)\n", " print('%s | current cash balance is %8.2f' % (date, self.amount))\n", " \n", " def place_buy_order(self, bar, units=None, amount=None):\n", " date, price = self.get_date_price(bar)\n", " if amount is not None:\n", " units = math.floor(amount / price)\n", " self.amount -= units * price\n", " self.units += units\n", " self.trades += 1\n", " if self.verbose is True:\n", " print('%s | buying %3d units for %8.2f' % (date, units, price))\n", " self.print_balance(bar)\n", " \n", " def place_sell_order(self, bar, units=None, amount=None):\n", " date, price = self.get_date_price(bar)\n", " if amount is not None:\n", " units = math.floor(amount / price)\n", " self.amount += units * price\n", " self.units -= units\n", " self.trades += 1\n", " if self.verbose is True:\n", " print('%s | selling %3d units for %8.2f' % (date, units, price))\n", " self.print_balance(bar)\n", " \n", " def close_out(self, bar):\n", " date, price = self.get_date_price(bar)\n", " self.amount += self.units * price\n", " print(50 * '=')\n", " print('Closing out the position.')\n", " print(50 * '=')\n", " if self.units != 0:\n", " self.trades += 1\n", " print('%s | selling %3d units for %8.2f' % (date, self.units, price))\n", " self.units -= self.units\n", " self.print_balance(bar)\n", " perf = ((self.amount - self.initial_amount) / self.initial_amount) * 100\n", " print('%s | net performance %8.2f' % (date, perf))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "bb = BacktestingBase('AAPL.O', 10000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bb.data.info()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bb.get_date_price(209)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bb.print_balance(209)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bb.place_buy_order(209, units=15)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(bb.units, bb.trades)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bb.place_buy_order(260, amount=2000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(bb.units, bb.trades)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bb.place_sell_order(300, units=40)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bb.place_sell_order(350, amount=500)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(bb.units, bb.trades)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bb.close_out(400)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Long Only Backtesting Class" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class LongOnlyBacktest(BacktestingBase):\n", " # def __init__(self, *args):\n", " # super(LongOnlyBacktest, self).__init__(*args)\n", " \n", " def run_strategy(self, SMA1, SMA2):\n", " print('\\n\\nRunning strategy for %s | SMA1=%d | SMA2=%d' % (self.symbol, SMA1, SMA2))\n", " print(50 * '=')\n", " self.units = 0\n", " self.trades = 0\n", " self.position = 0\n", " self.results = self.data.copy()\n", " self.results['SMA1'] = self.results[self.symbol].rolling(SMA1).mean()\n", " self.results['SMA2'] = self.results[self.symbol].rolling(SMA2).mean()\n", " \n", " for bar in range(SMA2 - 1, len(self.results)):\n", " \n", " if self.position == 0:\n", " if self.results['SMA1'].iloc[bar] > self.results['SMA2'].iloc[bar]:\n", " # self.place_buy_order(bar, units=100)\n", " # self.place_buy_order(bar, amount=self.amount)\n", " self.place_buy_order(bar, amount=5000)\n", " date, price = self.get_date_price(bar)\n", " self.entry_cost = self.units * price\n", " # place whatever logic reflects your strategy\n", " self.position = 1\n", " \n", " elif self.position == 1:\n", " if self.results['SMA1'].iloc[bar] < self.results['SMA2'].iloc[bar]:\n", " # self.place_sell_order(bar, units=100)\n", " self.place_sell_order(bar, units=self.units)\n", " self.position = 0\n", " # stop loss logic\n", " else:\n", " date, price = self.get_date_price(bar)\n", " current_position_value = self.units * price\n", " if (self.entry_cost - current_position_value) / self.entry_cost <= -0.05:\n", " self.place_sell_order(bar, units=self.units)\n", " self.position = -2\n", " self.entry_cost = 0\n", " self.wait_days = 10\n", " if self.verbose:\n", " print('Closing out due to stop loss.')\n", " \n", " elif self.position == -2 and self.wait_days > 0:\n", " self.wait_days -= 1\n", " if self.wait_days == 0:\n", " self.position = 0\n", " \n", " self.close_out(bar)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "sma = LongOnlyBacktest('AAPL.O', 10000, True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sma.run_strategy(42, 252)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "sma = LongOnlyBacktest('AAPL.O', 10000, True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sma.run_strategy(42, 252)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sma.run_strategy(30, 180)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from itertools import product" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for sym in sma.raw.columns.values:\n", " print(sym)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for sym in ['AAPL.O', 'MSFT.O']:\n", " sma = LongOnlyBacktest(sym, 10000, False)\n", " for SMA1, SMA2 in product([30, 42], [180, 252]):\n", " sma.run_strategy(SMA1, SMA2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Long-Short Strategies" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class LongShortBacktest(BacktestingBase):\n", " \n", " def run_strategy(self, SMA1, SMA2):\n", " print('\\n\\nRunning strategy for %s | SMA1=%d | SMA2=%d' % (self.symbol, SMA1, SMA2))\n", " print(50 * '=')\n", " self.units = 0\n", " self.trades = 0\n", " self.position = 0\n", " self.entry_value = 0\n", " self.results = self.data.copy()\n", " self.results['SMA1'] = self.results[self.symbol].rolling(SMA1).mean()\n", " self.results['SMA2'] = self.results[self.symbol].rolling(SMA2).mean()\n", " \n", " for bar in range(SMA2 - 1, len(self.results)):\n", " date, price = self.get_date_price(bar)\n", " current_position_value = self.units * price\n", " diff = current_position_value - self.entry_value\n", " rdiff = diff / self.entry_value\n", " rdiff = rdiff if self.position >= 0 else -rdiff\n", " if self.verbose:\n", " print('%s | %8.2f | %8.2f | %8.3f | %7.3f' %\n", " (date, self.entry_value, current_position_value, diff, rdiff))\n", " \n", " if self.position in [0, -1, -2]:\n", " if self.results['SMA1'].iloc[bar] > self.results['SMA2'].iloc[bar]:\n", " self.place_buy_order(bar, amount=5000)\n", " if self.position == -1:\n", " self.place_buy_order(bar, amount=5000)\n", " date, price = self.get_date_price(bar)\n", " self.entry_value = self.units * price\n", " self.position = 1\n", " elif self.entry_value != 0:\n", " if (current_position_value - self.entry_value) / -self.entry_value <= -0.075:\n", " self.place_buy_order(bar, units=-self.units)\n", " self.position = -2\n", " self.entry_value = 0\n", " if self.verbose:\n", " print('Closing out short position due to stop loss.')\n", " \n", " elif self.position in [0, 1, 2]:\n", " if self.results['SMA1'].iloc[bar] < self.results['SMA2'].iloc[bar]:\n", " # self.place_sell_order(bar, units=100)\n", " self.place_sell_order(bar, units=self.units)\n", " if self.position == 1:\n", " self.place_sell_order(bar, amount=5000)\n", " self.entry_value = self.units * price\n", " self.position = -1\n", " # stop loss logic\n", " elif self.entry_value != 0:\n", " if (current_position_value - self.entry_value) / self.entry_value <= -0.075:\n", " self.place_sell_order(bar, units=self.units)\n", " self.position = 2\n", " self.entry_value = 0\n", " if self.verbose:\n", " print('Closing out long position due to stop loss.')\n", " \n", " self.close_out(bar)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "sma = LongShortBacktest('AAPL.O', 10000, True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sma.run_strategy(42, 252)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for sym in ['AAPL.O', 'MSFT.O']:\n", " sma = LongShortBacktest(sym, 10000, False)\n", " for SMA1, SMA2 in product([30, 42], [180, 252]):\n", " sma.run_strategy(SMA1, SMA2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some improvements (as an exercise):\n", "\n", "* include different signals (momentum)\n", "* include proportional and fixed transaction costs\n", "* allow for different time periods for the backtest" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<img src=\"http://hilpisch.com/tpq_logo.png\" width=\"350px\" align=\"right\">" ] } ], "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.6.1" } }, "nbformat": 4, "nbformat_minor": 2 } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ # # Simple Tick Data Client # import zmq import datetime context = zmq.Context() socket = context.socket(zmq.SUB) socket.connect('tcp://127.0.0.1:5555') socket.setsockopt_string(zmq.SUBSCRIBE, '') while True: msg = socket.recv_string() t = datetime.datetime.now() print(str(t) + ' | ' + msg) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,33 @@ # # Simple Tick Data Collector # import zmq import datetime import pandas as pd context = zmq.Context() socket = context.socket(zmq.SUB) socket.connect('tcp://127.0.0.1:5555') socket.setsockopt_string(zmq.SUBSCRIBE, '') raw = pd.DataFrame() while True: msg = socket.recv_string() t = datetime.datetime.now() print(str(t) + ' | ' + msg) symbol, price = msg.split() raw = raw.append(pd.DataFrame({'SYM': symbol, 'PRICE': price}, index=[t])) data = raw.resample('5s', label='right').last() if len(data) % 4 == 0: print(50 * '=') print(data.tail()) print(50 * '=') # simple way of storing data, needs to be adjusted for your purposes if len(data) % 20 == 0: # h5 = pd.HDFStore('database.h5', 'a') # h5['data'] = data # h5.close() pass This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ # # Simple Tick Data Plotter with ZeroMQ & http://plot.ly # import zmq import datetime import plotly.plotly as ply from plotly.graph_objs import * import configparser # credentials c = configparser.ConfigParser() c.read('../pyalgo.cfg') stream_ids = c['plotly']['api_tokens'].split(',') # socket context = zmq.Context() socket = context.socket(zmq.SUB) socket.connect('tcp://127.0.0.1:5555') socket.setsockopt_string(zmq.SUBSCRIBE, '') # plotting s = Stream(maxpoints=100, token=stream_ids[0]) tr = Scatter(x=[], y=[], name='tick data', mode='lines+markers', stream=s) d = Data([tr]) l = Layout(title='EPAT Tick Data Example') f = Figure(data=d, layout=l) ply.plot(f, filename='epat_example', auto_open=True) st = ply.Stream(stream_ids[0]) st.open() while True: msg = socket.recv_string() t = datetime.datetime.now() print(str(t) + ' | ' + msg) sym, value = msg.split() st.write({'x': t, 'y': float(value)}) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ # # Simple Tick Data Server # import zmq import time import random context = zmq.Context() socket = context.socket(zmq.PUB) socket.bind('tcp://127.0.0.1:5555') AAPL = 100. while True: AAPL += random.gauss(0, 1) * 0.5 msg = 'AAPL %.3f' % AAPL socket.send_string(msg) print(msg) time.sleep(random.random() * 2) -
yhilpisch revised this gist
Jan 27, 2018 . 1 changed file with 2135 additions and 177 deletions.There are no files selected for viewing
-
yhilpisch revised this gist
Jan 27, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -28,7 +28,7 @@ "\n", "Dr. Yves J. Hilpisch | The Python Quants GmbH | http://tpq.io\n", "\n", "<img src=\"http://hilpisch.com/images/tpq_bootcamp.png\" width=\"350px\" align=\"left\">" ] }, { -
yhilpisch revised this gist
Jan 27, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -24,7 +24,7 @@ "\n", "Dr. Yves J. Hilpisch | The Python Quants GmbH | http://tpq.io\n", "\n", "<img src=\"http://hilpisch.com/images/tpq_bootcamp.png\" width=\"350px\" align=\"left\">" ] }, { -
yhilpisch revised this gist
Jan 27, 2018 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ "<img src=\"http://hilpisch.com/tpq_logo.png\" alt=\"The Python Quants\" width=\"35%\" align=\"right\" border=\"0\"><br>" ] }, { @@ -1041,7 +1041,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ "<img src=\"http://hilpisch.com/tpq_logo.png\" alt=\"The Python Quants\" width=\"35%\" align=\"right\" border=\"0\"><br>" ] } ], -
yhilpisch revised this gist
Jan 27, 2018 . 1 changed file with 1317 additions and 253 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -40,7 +40,7 @@ }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, @@ -55,16 +55,9 @@ " self.position += steps # <7>" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, @@ -75,25 +68,47 @@ }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Sandra'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sandra.first_name # <2>" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sandra.position # <2>" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, @@ -104,13 +119,55 @@ }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sandra.position # <4>" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "Sandra.walk_steps(-2)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sandra.position" ] }, { "cell_type": "markdown", "metadata": {}, @@ -127,7 +184,7 @@ }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, @@ -138,54 +195,120 @@ }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(n) # <2>" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n.numerator # <3>" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n.bit_length() # <4>" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n + n # <5>" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * n # <6>" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "28" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n.__sizeof__() # <7>" ] @@ -199,7 +322,7 @@ }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, @@ -210,25 +333,47 @@ }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(l) # <2>" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[0] # <3>" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": true }, @@ -239,36 +384,80 @@ }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 10, 1, 2, 3, 4, 10]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l + l # <5>" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 10, 1, 2, 3, 4, 10]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * l # <6>" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(l) # <7>" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "104" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.__sizeof__() # <8>" ] @@ -282,7 +471,7 @@ }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": true }, @@ -293,7 +482,7 @@ }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": true }, @@ -304,97 +493,212 @@ }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3],\n", " [ 4, 5, 6, 7],\n", " [ 8, 9, 10, 11],\n", " [12, 13, 14, 15]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a # <2>" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a) # <3>" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "128" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.nbytes # <1>" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "120" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.sum() # <2>" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3],\n", " [ 4, 6, 8, 10],\n", " [12, 15, 18, 21],\n", " [24, 28, 32, 36]])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.cumsum(axis=0) # <3>" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 2, 4, 6],\n", " [ 8, 10, 12, 14],\n", " [16, 18, 20, 22],\n", " [24, 26, 28, 30]])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a + a # <4>" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 2, 4, 6],\n", " [ 8, 10, 12, 14],\n", " [16, 18, 20, 22],\n", " [24, 26, 28, 30]])" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * a # <5>" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([24, 28, 32, 36])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(a) # <6>" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "120" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sum(a) # <7>" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "112" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.__sizeof__() # <8>" ] @@ -408,7 +712,7 @@ }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": true }, @@ -419,7 +723,7 @@ }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": true }, @@ -430,79 +734,354 @@ }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "pandas.core.frame.DataFrame" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(df) # <3>" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['a', 'b', 'c', 'd'], dtype='object')" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.columns # <1>" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "a 24\n", "b 28\n", "c 32\n", "d 36\n", "dtype: int64" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.sum() # <2>" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<div>\n", "<style scoped>\n", " .dataframe tbody tr th:only-of-type {\n", " vertical-align: middle;\n", " }\n", "\n", " .dataframe tbody tr th {\n", " vertical-align: top;\n", " }\n", "\n", " .dataframe thead th {\n", " text-align: right;\n", " }\n", "</style>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>a</th>\n", " <th>b</th>\n", " <th>c</th>\n", " <th>d</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>0</th>\n", " <td>0</td>\n", " <td>1</td>\n", " <td>2</td>\n", " <td>3</td>\n", " </tr>\n", " <tr>\n", " <th>1</th>\n", " <td>4</td>\n", " <td>6</td>\n", " <td>8</td>\n", " <td>10</td>\n", " </tr>\n", " <tr>\n", " <th>2</th>\n", " <td>12</td>\n", " <td>15</td>\n", " <td>18</td>\n", " <td>21</td>\n", " </tr>\n", " <tr>\n", " <th>3</th>\n", " <td>24</td>\n", " <td>28</td>\n", " <td>32</td>\n", " <td>36</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "</div>" ], "text/plain": [ " a b c d\n", "0 0 1 2 3\n", "1 4 6 8 10\n", "2 12 15 18 21\n", "3 24 28 32 36" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.cumsum() # <3>" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<div>\n", "<style scoped>\n", " .dataframe tbody tr th:only-of-type {\n", " vertical-align: middle;\n", " }\n", "\n", " .dataframe tbody tr th {\n", " vertical-align: top;\n", " }\n", "\n", " .dataframe thead th {\n", " text-align: right;\n", " }\n", "</style>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>a</th>\n", " <th>b</th>\n", " <th>c</th>\n", " <th>d</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>0</th>\n", " <td>0</td>\n", " <td>2</td>\n", " <td>4</td>\n", " <td>6</td>\n", " </tr>\n", " <tr>\n", " <th>1</th>\n", " <td>8</td>\n", " <td>10</td>\n", " <td>12</td>\n", " <td>14</td>\n", " </tr>\n", " <tr>\n", " <th>2</th>\n", " <td>16</td>\n", " <td>18</td>\n", " <td>20</td>\n", " <td>22</td>\n", " </tr>\n", " <tr>\n", " <th>3</th>\n", " <td>24</td>\n", " <td>26</td>\n", " <td>28</td>\n", " <td>30</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "</div>" ], "text/plain": [ " a b c d\n", "0 0 2 4 6\n", "1 8 10 12 14\n", "2 16 18 20 22\n", "3 24 26 28 30" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df + df # <4>" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<div>\n", "<style scoped>\n", " .dataframe tbody tr th:only-of-type {\n", " vertical-align: middle;\n", " }\n", "\n", " .dataframe tbody tr th {\n", " vertical-align: top;\n", " }\n", "\n", " .dataframe thead th {\n", " text-align: right;\n", " }\n", "</style>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>a</th>\n", " <th>b</th>\n", " <th>c</th>\n", " <th>d</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>0</th>\n", " <td>0</td>\n", " <td>2</td>\n", " <td>4</td>\n", " <td>6</td>\n", " </tr>\n", " <tr>\n", " <th>1</th>\n", " <td>8</td>\n", " <td>10</td>\n", " <td>12</td>\n", " <td>14</td>\n", " </tr>\n", " <tr>\n", " <th>2</th>\n", " <td>16</td>\n", " <td>18</td>\n", " <td>20</td>\n", " <td>22</td>\n", " </tr>\n", " <tr>\n", " <th>3</th>\n", " <td>24</td>\n", " <td>26</td>\n", " <td>28</td>\n", " <td>30</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "</div>" ], "text/plain": [ " a b c d\n", "0 0 2 4 6\n", "1 8 10 12 14\n", "2 16 18 20 22\n", "3 24 26 28 30" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * df # <5>" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "a 24\n", "b 28\n", "c 32\n", "d 36\n", "dtype: int64" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sum(df) # <6>" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "208" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.__sizeof__() # <7>" ] @@ -520,7 +1099,7 @@ }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": true }, @@ -532,7 +1111,7 @@ }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": true }, @@ -543,34 +1122,67 @@ }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "__main__.FinancialInstrument" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(fi) # <4>" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<__main__.FinancialInstrument at 0x10f894ba8>" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi # <4>" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'<__main__.FinancialInstrument object at 0x10f894ba8>'" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi.__str__() # <5>" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": true }, @@ -581,23 +1193,27 @@ }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi.price # <6>" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": true }, @@ -612,16 +1228,27 @@ }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Yves Hilpisch'" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "FinancialInstrument.author # <1>" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "collapsed": true }, @@ -632,25 +1259,47 @@ }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'AAPL'" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aapl.symbol # <5>" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Yves Hilpisch'" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aapl.author # <6>" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": true }, @@ -661,23 +1310,27 @@ }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "105" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aapl.price # <7>" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "collapsed": true }, @@ -692,7 +1345,7 @@ }, { "cell_type": "code", "execution_count": 61, "metadata": { "collapsed": true }, @@ -703,16 +1356,27 @@ }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi.get_price() # <6>" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "collapsed": true }, @@ -723,32 +1387,47 @@ }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "105" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi.get_price() # <6>" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "105" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi.price # <8>" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": true }, @@ -766,7 +1445,7 @@ }, { "cell_type": "code", "execution_count": 67, "metadata": { "collapsed": true }, @@ -777,34 +1456,68 @@ }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi.get_price() # <2>" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'FinancialInstrument' object has no attribute '__price'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-69-74c0dc05c9ae>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfi\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__price\u001b[0m \u001b[0;31m# <3>\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: 'FinancialInstrument' object has no attribute '__price'" ] } ], "source": [ "fi.__price # <3>" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi._FinancialInstrument__price # <4>" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "collapsed": true }, @@ -815,7 +1528,7 @@ }, { "cell_type": "code", "execution_count": 72, "metadata": { "collapsed": true }, @@ -824,16 +1537,9 @@ "fi.set_price(100) # <5>" ] }, { "cell_type": "code", "execution_count": 73, "metadata": { "collapsed": true }, @@ -854,7 +1560,7 @@ }, { "cell_type": "code", "execution_count": 74, "metadata": { "collapsed": true }, @@ -865,34 +1571,67 @@ }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pp.get_position_size()" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1000" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pp.get_position_value() # <3>" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pp.position.get_price() # <4>" ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "collapsed": true }, @@ -903,9 +1642,20 @@ }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1050" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pp.get_position_value() # <6>" ] @@ -923,7 +1673,7 @@ }, { "cell_type": "code", "execution_count": 80, "metadata": { "collapsed": true }, @@ -938,7 +1688,7 @@ }, { "cell_type": "code", "execution_count": 81, "metadata": { "collapsed": true, "slideshow": { @@ -952,27 +1702,31 @@ }, { "cell_type": "code", "execution_count": 82, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "<__main__.Vector at 0x10f8d3e80>" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v # <3>" ] }, { "cell_type": "code", "execution_count": 83, "metadata": { "collapsed": true }, @@ -985,7 +1739,7 @@ }, { "cell_type": "code", "execution_count": 84, "metadata": { "collapsed": true, "slideshow": { @@ -999,36 +1753,168 @@ }, { "cell_type": "code", "execution_count": 85, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "Vector(1, 2, 3)" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v # <1>" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Vector(1, 2, 3)\n" ] } ], "source": [ "print(v) # <1>" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(-2)" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(False)" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(True)" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(10)" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(0)" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(-1)" ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "collapsed": true }, @@ -1045,7 +1931,7 @@ }, { "cell_type": "code", "execution_count": 94, "metadata": { "collapsed": true, "slideshow": { @@ -1059,33 +1945,55 @@ }, { "cell_type": "code", "execution_count": 95, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "2.449489742783178" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(v)" ] }, { "cell_type": "code", "execution_count": 96, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(v)" ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "collapsed": true, "slideshow": { @@ -1099,45 +2007,113 @@ }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Vector(0, 0, 0)" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v # <3>" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(v)" ] }, { "cell_type": "code", "execution_count": 100, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(v)" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for +: 'Vector' and 'Vector'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-101-a22d03764239>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mv\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mv\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'Vector' and 'Vector'" ] } ], "source": [ "v + v" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for *: 'int' and 'Vector'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-102-6431819f7bfe>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m2\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mv\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for *: 'int' and 'Vector'" ] } ], "source": [ "2 * v" ] }, { "cell_type": "code", "execution_count": 103, "metadata": { "collapsed": true }, @@ -1158,7 +2134,7 @@ }, { "cell_type": "code", "execution_count": 104, "metadata": { "collapsed": true, "slideshow": { @@ -1172,40 +2148,76 @@ }, { "cell_type": "code", "execution_count": 105, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "Vector(3, 5, 7)" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v + Vector(2, 3, 4)" ] }, { "cell_type": "code", "execution_count": 106, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "Vector(2, 4, 6)" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v * 2" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "object of type 'Vector' has no len()", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-107-89a6b4061ae0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mv\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: object of type 'Vector' has no len()" ] } ], "source": [ "len(v)" ] }, { "cell_type": "code", "execution_count": 108, "metadata": { "collapsed": true }, @@ -1224,7 +2236,7 @@ }, { "cell_type": "code", "execution_count": 109, "metadata": { "collapsed": true, "slideshow": { @@ -1238,62 +2250,101 @@ }, { "cell_type": "code", "execution_count": 110, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 110, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(v)" ] }, { "cell_type": "code", "execution_count": 111, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v[0]" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v[-2]" ] }, { "cell_type": "code", "execution_count": 113, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "IndexError", "evalue": "Index out of range.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-113-0f5531c4b93d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mv\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m<ipython-input-108-eef2cdc22510>\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, i)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mz\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mIndexError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Index out of range.'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: Index out of range." ] } ], "source": [ "v[3]" ] }, { "cell_type": "code", "execution_count": 114, "metadata": { "collapsed": true }, @@ -1307,7 +2358,7 @@ }, { "cell_type": "code", "execution_count": 115, "metadata": { "collapsed": true, "slideshow": { @@ -1321,38 +2372,51 @@ }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for i in range(3): # <1>\n", " print(v[i]) # <1>" ] }, { "cell_type": "code", "execution_count": 117, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for coordinate in v: # <2>\n", " print(coordinate) # <2>" ] }, { "cell_type": "code", "execution_count": 118, "metadata": { "collapsed": true }, -
yhilpisch revised this gist
Jan 27, 2018 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,6 +4,7 @@ Executive Program in Algorithmic Trading (QuantInsti) Python Sessions by Dr. Yves J. Hilpisch | The Python Quants GmbH Online, 27. & 28. January 2018 <img src="http://hilpisch.com/images/finaince_visual_low.png" width=300px> -
yhilpisch revised this gist
Jan 27, 2018 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,8 +3,7 @@ Executive Program in Algorithmic Trading (QuantInsti) Python Sessions by Dr. Yves J. Hilpisch | The Python Quants GmbH Online, 27. & 28. January 2018 <img src="http://hilpisch.com/images/finaince_visual_low.png" width=300px> -
yhilpisch revised this gist
Jan 27, 2018 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -39,17 +39,17 @@ The code that follows uses Python 3.6. For example, download and install **Minic In any case, for **Linux/Mac** you should execute the following lines on the shell to create a new environment with the needed packages: conda create -n epat python=3.6 source activate epat conda install numpy pandas matplotlib statsmodels pip install plotly cufflinks conda install ipython jupyter jupyter notebook On **Windows**, execute the following lines on the command prompt: conda create -n epat python=3.6 activate epat conda install numpy pandas matplotlib statsmodels pip install plotly cufflinks pip install win-unicode-console -
yhilpisch revised this gist
Jan 27, 2018 . 3 changed files with 336 additions and 2985 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -59,6 +59,13 @@ On **Windows**, execute the following lines on the command prompt: Read more about the management of environments under https://conda.io/docs/using/envs.html Docker ------ To install **Docker** see https://docs.docker.com/install/ docker run -ti -p 9000:9000 -h epat -v /Users/yves/Temp/:/root/ ubuntu:latest /bin/bash ZeroMQ ------ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -40,7 +40,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -64,7 +64,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -75,47 +75,25 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Sandra.first_name # <2>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Sandra.position # <2>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -126,20 +104,9 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Sandra.position # <4>" ] @@ -160,7 +127,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -171,120 +138,54 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(n) # <2>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n.numerator # <3>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n.bit_length() # <4>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n + n # <5>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2 * n # <6>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n.__sizeof__() # <7>" ] @@ -298,7 +199,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -309,47 +210,25 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(l) # <2>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l[0] # <3>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -360,80 +239,36 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l + l # <5>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2 * l # <6>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sum(l) # <7>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "l.__sizeof__() # <8>" ] @@ -447,7 +282,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -458,7 +293,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -469,43 +304,18 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a # <2>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(a) # <3>" ] @@ -519,169 +329,72 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.nbytes # <1>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.sum() # <2>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.cumsum(axis=0) # <3>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a + a # <4>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2 * a # <5>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sum(a) # <6>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.sum(a) # <7>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.__sizeof__() # <8>" ] @@ -695,7 +408,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -706,7 +419,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -717,20 +430,9 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(df) # <3>" ] @@ -744,334 +446,63 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.columns # <1>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.sum() # <2>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.cumsum() # <3>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df + df # <4>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2 * df # <5>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.sum(df) # <6>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.__sizeof__() # <7>" ] @@ -1089,7 +520,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1101,7 +532,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1112,67 +543,34 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(fi) # <4>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fi # <4>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fi.__str__() # <5>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1183,20 +581,9 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fi.price # <6>" ] @@ -1210,7 +597,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1225,27 +612,16 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "FinancialInstrument.author # <1>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1256,47 +632,25 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "aapl.symbol # <5>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "aapl.author # <6>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1307,20 +661,9 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "aapl.price # <7>" ] @@ -1334,7 +677,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1349,7 +692,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1360,27 +703,16 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fi.get_price() # <6>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1391,40 +723,18 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fi.get_price() # <6>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fi.price # <8>" ] @@ -1438,7 +748,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1456,7 +766,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1467,68 +777,34 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fi.get_price() # <2>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fi.__price # <3>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fi._FinancialInstrument__price # <4>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1539,7 +815,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1557,7 +833,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1578,7 +854,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1589,67 +865,34 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pp.get_position_size()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pp.get_position_value() # <3>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pp.position.get_price() # <4>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1660,20 +903,9 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pp.get_position_value() # <6>" ] @@ -1691,7 +923,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1706,7 +938,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { @@ -1720,24 +952,13 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v # <3>" ] @@ -1751,7 +972,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1764,7 +985,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { @@ -1778,41 +999,22 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v # <1>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(v) # <1>" ] @@ -1826,7 +1028,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -1843,7 +1045,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { @@ -1857,55 +1059,33 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "abs(v)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "bool(v)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { @@ -1919,64 +1099,31 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v # <3>" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "abs(v)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "bool(v)" ] @@ -1990,7 +1137,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -2011,7 +1158,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { @@ -2025,48 +1172,26 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v + Vector(2, 3, 4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v * 2" ] @@ -2080,7 +1205,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -2099,7 +1224,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { @@ -2113,94 +1238,48 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "len(v)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v[-2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v[3]" ] @@ -2214,7 +1293,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, @@ -2228,7 +1307,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { @@ -2242,43 +1321,23 @@ }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(3): # <1>\n", " print(v[i]) # <1>" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "for coordinate in v: # <2>\n", " print(coordinate) # <2>" @@ -2293,7 +1352,7 @@ }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, -
yhilpisch revised this gist
Jan 27, 2018 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,6 +8,11 @@ Online, 27. & 28. October 2018 <img src="http://hilpisch.com/images/finaince_visual_low.png" width=300px> Short Link ---------- https://goo.gl/gc6TYW Resources --------- -
yhilpisch revised this gist
Jan 27, 2018 . No changes.There are no files selected for viewing
-
yhilpisch created this gist
Jan 27, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,75 @@ Executive Program in Algorithmic Trading (QuantInsti) ===================================================== Python Sessions by Dr. Yves J. Hilpisch | The Python Quants GmbH Online, 27. & 28. October 2018 <img src="http://hilpisch.com/images/finaince_visual_low.png" width=300px> Resources --------- * http://tpq.io * http://hilpisch.com * http://twitter.com/dyjh * http://certificate.tpq.io Slides & Materials ------------------ You find the introduction slides under http://hilpisch.com/epat.pdf You find the materials about OOP under http://hilpisch.com/py4fi_oop_epat.html Python ------ If you have either Miniconda or Anaconda already installed, there is no need to install anything new. The code that follows uses Python 3.6. For example, download and install **Miniconda 3.6** from https://conda.io/miniconda.html if you do not have `conda` already installed. In any case, for **Linux/Mac** you should execute the following lines on the shell to create a new environment with the needed packages: conda create -n fxcm python=3.6 source activate fxcm conda install numpy pandas matplotlib statsmodels pip install plotly cufflinks conda install ipython jupyter jupyter notebook On **Windows**, execute the following lines on the command prompt: conda create -n fxcm python=3.6 activate fxcm conda install numpy pandas matplotlib statsmodels pip install plotly cufflinks pip install win-unicode-console set PYTHONIOENCODING=UTF-8 conda install ipython jupyter jupyter notebook Read more about the management of environments under https://conda.io/docs/using/envs.html ZeroMQ ------ The major resource for the `ZeroMQ` distributed messaging package based on sockets is http://zeromq.org/ Cloud ----- Use this link to get a 10 USD bonus on **[DigitalOcean](https://m.do.co/c/fbe512dd3dac)** when signing up for a new account. Books ----- Good book about everything important in Python data analysis: [Python Data Science Handbook, O'Reilly](http://shop.oreilly.com/product/0636920034919.do) Good book covering object-oriented programming in Python: [Fluent Python, O'Reilly](http://shop.oreilly.com/product/0636920032519.do) <img src="http://hilpisch.com/tpq_logo.png" width=250px> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,2376 @@ { "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "<img src=\"http://hilpisch.com/tpq_logo.png\" alt=\"The Python Quants\" width=\"35%\" align=\"right\" border=\"0\"><br>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# EPAT Session 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Executive Program in Algorithmic Trading**\n", "\n", "**_Basics of Object Orientation_**\n", "\n", "Dr. Yves J. Hilpisch | The Python Quants GmbH | http://tpq.io\n", "\n", "<img src=\"http://hilpisch.com/images/tpq_bootcamp.png\" width=350px align=left>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class HumanBeing(object): # <1>\n", " def __init__(self, first_name, eye_color): # <2>\n", " self.first_name = first_name # <3>\n", " self.eye_color = eye_color # <4>\n", " self.position = 0 # <5>\n", " def walk_steps(self, steps): # <6>\n", " self.position += steps # <7>" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "# tag::OOP_02[]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "Sandra = HumanBeing('Sandra', 'blue') # <1>" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Sandra'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sandra.first_name # <2>" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sandra.position # <2>" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "Sandra.walk_steps(5) # <3>" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sandra.position # <4>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A Brief Look at Standard Objects" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### int" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "n = 5 # <1>" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(n) # <2>" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n.numerator # <3>" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n.bit_length() # <4>" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n + n # <5>" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * n # <6>" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "28" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n.__sizeof__() # <7>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### list" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "l = [1, 2, 3, 4] # <1>" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(l) # <2>" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[0] # <3>" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": true }, "outputs": [], "source": [ "l.append(10) # <4>" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 10, 1, 2, 3, 4, 10]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l + l # <5>" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 10, 1, 2, 3, 4, 10]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * l # <6>" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(l) # <7>" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "104" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.__sizeof__() # <8>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ndarray" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np # <1>" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = np.arange(16).reshape((4, 4)) # <2>" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3],\n", " [ 4, 5, 6, 7],\n", " [ 8, 9, 10, 11],\n", " [12, 13, 14, 15]])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a # <2>" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a) # <3>" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "# tag::OOP_06[]" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "128" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.nbytes # <1>" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "120" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.sum() # <2>" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3],\n", " [ 4, 6, 8, 10],\n", " [12, 15, 18, 21],\n", " [24, 28, 32, 36]])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.cumsum(axis=0) # <3>" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 2, 4, 6],\n", " [ 8, 10, 12, 14],\n", " [16, 18, 20, 22],\n", " [24, 26, 28, 30]])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a + a # <4>" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 2, 4, 6],\n", " [ 8, 10, 12, 14],\n", " [16, 18, 20, 22],\n", " [24, 26, 28, 30]])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * a # <5>" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([24, 28, 32, 36])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(a) # <6>" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "120" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sum(a) # <7>" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "112" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.__sizeof__() # <8>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### DataFrame" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd # <1>" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": true }, "outputs": [], "source": [ "df = pd.DataFrame(a, columns=list('abcd')) # <2>" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "pandas.core.frame.DataFrame" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(df) # <3>" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "# tag::OOP_08[]" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['a', 'b', 'c', 'd'], dtype='object')" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.columns # <1>" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "a 24\n", "b 28\n", "c 32\n", "d 36\n", "dtype: int64" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.sum() # <2>" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<div>\n", "<style scoped>\n", " .dataframe tbody tr th:only-of-type {\n", " vertical-align: middle;\n", " }\n", "\n", " .dataframe tbody tr th {\n", " vertical-align: top;\n", " }\n", "\n", " .dataframe thead th {\n", " text-align: right;\n", " }\n", "</style>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>a</th>\n", " <th>b</th>\n", " <th>c</th>\n", " <th>d</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>0</th>\n", " <td>0</td>\n", " <td>1</td>\n", " <td>2</td>\n", " <td>3</td>\n", " </tr>\n", " <tr>\n", " <th>1</th>\n", " <td>4</td>\n", " <td>6</td>\n", " <td>8</td>\n", " <td>10</td>\n", " </tr>\n", " <tr>\n", " <th>2</th>\n", " <td>12</td>\n", " <td>15</td>\n", " <td>18</td>\n", " <td>21</td>\n", " </tr>\n", " <tr>\n", " <th>3</th>\n", " <td>24</td>\n", " <td>28</td>\n", " <td>32</td>\n", " <td>36</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "</div>" ], "text/plain": [ " a b c d\n", "0 0 1 2 3\n", "1 4 6 8 10\n", "2 12 15 18 21\n", "3 24 28 32 36" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.cumsum() # <3>" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<div>\n", "<style scoped>\n", " .dataframe tbody tr th:only-of-type {\n", " vertical-align: middle;\n", " }\n", "\n", " .dataframe tbody tr th {\n", " vertical-align: top;\n", " }\n", "\n", " .dataframe thead th {\n", " text-align: right;\n", " }\n", "</style>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>a</th>\n", " <th>b</th>\n", " <th>c</th>\n", " <th>d</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>0</th>\n", " <td>0</td>\n", " <td>2</td>\n", " <td>4</td>\n", " <td>6</td>\n", " </tr>\n", " <tr>\n", " <th>1</th>\n", " <td>8</td>\n", " <td>10</td>\n", " <td>12</td>\n", " <td>14</td>\n", " </tr>\n", " <tr>\n", " <th>2</th>\n", " <td>16</td>\n", " <td>18</td>\n", " <td>20</td>\n", " <td>22</td>\n", " </tr>\n", " <tr>\n", " <th>3</th>\n", " <td>24</td>\n", " <td>26</td>\n", " <td>28</td>\n", " <td>30</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "</div>" ], "text/plain": [ " a b c d\n", "0 0 2 4 6\n", "1 8 10 12 14\n", "2 16 18 20 22\n", "3 24 26 28 30" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df + df # <4>" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/html": [ "<div>\n", "<style scoped>\n", " .dataframe tbody tr th:only-of-type {\n", " vertical-align: middle;\n", " }\n", "\n", " .dataframe tbody tr th {\n", " vertical-align: top;\n", " }\n", "\n", " .dataframe thead th {\n", " text-align: right;\n", " }\n", "</style>\n", "<table border=\"1\" class=\"dataframe\">\n", " <thead>\n", " <tr style=\"text-align: right;\">\n", " <th></th>\n", " <th>a</th>\n", " <th>b</th>\n", " <th>c</th>\n", " <th>d</th>\n", " </tr>\n", " </thead>\n", " <tbody>\n", " <tr>\n", " <th>0</th>\n", " <td>0</td>\n", " <td>2</td>\n", " <td>4</td>\n", " <td>6</td>\n", " </tr>\n", " <tr>\n", " <th>1</th>\n", " <td>8</td>\n", " <td>10</td>\n", " <td>12</td>\n", " <td>14</td>\n", " </tr>\n", " <tr>\n", " <th>2</th>\n", " <td>16</td>\n", " <td>18</td>\n", " <td>20</td>\n", " <td>22</td>\n", " </tr>\n", " <tr>\n", " <th>3</th>\n", " <td>24</td>\n", " <td>26</td>\n", " <td>28</td>\n", " <td>30</td>\n", " </tr>\n", " </tbody>\n", "</table>\n", "</div>" ], "text/plain": [ " a b c d\n", "0 0 2 4 6\n", "1 8 10 12 14\n", "2 16 18 20 22\n", "3 24 26 28 30" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 * df # <5>" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "a 24\n", "b 28\n", "c 32\n", "d 36\n", "dtype: int64" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sum(df) # <6>" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "208" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.__sizeof__() # <7>" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Basics of Python Classes" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class FinancialInstrument(object): # <1>\n", " pass # <2>" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fi = FinancialInstrument() # <3>" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "__main__.FinancialInstrument" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(fi) # <4>" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<__main__.FinancialInstrument at 0x10d602278>" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi # <4>" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'<__main__.FinancialInstrument object at 0x10d602278>'" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi.__str__() # <5>" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fi.price = 100 # <6>" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi.price # <6>" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "# tag::OOP_10[]" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class FinancialInstrument(object):\n", " author = 'Yves Hilpisch' # <1>\n", " def __init__(self, symbol, price): # <2>\n", " self.symbol = symbol # <3>\n", " self.price = price # <3>" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Yves Hilpisch'" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "FinancialInstrument.author # <1>" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": true }, "outputs": [], "source": [ "aapl = FinancialInstrument('AAPL', 100) # <4>" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'AAPL'" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aapl.symbol # <5>" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Yves Hilpisch'" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aapl.author # <6>" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": true }, "outputs": [], "source": [ "aapl.price = 105 # <7>" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "105" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aapl.price # <7>" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "# tag::OOP_11[]" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class FinancialInstrument(FinancialInstrument): # <1>\n", " def get_price(self): # <2>\n", " return self.price # <2>\n", " def set_price(self, price): # <3>\n", " self.price = price # <4>" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fi = FinancialInstrument('AAPL', 100) # <5>" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi.get_price() # <6>" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fi.set_price(105) # <7>" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "105" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi.get_price() # <6>" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "105" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi.price # <8>" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "# tag::OOP_12[]" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class FinancialInstrument(object):\n", " def __init__(self, symbol, price):\n", " self.symbol = symbol \n", " self.__price = price # <1>\n", " def get_price(self):\n", " return self.__price\n", " def set_price(self, price):\n", " self.__price = price" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fi = FinancialInstrument('AAPL', 100)" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi.get_price() # <2>" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'FinancialInstrument' object has no attribute '__price'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-67-74c0dc05c9ae>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfi\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__price\u001b[0m \u001b[0;31m# <3>\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m: 'FinancialInstrument' object has no attribute '__price'" ] } ], "source": [ "fi.__price # <3>" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fi._FinancialInstrument__price # <4>" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fi._FinancialInstrument__price = 105 # <4>" ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "collapsed": true }, "outputs": [], "source": [ "fi.set_price(100) # <5>" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "# tag::OOP_13[]" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class PortfolioPosition(object):\n", " def __init__(self, financial_instrument, position_size):\n", " self.position = financial_instrument # <1>\n", " self.__position_size = position_size # <2>\n", " def get_position_size(self):\n", " return self.__position_size\n", " def update_position_size(self, position_size):\n", " self.__position_size = position_size\n", " def get_position_value(self):\n", " return self.__position_size * \\\n", " self.position.get_price() # <3>" ] }, { "cell_type": "code", "execution_count": 72, "metadata": { "collapsed": true }, "outputs": [], "source": [ "pp = PortfolioPosition(fi, 10)" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pp.get_position_size()" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1000" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pp.get_position_value() # <3>" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pp.position.get_price() # <4>" ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "collapsed": true }, "outputs": [], "source": [ "pp.position.set_price(105) # <5>" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1050" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pp.get_position_value() # <6>" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Python Data Model" ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Vector(object):\n", " def __init__(self, x=0, y=0, z=0): # <1>\n", " self.x = x # <1>\n", " self.y = y # <1>\n", " self.z = z # <1>" ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v = Vector(1, 2, 3) # <2>" ] }, { "cell_type": "code", "execution_count": 80, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "<__main__.Vector at 0x10d626da0>" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v # <3>" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "# tag::OOP_15[]" ] }, { "cell_type": "code", "execution_count": 81, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Vector(Vector):\n", " def __repr__(self):\n", " return 'Vector(%r, %r, %r)' % (self.x, self.y, self.z)" ] }, { "cell_type": "code", "execution_count": 82, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v = Vector(1, 2, 3)" ] }, { "cell_type": "code", "execution_count": 83, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "Vector(1, 2, 3)" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v # <1>" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Vector(1, 2, 3)\n" ] } ], "source": [ "print(v) # <1>" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "# tag::OOP_16[]" ] }, { "cell_type": "code", "execution_count": 85, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Vector(Vector):\n", " def __abs__(self):\n", " return (self.x ** 2 + self.y ** 2 +\n", " self.z ** 2) ** 0.5 # <1>\n", " \n", " def __bool__(self):\n", " return bool(abs(self))" ] }, { "cell_type": "code", "execution_count": 86, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v = Vector(1, 2, -1) # <2>" ] }, { "cell_type": "code", "execution_count": 87, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "2.449489742783178" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(v)" ] }, { "cell_type": "code", "execution_count": 88, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(v)" ] }, { "cell_type": "code", "execution_count": 89, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v = Vector() # <3>" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Vector(0, 0, 0)" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v # <3>" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(v)" ] }, { "cell_type": "code", "execution_count": 92, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 92, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(v)" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "# tag::OOP_17[]" ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Vector(Vector):\n", " def __add__(self, other):\n", " x = self.x + other.x\n", " y = self.y + other.y\n", " z = self.z + other.z\n", " return Vector(x, y, z) # <1>\n", " \n", " def __mul__(self, scalar):\n", " return Vector(self.x * scalar,\n", " self.y * scalar,\n", " self.z * scalar) # <1>" ] }, { "cell_type": "code", "execution_count": 94, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v = Vector(1, 2, 3)" ] }, { "cell_type": "code", "execution_count": 95, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "Vector(3, 5, 7)" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v + Vector(2, 3, 4)" ] }, { "cell_type": "code", "execution_count": 96, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "Vector(2, 4, 6)" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v * 2" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "# tag::OOP_18[]" ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Vector(Vector):\n", " def __len__(self):\n", " return 3 # <1>\n", " \n", " def __getitem__(self, i):\n", " if i in [0, -3]: return self.x\n", " elif i in [1, -2]: return self.y\n", " elif i in [2, -1]: return self.z\n", " else: raise IndexError('Index out of range.')" ] }, { "cell_type": "code", "execution_count": 98, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v = Vector(1, 2, 3)" ] }, { "cell_type": "code", "execution_count": 99, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(v)" ] }, { "cell_type": "code", "execution_count": 100, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v[0]" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v[-2]" ] }, { "cell_type": "code", "execution_count": 102, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "IndexError", "evalue": "Index out of range.", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-102-0f5531c4b93d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mv\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m<ipython-input-97-eef2cdc22510>\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, i)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mz\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mIndexError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Index out of range.'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: Index out of range." ] } ], "source": [ "v[3]" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "# tag::OOP_19[]" ] }, { "cell_type": "code", "execution_count": 103, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Vector(Vector):\n", " def __iter__(self):\n", " for i in range(len(self)):\n", " yield self[i]" ] }, { "cell_type": "code", "execution_count": 104, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "v = Vector(1, 2, 3)" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for i in range(3): # <1>\n", " print(v[i]) # <1>" ] }, { "cell_type": "code", "execution_count": 106, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for coordinate in v: # <2>\n", " print(coordinate) # <2>" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "# tag::OOP_20[]" ] }, { "cell_type": "code", "execution_count": 107, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Vector(object):\n", " def __init__(self, x=0, y=0, z=0):\n", " self.x = x\n", " self.y = y\n", " self.z = z\n", " \n", " def __repr__(self):\n", " return 'Vector(%r, %r, %r)' % (self.x, self.y, self.z)\n", " \n", " def __abs__(self):\n", " return (self.x ** 2 + self.y ** 2 + self.z ** 2) ** 0.5\n", " \n", " def __bool__(self):\n", " return bool(abs(self))\n", " \n", " def __add__(self, other):\n", " x = self.x + other.x\n", " y = self.y + other.y\n", " z = self.z + other.z\n", " return Vector(x, y, z)\n", " \n", " def __mul__(self, scalar):\n", " return Vector(self.x * scalar,\n", " self.y * scalar,\n", " self.z * scalar)\n", " \n", " def __len__(self):\n", " return 3\n", " \n", " def __getitem__(self, i):\n", " if i in [0, -3]: return self.x\n", " elif i in [1, -2]: return self.y\n", " elif i in [2, -1]: return self.z\n", " else: raise IndexError('Index out of range.')\n", " \n", " def __iter__(self):\n", " for i in range(len(self)):\n", " yield self[i]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "<img src=\"http://hilpisch.com/tpq_logo.png\" alt=\"The Python Quants\" width=\"35%\" align=\"right\" border=\"0\"><br>" ] } ], "metadata": { "anaconda-cloud": {}, "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.6.1" } }, "nbformat": 4, "nbformat_minor": 1 }