Last active
June 1, 2019 23:52
-
-
Save jacktator/29de16f85b0371d820d5b464eb685e1f to your computer and use it in GitHub Desktop.
This file defines a Class for TradeDay
This file contains 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 characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": { | |
"collapsed": true, | |
"pycharm": { | |
"name": "#%% This file defines a Class for TradeDay\n", | |
"is_executing": false | |
} | |
}, | |
"outputs": [], | |
"source": "from datetime import date\n\nclass Prices(object):\n \n def __init__(self,\n the_open: float, \n the_high: float, \n the_low: float,\n the_close: float):\n \n # Data Validation, Commented out for performance testing purpose\n # self.__validate_data(the_open, the_high, the_low, the_close)\n \n # Processing Data\n # Silence is told\n \n # Storing Data\n self.open \u003d the_open\n self.high \u003d the_high\n self.low \u003d the_low\n self.close \u003d the_close\n \n # noinspection PyMethodMayBeStatic\n def __validate_data(self, \n the_open: float, \n the_high: float, \n the_low: float,\n the_close: float):\n \"\"\"\n Validates all inputs\n :type the_open: float\n :type the_high: float\n :type the_low: float\n :type the_close: float\n :param the_open: The opening price. This needs to be a float. \n :param the_high: The highest price. This needs to be a float.\n :param the_low: The lowest price. This needs to be a float.\n :param the_close: The closing price. This needs to be a float.\n \"\"\"\n if (not isinstance(the_open, float) and not isinstance(the_open, int)) or \\\n (not isinstance(the_high, float) and not isinstance(the_high, int)) or \\\n (not isinstance(the_low, float) and not isinstance(the_low, int)) or \\\n (not isinstance(the_close, float) and not isinstance(the_close, int)):\n raise Exception(\"\u0027open\u0027, \u0027close\u0027, \u0027high\u0027, \u0027low\u0027 must be a float. \")\n\n if the_low \u003e the_high:\n raise Exception(\"\u0027low\u0027 must be lower or equal to \u0027high\u0027. \")\n \n def __str__(self):\n return str(\"Prices(open: {}, high: {}, low: {}, close: {})\".format(\n self.open, \n self.high, \n self.low, \n self.close))\n \n __repr__ \u003d __str__\n\n def __hash__(self) -\u003e int:\n return hash((self.open, self.high, self.low, self.close))\n\n\nclass TradeDay(object):\n \n def __init__(self, \n the_symbol: str,\n the_date: date,\n the_prices: Prices):\n \"\"\"\n Initialize a TradeDay object\n :type the_symbol: str\n :type the_date: date\n :type the_prices: Prices\n :param the_symbol: The asset symbol\n :param the_date: The date\n :param the_prices: The Prices object.\n \"\"\"\n \n # Data Validation, Commented out for performance testing purpose\n # self.__validate_data(the_symbol, the_date, the_prices)\n \n # Processing Data\n \"\"\"\n The following function was commented out in favor of lambda \n \"\"\"\n # the_change \u003d round(\n # (the_close - the_open) / the_open, \n # 4)\n \n # Storing Data\n self.symbol \u003d the_symbol\n self.date \u003d the_date\n self.prices \u003d the_prices\n self.change \u003d lambda : round(\n (self.prices.close - self.prices.open) / self.prices.open, \n 4)\n \n # noinspection PyMethodMayBeStatic\n def __validate_data(self, \n the_symbol: str,\n the_date: date,\n the_prices: Prices):\n \"\"\"\n Validates all inputs\n :type the_symbol: str\n :type the_date: date\n :param the_symbol: The asset symbol\n :param the_date: The date\n \"\"\"\n if not isinstance(the_symbol, str):\n raise Exception(\"\u0027symbol\u0027 must be a str\")\n if not isinstance(the_date, date):\n raise Exception(\"\u0027date\u0027 must be a date\")\n if not isinstance(the_prices, Prices):\n raise Exception(\"\u0027date\u0027 must be a date\")\n \n def __str__(self):\n return str(\"TradeDay(symbol: {}, date: {}, prices: {}, change: {})\".format(\n self.symbol, \n self.date, \n self.prices, \n self.change))\n \n __repr__ \u003d __str__\n\n def __hash__(self) -\u003e int:\n return hash((self.symbol, self.date, self.prices, self.change))\n " | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": "TradeDay(symbol: MA, date: 2019-06-01, prices: Prices(open: 10.0, high: 30.0, low: 5.0, close: 20.0), change: \u003cfunction TradeDay.__init__.\u003clocals\u003e.\u003clambda\u003e at 0x10c9eb598\u003e)" | |
}, | |
"metadata": {}, | |
"output_type": "execute_result", | |
"execution_count": 16 | |
} | |
], | |
"source": "import datetime\n\nday: TradeDay \u003d TradeDay(\"MA\", datetime.date.today(), Prices(10.0, 30.0, 5.0, 20.0))\n\nday\n", | |
"metadata": { | |
"pycharm": { | |
"metadata": false, | |
"name": "#%% Usage Example\n", | |
"is_executing": false | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"text": [ | |
"\u003d\u003d\u003d\u003d\u003d\u003d Performance Report \u003d\u003d\u003d\u003d\u003d\u003d\n\nThis report shows the performance of storing and accessing a data object as a class.\nTradeDay(symbol: MA, date: 2019-06-01, prices: Prices(open: 10.0, high: 30.0, low: 5.0, close: 20.0), change: \u003cfunction TradeDay.__init__.\u003clocals\u003e.\u003clambda\u003e at 0x10ca211e0\u003e)\n\n------ Speed Report -----\n\nTime it takes to create \u0027day\u0027 object is: \n", | |
"2.78 µs ± 97.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\nTime it takes to read \u0027day\u0027 object is: \n", | |
"24.5 ns ± 1.15 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)\nTime it takes to read \u0027day.prices\u0027 dict attribute is: \n", | |
"47.5 ns ± 0.5 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)\nTime it takes to execute \u0027day.change()\u0027 function is: \n", | |
"803 ns ± 4.01 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n\n------ Size Report ------\n\nThe Size of \u0027day\u0027 object: 56 bytes\nThe Size of \u0027day\u0027 dict: 112 bytes\n\n\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d End of Report \u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\n" | |
], | |
"output_type": "stream" | |
} | |
], | |
"source": "import datetime\nimport sys\n\nday: TradeDay \u003d TradeDay(\"MA\", datetime.date.today(), Prices(10.0, 30.0, 5.0, 20.0))\n\nprint(\"\u003d\u003d\u003d\u003d\u003d\u003d Performance Report \u003d\u003d\u003d\u003d\u003d\u003d\\n\")\nprint(\"This report shows the performance of storing and accessing a data object as a class.\")\nprint(day)\n\nprint(\"\\n------ Speed Report -----\\n\")\nprint(\"Time it takes to create \u0027day\u0027 object is: \")\n%timeit day: TradeDay \u003d TradeDay(\"MA\", datetime.date.today(), Prices(10.0, 30.0, 5.0, 20.0))\nprint(\"Time it takes to read \u0027day\u0027 object is: \")\n%timeit day\nprint(\"Time it takes to read \u0027day.prices\u0027 dict attribute is: \")\n%timeit day.prices\nprint(\"Time it takes to execute \u0027day.change()\u0027 function is: \")\n%timeit day.change()\n\nprint(\"\\n------ Size Report ------\\n\")\nprint(\"The Size of \u0027day\u0027 object: {} bytes\".format(sys.getsizeof(day)))\nprint(\"The Size of \u0027day\u0027 dict: {} bytes\".format(sys.getsizeof(vars(day))))\nprint(\"\\n\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d End of Report \u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\u003d\")", | |
"metadata": { | |
"pycharm": { | |
"metadata": false, | |
"name": "#%% Performance Test\n", | |
"is_executing": false | |
} | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"outputs": [], | |
"source": "", | |
"metadata": { | |
"pycharm": { | |
"metadata": false, | |
"name": "#%%\n", | |
"is_executing": false | |
} | |
} | |
} | |
], | |
"metadata": { | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.6" | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"language": "python", | |
"display_name": "Python 3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment