{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Technical Analysis and Pattern Recognition:\n\nTA-Lib is widely used by trading software developers requiring to perform technical analysis of financial market data. It includes 200 indicators such as ADX, MACD, RSI, Stochastic, Bollinger Bands, and has Candlestick pattern recognition. Since Talib does not come with a standard python package, the installation is a bit difficult. In this notebook, I am using 'how to install ta-lib in Google Drive' method to install and import the talib library. Finally, with usage of Abstract API, I will generate new features for all of the candlestick patterns available in talib.\n\nImportant Links:\n\n* TA-Lib Python Wrapper documentation: <a href=\"https://github.com/mrjbq7/ta-lib\" target=\"_blank\">here</a>\n* TA-Lib Website: <a href=\"https://www.ta-lib.org/\" target=\"_blank\">here</a>\n* How to install talib in python: <a href=\"https://blog.quantinsti.com/install-ta-lib-python/\" target=\"_blank\">here</a>\n* How to install ta-lib in Google Drive: <a href=\"https://stackoverflow.com/questions/49648391/how-to-install-ta-lib-in-google-colab?newreg=5d6b616eb23c4a2f9f6e78a8c948d56d\" target=\"_blank\">here</a>\n* All Tech Indicators: <a href=\"https://www.programcreek.com/python/index/7769/talib\" target=\"_blank\">here</a>\n\n* Using abstract API: <a href=\"https://mrjbq7.github.io/ta-lib/abstract.html\" target=\"_blank\">here</a>\n","metadata":{}},{"cell_type":"code","source":"url = 'https://launchpad.net/~mario-mariomedina/+archive/ubuntu/talib/+files'\next = '0.4.0-oneiric1_amd64.deb -qO'\n!wget $url/libta-lib0_$ext libta.deb\n!wget $url/ta-lib0-dev_$ext ta.deb\n!dpkg -i libta.deb ta.deb\n!pip install ta-lib","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-04T10:54:17.304879Z","iopub.execute_input":"2021-11-04T10:54:17.305274Z","iopub.status.idle":"2021-11-04T10:55:10.265768Z","shell.execute_reply.started":"2021-11-04T10:54:17.305226Z","shell.execute_reply":"2021-11-04T10:55:10.265044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# loading libraries\nimport pandas as pd\nimport talib\nfrom talib import abstract","metadata":{"execution":{"iopub.status.busy":"2021-11-04T10:55:10.267745Z","iopub.execute_input":"2021-11-04T10:55:10.268027Z","iopub.status.idle":"2021-11-04T10:55:10.294942Z","shell.execute_reply.started":"2021-11-04T10:55:10.267996Z","shell.execute_reply":"2021-11-04T10:55:10.294094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# loading dataset and converting the price column names to lowercase so abstract API can recognize the input\ndf = pd.read_csv('../input/g-research-crypto-forecasting/train.csv')\ndf.rename(columns={'Open': 'open', 'High': 'high','Low': 'low','Close': 'close', 'Volume': 'volume'}, inplace= True)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-04T11:01:36.854377Z","iopub.execute_input":"2021-11-04T11:01:36.854750Z","iopub.status.idle":"2021-11-04T11:02:15.339062Z","shell.execute_reply.started":"2021-11-04T11:01:36.854709Z","shell.execute_reply":"2021-11-04T11:02:15.338257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"If you're already familiar with using the function API, you should feel right at home using the abstract API. Every function takes the same input. In this notebook we focus on the Pattern Recognition. Candlestick patterns can be a great way of deciding if and when we should open or close a trade. For example, if we get a ‘hammer’ candlestick, this is usually a bullish sign. Paired with a moving average, this can be a powerful strategy. ","metadata":{}},{"cell_type":"code","source":"# talib library has many categories of indicators, feel free to add more indicator groups to your features.\ntalib.get_function_groups().keys()","metadata":{"execution":{"iopub.status.busy":"2021-11-04T11:02:37.677398Z","iopub.execute_input":"2021-11-04T11:02:37.678101Z","iopub.status.idle":"2021-11-04T11:02:37.683869Z","shell.execute_reply.started":"2021-11-04T11:02:37.678030Z","shell.execute_reply":"2021-11-04T11:02:37.683284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# list of all indicators\ntalib.get_function_groups()","metadata":{"execution":{"iopub.status.busy":"2021-11-04T11:09:40.473318Z","iopub.execute_input":"2021-11-04T11:09:40.474446Z","iopub.status.idle":"2021-11-04T11:09:40.484861Z","shell.execute_reply.started":"2021-11-04T11:09:40.474381Z","shell.execute_reply":"2021-11-04T11:09:40.484161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# due to the limitations of kaggle notebook, let's choose the first 1000 rows of data\ndf_debug = df.iloc[:1000,:]\ndf_debug.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-04T11:02:45.825837Z","iopub.execute_input":"2021-11-04T11:02:45.826493Z","iopub.status.idle":"2021-11-04T11:02:45.834101Z","shell.execute_reply.started":"2021-11-04T11:02:45.826423Z","shell.execute_reply":"2021-11-04T11:02:45.833235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# all Pattern Recognition indicators start with CDL\nall_indicators = [method for method in dir(abstract) if method.startswith('CDL')]","metadata":{"execution":{"iopub.status.busy":"2021-11-04T11:03:04.242578Z","iopub.execute_input":"2021-11-04T11:03:04.243265Z","iopub.status.idle":"2021-11-04T11:03:04.247440Z","shell.execute_reply.started":"2021-11-04T11:03:04.243220Z","shell.execute_reply":"2021-11-04T11:03:04.246680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_indicators","metadata":{"execution":{"iopub.status.busy":"2021-11-04T11:03:35.271941Z","iopub.execute_input":"2021-11-04T11:03:35.272544Z","iopub.status.idle":"2021-11-04T11:03:35.278532Z","shell.execute_reply.started":"2021-11-04T11:03:35.272489Z","shell.execute_reply":"2021-11-04T11:03:35.277934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_indicator = pd.DataFrame()\nfor indicator in all_indicators:\n    df_indicator[str(indicator)] = getattr(abstract, indicator)(df_debug)","metadata":{"execution":{"iopub.status.busy":"2021-11-04T11:06:03.176000Z","iopub.execute_input":"2021-11-04T11:06:03.177143Z","iopub.status.idle":"2021-11-04T11:06:03.217758Z","shell.execute_reply.started":"2021-11-04T11:06:03.177072Z","shell.execute_reply":"2021-11-04T11:06:03.216898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_indicator","metadata":{"execution":{"iopub.status.busy":"2021-11-04T11:06:12.046406Z","iopub.execute_input":"2021-11-04T11:06:12.046864Z","iopub.status.idle":"2021-11-04T11:06:12.080100Z","shell.execute_reply.started":"2021-11-04T11:06:12.046832Z","shell.execute_reply":"2021-11-04T11:06:12.079108Z"},"trusted":true},"execution_count":null,"outputs":[]}]}