├── 1.1 - Getting Instrument Token of a Scrip Using Python and Zerodha API.ipynb ├── 1.2 - Getting Instrument Token of a Scrip Using Python and Zerodha Kite LTP Method.ipynb ├── 1.3 - Getting Historical Data of Reliance​ in JSON Using Python and Zerodha API.ipynb ├── 1.4 - Getting Historical Data of Reliance​ In Pandas Using Python and Zerodha API.ipynb ├── 1.5 - Organizing Expiry Dates of Financial Instruments with Python.ipynb ├── 1.7 - Buy on RSI Strategy Coding in Python using Zerodha.ipynb ├── 1.8 - Guppy Strategy Screener Using Python and Zerodha.ipynb ├── 1.9 - Guppy Strategy Trading Bot Using Python and Zerodha.ipynb ├── 2.1 - Guppy Indicator Trading Bot Using Python and Zerodha.ipynb ├── 2.2 - Plotting OHLC to Candlestick Chart.ipynb ├── 2.2 - Plotting Zerodha OHLC to Candlestick Chart.ipynb ├── 2.3 - Converting Candles to Heikin Ashi Using Zerodha KiteConnect.ipynb ├── 2.3 - Converting Candlestick to Heikin Ashi Using Zerodha KiteConnect.ipynb ├── 2.4 - Creating TimeFrames from Minute-Based Data Using Python.ipynb ├── 2.5 - Multi-Stock Bot Using Guppy Strategy with Screener and Backtesting.ipynb ├── 2.6 - RSI Based Trading Bot with Python using Zerodha API.ipynb ├── 2.7 - Multi-Timeframe Bot Using Guppy Strategy and Screener.ipynb ├── 2.8 - Backtesting Guppy Multiple Moving Average (GMMA) with Python using Zerodha API.ipynb ├── 2.9 - Exploring Technical Indicators in the Indian Stock Market with Zerodha API and Python.ipynb └── README.md /1.1 - Getting Instrument Token of a Scrip Using Python and Zerodha API.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "2eb7c931", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "# https://unofficed.com/courses/mastering-algotrading-a-beginners-guide-using-kiteconnect-api/lessons/getting-instrument-token-of-a-scrip-using-python-and-zerodha-api/" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "id": "a59c49d2", 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stderr", 21 | "output_type": "stream", 22 | "text": [ 23 | "/tmp/ipykernel_4126091/2308159906.py:10: DtypeWarning: Columns (1) have mixed types.Specify dtype option on import or set low_memory=False.\n", 24 | " from kite.kitecustom import *\n" 25 | ] 26 | } 27 | ], 28 | "source": [ 29 | "import logging\n", 30 | "from kiteconnect import KiteConnect\n", 31 | "\n", 32 | "logging.basicConfig(level=logging.DEBUG)\n", 33 | "\n", 34 | "kite = KiteConnect(api_key=\"your_api_key\")\n", 35 | "\n", 36 | "# Redirect the user to the login url obtained\n", 37 | "# from kite.login_url(), and receive the request_token\n", 38 | "# from the registered redirect url after the login flow.\n", 39 | "# Once you have the request_token, obtain the access_token\n", 40 | "# as follows.\n", 41 | "\n", 42 | "data = kite.generate_session(\"request_token_here\", api_secret=\"your_secret\")\n", 43 | "kite.set_access_token(data[\"access_token\"])" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 2, 49 | "id": "56ed6214", 50 | "metadata": {}, 51 | "outputs": [ 52 | { 53 | "name": "stdout", 54 | "output_type": "stream", 55 | "text": [ 56 | " instrument_token exchange_token tradingsymbol \\\n", 57 | "0 537733638 2100522 EURINR23DEC83.25CE \n", 58 | "1 537730310 2100509 EURINR23DEC83.25PE \n", 59 | "2 537617158 2100067 EURINR23DEC83.5CE \n", 60 | "3 537614342 2100056 EURINR23DEC83.5PE \n", 61 | "4 537747718 2100577 EURINR23DEC83.75CE \n", 62 | "... ... ... ... \n", 63 | "98011 2916865 11394 ZOTA \n", 64 | "98012 7436801 29050 ZUARI \n", 65 | "98013 979713 3827 ZUARIIND \n", 66 | "98014 2029825 7929 ZYDUSLIFE \n", 67 | "98015 4514561 17635 ZYDUSWELL \n", 68 | "\n", 69 | " name last_price expiry strike tick_size \\\n", 70 | "0 EURINR 0 2023-12-27 83.25 0.0025 \n", 71 | "1 EURINR 0 2023-12-27 83.25 0.0025 \n", 72 | "2 EURINR 0 2023-12-27 83.50 0.0025 \n", 73 | "3 EURINR 0 2023-12-27 83.50 0.0025 \n", 74 | "4 EURINR 0 2023-12-27 83.75 0.0025 \n", 75 | "... ... ... ... ... ... \n", 76 | "98011 ZOTA HEALTH CARE 0 NaN 0.00 0.0500 \n", 77 | "98012 ZUARI AGRO CHEMICALS 0 NaN 0.00 0.0500 \n", 78 | "98013 ZUARI INDUSTRIES 0 NaN 0.00 0.0500 \n", 79 | "98014 ZYDUS LIFESCIENCES 0 NaN 0.00 0.0500 \n", 80 | "98015 ZYDUS WELLNESS 0 NaN 0.00 0.0500 \n", 81 | "\n", 82 | " lot_size instrument_type segment exchange \n", 83 | "0 1 CE BCD-OPT BCD \n", 84 | "1 1 PE BCD-OPT BCD \n", 85 | "2 1 CE BCD-OPT BCD \n", 86 | "3 1 PE BCD-OPT BCD \n", 87 | "4 1 CE BCD-OPT BCD \n", 88 | "... ... ... ... ... \n", 89 | "98011 1 EQ NSE NSE \n", 90 | "98012 1 EQ NSE NSE \n", 91 | "98013 1 EQ NSE NSE \n", 92 | "98014 1 EQ NSE NSE \n", 93 | "98015 1 EQ NSE NSE \n", 94 | "\n", 95 | "[98016 rows x 12 columns]\n" 96 | ] 97 | } 98 | ], 99 | "source": [ 100 | "import pandas as pd\n", 101 | "\n", 102 | "instrumentList = pd.read_csv(\"https://api.kite.trade/instruments\")\n", 103 | "print(instrumentList)" 104 | ] 105 | }, 106 | { 107 | "cell_type": "code", 108 | "execution_count": 3, 109 | "id": "219b9be3", 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "name": "stdout", 114 | "output_type": "stream", 115 | "text": [ 116 | " instrument_token exchange_token tradingsymbol name \\\n", 117 | "97272 738561 2885 RELIANCE RELIANCE INDUSTRIES \n", 118 | "\n", 119 | " last_price expiry strike tick_size lot_size instrument_type segment \\\n", 120 | "97272 0 NaN 0.0 0.05 1 EQ NSE \n", 121 | "\n", 122 | " exchange \n", 123 | "97272 NSE \n" 124 | ] 125 | } 126 | ], 127 | "source": [ 128 | "tradesymbol = \"RELIANCE\"\n", 129 | "exchange = \"NSE\"\n", 130 | "dataToken = instrumentList[(instrumentList['tradingsymbol'] == tradesymbol)&(instrumentList['exchange']==exchange)]\n", 131 | "print(dataToken)" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 5, 137 | "id": "8c813dd5", 138 | "metadata": {}, 139 | "outputs": [], 140 | "source": [ 141 | "def get_insToken(tradesymbol,exchange=\"NFO\"):\n", 142 | " if(exchange==\"NSE\"):\n", 143 | " if(tradesymbol==\"NIFTY\"):tradesymbol=\"NIFTY 50\"\n", 144 | " if(tradesymbol==\"BANKNIFTY\"):tradesymbol=\"NIFTY BANK\"\n", 145 | " if(tradesymbol==\"FINNIFTY\"):tradesymbol=\"NIFTY FIN SERVICE\"\n", 146 | " dataToken = instrumentList[(instrumentList['tradingsymbol'] == tradesymbol)&(instrumentList['exchange']==exchange)]\n", 147 | " return dataToken.instrument_token.iloc[0]" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 6, 153 | "id": "df9ec918", 154 | "metadata": {}, 155 | "outputs": [ 156 | { 157 | "name": "stdout", 158 | "output_type": "stream", 159 | "text": [ 160 | "738561\n" 161 | ] 162 | } 163 | ], 164 | "source": [ 165 | "symbol = \"RELIANCE\"\n", 166 | "token = get_insToken(symbol,\"NSE\")\n", 167 | "print(token)" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": null, 173 | "id": "036e1aa7", 174 | "metadata": {}, 175 | "outputs": [], 176 | "source": [] 177 | } 178 | ], 179 | "metadata": { 180 | "kernelspec": { 181 | "display_name": "Python 3 (ipykernel)", 182 | "language": "python", 183 | "name": "python3" 184 | }, 185 | "language_info": { 186 | "codemirror_mode": { 187 | "name": "ipython", 188 | "version": 3 189 | }, 190 | "file_extension": ".py", 191 | "mimetype": "text/x-python", 192 | "name": "python", 193 | "nbconvert_exporter": "python", 194 | "pygments_lexer": "ipython3", 195 | "version": "3.10.12" 196 | } 197 | }, 198 | "nbformat": 4, 199 | "nbformat_minor": 5 200 | } 201 | -------------------------------------------------------------------------------- /1.2 - Getting Instrument Token of a Scrip Using Python and Zerodha Kite LTP Method.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "1a6b80c8", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "# https://unofficed.com/courses/mastering-algotrading-a-beginners-guide-using-kiteconnect-api/lessons/getting-instrument-token-zerodha-kite-ltp-method/" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "id": "2d47c7e8", 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stderr", 21 | "output_type": "stream", 22 | "text": [ 23 | "/tmp/ipykernel_4126731/1105643221.py:8: DtypeWarning: Columns (1) have mixed types.Specify dtype option on import or set low_memory=False.\n", 24 | " from kite.kitecustom import *\n" 25 | ] 26 | } 27 | ], 28 | "source": [ 29 | "import logging\n", 30 | "from kiteconnect import KiteConnect\n", 31 | "\n", 32 | "logging.basicConfig(level=logging.DEBUG)\n", 33 | "\n", 34 | "kite = KiteConnect(api_key=\"your_api_key\")\n", 35 | "\n", 36 | "# Redirect the user to the login url obtained\n", 37 | "# from kite.login_url(), and receive the request_token\n", 38 | "# from the registered redirect url after the login flow.\n", 39 | "# Once you have the request_token, obtain the access_token\n", 40 | "# as follows.\n", 41 | "\n", 42 | "data = kite.generate_session(\"request_token_here\", api_secret=\"your_secret\")\n", 43 | "kite.set_access_token(data[\"access_token\"])" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 3, 49 | "id": "cb04de4d", 50 | "metadata": {}, 51 | "outputs": [ 52 | { 53 | "data": { 54 | "text/plain": [ 55 | "{'NSE:RELIANCE': {'instrument_token': 738561, 'last_price': 2397.65}}" 56 | ] 57 | }, 58 | "execution_count": 3, 59 | "metadata": {}, 60 | "output_type": "execute_result" 61 | } 62 | ], 63 | "source": [ 64 | "# Use Kite Connect's search instruments API to find RELIANCE's instrument token\n", 65 | "instruments = kite.ltp(['NSE:RELIANCE'])\n", 66 | "instruments" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 4, 72 | "id": "110282f3", 73 | "metadata": {}, 74 | "outputs": [ 75 | { 76 | "name": "stdout", 77 | "output_type": "stream", 78 | "text": [ 79 | "738561\n" 80 | ] 81 | } 82 | ], 83 | "source": [ 84 | "# Check if the instrument token for RELIANCE is in the instruments dictionary\n", 85 | "if 'NSE:RELIANCE' in instruments:\n", 86 | " instrument_token = instruments['NSE:RELIANCE']['instrument_token']\n", 87 | " print(instrument_token)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": null, 93 | "id": "fd00ee05", 94 | "metadata": {}, 95 | "outputs": [], 96 | "source": [] 97 | } 98 | ], 99 | "metadata": { 100 | "kernelspec": { 101 | "display_name": "Python 3 (ipykernel)", 102 | "language": "python", 103 | "name": "python3" 104 | }, 105 | "language_info": { 106 | "codemirror_mode": { 107 | "name": "ipython", 108 | "version": 3 109 | }, 110 | "file_extension": ".py", 111 | "mimetype": "text/x-python", 112 | "name": "python", 113 | "nbconvert_exporter": "python", 114 | "pygments_lexer": "ipython3", 115 | "version": "3.10.12" 116 | } 117 | }, 118 | "nbformat": 4, 119 | "nbformat_minor": 5 120 | } 121 | -------------------------------------------------------------------------------- /1.3 - Getting Historical Data of Reliance​ in JSON Using Python and Zerodha API.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "278cf859", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "# https://unofficed.com/courses/mastering-algotrading-a-beginners-guide-using-kiteconnect-api/lessons/getting-historical-data-of-reliance-in-json-using-python-and-zerodha-api/" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "id": "a0737d28", 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stderr", 21 | "output_type": "stream", 22 | "text": [ 23 | "/tmp/ipykernel_4126809/1105643221.py:8: DtypeWarning: Columns (1) have mixed types.Specify dtype option on import or set low_memory=False.\n", 24 | " from kite.kitecustom import *\n" 25 | ] 26 | } 27 | ], 28 | "source": [ 29 | "import logging\n", 30 | "from kiteconnect import KiteConnect\n", 31 | "\n", 32 | "logging.basicConfig(level=logging.DEBUG)\n", 33 | "\n", 34 | "kite = KiteConnect(api_key=\"your_api_key\")\n", 35 | "\n", 36 | "# Redirect the user to the login url obtained\n", 37 | "# from kite.login_url(), and receive the request_token\n", 38 | "# from the registered redirect url after the login flow.\n", 39 | "# Once you have the request_token, obtain the access_token\n", 40 | "# as follows.\n", 41 | "\n", 42 | "data = kite.generate_session(\"request_token_here\", api_secret=\"your_secret\")\n", 43 | "kite.set_access_token(data[\"access_token\"])" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 3, 49 | "id": "5d81a004", 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "instrument_token = 738561" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 4, 59 | "id": "35116fa4", 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "ename": "InputException", 64 | "evalue": "interval exceeds max limit: 60 days", 65 | "output_type": "error", 66 | "traceback": [ 67 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 68 | "\u001b[0;31mInputException\u001b[0m Traceback (most recent call last)", 69 | "Cell \u001b[0;32mIn[4], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# Fetch historical data for RELIANCE\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m historical_data \u001b[38;5;241m=\u001b[39m \u001b[43mkite\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mhistorical_data\u001b[49m\u001b[43m(\u001b[49m\u001b[43minstrument_token\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m2023-01-01\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m2023-09-01\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43minterval\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mminute\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3\u001b[0m historical_data\n", 70 | "File \u001b[0;32m~/apps/zerodha/../kiteconnect/connect.py:503\u001b[0m, in \u001b[0;36mKiteConnect.historical_data\u001b[0;34m(self, instrument_token, from_date, to_date, interval, continuous, oi)\u001b[0m\n\u001b[1;32m 500\u001b[0m from_date_string \u001b[38;5;241m=\u001b[39m from_date\u001b[38;5;241m.\u001b[39mstrftime(date_string_format) \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mtype\u001b[39m(from_date) \u001b[38;5;241m==\u001b[39m datetime\u001b[38;5;241m.\u001b[39mdatetime \u001b[38;5;28;01melse\u001b[39;00m from_date\n\u001b[1;32m 501\u001b[0m to_date_string \u001b[38;5;241m=\u001b[39m to_date\u001b[38;5;241m.\u001b[39mstrftime(date_string_format) \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mtype\u001b[39m(to_date) \u001b[38;5;241m==\u001b[39m datetime\u001b[38;5;241m.\u001b[39mdatetime \u001b[38;5;28;01melse\u001b[39;00m to_date\n\u001b[0;32m--> 503\u001b[0m data \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmarket.historical\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\n\u001b[1;32m 504\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43minstrument_token\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43minstrument_token\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 505\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mfrom\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mfrom_date_string\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 506\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mto\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mto_date_string\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 507\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43minterval\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43minterval\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 508\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcontinuous\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mcontinuous\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01melse\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 509\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43moi\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43moi\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01melse\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\n\u001b[1;32m 510\u001b[0m \u001b[43m\u001b[49m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 512\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_format_historical(data, oi)\n", 71 | "File \u001b[0;32m~/apps/zerodha/../kiteconnect/connect.py:601\u001b[0m, in \u001b[0;36mKiteConnect._get\u001b[0;34m(self, route, params)\u001b[0m\n\u001b[1;32m 599\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_get\u001b[39m(\u001b[38;5;28mself\u001b[39m, route, params\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[1;32m 600\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Alias for sending a GET request.\"\"\"\u001b[39;00m\n\u001b[0;32m--> 601\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_request\u001b[49m\u001b[43m(\u001b[49m\u001b[43mroute\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mGET\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mparams\u001b[49m\u001b[43m)\u001b[49m\n", 72 | "File \u001b[0;32m~/apps/zerodha/../kiteconnect/connect.py:671\u001b[0m, in \u001b[0;36mKiteConnect._request\u001b[0;34m(self, route, method, parameters)\u001b[0m\n\u001b[1;32m 669\u001b[0m \u001b[38;5;66;03m# native Kite errors\u001b[39;00m\n\u001b[1;32m 670\u001b[0m exp \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(ex, data[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124merror_type\u001b[39m\u001b[38;5;124m\"\u001b[39m], ex\u001b[38;5;241m.\u001b[39mGeneralException)\n\u001b[0;32m--> 671\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exp(data[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmessage\u001b[39m\u001b[38;5;124m\"\u001b[39m], code\u001b[38;5;241m=\u001b[39mr\u001b[38;5;241m.\u001b[39mstatus_code)\n\u001b[1;32m 673\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m data[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdata\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[1;32m 674\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcsv\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m r\u001b[38;5;241m.\u001b[39mheaders[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcontent-type\u001b[39m\u001b[38;5;124m\"\u001b[39m]:\n", 73 | "\u001b[0;31mInputException\u001b[0m: interval exceeds max limit: 60 days" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "# Fetch historical data for RELIANCE\n", 79 | "historical_data = kite.historical_data(instrument_token, '2023-01-01', '2023-09-01', interval='minute')\n", 80 | "historical_data" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 5, 86 | "id": "0368b7c7", 87 | "metadata": {}, 88 | "outputs": [ 89 | { 90 | "data": { 91 | "text/plain": [ 92 | "[{'date': datetime.datetime(2023, 1, 2, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 93 | " 'open': 2430.65,\n", 94 | " 'high': 2458.3,\n", 95 | " 'low': 2428.95,\n", 96 | " 'close': 2455.35,\n", 97 | " 'volume': 2573872},\n", 98 | " {'date': datetime.datetime(2023, 1, 3, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 99 | " 'open': 2445,\n", 100 | " 'high': 2452.6,\n", 101 | " 'low': 2428.55,\n", 102 | " 'close': 2437.4,\n", 103 | " 'volume': 3708138},\n", 104 | " {'date': datetime.datetime(2023, 1, 4, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 105 | " 'open': 2437.35,\n", 106 | " 'high': 2441.2,\n", 107 | " 'low': 2396.35,\n", 108 | " 'close': 2400.7,\n", 109 | " 'volume': 4485677},\n", 110 | " {'date': datetime.datetime(2023, 1, 5, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 111 | " 'open': 2405.4,\n", 112 | " 'high': 2417.7,\n", 113 | " 'low': 2386.8,\n", 114 | " 'close': 2396.4,\n", 115 | " 'volume': 6602518},\n", 116 | " {'date': datetime.datetime(2023, 1, 6, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 117 | " 'open': 2408.4,\n", 118 | " 'high': 2428.7,\n", 119 | " 'low': 2400.45,\n", 120 | " 'close': 2418.15,\n", 121 | " 'volume': 3074212},\n", 122 | " {'date': datetime.datetime(2023, 1, 9, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 123 | " 'open': 2426.2,\n", 124 | " 'high': 2480.25,\n", 125 | " 'low': 2424.3,\n", 126 | " 'close': 2475.25,\n", 127 | " 'volume': 5191513},\n", 128 | " {'date': datetime.datetime(2023, 1, 10, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 129 | " 'open': 2484.05,\n", 130 | " 'high': 2484.05,\n", 131 | " 'low': 2426.85,\n", 132 | " 'close': 2438.65,\n", 133 | " 'volume': 3896093},\n", 134 | " {'date': datetime.datetime(2023, 1, 11, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 135 | " 'open': 2433.05,\n", 136 | " 'high': 2438.3,\n", 137 | " 'low': 2403.1,\n", 138 | " 'close': 2407.95,\n", 139 | " 'volume': 5572449},\n", 140 | " {'date': datetime.datetime(2023, 1, 12, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 141 | " 'open': 2406.7,\n", 142 | " 'high': 2414,\n", 143 | " 'low': 2349.65,\n", 144 | " 'close': 2355.95,\n", 145 | " 'volume': 8564171},\n", 146 | " {'date': datetime.datetime(2023, 1, 13, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 147 | " 'open': 2343.35,\n", 148 | " 'high': 2357.15,\n", 149 | " 'low': 2320.65,\n", 150 | " 'close': 2352.1,\n", 151 | " 'volume': 9982664},\n", 152 | " {'date': datetime.datetime(2023, 1, 16, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 153 | " 'open': 2357,\n", 154 | " 'high': 2363.6,\n", 155 | " 'low': 2313.4,\n", 156 | " 'close': 2329.7,\n", 157 | " 'volume': 6596106},\n", 158 | " {'date': datetime.datetime(2023, 1, 17, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 159 | " 'open': 2342.95,\n", 160 | " 'high': 2366.8,\n", 161 | " 'low': 2335.9,\n", 162 | " 'close': 2362.8,\n", 163 | " 'volume': 5205189},\n", 164 | " {'date': datetime.datetime(2023, 1, 18, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 165 | " 'open': 2357.75,\n", 166 | " 'high': 2374.5,\n", 167 | " 'low': 2345.2,\n", 168 | " 'close': 2358.9,\n", 169 | " 'volume': 6511103},\n", 170 | " {'date': datetime.datetime(2023, 1, 19, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 171 | " 'open': 2357.15,\n", 172 | " 'high': 2365.05,\n", 173 | " 'low': 2341.7,\n", 174 | " 'close': 2356.35,\n", 175 | " 'volume': 5780879},\n", 176 | " {'date': datetime.datetime(2023, 1, 20, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 177 | " 'open': 2359.15,\n", 178 | " 'high': 2359.15,\n", 179 | " 'low': 2323.2,\n", 180 | " 'close': 2328.35,\n", 181 | " 'volume': 7228626},\n", 182 | " {'date': datetime.datetime(2023, 1, 23, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 183 | " 'open': 2334.4,\n", 184 | " 'high': 2350.8,\n", 185 | " 'low': 2311.5,\n", 186 | " 'close': 2316.55,\n", 187 | " 'volume': 5303530},\n", 188 | " {'date': datetime.datetime(2023, 1, 24, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 189 | " 'open': 2325.8,\n", 190 | " 'high': 2329.3,\n", 191 | " 'low': 2275.6,\n", 192 | " 'close': 2302.9,\n", 193 | " 'volume': 7983172},\n", 194 | " {'date': datetime.datetime(2023, 1, 25, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 195 | " 'open': 2299.55,\n", 196 | " 'high': 2301.7,\n", 197 | " 'low': 2268.6,\n", 198 | " 'close': 2271.05,\n", 199 | " 'volume': 5996313},\n", 200 | " {'date': datetime.datetime(2023, 1, 27, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 201 | " 'open': 2272.8,\n", 202 | " 'high': 2275.6,\n", 203 | " 'low': 2203.45,\n", 204 | " 'close': 2227.95,\n", 205 | " 'volume': 12506288},\n", 206 | " {'date': datetime.datetime(2023, 1, 30, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 207 | " 'open': 2222.6,\n", 208 | " 'high': 2260.9,\n", 209 | " 'low': 2193.3,\n", 210 | " 'close': 2249.3,\n", 211 | " 'volume': 10541150},\n", 212 | " {'date': datetime.datetime(2023, 1, 31, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 213 | " 'open': 2270.5,\n", 214 | " 'high': 2275.9,\n", 215 | " 'low': 2231.45,\n", 216 | " 'close': 2243.7,\n", 217 | " 'volume': 10986329},\n", 218 | " {'date': datetime.datetime(2023, 2, 1, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 219 | " 'open': 2268.55,\n", 220 | " 'high': 2268.55,\n", 221 | " 'low': 2197.15,\n", 222 | " 'close': 2230.4,\n", 223 | " 'volume': 9367403},\n", 224 | " {'date': datetime.datetime(2023, 2, 2, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 225 | " 'open': 2209.5,\n", 226 | " 'high': 2238.1,\n", 227 | " 'low': 2202.85,\n", 228 | " 'close': 2218.05,\n", 229 | " 'volume': 6614895},\n", 230 | " {'date': datetime.datetime(2023, 2, 3, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 231 | " 'open': 2239.05,\n", 232 | " 'high': 2239.05,\n", 233 | " 'low': 2185.7,\n", 234 | " 'close': 2220,\n", 235 | " 'volume': 11958511},\n", 236 | " {'date': datetime.datetime(2023, 2, 6, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 237 | " 'open': 2206.65,\n", 238 | " 'high': 2212.4,\n", 239 | " 'low': 2197.9,\n", 240 | " 'close': 2203.25,\n", 241 | " 'volume': 7183886},\n", 242 | " {'date': datetime.datetime(2023, 2, 7, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 243 | " 'open': 2203.8,\n", 244 | " 'high': 2218.5,\n", 245 | " 'low': 2185.7,\n", 246 | " 'close': 2198,\n", 247 | " 'volume': 7327951},\n", 248 | " {'date': datetime.datetime(2023, 2, 8, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 249 | " 'open': 2204.75,\n", 250 | " 'high': 2249.45,\n", 251 | " 'low': 2199.05,\n", 252 | " 'close': 2241.9,\n", 253 | " 'volume': 10244956},\n", 254 | " {'date': datetime.datetime(2023, 2, 9, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 255 | " 'open': 2243.3,\n", 256 | " 'high': 2259.9,\n", 257 | " 'low': 2224.75,\n", 258 | " 'close': 2245.8,\n", 259 | " 'volume': 6377706},\n", 260 | " {'date': datetime.datetime(2023, 2, 10, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 261 | " 'open': 2244.7,\n", 262 | " 'high': 2244.7,\n", 263 | " 'low': 2212.65,\n", 264 | " 'close': 2227.3,\n", 265 | " 'volume': 5224287},\n", 266 | " {'date': datetime.datetime(2023, 2, 13, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 267 | " 'open': 2230.7,\n", 268 | " 'high': 2240,\n", 269 | " 'low': 2205.3,\n", 270 | " 'close': 2214.6,\n", 271 | " 'volume': 5012248},\n", 272 | " {'date': datetime.datetime(2023, 2, 14, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 273 | " 'open': 2220.9,\n", 274 | " 'high': 2270.45,\n", 275 | " 'low': 2214.9,\n", 276 | " 'close': 2266.8,\n", 277 | " 'volume': 6934080},\n", 278 | " {'date': datetime.datetime(2023, 2, 15, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 279 | " 'open': 2264.8,\n", 280 | " 'high': 2323.15,\n", 281 | " 'low': 2261.95,\n", 282 | " 'close': 2318.15,\n", 283 | " 'volume': 16221050},\n", 284 | " {'date': datetime.datetime(2023, 2, 16, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 285 | " 'open': 2334.8,\n", 286 | " 'high': 2348.5,\n", 287 | " 'low': 2311.55,\n", 288 | " 'close': 2316.3,\n", 289 | " 'volume': 8358749},\n", 290 | " {'date': datetime.datetime(2023, 2, 17, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 291 | " 'open': 2306.75,\n", 292 | " 'high': 2332.95,\n", 293 | " 'low': 2297.8,\n", 294 | " 'close': 2326,\n", 295 | " 'volume': 5717681},\n", 296 | " {'date': datetime.datetime(2023, 2, 20, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 297 | " 'open': 2330.55,\n", 298 | " 'high': 2335.1,\n", 299 | " 'low': 2297.3,\n", 300 | " 'close': 2301.4,\n", 301 | " 'volume': 4278479},\n", 302 | " {'date': datetime.datetime(2023, 2, 21, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 303 | " 'open': 2310.55,\n", 304 | " 'high': 2332.3,\n", 305 | " 'low': 2299.45,\n", 306 | " 'close': 2320.2,\n", 307 | " 'volume': 6444922},\n", 308 | " {'date': datetime.datetime(2023, 2, 22, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 309 | " 'open': 2312.4,\n", 310 | " 'high': 2316.3,\n", 311 | " 'low': 2263.9,\n", 312 | " 'close': 2267.55,\n", 313 | " 'volume': 5337526},\n", 314 | " {'date': datetime.datetime(2023, 2, 23, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 315 | " 'open': 2266.7,\n", 316 | " 'high': 2282.7,\n", 317 | " 'low': 2250.85,\n", 318 | " 'close': 2256.7,\n", 319 | " 'volume': 4769887},\n", 320 | " {'date': datetime.datetime(2023, 2, 24, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 321 | " 'open': 2261,\n", 322 | " 'high': 2284,\n", 323 | " 'low': 2257.2,\n", 324 | " 'close': 2272.15,\n", 325 | " 'volume': 4567578},\n", 326 | " {'date': datetime.datetime(2023, 2, 27, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 327 | " 'open': 2270.5,\n", 328 | " 'high': 2277.8,\n", 329 | " 'low': 2241.65,\n", 330 | " 'close': 2257.05,\n", 331 | " 'volume': 5422438},\n", 332 | " {'date': datetime.datetime(2023, 2, 28, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 333 | " 'open': 2245.85,\n", 334 | " 'high': 2256.2,\n", 335 | " 'low': 2205.35,\n", 336 | " 'close': 2213.85,\n", 337 | " 'volume': 10648602},\n", 338 | " {'date': datetime.datetime(2023, 3, 1, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 339 | " 'open': 2234.3,\n", 340 | " 'high': 2235.75,\n", 341 | " 'low': 2214.5,\n", 342 | " 'close': 2234.2,\n", 343 | " 'volume': 5592586},\n", 344 | " {'date': datetime.datetime(2023, 3, 2, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 345 | " 'open': 2227.65,\n", 346 | " 'high': 2241.6,\n", 347 | " 'low': 2212.95,\n", 348 | " 'close': 2217.2,\n", 349 | " 'volume': 4518911},\n", 350 | " {'date': datetime.datetime(2023, 3, 3, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 351 | " 'open': 2239.2,\n", 352 | " 'high': 2281.45,\n", 353 | " 'low': 2231.95,\n", 354 | " 'close': 2273.75,\n", 355 | " 'volume': 7089925},\n", 356 | " {'date': datetime.datetime(2023, 3, 6, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 357 | " 'open': 2287.7,\n", 358 | " 'high': 2311.15,\n", 359 | " 'low': 2287.7,\n", 360 | " 'close': 2295.95,\n", 361 | " 'volume': 4924821},\n", 362 | " {'date': datetime.datetime(2023, 3, 8, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 363 | " 'open': 2295.95,\n", 364 | " 'high': 2305.8,\n", 365 | " 'low': 2273.4,\n", 366 | " 'close': 2304.2,\n", 367 | " 'volume': 7641309},\n", 368 | " {'date': datetime.datetime(2023, 3, 9, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 369 | " 'open': 2302.95,\n", 370 | " 'high': 2303.8,\n", 371 | " 'low': 2244.9,\n", 372 | " 'close': 2248.85,\n", 373 | " 'volume': 7860376},\n", 374 | " {'date': datetime.datetime(2023, 3, 10, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 375 | " 'open': 2231.9,\n", 376 | " 'high': 2234.3,\n", 377 | " 'low': 2206.7,\n", 378 | " 'close': 2214,\n", 379 | " 'volume': 6455814},\n", 380 | " {'date': datetime.datetime(2023, 3, 13, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 381 | " 'open': 2220,\n", 382 | " 'high': 2234.3,\n", 383 | " 'low': 2168.55,\n", 384 | " 'close': 2177.6,\n", 385 | " 'volume': 6183476},\n", 386 | " {'date': datetime.datetime(2023, 3, 14, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 387 | " 'open': 2173.3,\n", 388 | " 'high': 2197.1,\n", 389 | " 'low': 2162.85,\n", 390 | " 'close': 2169.45,\n", 391 | " 'volume': 7759862},\n", 392 | " {'date': datetime.datetime(2023, 3, 15, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 393 | " 'open': 2177.2,\n", 394 | " 'high': 2190.75,\n", 395 | " 'low': 2123.15,\n", 396 | " 'close': 2132.35,\n", 397 | " 'volume': 10520066},\n", 398 | " {'date': datetime.datetime(2023, 3, 16, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 399 | " 'open': 2138.05,\n", 400 | " 'high': 2148.5,\n", 401 | " 'low': 2099.15,\n", 402 | " 'close': 2121.75,\n", 403 | " 'volume': 8890681},\n", 404 | " {'date': datetime.datetime(2023, 3, 17, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 405 | " 'open': 2139.7,\n", 406 | " 'high': 2146.55,\n", 407 | " 'low': 2109.15,\n", 408 | " 'close': 2119.05,\n", 409 | " 'volume': 16468273},\n", 410 | " {'date': datetime.datetime(2023, 3, 20, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 411 | " 'open': 2111.35,\n", 412 | " 'high': 2116.1,\n", 413 | " 'low': 2078,\n", 414 | " 'close': 2098.25,\n", 415 | " 'volume': 9452997},\n", 416 | " {'date': datetime.datetime(2023, 3, 21, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 417 | " 'open': 2115.1,\n", 418 | " 'high': 2167.6,\n", 419 | " 'low': 2113.25,\n", 420 | " 'close': 2163.5,\n", 421 | " 'volume': 10361168},\n", 422 | " {'date': datetime.datetime(2023, 3, 22, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 423 | " 'open': 2178.05,\n", 424 | " 'high': 2184.2,\n", 425 | " 'low': 2163.75,\n", 426 | " 'close': 2170.05,\n", 427 | " 'volume': 5287057},\n", 428 | " {'date': datetime.datetime(2023, 3, 23, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 429 | " 'open': 2161.85,\n", 430 | " 'high': 2163.65,\n", 431 | " 'low': 2138.05,\n", 432 | " 'close': 2142.6,\n", 433 | " 'volume': 7867272},\n", 434 | " {'date': datetime.datetime(2023, 3, 24, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 435 | " 'open': 2140.15,\n", 436 | " 'high': 2144.7,\n", 437 | " 'low': 2095.15,\n", 438 | " 'close': 2100.2,\n", 439 | " 'volume': 5991920},\n", 440 | " {'date': datetime.datetime(2023, 3, 27, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 441 | " 'open': 2113.7,\n", 442 | " 'high': 2148.5,\n", 443 | " 'low': 2097.25,\n", 444 | " 'close': 2132.85,\n", 445 | " 'volume': 6804521},\n", 446 | " {'date': datetime.datetime(2023, 3, 28, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 447 | " 'open': 2139.95,\n", 448 | " 'high': 2150.9,\n", 449 | " 'low': 2132.3,\n", 450 | " 'close': 2142.8,\n", 451 | " 'volume': 6088984},\n", 452 | " {'date': datetime.datetime(2023, 3, 29, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 453 | " 'open': 2134.2,\n", 454 | " 'high': 2139.75,\n", 455 | " 'low': 2107.6,\n", 456 | " 'close': 2130.1,\n", 457 | " 'volume': 9104149},\n", 458 | " {'date': datetime.datetime(2023, 3, 31, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 459 | " 'open': 2149.45,\n", 460 | " 'high': 2233.8,\n", 461 | " 'low': 2149.2,\n", 462 | " 'close': 2221.95,\n", 463 | " 'volume': 13639328},\n", 464 | " {'date': datetime.datetime(2023, 4, 3, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 465 | " 'open': 2235.25,\n", 466 | " 'high': 2239.05,\n", 467 | " 'low': 2206.65,\n", 468 | " 'close': 2222.35,\n", 469 | " 'volume': 4983465},\n", 470 | " {'date': datetime.datetime(2023, 4, 5, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 471 | " 'open': 2238.1,\n", 472 | " 'high': 2238.1,\n", 473 | " 'low': 2200.5,\n", 474 | " 'close': 2217,\n", 475 | " 'volume': 7873836},\n", 476 | " {'date': datetime.datetime(2023, 4, 6, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 477 | " 'open': 2209.65,\n", 478 | " 'high': 2243.85,\n", 479 | " 'low': 2209.65,\n", 480 | " 'close': 2231.85,\n", 481 | " 'volume': 8925486},\n", 482 | " {'date': datetime.datetime(2023, 4, 10, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 483 | " 'open': 2240,\n", 484 | " 'high': 2240.4,\n", 485 | " 'low': 2212.9,\n", 486 | " 'close': 2216.05,\n", 487 | " 'volume': 6275377},\n", 488 | " {'date': datetime.datetime(2023, 4, 11, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 489 | " 'open': 2224.75,\n", 490 | " 'high': 2231.45,\n", 491 | " 'low': 2215.3,\n", 492 | " 'close': 2227,\n", 493 | " 'volume': 5104178},\n", 494 | " {'date': datetime.datetime(2023, 4, 12, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 495 | " 'open': 2227,\n", 496 | " 'high': 2257,\n", 497 | " 'low': 2221,\n", 498 | " 'close': 2236.85,\n", 499 | " 'volume': 8801561},\n", 500 | " {'date': datetime.datetime(2023, 4, 13, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 501 | " 'open': 2242.9,\n", 502 | " 'high': 2255.1,\n", 503 | " 'low': 2232.4,\n", 504 | " 'close': 2245.25,\n", 505 | " 'volume': 5993015},\n", 506 | " {'date': datetime.datetime(2023, 4, 17, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 507 | " 'open': 2311.45,\n", 508 | " 'high': 2311.45,\n", 509 | " 'low': 2239.1,\n", 510 | " 'close': 2256.65,\n", 511 | " 'volume': 8680711},\n", 512 | " {'date': datetime.datetime(2023, 4, 18, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 513 | " 'open': 2265.75,\n", 514 | " 'high': 2265.75,\n", 515 | " 'low': 2217.15,\n", 516 | " 'close': 2230.8,\n", 517 | " 'volume': 5037949},\n", 518 | " {'date': datetime.datetime(2023, 4, 19, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 519 | " 'open': 2229.6,\n", 520 | " 'high': 2247.55,\n", 521 | " 'low': 2223.05,\n", 522 | " 'close': 2241.95,\n", 523 | " 'volume': 5942308},\n", 524 | " {'date': datetime.datetime(2023, 4, 20, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 525 | " 'open': 2243.95,\n", 526 | " 'high': 2248.6,\n", 527 | " 'low': 2222.95,\n", 528 | " 'close': 2236.25,\n", 529 | " 'volume': 3392659},\n", 530 | " {'date': datetime.datetime(2023, 4, 21, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 531 | " 'open': 2240.65,\n", 532 | " 'high': 2250.5,\n", 533 | " 'low': 2227.05,\n", 534 | " 'close': 2239.05,\n", 535 | " 'volume': 3702514},\n", 536 | " {'date': datetime.datetime(2023, 4, 24, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 537 | " 'open': 2263.85,\n", 538 | " 'high': 2269.45,\n", 539 | " 'low': 2238.1,\n", 540 | " 'close': 2247.65,\n", 541 | " 'volume': 6263165},\n", 542 | " {'date': datetime.datetime(2023, 4, 25, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 543 | " 'open': 2255.25,\n", 544 | " 'high': 2269.2,\n", 545 | " 'low': 2240.5,\n", 546 | " 'close': 2264.85,\n", 547 | " 'volume': 4471750},\n", 548 | " {'date': datetime.datetime(2023, 4, 26, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 549 | " 'open': 2267.65,\n", 550 | " 'high': 2274.45,\n", 551 | " 'low': 2243.9,\n", 552 | " 'close': 2251.55,\n", 553 | " 'volume': 4172398},\n", 554 | " {'date': datetime.datetime(2023, 4, 27, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 555 | " 'open': 2263.85,\n", 556 | " 'high': 2272.45,\n", 557 | " 'low': 2253.35,\n", 558 | " 'close': 2265.8,\n", 559 | " 'volume': 4438342},\n", 560 | " {'date': datetime.datetime(2023, 4, 28, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 561 | " 'open': 2270.5,\n", 562 | " 'high': 2310.45,\n", 563 | " 'low': 2270.3,\n", 564 | " 'close': 2307.2,\n", 565 | " 'volume': 7536030},\n", 566 | " {'date': datetime.datetime(2023, 5, 2, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 567 | " 'open': 2322.2,\n", 568 | " 'high': 2331.35,\n", 569 | " 'low': 2314.45,\n", 570 | " 'close': 2326.8,\n", 571 | " 'volume': 6285252},\n", 572 | " {'date': datetime.datetime(2023, 5, 3, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 573 | " 'open': 2330.55,\n", 574 | " 'high': 2330.55,\n", 575 | " 'low': 2300.1,\n", 576 | " 'close': 2306.85,\n", 577 | " 'volume': 3419066},\n", 578 | " {'date': datetime.datetime(2023, 5, 4, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 579 | " 'open': 2307.85,\n", 580 | " 'high': 2338.05,\n", 581 | " 'low': 2301.6,\n", 582 | " 'close': 2333.45,\n", 583 | " 'volume': 4180822},\n", 584 | " {'date': datetime.datetime(2023, 5, 5, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 585 | " 'open': 2326.75,\n", 586 | " 'high': 2346.15,\n", 587 | " 'low': 2322,\n", 588 | " 'close': 2327.5,\n", 589 | " 'volume': 4234734},\n", 590 | " {'date': datetime.datetime(2023, 5, 8, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 591 | " 'open': 2335.35,\n", 592 | " 'high': 2367.4,\n", 593 | " 'low': 2329.75,\n", 594 | " 'close': 2356.2,\n", 595 | " 'volume': 4431882},\n", 596 | " {'date': datetime.datetime(2023, 5, 9, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 597 | " 'open': 2359.15,\n", 598 | " 'high': 2370.4,\n", 599 | " 'low': 2347.95,\n", 600 | " 'close': 2363.5,\n", 601 | " 'volume': 3129950},\n", 602 | " {'date': datetime.datetime(2023, 5, 10, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 603 | " 'open': 2373.45,\n", 604 | " 'high': 2382.85,\n", 605 | " 'low': 2365.1,\n", 606 | " 'close': 2379.75,\n", 607 | " 'volume': 5263895},\n", 608 | " {'date': datetime.datetime(2023, 5, 11, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 609 | " 'open': 2382,\n", 610 | " 'high': 2392.05,\n", 611 | " 'low': 2360.65,\n", 612 | " 'close': 2364.2,\n", 613 | " 'volume': 5111679},\n", 614 | " {'date': datetime.datetime(2023, 5, 12, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 615 | " 'open': 2354.2,\n", 616 | " 'high': 2371,\n", 617 | " 'low': 2341.55,\n", 618 | " 'close': 2368.1,\n", 619 | " 'volume': 3367488},\n", 620 | " {'date': datetime.datetime(2023, 5, 15, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 621 | " 'open': 2373.35,\n", 622 | " 'high': 2387.35,\n", 623 | " 'low': 2357.4,\n", 624 | " 'close': 2372.75,\n", 625 | " 'volume': 3308422},\n", 626 | " {'date': datetime.datetime(2023, 5, 16, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 627 | " 'open': 2377.3,\n", 628 | " 'high': 2382.6,\n", 629 | " 'low': 2335.5,\n", 630 | " 'close': 2338.95,\n", 631 | " 'volume': 4220521},\n", 632 | " {'date': datetime.datetime(2023, 5, 17, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 633 | " 'open': 2347.75,\n", 634 | " 'high': 2349.65,\n", 635 | " 'low': 2315.3,\n", 636 | " 'close': 2325.15,\n", 637 | " 'volume': 4260095},\n", 638 | " {'date': datetime.datetime(2023, 5, 18, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 639 | " 'open': 2334.9,\n", 640 | " 'high': 2342,\n", 641 | " 'low': 2313.55,\n", 642 | " 'close': 2320.15,\n", 643 | " 'volume': 5096026},\n", 644 | " {'date': datetime.datetime(2023, 5, 19, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 645 | " 'open': 2320.15,\n", 646 | " 'high': 2331.5,\n", 647 | " 'low': 2305.65,\n", 648 | " 'close': 2327.65,\n", 649 | " 'volume': 3897693},\n", 650 | " {'date': datetime.datetime(2023, 5, 22, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 651 | " 'open': 2321.05,\n", 652 | " 'high': 2350.6,\n", 653 | " 'low': 2318.5,\n", 654 | " 'close': 2340.1,\n", 655 | " 'volume': 3599673},\n", 656 | " {'date': datetime.datetime(2023, 5, 23, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 657 | " 'open': 2345.5,\n", 658 | " 'high': 2352.5,\n", 659 | " 'low': 2336.7,\n", 660 | " 'close': 2339.7,\n", 661 | " 'volume': 2949511},\n", 662 | " {'date': datetime.datetime(2023, 5, 24, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 663 | " 'open': 2331.45,\n", 664 | " 'high': 2348.7,\n", 665 | " 'low': 2319.55,\n", 666 | " 'close': 2326.05,\n", 667 | " 'volume': 3927617},\n", 668 | " {'date': datetime.datetime(2023, 5, 25, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 669 | " 'open': 2320,\n", 670 | " 'high': 2334.4,\n", 671 | " 'low': 2303,\n", 672 | " 'close': 2325.75,\n", 673 | " 'volume': 5561947},\n", 674 | " {'date': datetime.datetime(2023, 5, 26, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 675 | " 'open': 2342.95,\n", 676 | " 'high': 2391.6,\n", 677 | " 'low': 2336.05,\n", 678 | " 'close': 2389.2,\n", 679 | " 'volume': 6113112},\n", 680 | " {'date': datetime.datetime(2023, 5, 29, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 681 | " 'open': 2403,\n", 682 | " 'high': 2411.6,\n", 683 | " 'low': 2389.85,\n", 684 | " 'close': 2402.65,\n", 685 | " 'volume': 4846575},\n", 686 | " {'date': datetime.datetime(2023, 5, 30, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 687 | " 'open': 2402.65,\n", 688 | " 'high': 2418.95,\n", 689 | " 'low': 2396.7,\n", 690 | " 'close': 2402,\n", 691 | " 'volume': 5491571},\n", 692 | " {'date': datetime.datetime(2023, 5, 31, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 693 | " 'open': 2383,\n", 694 | " 'high': 2392.4,\n", 695 | " 'low': 2345.8,\n", 696 | " 'close': 2354.3,\n", 697 | " 'volume': 13124535},\n", 698 | " {'date': datetime.datetime(2023, 6, 1, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 699 | " 'open': 2364.1,\n", 700 | " 'high': 2368.6,\n", 701 | " 'low': 2342.95,\n", 702 | " 'close': 2347.95,\n", 703 | " 'volume': 7065177},\n", 704 | " {'date': datetime.datetime(2023, 6, 2, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 705 | " 'open': 2355.35,\n", 706 | " 'high': 2366.65,\n", 707 | " 'low': 2336.3,\n", 708 | " 'close': 2340.3,\n", 709 | " 'volume': 7650017},\n", 710 | " {'date': datetime.datetime(2023, 6, 5, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 711 | " 'open': 2351.75,\n", 712 | " 'high': 2367.5,\n", 713 | " 'low': 2343.4,\n", 714 | " 'close': 2361.3,\n", 715 | " 'volume': 5281532},\n", 716 | " {'date': datetime.datetime(2023, 6, 6, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 717 | " 'open': 2367.2,\n", 718 | " 'high': 2371.3,\n", 719 | " 'low': 2346.8,\n", 720 | " 'close': 2363.3,\n", 721 | " 'volume': 3627950},\n", 722 | " {'date': datetime.datetime(2023, 6, 7, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 723 | " 'open': 2370.6,\n", 724 | " 'high': 2383,\n", 725 | " 'low': 2356.9,\n", 726 | " 'close': 2381.25,\n", 727 | " 'volume': 4992907},\n", 728 | " {'date': datetime.datetime(2023, 6, 8, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 729 | " 'open': 2388.7,\n", 730 | " 'high': 2399.05,\n", 731 | " 'low': 2374.4,\n", 732 | " 'close': 2382.65,\n", 733 | " 'volume': 5844331},\n", 734 | " {'date': datetime.datetime(2023, 6, 9, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 735 | " 'open': 2389.65,\n", 736 | " 'high': 2391.15,\n", 737 | " 'low': 2362.05,\n", 738 | " 'close': 2365.8,\n", 739 | " 'volume': 2845409},\n", 740 | " {'date': datetime.datetime(2023, 6, 12, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 741 | " 'open': 2359.65,\n", 742 | " 'high': 2372.7,\n", 743 | " 'low': 2349.65,\n", 744 | " 'close': 2367.9,\n", 745 | " 'volume': 3094727},\n", 746 | " {'date': datetime.datetime(2023, 6, 13, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 747 | " 'open': 2378.25,\n", 748 | " 'high': 2405.35,\n", 749 | " 'low': 2369.9,\n", 750 | " 'close': 2402.85,\n", 751 | " 'volume': 5445824},\n", 752 | " {'date': datetime.datetime(2023, 6, 14, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 753 | " 'open': 2403,\n", 754 | " 'high': 2435.45,\n", 755 | " 'low': 2399.2,\n", 756 | " 'close': 2432.4,\n", 757 | " 'volume': 6428357},\n", 758 | " {'date': datetime.datetime(2023, 6, 15, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 759 | " 'open': 2432.4,\n", 760 | " 'high': 2442.6,\n", 761 | " 'low': 2416.85,\n", 762 | " 'close': 2433.1,\n", 763 | " 'volume': 5719605},\n", 764 | " {'date': datetime.datetime(2023, 6, 16, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 765 | " 'open': 2440.4,\n", 766 | " 'high': 2461.55,\n", 767 | " 'low': 2440.4,\n", 768 | " 'close': 2456.8,\n", 769 | " 'volume': 11702877},\n", 770 | " {'date': datetime.datetime(2023, 6, 19, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 771 | " 'open': 2463.05,\n", 772 | " 'high': 2463.05,\n", 773 | " 'low': 2423.65,\n", 774 | " 'close': 2432.4,\n", 775 | " 'volume': 3291579},\n", 776 | " {'date': datetime.datetime(2023, 6, 20, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 777 | " 'open': 2428.3,\n", 778 | " 'high': 2444,\n", 779 | " 'low': 2416.35,\n", 780 | " 'close': 2437.45,\n", 781 | " 'volume': 3350908},\n", 782 | " {'date': datetime.datetime(2023, 6, 21, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 783 | " 'open': 2437.35,\n", 784 | " 'high': 2449.6,\n", 785 | " 'low': 2428.75,\n", 786 | " 'close': 2444.3,\n", 787 | " 'volume': 3370520},\n", 788 | " {'date': datetime.datetime(2023, 6, 22, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 789 | " 'open': 2436.4,\n", 790 | " 'high': 2452.85,\n", 791 | " 'low': 2413.5,\n", 792 | " 'close': 2416.85,\n", 793 | " 'volume': 3688885},\n", 794 | " {'date': datetime.datetime(2023, 6, 23, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 795 | " 'open': 2410.55,\n", 796 | " 'high': 2414.85,\n", 797 | " 'low': 2394.45,\n", 798 | " 'close': 2397.05,\n", 799 | " 'volume': 3209279},\n", 800 | " {'date': datetime.datetime(2023, 6, 26, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 801 | " 'open': 2389.65,\n", 802 | " 'high': 2397.7,\n", 803 | " 'low': 2372.25,\n", 804 | " 'close': 2378.75,\n", 805 | " 'volume': 6120325},\n", 806 | " {'date': datetime.datetime(2023, 6, 27, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 807 | " 'open': 2379.7,\n", 808 | " 'high': 2392.05,\n", 809 | " 'low': 2370.25,\n", 810 | " 'close': 2379.6,\n", 811 | " 'volume': 4691238},\n", 812 | " {'date': datetime.datetime(2023, 6, 28, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 813 | " 'open': 2395.4,\n", 814 | " 'high': 2419.1,\n", 815 | " 'low': 2380.95,\n", 816 | " 'close': 2411.1,\n", 817 | " 'volume': 5497139},\n", 818 | " {'date': datetime.datetime(2023, 6, 30, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 819 | " 'open': 2427.25,\n", 820 | " 'high': 2437,\n", 821 | " 'low': 2414.9,\n", 822 | " 'close': 2430.9,\n", 823 | " 'volume': 5399121},\n", 824 | " {'date': datetime.datetime(2023, 7, 3, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 825 | " 'open': 2438.35,\n", 826 | " 'high': 2499.3,\n", 827 | " 'low': 2435.75,\n", 828 | " 'close': 2493.3,\n", 829 | " 'volume': 5884651},\n", 830 | " {'date': datetime.datetime(2023, 7, 4, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 831 | " 'open': 2502.15,\n", 832 | " 'high': 2502.15,\n", 833 | " 'low': 2452.8,\n", 834 | " 'close': 2467.6,\n", 835 | " 'volume': 3903114},\n", 836 | " {'date': datetime.datetime(2023, 7, 5, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 837 | " 'open': 2486.9,\n", 838 | " 'high': 2486.9,\n", 839 | " 'low': 2455.25,\n", 840 | " 'close': 2463.55,\n", 841 | " 'volume': 4961687},\n", 842 | " {'date': datetime.datetime(2023, 7, 6, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 843 | " 'open': 2455.5,\n", 844 | " 'high': 2520.7,\n", 845 | " 'low': 2455.5,\n", 846 | " 'close': 2515.25,\n", 847 | " 'volume': 9256137},\n", 848 | " {'date': datetime.datetime(2023, 7, 7, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 849 | " 'open': 2511.7,\n", 850 | " 'high': 2540.25,\n", 851 | " 'low': 2505,\n", 852 | " 'close': 2510.35,\n", 853 | " 'volume': 6475750},\n", 854 | " {'date': datetime.datetime(2023, 7, 10, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 855 | " 'open': 2563.05,\n", 856 | " 'high': 2627,\n", 857 | " 'low': 2549.8,\n", 858 | " 'close': 2607.05,\n", 859 | " 'volume': 16093438},\n", 860 | " {'date': datetime.datetime(2023, 7, 11, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 861 | " 'open': 2624.05,\n", 862 | " 'high': 2640.35,\n", 863 | " 'low': 2609.5,\n", 864 | " 'close': 2635.3,\n", 865 | " 'volume': 9716747},\n", 866 | " {'date': datetime.datetime(2023, 7, 12, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 867 | " 'open': 2636.85,\n", 868 | " 'high': 2670.85,\n", 869 | " 'low': 2632.4,\n", 870 | " 'close': 2638.2,\n", 871 | " 'volume': 9070147},\n", 872 | " {'date': datetime.datetime(2023, 7, 13, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 873 | " 'open': 2653.6,\n", 874 | " 'high': 2668,\n", 875 | " 'low': 2609.15,\n", 876 | " 'close': 2614.65,\n", 877 | " 'volume': 7108868},\n", 878 | " {'date': datetime.datetime(2023, 7, 14, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 879 | " 'open': 2621.3,\n", 880 | " 'high': 2631.7,\n", 881 | " 'low': 2597.55,\n", 882 | " 'close': 2612.45,\n", 883 | " 'volume': 7322484},\n", 884 | " {'date': datetime.datetime(2023, 7, 17, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 885 | " 'open': 2618.45,\n", 886 | " 'high': 2683.3,\n", 887 | " 'low': 2600.35,\n", 888 | " 'close': 2665.8,\n", 889 | " 'volume': 10758023},\n", 890 | " {'date': datetime.datetime(2023, 7, 18, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 891 | " 'open': 2685.15,\n", 892 | " 'high': 2704.65,\n", 893 | " 'low': 2662.3,\n", 894 | " 'close': 2688.45,\n", 895 | " 'volume': 12523881},\n", 896 | " {'date': datetime.datetime(2023, 7, 19, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 897 | " 'open': 2697.55,\n", 898 | " 'high': 2722.35,\n", 899 | " 'low': 2666.1,\n", 900 | " 'close': 2708.85,\n", 901 | " 'volume': 18941327},\n", 902 | " {'date': datetime.datetime(2023, 7, 20, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 903 | " 'open': 2580,\n", 904 | " 'high': 2630.95,\n", 905 | " 'low': 2580,\n", 906 | " 'close': 2619.85,\n", 907 | " 'volume': 19358812},\n", 908 | " {'date': datetime.datetime(2023, 7, 21, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 909 | " 'open': 2609,\n", 910 | " 'high': 2614.9,\n", 911 | " 'low': 2523.6,\n", 912 | " 'close': 2538.75,\n", 913 | " 'volume': 15124619},\n", 914 | " {'date': datetime.datetime(2023, 7, 24, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 915 | " 'open': 2481,\n", 916 | " 'high': 2514.95,\n", 917 | " 'low': 2469.3,\n", 918 | " 'close': 2487.4,\n", 919 | " 'volume': 11863933},\n", 920 | " {'date': datetime.datetime(2023, 7, 25, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 921 | " 'open': 2494,\n", 922 | " 'high': 2505.2,\n", 923 | " 'low': 2480,\n", 924 | " 'close': 2485.8,\n", 925 | " 'volume': 6013010},\n", 926 | " {'date': datetime.datetime(2023, 7, 26, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 927 | " 'open': 2485,\n", 928 | " 'high': 2547,\n", 929 | " 'low': 2485,\n", 930 | " 'close': 2526.2,\n", 931 | " 'volume': 6455506},\n", 932 | " {'date': datetime.datetime(2023, 7, 27, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 933 | " 'open': 2534.05,\n", 934 | " 'high': 2537.65,\n", 935 | " 'low': 2490.35,\n", 936 | " 'close': 2502.7,\n", 937 | " 'volume': 8613449},\n", 938 | " {'date': datetime.datetime(2023, 7, 28, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 939 | " 'open': 2512.05,\n", 940 | " 'high': 2542.85,\n", 941 | " 'low': 2500.55,\n", 942 | " 'close': 2527.85,\n", 943 | " 'volume': 10413926},\n", 944 | " {'date': datetime.datetime(2023, 7, 31, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 945 | " 'open': 2527.85,\n", 946 | " 'high': 2553.9,\n", 947 | " 'low': 2517.95,\n", 948 | " 'close': 2549.25,\n", 949 | " 'volume': 4275587},\n", 950 | " {'date': datetime.datetime(2023, 8, 1, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 951 | " 'open': 2555,\n", 952 | " 'high': 2559,\n", 953 | " 'low': 2505.3,\n", 954 | " 'close': 2513.2,\n", 955 | " 'volume': 4768316},\n", 956 | " {'date': datetime.datetime(2023, 8, 2, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 957 | " 'open': 2509.2,\n", 958 | " 'high': 2512,\n", 959 | " 'low': 2463.6,\n", 960 | " 'close': 2486.35,\n", 961 | " 'volume': 6374107},\n", 962 | " {'date': datetime.datetime(2023, 8, 3, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 963 | " 'open': 2475.3,\n", 964 | " 'high': 2500.9,\n", 965 | " 'low': 2458.05,\n", 966 | " 'close': 2475.9,\n", 967 | " 'volume': 5084175},\n", 968 | " {'date': datetime.datetime(2023, 8, 4, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 969 | " 'open': 2498.8,\n", 970 | " 'high': 2516,\n", 971 | " 'low': 2471.6,\n", 972 | " 'close': 2509.55,\n", 973 | " 'volume': 11442805},\n", 974 | " {'date': datetime.datetime(2023, 8, 7, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 975 | " 'open': 2521,\n", 976 | " 'high': 2528.4,\n", 977 | " 'low': 2505,\n", 978 | " 'close': 2523.85,\n", 979 | " 'volume': 2991435},\n", 980 | " {'date': datetime.datetime(2023, 8, 8, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 981 | " 'open': 2528,\n", 982 | " 'high': 2534.45,\n", 983 | " 'low': 2499.55,\n", 984 | " 'close': 2508.65,\n", 985 | " 'volume': 4922036},\n", 986 | " {'date': datetime.datetime(2023, 8, 9, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 987 | " 'open': 2504.85,\n", 988 | " 'high': 2529,\n", 989 | " 'low': 2481.1,\n", 990 | " 'close': 2525.2,\n", 991 | " 'volume': 5743293},\n", 992 | " {'date': datetime.datetime(2023, 8, 10, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 993 | " 'open': 2524.95,\n", 994 | " 'high': 2550,\n", 995 | " 'low': 2508.45,\n", 996 | " 'close': 2536.45,\n", 997 | " 'volume': 7735299},\n", 998 | " {'date': datetime.datetime(2023, 8, 11, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 999 | " 'open': 2532.45,\n", 1000 | " 'high': 2558.85,\n", 1001 | " 'low': 2512.35,\n", 1002 | " 'close': 2547.15,\n", 1003 | " 'volume': 11089665},\n", 1004 | " {'date': datetime.datetime(2023, 8, 14, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 1005 | " 'open': 2539,\n", 1006 | " 'high': 2582.3,\n", 1007 | " 'low': 2525,\n", 1008 | " 'close': 2577.25,\n", 1009 | " 'volume': 4787906},\n", 1010 | " {'date': datetime.datetime(2023, 8, 16, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 1011 | " 'open': 2551,\n", 1012 | " 'high': 2582.8,\n", 1013 | " 'low': 2551,\n", 1014 | " 'close': 2575.15,\n", 1015 | " 'volume': 5101556},\n", 1016 | " {'date': datetime.datetime(2023, 8, 17, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 1017 | " 'open': 2567.1,\n", 1018 | " 'high': 2578.1,\n", 1019 | " 'low': 2532.85,\n", 1020 | " 'close': 2538,\n", 1021 | " 'volume': 6836872},\n", 1022 | " {'date': datetime.datetime(2023, 8, 18, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 1023 | " 'open': 2531.25,\n", 1024 | " 'high': 2577.6,\n", 1025 | " 'low': 2508.55,\n", 1026 | " 'close': 2556.8,\n", 1027 | " 'volume': 9319989},\n", 1028 | " {'date': datetime.datetime(2023, 8, 21, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 1029 | " 'open': 2539.95,\n", 1030 | " 'high': 2555.45,\n", 1031 | " 'low': 2515.65,\n", 1032 | " 'close': 2520,\n", 1033 | " 'volume': 4610873},\n", 1034 | " {'date': datetime.datetime(2023, 8, 22, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 1035 | " 'open': 2516.9,\n", 1036 | " 'high': 2537.95,\n", 1037 | " 'low': 2499,\n", 1038 | " 'close': 2519.4,\n", 1039 | " 'volume': 3856522},\n", 1040 | " {'date': datetime.datetime(2023, 8, 23, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 1041 | " 'open': 2524.2,\n", 1042 | " 'high': 2542.85,\n", 1043 | " 'low': 2516.95,\n", 1044 | " 'close': 2522.2,\n", 1045 | " 'volume': 4758976},\n", 1046 | " {'date': datetime.datetime(2023, 8, 24, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 1047 | " 'open': 2539.9,\n", 1048 | " 'high': 2539.9,\n", 1049 | " 'low': 2471,\n", 1050 | " 'close': 2479.8,\n", 1051 | " 'volume': 7070010},\n", 1052 | " {'date': datetime.datetime(2023, 8, 25, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 1053 | " 'open': 2456,\n", 1054 | " 'high': 2505,\n", 1055 | " 'low': 2442.6,\n", 1056 | " 'close': 2468.35,\n", 1057 | " 'volume': 11111200},\n", 1058 | " {'date': datetime.datetime(2023, 8, 28, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 1059 | " 'open': 2472,\n", 1060 | " 'high': 2484,\n", 1061 | " 'low': 2431.1,\n", 1062 | " 'close': 2443.75,\n", 1063 | " 'volume': 6290413},\n", 1064 | " {'date': datetime.datetime(2023, 8, 29, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 1065 | " 'open': 2452.05,\n", 1066 | " 'high': 2453.45,\n", 1067 | " 'low': 2408.15,\n", 1068 | " 'close': 2420.35,\n", 1069 | " 'volume': 8438322},\n", 1070 | " {'date': datetime.datetime(2023, 8, 30, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 1071 | " 'open': 2432,\n", 1072 | " 'high': 2443,\n", 1073 | " 'low': 2415,\n", 1074 | " 'close': 2418.05,\n", 1075 | " 'volume': 6084473},\n", 1076 | " {'date': datetime.datetime(2023, 8, 31, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 1077 | " 'open': 2423,\n", 1078 | " 'high': 2425,\n", 1079 | " 'low': 2399.9,\n", 1080 | " 'close': 2407,\n", 1081 | " 'volume': 10819300},\n", 1082 | " {'date': datetime.datetime(2023, 9, 1, 0, 0, tzinfo=tzoffset(None, 19800)),\n", 1083 | " 'open': 2406.55,\n", 1084 | " 'high': 2425.65,\n", 1085 | " 'low': 2401.65,\n", 1086 | " 'close': 2412.65,\n", 1087 | " 'volume': 9056421}]" 1088 | ] 1089 | }, 1090 | "execution_count": 5, 1091 | "metadata": {}, 1092 | "output_type": "execute_result" 1093 | } 1094 | ], 1095 | "source": [ 1096 | "# Fetch historical data for RELIANCE\n", 1097 | "historical_data = kite.historical_data(instrument_token, '2023-01-01', '2023-09-01', interval='day')\n", 1098 | "historical_data" 1099 | ] 1100 | }, 1101 | { 1102 | "cell_type": "code", 1103 | "execution_count": null, 1104 | "id": "eaf97a37", 1105 | "metadata": {}, 1106 | "outputs": [], 1107 | "source": [] 1108 | } 1109 | ], 1110 | "metadata": { 1111 | "kernelspec": { 1112 | "display_name": "Python 3 (ipykernel)", 1113 | "language": "python", 1114 | "name": "python3" 1115 | }, 1116 | "language_info": { 1117 | "codemirror_mode": { 1118 | "name": "ipython", 1119 | "version": 3 1120 | }, 1121 | "file_extension": ".py", 1122 | "mimetype": "text/x-python", 1123 | "name": "python", 1124 | "nbconvert_exporter": "python", 1125 | "pygments_lexer": "ipython3", 1126 | "version": "3.10.12" 1127 | } 1128 | }, 1129 | "nbformat": 4, 1130 | "nbformat_minor": 5 1131 | } 1132 | -------------------------------------------------------------------------------- /1.4 - Getting Historical Data of Reliance​ In Pandas Using Python and Zerodha API.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "80cf8822", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "# https://unofficed.com/courses/mastering-algotrading-a-beginners-guide-using-kiteconnect-api/lessons/getting-historical-data-of-reliance-stocks-using-python-and-zerodha-api/" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "id": "1d446c7b", 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stderr", 21 | "output_type": "stream", 22 | "text": [ 23 | "/tmp/ipykernel_4154679/3058104947.py:7: DtypeWarning: Columns (1) have mixed types.Specify dtype option on import or set low_memory=False.\n", 24 | " from kite.kitecustom import *\n" 25 | ] 26 | } 27 | ], 28 | "source": [ 29 | "import logging\n", 30 | "from kiteconnect import KiteConnect\n", 31 | "\n", 32 | "logging.basicConfig(level=logging.DEBUG)\n", 33 | "\n", 34 | "kite = KiteConnect(api_key=\"your_api_key\")\n", 35 | "\n", 36 | "# Redirect the user to the login url obtained\n", 37 | "# from kite.login_url(), and receive the request_token\n", 38 | "# from the registered redirect url after the login flow.\n", 39 | "# Once you have the request_token, obtain the access_token\n", 40 | "# as follows.\n", 41 | "\n", 42 | "data = kite.generate_session(\"request_token_here\", api_secret=\"your_secret\")\n", 43 | "kite.set_access_token(data[\"access_token\"])" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 2, 49 | "id": "58dbcb1d", 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "token = 738561" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 3, 59 | "id": "3a7cc37a", 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "import datetime\n", 64 | "starting_date=datetime.datetime(2019, 1, 1)\n", 65 | "ending_date=datetime.datetime.today()" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 4, 71 | "id": "eb26ad4c", 72 | "metadata": {}, 73 | "outputs": [ 74 | { 75 | "name": "stdout", 76 | "output_type": "stream", 77 | "text": [ 78 | " date open high low close volume\n", 79 | "0 2019-01-01 00:00:00+05:30 1072.60 1074.55 1058.15 1068.55 4674623\n", 80 | "1 2019-01-02 00:00:00+05:30 1062.35 1074.25 1049.45 1054.60 7495774\n", 81 | "2 2019-01-03 00:00:00+05:30 1055.65 1062.45 1039.05 1041.60 7812063\n", 82 | "3 2019-01-04 00:00:00+05:30 1046.05 1052.75 1030.50 1047.25 8880762\n", 83 | "4 2019-01-07 00:00:00+05:30 1055.20 1066.10 1049.45 1053.05 5784264\n", 84 | "5 2019-01-08 00:00:00+05:30 1053.35 1058.00 1044.70 1052.95 5901337\n", 85 | "6 2019-01-09 00:00:00+05:30 1059.95 1064.70 1047.30 1058.75 6049944\n", 86 | "7 2019-01-10 00:00:00+05:30 1055.90 1059.00 1051.40 1055.65 4280617\n", 87 | "8 2019-01-11 00:00:00+05:30 1055.75 1061.65 1037.65 1046.65 6781268\n", 88 | "9 2019-01-14 00:00:00+05:30 1043.75 1049.00 1035.55 1045.45 4313662\n" 89 | ] 90 | } 91 | ], 92 | "source": [ 93 | "zap=kite.historical_data(token,starting_date,ending_date,\"day\")\n", 94 | "zap= pd.DataFrame(zap)\n", 95 | "print(zap.head(10))" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 6, 101 | "id": "a94ee70f", 102 | "metadata": {}, 103 | "outputs": [ 104 | { 105 | "ename": "InputException", 106 | "evalue": "interval exceeds max limit: 60 days", 107 | "output_type": "error", 108 | "traceback": [ 109 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 110 | "\u001b[0;31mInputException\u001b[0m Traceback (most recent call last)", 111 | "Cell \u001b[0;32mIn[6], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m zap\u001b[38;5;241m=\u001b[39m\u001b[43mkite\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mhistorical_data\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtoken\u001b[49m\u001b[43m,\u001b[49m\u001b[43mstarting_date\u001b[49m\u001b[43m,\u001b[49m\u001b[43mending_date\u001b[49m\u001b[43m,\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mminute\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2\u001b[0m zap\u001b[38;5;241m=\u001b[39m pd\u001b[38;5;241m.\u001b[39mDataFrame(zap)\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(zap\u001b[38;5;241m.\u001b[39mhead(\u001b[38;5;241m10\u001b[39m))\n", 112 | "File \u001b[0;32m~/apps/zerodha/../kiteconnect/connect.py:503\u001b[0m, in \u001b[0;36mKiteConnect.historical_data\u001b[0;34m(self, instrument_token, from_date, to_date, interval, continuous, oi)\u001b[0m\n\u001b[1;32m 500\u001b[0m from_date_string \u001b[38;5;241m=\u001b[39m from_date\u001b[38;5;241m.\u001b[39mstrftime(date_string_format) \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mtype\u001b[39m(from_date) \u001b[38;5;241m==\u001b[39m datetime\u001b[38;5;241m.\u001b[39mdatetime \u001b[38;5;28;01melse\u001b[39;00m from_date\n\u001b[1;32m 501\u001b[0m to_date_string \u001b[38;5;241m=\u001b[39m to_date\u001b[38;5;241m.\u001b[39mstrftime(date_string_format) \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mtype\u001b[39m(to_date) \u001b[38;5;241m==\u001b[39m datetime\u001b[38;5;241m.\u001b[39mdatetime \u001b[38;5;28;01melse\u001b[39;00m to_date\n\u001b[0;32m--> 503\u001b[0m data \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmarket.historical\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m{\u001b[49m\n\u001b[1;32m 504\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43minstrument_token\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43minstrument_token\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 505\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mfrom\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mfrom_date_string\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 506\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mto\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43mto_date_string\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 507\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43minterval\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[43minterval\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 508\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcontinuous\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mcontinuous\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01melse\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 509\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43moi\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43moi\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01melse\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\n\u001b[1;32m 510\u001b[0m \u001b[43m\u001b[49m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 512\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_format_historical(data, oi)\n", 113 | "File \u001b[0;32m~/apps/zerodha/../kiteconnect/connect.py:601\u001b[0m, in \u001b[0;36mKiteConnect._get\u001b[0;34m(self, route, params)\u001b[0m\n\u001b[1;32m 599\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_get\u001b[39m(\u001b[38;5;28mself\u001b[39m, route, params\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[1;32m 600\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Alias for sending a GET request.\"\"\"\u001b[39;00m\n\u001b[0;32m--> 601\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_request\u001b[49m\u001b[43m(\u001b[49m\u001b[43mroute\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mGET\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mparams\u001b[49m\u001b[43m)\u001b[49m\n", 114 | "File \u001b[0;32m~/apps/zerodha/../kiteconnect/connect.py:671\u001b[0m, in \u001b[0;36mKiteConnect._request\u001b[0;34m(self, route, method, parameters)\u001b[0m\n\u001b[1;32m 669\u001b[0m \u001b[38;5;66;03m# native Kite errors\u001b[39;00m\n\u001b[1;32m 670\u001b[0m exp \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mgetattr\u001b[39m(ex, data[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124merror_type\u001b[39m\u001b[38;5;124m\"\u001b[39m], ex\u001b[38;5;241m.\u001b[39mGeneralException)\n\u001b[0;32m--> 671\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exp(data[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmessage\u001b[39m\u001b[38;5;124m\"\u001b[39m], code\u001b[38;5;241m=\u001b[39mr\u001b[38;5;241m.\u001b[39mstatus_code)\n\u001b[1;32m 673\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m data[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdata\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[1;32m 674\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcsv\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m r\u001b[38;5;241m.\u001b[39mheaders[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcontent-type\u001b[39m\u001b[38;5;124m\"\u001b[39m]:\n", 115 | "\u001b[0;31mInputException\u001b[0m: interval exceeds max limit: 60 days" 116 | ] 117 | } 118 | ], 119 | "source": [ 120 | "zap=kite.historical_data(token,starting_date,ending_date,\"minute\")\n", 121 | "zap= pd.DataFrame(zap)\n", 122 | "print(zap.head(10))" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 7, 128 | "id": "127aa37e", 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "data": { 133 | "text/html": [ 134 | "
\n", 135 | "\n", 148 | "\n", 149 | " \n", 150 | " \n", 151 | " \n", 152 | " \n", 153 | " \n", 154 | " \n", 155 | " \n", 156 | " \n", 157 | " \n", 158 | " \n", 159 | " \n", 160 | " \n", 161 | " \n", 162 | " \n", 163 | " \n", 164 | " \n", 165 | " \n", 166 | " \n", 167 | " \n", 168 | " \n", 169 | " \n", 170 | " \n", 171 | " \n", 172 | " \n", 173 | " \n", 174 | " \n", 175 | " \n", 176 | " \n", 177 | " \n", 178 | " \n", 179 | " \n", 180 | " \n", 181 | " \n", 182 | " \n", 183 | " \n", 184 | " \n", 185 | " \n", 186 | " \n", 187 | " \n", 188 | " \n", 189 | " \n", 190 | " \n", 191 | " \n", 192 | " \n", 193 | " \n", 194 | " \n", 195 | " \n", 196 | " \n", 197 | " \n", 198 | " \n", 199 | " \n", 200 | " \n", 201 | " \n", 202 | " \n", 203 | " \n", 204 | " \n", 205 | " \n", 206 | " \n", 207 | " \n", 208 | " \n", 209 | " \n", 210 | " \n", 211 | " \n", 212 | " \n", 213 | " \n", 214 | " \n", 215 | " \n", 216 | " \n", 217 | " \n", 218 | " \n", 219 | " \n", 220 | " \n", 221 | " \n", 222 | " \n", 223 | " \n", 224 | " \n", 225 | " \n", 226 | " \n", 227 | " \n", 228 | " \n", 229 | " \n", 230 | " \n", 231 | " \n", 232 | " \n", 233 | " \n", 234 | " \n", 235 | " \n", 236 | " \n", 237 | " \n", 238 | " \n", 239 | " \n", 240 | " \n", 241 | " \n", 242 | " \n", 243 | " \n", 244 | " \n", 245 | " \n", 246 | " \n", 247 | " \n", 248 | " \n", 249 | " \n", 250 | " \n", 251 | " \n", 252 | " \n", 253 | " \n", 254 | " \n", 255 | " \n", 256 | " \n", 257 | " \n", 258 | " \n", 259 | " \n", 260 | " \n", 261 | "
dateopenhighlowclosevolume
02019-01-01 09:15:00+05:301062.551063.901060.251061.6553427
12019-01-01 09:16:00+05:301062.251063.851061.651063.4018209
22019-01-01 09:17:00+05:301063.401063.901063.251063.4042785
32019-01-01 09:18:00+05:301063.401063.501062.301062.6040647
42019-01-01 09:19:00+05:301062.301062.301059.501059.5044316
.....................
4540632023-11-24 15:25:00+05:302395.952396.752395.002395.6518228
4540642023-11-24 15:26:00+05:302395.552396.002394.852394.8512166
4540652023-11-24 15:27:00+05:302395.002395.302394.502394.8017623
4540662023-11-24 15:28:00+05:302394.552394.802394.002394.0019879
4540672023-11-24 15:29:00+05:302394.402394.902394.002394.259111
\n", 262 | "

454068 rows × 6 columns

\n", 263 | "
" 264 | ], 265 | "text/plain": [ 266 | " date open high low close volume\n", 267 | "0 2019-01-01 09:15:00+05:30 1062.55 1063.90 1060.25 1061.65 53427\n", 268 | "1 2019-01-01 09:16:00+05:30 1062.25 1063.85 1061.65 1063.40 18209\n", 269 | "2 2019-01-01 09:17:00+05:30 1063.40 1063.90 1063.25 1063.40 42785\n", 270 | "3 2019-01-01 09:18:00+05:30 1063.40 1063.50 1062.30 1062.60 40647\n", 271 | "4 2019-01-01 09:19:00+05:30 1062.30 1062.30 1059.50 1059.50 44316\n", 272 | "... ... ... ... ... ... ...\n", 273 | "454063 2023-11-24 15:25:00+05:30 2395.95 2396.75 2395.00 2395.65 18228\n", 274 | "454064 2023-11-24 15:26:00+05:30 2395.55 2396.00 2394.85 2394.85 12166\n", 275 | "454065 2023-11-24 15:27:00+05:30 2395.00 2395.30 2394.50 2394.80 17623\n", 276 | "454066 2023-11-24 15:28:00+05:30 2394.55 2394.80 2394.00 2394.00 19879\n", 277 | "454067 2023-11-24 15:29:00+05:30 2394.40 2394.90 2394.00 2394.25 9111\n", 278 | "\n", 279 | "[454068 rows x 6 columns]" 280 | ] 281 | }, 282 | "execution_count": 7, 283 | "metadata": {}, 284 | "output_type": "execute_result" 285 | } 286 | ], 287 | "source": [ 288 | "def fetch_historical_data(kite, token, start_date, end_date, interval):\n", 289 | " # Initialize an empty DataFrame to store the historical data\n", 290 | " full_data = pd.DataFrame()\n", 291 | "\n", 292 | " # Split the date range into 60-day intervals\n", 293 | " while start_date < end_date:\n", 294 | " # Calculate the end date of the 60-day interval\n", 295 | " interval_end = start_date + datetime.timedelta(days=60)\n", 296 | "\n", 297 | " # Ensure the interval end does not exceed the overall end date\n", 298 | " if interval_end > end_date:\n", 299 | " interval_end = end_date\n", 300 | "\n", 301 | " # Fetch historical data for the 60-day interval\n", 302 | " data = kite.historical_data(token, start_date, interval_end, interval)\n", 303 | " data_df = pd.DataFrame(data)\n", 304 | "\n", 305 | " # Append the fetched data to the full dataset\n", 306 | " full_data = full_data.append(data_df, ignore_index=True)\n", 307 | "\n", 308 | " # Update the start date for the next interval\n", 309 | " start_date = interval_end\n", 310 | "\n", 311 | " return full_data\n", 312 | "\n", 313 | "historical_data = fetch_historical_data(kite, token, starting_date, ending_date, \"minute\")\n", 314 | "historical_data" 315 | ] 316 | }, 317 | { 318 | "cell_type": "code", 319 | "execution_count": null, 320 | "id": "e51b086c", 321 | "metadata": {}, 322 | "outputs": [], 323 | "source": [] 324 | } 325 | ], 326 | "metadata": { 327 | "kernelspec": { 328 | "display_name": "Python 3 (ipykernel)", 329 | "language": "python", 330 | "name": "python3" 331 | }, 332 | "language_info": { 333 | "codemirror_mode": { 334 | "name": "ipython", 335 | "version": 3 336 | }, 337 | "file_extension": ".py", 338 | "mimetype": "text/x-python", 339 | "name": "python", 340 | "nbconvert_exporter": "python", 341 | "pygments_lexer": "ipython3", 342 | "version": "3.10.12" 343 | } 344 | }, 345 | "nbformat": 4, 346 | "nbformat_minor": 5 347 | } 348 | -------------------------------------------------------------------------------- /2.4 - Creating TimeFrames from Minute-Based Data Using Python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "id": "93e36e7e", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "# https://unofficed.com/courses/mastering-algotrading-a-beginners-guide-using-kiteconnect-api/lessons/creating-timeframes-from-minute-based-data-using-python/" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "id": "ba490eb9", 17 | "metadata": {}, 18 | "outputs": [ 19 | { 20 | "name": "stderr", 21 | "output_type": "stream", 22 | "text": [ 23 | "/tmp/ipykernel_4148432/1105643221.py:8: DtypeWarning: Columns (1) have mixed types.Specify dtype option on import or set low_memory=False.\n", 24 | " from kite.kitecustom import *\n" 25 | ] 26 | } 27 | ], 28 | "source": [ 29 | "import logging\n", 30 | "from kiteconnect import KiteConnect\n", 31 | "\n", 32 | "logging.basicConfig(level=logging.DEBUG)\n", 33 | "\n", 34 | "kite = KiteConnect(api_key=\"your_api_key\")\n", 35 | "\n", 36 | "# Redirect the user to the login url obtained\n", 37 | "# from kite.login_url(), and receive the request_token\n", 38 | "# from the registered redirect url after the login flow.\n", 39 | "# Once you have the request_token, obtain the access_token\n", 40 | "# as follows.\n", 41 | "\n", 42 | "data = kite.generate_session(\"request_token_here\", api_secret=\"your_secret\")\n", 43 | "kite.set_access_token(data[\"access_token\"])" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 6, 49 | "id": "51e31406", 50 | "metadata": {}, 51 | "outputs": [ 52 | { 53 | "data": { 54 | "text/html": [ 55 | "
\n", 56 | "\n", 69 | "\n", 70 | " \n", 71 | " \n", 72 | " \n", 73 | " \n", 74 | " \n", 75 | " \n", 76 | " \n", 77 | " \n", 78 | " \n", 79 | " \n", 80 | " \n", 81 | " \n", 82 | " \n", 83 | " \n", 84 | " \n", 85 | " \n", 86 | " \n", 87 | " \n", 88 | " \n", 89 | " \n", 90 | " \n", 91 | " \n", 92 | " \n", 93 | " \n", 94 | " \n", 95 | " \n", 96 | " \n", 97 | " \n", 98 | " \n", 99 | " \n", 100 | " \n", 101 | " \n", 102 | " \n", 103 | " \n", 104 | " \n", 105 | " \n", 106 | " \n", 107 | " \n", 108 | " \n", 109 | " \n", 110 | " \n", 111 | " \n", 112 | " \n", 113 | " \n", 114 | " \n", 115 | " \n", 116 | " \n", 117 | " \n", 118 | " \n", 119 | " \n", 120 | " \n", 121 | " \n", 122 | " \n", 123 | " \n", 124 | " \n", 125 | " \n", 126 | " \n", 127 | " \n", 128 | " \n", 129 | " \n", 130 | " \n", 131 | " \n", 132 | " \n", 133 | " \n", 134 | " \n", 135 | " \n", 136 | " \n", 137 | " \n", 138 | " \n", 139 | " \n", 140 | " \n", 141 | " \n", 142 | " \n", 143 | " \n", 144 | " \n", 145 | " \n", 146 | " \n", 147 | " \n", 148 | " \n", 149 | " \n", 150 | " \n", 151 | " \n", 152 | " \n", 153 | " \n", 154 | " \n", 155 | " \n", 156 | " \n", 157 | " \n", 158 | " \n", 159 | " \n", 160 | " \n", 161 | " \n", 162 | " \n", 163 | " \n", 164 | " \n", 165 | " \n", 166 | " \n", 167 | " \n", 168 | " \n", 169 | " \n", 170 | " \n", 171 | " \n", 172 | " \n", 173 | " \n", 174 | " \n", 175 | " \n", 176 | " \n", 177 | " \n", 178 | " \n", 179 | " \n", 180 | " \n", 181 | " \n", 182 | "
dateopenhighlowclosevolume
02023-11-10 09:15:00+05:302305.552308.902303.552305.0041248
12023-11-10 09:16:00+05:302304.952306.252303.002305.7028564
22023-11-10 09:17:00+05:302305.052308.802305.052308.6033568
32023-11-10 09:18:00+05:302308.602310.452308.202310.4539478
42023-11-10 09:19:00+05:302310.452311.952310.002310.1023513
.....................
38052023-11-24 15:25:00+05:302395.952396.752395.002395.6518228
38062023-11-24 15:26:00+05:302395.552396.002394.852394.8512166
38072023-11-24 15:27:00+05:302395.002395.302394.502394.8017623
38082023-11-24 15:28:00+05:302394.552394.802394.002394.0019879
38092023-11-24 15:29:00+05:302394.402394.902394.002394.259111
\n", 183 | "

3810 rows × 6 columns

\n", 184 | "
" 185 | ], 186 | "text/plain": [ 187 | " date open high low close volume\n", 188 | "0 2023-11-10 09:15:00+05:30 2305.55 2308.90 2303.55 2305.00 41248\n", 189 | "1 2023-11-10 09:16:00+05:30 2304.95 2306.25 2303.00 2305.70 28564\n", 190 | "2 2023-11-10 09:17:00+05:30 2305.05 2308.80 2305.05 2308.60 33568\n", 191 | "3 2023-11-10 09:18:00+05:30 2308.60 2310.45 2308.20 2310.45 39478\n", 192 | "4 2023-11-10 09:19:00+05:30 2310.45 2311.95 2310.00 2310.10 23513\n", 193 | "... ... ... ... ... ... ...\n", 194 | "3805 2023-11-24 15:25:00+05:30 2395.95 2396.75 2395.00 2395.65 18228\n", 195 | "3806 2023-11-24 15:26:00+05:30 2395.55 2396.00 2394.85 2394.85 12166\n", 196 | "3807 2023-11-24 15:27:00+05:30 2395.00 2395.30 2394.50 2394.80 17623\n", 197 | "3808 2023-11-24 15:28:00+05:30 2394.55 2394.80 2394.00 2394.00 19879\n", 198 | "3809 2023-11-24 15:29:00+05:30 2394.40 2394.90 2394.00 2394.25 9111\n", 199 | "\n", 200 | "[3810 rows x 6 columns]" 201 | ] 202 | }, 203 | "execution_count": 6, 204 | "metadata": {}, 205 | "output_type": "execute_result" 206 | } 207 | ], 208 | "source": [ 209 | "from datetime import date,timedelta\n", 210 | "starting_date= (date.today() - timedelta(days=15)).strftime(\"%Y-%m-%d\")\n", 211 | "ending_date=date.today().strftime(\"%Y-%m-%d\")\n", 212 | "\n", 213 | "token = \"738561\" #Reliance Token \n", 214 | "\n", 215 | "zap=kite.historical_data(token,starting_date,ending_date,\"minute\")\n", 216 | "zap= pd.DataFrame(zap)\n", 217 | "zap" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 8, 223 | "id": "cade917c", 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "data": { 228 | "text/html": [ 229 | "
\n", 230 | "\n", 243 | "\n", 244 | " \n", 245 | " \n", 246 | " \n", 247 | " \n", 248 | " \n", 249 | " \n", 250 | " \n", 251 | " \n", 252 | " \n", 253 | " \n", 254 | " \n", 255 | " \n", 256 | " \n", 257 | " \n", 258 | " \n", 259 | " \n", 260 | " \n", 261 | " \n", 262 | " \n", 263 | " \n", 264 | " \n", 265 | " \n", 266 | " \n", 267 | " \n", 268 | " \n", 269 | " \n", 270 | " \n", 271 | " \n", 272 | " \n", 273 | " \n", 274 | " \n", 275 | " \n", 276 | " \n", 277 | " \n", 278 | " \n", 279 | " \n", 280 | " \n", 281 | " \n", 282 | " \n", 283 | " \n", 284 | " \n", 285 | " \n", 286 | " \n", 287 | " \n", 288 | " \n", 289 | " \n", 290 | " \n", 291 | " \n", 292 | " \n", 293 | " \n", 294 | " \n", 295 | " \n", 296 | " \n", 297 | " \n", 298 | " \n", 299 | " \n", 300 | " \n", 301 | " \n", 302 | " \n", 303 | " \n", 304 | " \n", 305 | " \n", 306 | " \n", 307 | " \n", 308 | " \n", 309 | " \n", 310 | " \n", 311 | " \n", 312 | " \n", 313 | " \n", 314 | " \n", 315 | " \n", 316 | " \n", 317 | " \n", 318 | " \n", 319 | " \n", 320 | " \n", 321 | " \n", 322 | " \n", 323 | " \n", 324 | " \n", 325 | " \n", 326 | " \n", 327 | " \n", 328 | " \n", 329 | " \n", 330 | " \n", 331 | " \n", 332 | " \n", 333 | " \n", 334 | " \n", 335 | " \n", 336 | " \n", 337 | " \n", 338 | " \n", 339 | " \n", 340 | " \n", 341 | " \n", 342 | " \n", 343 | " \n", 344 | " \n", 345 | " \n", 346 | " \n", 347 | " \n", 348 | " \n", 349 | " \n", 350 | " \n", 351 | " \n", 352 | " \n", 353 | " \n", 354 | " \n", 355 | " \n", 356 | " \n", 357 | " \n", 358 | " \n", 359 | " \n", 360 | " \n", 361 | " \n", 362 | " \n", 363 | " \n", 364 | " \n", 365 | " \n", 366 | " \n", 367 | " \n", 368 | " \n", 369 | " \n", 370 | " \n", 371 | " \n", 372 | " \n", 373 | " \n", 374 | " \n", 375 | " \n", 376 | " \n", 377 | " \n", 378 | " \n", 379 | " \n", 380 | " \n", 381 | " \n", 382 | " \n", 383 | " \n", 384 | " \n", 385 | " \n", 386 | " \n", 387 | " \n", 388 | " \n", 389 | " \n", 390 | " \n", 391 | " \n", 392 | " \n", 393 | " \n", 394 | " \n", 395 | " \n", 396 | " \n", 397 | " \n", 398 | " \n", 399 | " \n", 400 | " \n", 401 | " \n", 402 | " \n", 403 | " \n", 404 | " \n", 405 | " \n", 406 | " \n", 407 | " \n", 408 | " \n", 409 | " \n", 410 | " \n", 411 | " \n", 412 | " \n", 413 | " \n", 414 | " \n", 415 | " \n", 416 | " \n", 417 | " \n", 418 | " \n", 419 | " \n", 420 | " \n", 421 | " \n", 422 | " \n", 423 | " \n", 424 | " \n", 425 | " \n", 426 | " \n", 427 | " \n", 428 | " \n", 429 | " \n", 430 | " \n", 431 | " \n", 432 | " \n", 433 | " \n", 434 | " \n", 435 | " \n", 436 | " \n", 437 | " \n", 438 | " \n", 439 | " \n", 440 | " \n", 441 | " \n", 442 | " \n", 443 | " \n", 444 | " \n", 445 | " \n", 446 | " \n", 447 | " \n", 448 | " \n", 449 | " \n", 450 | " \n", 451 | " \n", 452 | " \n", 453 | " \n", 454 | " \n", 455 | " \n", 456 | " \n", 457 | " \n", 458 | " \n", 459 | " \n", 460 | " \n", 461 | " \n", 462 | " \n", 463 | " \n", 464 | " \n", 465 | " \n", 466 | " \n", 467 | " \n", 468 | " \n", 469 | " \n", 470 | " \n", 471 | " \n", 472 | " \n", 473 | " \n", 474 | " \n", 475 | " \n", 476 | " \n", 477 | " \n", 478 | " \n", 479 | " \n", 480 | " \n", 481 | " \n", 482 | " \n", 483 | " \n", 484 | " \n", 485 | " \n", 486 | " \n", 487 | " \n", 488 | " \n", 489 | " \n", 490 | " \n", 491 | " \n", 492 | " \n", 493 | " \n", 494 | " \n", 495 | " \n", 496 | " \n", 497 | " \n", 498 | " \n", 499 | " \n", 500 | " \n", 501 | " \n", 502 | " \n", 503 | " \n", 504 | " \n", 505 | " \n", 506 | " \n", 507 | " \n", 508 | " \n", 509 | " \n", 510 | " \n", 511 | " \n", 512 | " \n", 513 | " \n", 514 | " \n", 515 | " \n", 516 | " \n", 517 | " \n", 518 | " \n", 519 | " \n", 520 | " \n", 521 | " \n", 522 | " \n", 523 | " \n", 524 | " \n", 525 | " \n", 526 | " \n", 527 | " \n", 528 | " \n", 529 | " \n", 530 | " \n", 531 | " \n", 532 | " \n", 533 | " \n", 534 | " \n", 535 | " \n", 536 | " \n", 537 | " \n", 538 | " \n", 539 | " \n", 540 | " \n", 541 | " \n", 542 | " \n", 543 | " \n", 544 | " \n", 545 | " \n", 546 | " \n", 547 | " \n", 548 | " \n", 549 | " \n", 550 | " \n", 551 | " \n", 552 | " \n", 553 | " \n", 554 | " \n", 555 | " \n", 556 | " \n", 557 | " \n", 558 | " \n", 559 | " \n", 560 | " \n", 561 | " \n", 562 | " \n", 563 | " \n", 564 | " \n", 565 | " \n", 566 | " \n", 567 | " \n", 568 | " \n", 569 | " \n", 570 | " \n", 571 | " \n", 572 | " \n", 573 | " \n", 574 | " \n", 575 | " \n", 576 | " \n", 577 | " \n", 578 | " \n", 579 | " \n", 580 | " \n", 581 | " \n", 582 | " \n", 583 | " \n", 584 | " \n", 585 | " \n", 586 | " \n", 587 | " \n", 588 | " \n", 589 | " \n", 590 | " \n", 591 | " \n", 592 | " \n", 593 | " \n", 594 | " \n", 595 | " \n", 596 | " \n", 597 | " \n", 598 | " \n", 599 | " \n", 600 | " \n", 601 | " \n", 602 | " \n", 603 | " \n", 604 | " \n", 605 | " \n", 606 | " \n", 607 | " \n", 608 | " \n", 609 | " \n", 610 | " \n", 611 | " \n", 612 | " \n", 613 | " \n", 614 | " \n", 615 | " \n", 616 | " \n", 617 | " \n", 618 | " \n", 619 | " \n", 620 | " \n", 621 | " \n", 622 | " \n", 623 | " \n", 624 | " \n", 625 | " \n", 626 | "
dateopenhighlowclosevolume
02023-11-10 08:00:00+05:302305.552316.352298.902301.001417820
12023-11-10 10:00:00+05:302301.002307.352298.052305.60645230
22023-11-10 12:00:00+05:302305.652315.102305.102309.10813375
32023-11-10 14:00:00+05:302309.152316.002307.002315.90942613
42023-11-12 18:00:00+05:302326.052332.002322.702330.90685748
52023-11-13 08:00:00+05:302322.902324.602316.202317.00295834
62023-11-13 10:00:00+05:302317.052318.852311.702315.50418459
72023-11-13 12:00:00+05:302315.302318.002312.002314.75367694
82023-11-13 14:00:00+05:302314.952316.752312.002315.10798828
92023-11-15 08:00:00+05:302340.002342.602327.002335.551015224
102023-11-15 10:00:00+05:302335.552348.002335.002344.101867153
112023-11-15 12:00:00+05:302344.002352.902343.852346.85992835
122023-11-15 14:00:00+05:302346.952361.952346.802356.102003118
132023-11-16 08:00:00+05:302351.102364.002346.952355.25923205
142023-11-16 10:00:00+05:302355.002369.602354.702366.451049582
152023-11-16 12:00:00+05:302366.502369.352363.802367.00727641
162023-11-16 14:00:00+05:302366.752374.152350.552359.003758974
172023-11-17 08:00:00+05:302352.902370.302352.052369.95513251
182023-11-17 10:00:00+05:302370.052373.252363.802367.001017076
192023-11-17 12:00:00+05:302366.802371.602364.252366.80849327
202023-11-17 14:00:00+05:302366.802368.002353.052354.851454868
212023-11-20 08:00:00+05:302348.552358.402342.002345.45507002
222023-11-20 10:00:00+05:302346.352347.252336.402341.30478746
232023-11-20 12:00:00+05:302341.252347.952340.352342.25354916
242023-11-20 14:00:00+05:302342.252352.702341.152349.00826822
252023-11-21 08:00:00+05:302366.002380.002360.202372.70876474
262023-11-21 10:00:00+05:302372.952379.652371.002379.00857184
272023-11-21 12:00:00+05:302378.802388.002378.302381.201150310
282023-11-21 14:00:00+05:302381.202382.852375.002377.001065302
292023-11-22 08:00:00+05:302375.002394.452375.002387.901362028
302023-11-22 10:00:00+05:302387.702392.802378.052379.951190981
312023-11-22 12:00:00+05:302379.952385.002372.202380.00921422
322023-11-22 14:00:00+05:302379.752390.002378.052387.00702310
332023-11-23 08:00:00+05:302388.202399.752388.202396.251124039
342023-11-23 10:00:00+05:302396.202400.002390.252392.751194248
352023-11-23 12:00:00+05:302393.002399.002392.152398.45819897
362023-11-23 14:00:00+05:302398.452399.102390.502393.901067299
372023-11-24 08:00:00+05:302391.602402.602391.052397.50493994
382023-11-24 10:00:00+05:302397.402401.002393.552400.05971111
392023-11-24 12:00:00+05:302400.102401.002395.102397.85941540
402023-11-24 14:00:00+05:302397.902399.502392.052394.25934921
\n", 627 | "
" 628 | ], 629 | "text/plain": [ 630 | " date open high low close volume\n", 631 | "0 2023-11-10 08:00:00+05:30 2305.55 2316.35 2298.90 2301.00 1417820\n", 632 | "1 2023-11-10 10:00:00+05:30 2301.00 2307.35 2298.05 2305.60 645230\n", 633 | "2 2023-11-10 12:00:00+05:30 2305.65 2315.10 2305.10 2309.10 813375\n", 634 | "3 2023-11-10 14:00:00+05:30 2309.15 2316.00 2307.00 2315.90 942613\n", 635 | "4 2023-11-12 18:00:00+05:30 2326.05 2332.00 2322.70 2330.90 685748\n", 636 | "5 2023-11-13 08:00:00+05:30 2322.90 2324.60 2316.20 2317.00 295834\n", 637 | "6 2023-11-13 10:00:00+05:30 2317.05 2318.85 2311.70 2315.50 418459\n", 638 | "7 2023-11-13 12:00:00+05:30 2315.30 2318.00 2312.00 2314.75 367694\n", 639 | "8 2023-11-13 14:00:00+05:30 2314.95 2316.75 2312.00 2315.10 798828\n", 640 | "9 2023-11-15 08:00:00+05:30 2340.00 2342.60 2327.00 2335.55 1015224\n", 641 | "10 2023-11-15 10:00:00+05:30 2335.55 2348.00 2335.00 2344.10 1867153\n", 642 | "11 2023-11-15 12:00:00+05:30 2344.00 2352.90 2343.85 2346.85 992835\n", 643 | "12 2023-11-15 14:00:00+05:30 2346.95 2361.95 2346.80 2356.10 2003118\n", 644 | "13 2023-11-16 08:00:00+05:30 2351.10 2364.00 2346.95 2355.25 923205\n", 645 | "14 2023-11-16 10:00:00+05:30 2355.00 2369.60 2354.70 2366.45 1049582\n", 646 | "15 2023-11-16 12:00:00+05:30 2366.50 2369.35 2363.80 2367.00 727641\n", 647 | "16 2023-11-16 14:00:00+05:30 2366.75 2374.15 2350.55 2359.00 3758974\n", 648 | "17 2023-11-17 08:00:00+05:30 2352.90 2370.30 2352.05 2369.95 513251\n", 649 | "18 2023-11-17 10:00:00+05:30 2370.05 2373.25 2363.80 2367.00 1017076\n", 650 | "19 2023-11-17 12:00:00+05:30 2366.80 2371.60 2364.25 2366.80 849327\n", 651 | "20 2023-11-17 14:00:00+05:30 2366.80 2368.00 2353.05 2354.85 1454868\n", 652 | "21 2023-11-20 08:00:00+05:30 2348.55 2358.40 2342.00 2345.45 507002\n", 653 | "22 2023-11-20 10:00:00+05:30 2346.35 2347.25 2336.40 2341.30 478746\n", 654 | "23 2023-11-20 12:00:00+05:30 2341.25 2347.95 2340.35 2342.25 354916\n", 655 | "24 2023-11-20 14:00:00+05:30 2342.25 2352.70 2341.15 2349.00 826822\n", 656 | "25 2023-11-21 08:00:00+05:30 2366.00 2380.00 2360.20 2372.70 876474\n", 657 | "26 2023-11-21 10:00:00+05:30 2372.95 2379.65 2371.00 2379.00 857184\n", 658 | "27 2023-11-21 12:00:00+05:30 2378.80 2388.00 2378.30 2381.20 1150310\n", 659 | "28 2023-11-21 14:00:00+05:30 2381.20 2382.85 2375.00 2377.00 1065302\n", 660 | "29 2023-11-22 08:00:00+05:30 2375.00 2394.45 2375.00 2387.90 1362028\n", 661 | "30 2023-11-22 10:00:00+05:30 2387.70 2392.80 2378.05 2379.95 1190981\n", 662 | "31 2023-11-22 12:00:00+05:30 2379.95 2385.00 2372.20 2380.00 921422\n", 663 | "32 2023-11-22 14:00:00+05:30 2379.75 2390.00 2378.05 2387.00 702310\n", 664 | "33 2023-11-23 08:00:00+05:30 2388.20 2399.75 2388.20 2396.25 1124039\n", 665 | "34 2023-11-23 10:00:00+05:30 2396.20 2400.00 2390.25 2392.75 1194248\n", 666 | "35 2023-11-23 12:00:00+05:30 2393.00 2399.00 2392.15 2398.45 819897\n", 667 | "36 2023-11-23 14:00:00+05:30 2398.45 2399.10 2390.50 2393.90 1067299\n", 668 | "37 2023-11-24 08:00:00+05:30 2391.60 2402.60 2391.05 2397.50 493994\n", 669 | "38 2023-11-24 10:00:00+05:30 2397.40 2401.00 2393.55 2400.05 971111\n", 670 | "39 2023-11-24 12:00:00+05:30 2400.10 2401.00 2395.10 2397.85 941540\n", 671 | "40 2023-11-24 14:00:00+05:30 2397.90 2399.50 2392.05 2394.25 934921" 672 | ] 673 | }, 674 | "execution_count": 8, 675 | "metadata": {}, 676 | "output_type": "execute_result" 677 | } 678 | ], 679 | "source": [ 680 | "import pandas as pd\n", 681 | "\n", 682 | "def minute_conversion(data, timeframe):\n", 683 | " # Ensure 'date' column is in datetime format\n", 684 | " data['date'] = pd.to_datetime(data['date'])\n", 685 | "\n", 686 | " # Initialize an empty list to store the aggregated data\n", 687 | " aggregated_data = []\n", 688 | "\n", 689 | " # Group data by the desired timeframe\n", 690 | " grouped = data.groupby(pd.Grouper(key='date', freq=f'{timeframe}T'))\n", 691 | "\n", 692 | " # Aggregate data within each group\n", 693 | " for name, group in grouped:\n", 694 | " if not group.empty:\n", 695 | " open_price = group.iloc[0]['open']\n", 696 | " high_price = group['high'].max()\n", 697 | " low_price = group['low'].min()\n", 698 | " close_price = group.iloc[-1]['close']\n", 699 | " volume = group['volume'].sum()\n", 700 | "\n", 701 | " # Append the aggregated values to the list\n", 702 | " aggregated_data.append([name, open_price, high_price, low_price, close_price, volume])\n", 703 | "\n", 704 | " # Convert the list to a DataFrame\n", 705 | " new_df = pd.DataFrame(aggregated_data, columns=['date', 'open', 'high', 'low', 'close', 'volume'])\n", 706 | "\n", 707 | " return new_df\n", 708 | "\n", 709 | "# Example usage:\n", 710 | "# zap is your original DataFrame\n", 711 | "timeframe = 120 # 120 minutes\n", 712 | "converted_df = minute_conversion(zap, timeframe)\n", 713 | "converted_df" 714 | ] 715 | }, 716 | { 717 | "cell_type": "code", 718 | "execution_count": null, 719 | "id": "8531cc0f", 720 | "metadata": {}, 721 | "outputs": [], 722 | "source": [] 723 | } 724 | ], 725 | "metadata": { 726 | "kernelspec": { 727 | "display_name": "Python 3 (ipykernel)", 728 | "language": "python", 729 | "name": "python3" 730 | }, 731 | "language_info": { 732 | "codemirror_mode": { 733 | "name": "ipython", 734 | "version": 3 735 | }, 736 | "file_extension": ".py", 737 | "mimetype": "text/x-python", 738 | "name": "python", 739 | "nbconvert_exporter": "python", 740 | "pygments_lexer": "ipython3", 741 | "version": "3.10.12" 742 | } 743 | }, 744 | "nbformat": 4, 745 | "nbformat_minor": 5 746 | } 747 | -------------------------------------------------------------------------------- /2.6 - RSI Based Trading Bot with Python using Zerodha API.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "#https://unofficed.com/courses/mastering-algotrading-a-beginners-guide-using-kiteconnect-api/lessons/rsi-based-trading-bot-with-python-using-zerodha-api/" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 5, 15 | "metadata": { 16 | "colab": {}, 17 | "colab_type": "code", 18 | "id": "kOrIloxAuSGw" 19 | }, 20 | "outputs": [], 21 | "source": [ 22 | "import logging\n", 23 | "from kiteconnect import KiteConnect\n", 24 | "\n", 25 | "logging.basicConfig(level=logging.DEBUG)\n", 26 | "\n", 27 | "kite = KiteConnect(api_key=\"your_api_key\")\n", 28 | "\n", 29 | "# Redirect the user to the login url obtained\n", 30 | "# from kite.login_url(), and receive the request_token\n", 31 | "# from the registered redirect url after the login flow.\n", 32 | "# Once you have the request_token, obtain the access_token\n", 33 | "# as follows.\n", 34 | "\n", 35 | "data = kite.generate_session(\"request_token_here\", api_secret=\"your_secret\")\n", 36 | "kite.set_access_token(data[\"access_token\"])" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": null, 42 | "metadata": { 43 | "colab": { 44 | "base_uri": "https://localhost:8080/", 45 | "height": 34 46 | }, 47 | "colab_type": "code", 48 | "executionInfo": { 49 | "elapsed": 898, 50 | "status": "ok", 51 | "timestamp": 1562581444138, 52 | "user": { 53 | "displayName": "ashish kumar", 54 | "photoUrl": "https://lh4.googleusercontent.com/-gyDeK-Cm9Bw/AAAAAAAAAAI/AAAAAAAAAAc/zxhe91XBOvQ/s64/photo.jpg", 55 | "userId": "08220617051679634216" 56 | }, 57 | "user_tz": 0 58 | }, 59 | "id": "0hFcbLHsuSG2", 60 | "outputId": "4bd14f63-ee7f-45bc-e2d9-ed6d890c5ddb" 61 | }, 62 | "outputs": [], 63 | "source": [ 64 | "import numpy as np\n", 65 | "import pandas as pd\n", 66 | "from pytz import timezone\n", 67 | "from pprint import pprint\n", 68 | "from datetime import datetime\n", 69 | "from time import gmtime, strftime" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": null, 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [ 78 | "print(\"input token name\")\n", 79 | "token_n=str(input())\n", 80 | "print(\"enter exchange\")\n", 81 | "exc=str(input())\n", 82 | "pd.DataFrame(kite.instruments(exc))[[\"tradingsymbol\",\"instrument_token\",\"name\",\"exchange\"]][pd.DataFrame(kite.instruments(exc))[[\"tradingsymbol\",\"instrument_token\",\"name\",\"exchange\"]]['tradingsymbol'].str.contains(token_n)] " 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 7, 88 | "metadata": { 89 | "colab": {}, 90 | "colab_type": "code", 91 | "id": "NxSDVBuSuSHD" 92 | }, 93 | "outputs": [], 94 | "source": [ 95 | "ttradingsymbol =\"SBIN\"\n", 96 | "eexchange =\"NSE\"\n", 97 | "productt =\"MIS\"\n", 98 | "qu =1\n", 99 | "RSI_VAL =30\n", 100 | "RSI_VAL2 =30\n", 101 | "profit_booking =70\n", 102 | "time_frame =\"minute\"\n", 103 | "sdate =\"2019-06-05\"\n", 104 | "todate =\"2019-10-02\"\n", 105 | "#Do not change here#\n", 106 | "buy_add=.50\n", 107 | "sell_add=.50\n", 108 | "ttoken=int(pd.DataFrame(kite.ltp(eexchange+\":\"+ttradingsymbol)).iloc[-2,0])" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "metadata": { 115 | "colab": { 116 | "base_uri": "https://localhost:8080/", 117 | "height": 818 118 | }, 119 | "colab_type": "code", 120 | "executionInfo": { 121 | "elapsed": 2553, 122 | "status": "error", 123 | "timestamp": 1562344675553, 124 | "user": { 125 | "displayName": "ashish kumar", 126 | "photoUrl": "https://lh4.googleusercontent.com/-gyDeK-Cm9Bw/AAAAAAAAAAI/AAAAAAAAAAc/zxhe91XBOvQ/s64/photo.jpg", 127 | "userId": "08220617051679634216" 128 | }, 129 | "user_tz": 0 130 | }, 131 | "id": "5HoNC5kYuSHd", 132 | "outputId": "88d56bd2-8a90-435d-ccc2-6be9e9a3b384", 133 | "scrolled": true 134 | }, 135 | "outputs": [ 136 | { 137 | "name": "stdout", 138 | "output_type": "stream", 139 | "text": [ 140 | " \n", 141 | " \t \t \t \t ZERODHA RSI 'Relative Strength Index BOT' [5]\n", 142 | " \n", 143 | " \t \t \t \t ZERODHA RSI 'Relative Strength Index BOT'\n", 144 | "\n", 145 | " \n", 146 | "Zerodha RSI BOT start Time 2019-08-31 _ 21:26:07 \n", 147 | "\n", 148 | " \n", 149 | "BOT working succeesfully on time: 2019-08-31 _ 21:27:05 \n", 150 | "\n", 151 | " \n", 152 | "Trading symbol is SBIN Token is 779521 Exchange is NSE RSI condition is 30 30 and profit booking RSI level is 70 \t \n", 153 | " and product type is MIS Quantity is 1 time frame for Historical Data is minute Starting and Ending Date of Historical Data is 2019-06-05 2019-10-02\n", 154 | "\n", 155 | " \n", 156 | "RSI condition Occured but high Not break 0\n", 157 | "\n", 158 | " \n", 159 | "\n", 160 | " \n", 161 | "\n", 162 | " \n", 163 | "Current ATR status is 0.41822857770302235\n", 164 | "Current RSI status is 50.23005471188229\n", 165 | "Previous RSI status is 50.32567294164271\n", 166 | "Current HIGH is 273.6\n", 167 | "Previous HIGH satus is 273.6\n", 168 | "Lowest-Low when enter in RSI Condition []\n", 169 | "RSI at ENTRY in Condition Rough detail 0\n", 170 | "\n", 171 | " \n", 172 | "\t \t \t BUY & SELL details and Number of Order Placed \n", 173 | " \n", 174 | "Orignal Price while placing order is []\n", 175 | "BUY bid trigger price is []\n", 176 | "BUY bid price is []\n", 177 | "SELL SL trigger price is []\n", 178 | "SELL stoploss is price is []\n", 179 | "Number of BUY/SELL order till now is 0\n", 180 | "Number of time BOT complete RSI profit booking strategy 0\n", 181 | "Number of time BOT complet with execution of sell bid 0\n", 182 | "\n", 183 | " \n" 184 | ] 185 | } 186 | ], 187 | "source": [ 188 | "InteractiveShell.ast_node_interactivity = \"all\"\n", 189 | "changesver=[5]\n", 190 | "a=[0]\n", 191 | "b=[0]\n", 192 | "hh=[]\n", 193 | "ii=[]\n", 194 | "ggg=[]\n", 195 | "firs=[]\n", 196 | "inpu=[]\n", 197 | "glkk=[]\n", 198 | "akki=[]\n", 199 | "ame=[21]\n", 200 | "check=[]\n", 201 | "bii_by=[]\n", 202 | "orprice=[]\n", 203 | "datalow=[]\n", 204 | "progrun=[]\n", 205 | "rsichec=[]\n", 206 | "datarsi=[0]\n", 207 | "buyprice=[]\n", 208 | "beak_high=[]\n", 209 | "sellorder=[]\n", 210 | "org_price=[]\n", 211 | "limitprice=[]\n", 212 | "sellorder1=[]\n", 213 | "orderplaced=[]\n", 214 | "prog_starts=[]\n", 215 | "high_to_high=[]\n", 216 | "rsi_strategy=[]\n", 217 | "sell_strategy=[]\n", 218 | "rsi_condition=[]\n", 219 | "orgtimersicondition=[]\n", 220 | "sellbit_execute_limit=[]\n", 221 | "buy_order_sell_status=[]\n", 222 | "buy_order_info_buy_orid=[]\n", 223 | "buy_order_info_buy_symbol=[]\n", 224 | "buy_order_info_buy_status=[]\n", 225 | "buy_order_info_buy_status=[]\n", 226 | "buy_order_info_buy_orderid=[]\n", 227 | "buy_order_info_sell_orderid=[]\n", 228 | "class ThreadingData(object):\n", 229 | " global app_z_status\n", 230 | " global app_z_order\n", 231 | " global app_z_order_type\n", 232 | " global app_z_transaction_type\n", 233 | " app_z_status=[]\n", 234 | " app_z_order=[]\n", 235 | " app_z_order_type=[]\n", 236 | " app_z_transaction_type=[]\n", 237 | " def __init__(self):\n", 238 | " thread = threading.Thread(target=self.run, args=())\n", 239 | " thread.daemon = True\n", 240 | " thread.start()\n", 241 | " def run(self):\n", 242 | " access_token=token\n", 243 | " api_key=api_key\n", 244 | " logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')\n", 245 | " clear_output(wait=True)\n", 246 | " kws = KiteTicker(api_key, access_token, debug=True)\n", 247 | " global app_z_status\n", 248 | " global app_z_order\n", 249 | " global app_z_order_type\n", 250 | " global app_z_transaction_type\n", 251 | " tokens = [ttoken]\n", 252 | " print(\"Tokens length\", len(tokens))\n", 253 | " def on_connect(ws, response):\n", 254 | " logging.debug(\"on connect: {}\".format(response))\n", 255 | " ws.subscribe(tokens)\n", 256 | " ws.set_mode(ws.MODE_FULL, tokens)\n", 257 | " def on_close(ws, code, reason):\n", 258 | " logging.error(\"closed connection on close: {} {}\".format(code, reason))\n", 259 | " def on_error(ws, code, reason):\n", 260 | " logging.error(\"closed connection on error: {} {}\".format(code, reason))\n", 261 | " def on_noreconnect(ws):\n", 262 | " logging.error(\"Reconnecting the websocket failed\")\n", 263 | " def on_reconnect(ws, attempt_count):\n", 264 | " logging.debug(\"Reconnecting the websocket: {}\".format(attempt_count))\n", 265 | " def on_order_update(ws, data):\n", 266 | " z_status=data['status']\n", 267 | " z_order=data[\"order_id\"]\n", 268 | " z_order_type=data[\"order_type\"]\n", 269 | " z_transaction_type=[\"transaction_type\"]\n", 270 | " app_z_status.append(z_status)\n", 271 | " app_z_order.append(z_order)\n", 272 | " app_z_order_type.append(z_order_type)\n", 273 | " app_z_transaction_type.append(z_transaction_type)\n", 274 | " print(app_z_status)\n", 275 | " print(app_z_order)\n", 276 | " print(app_z_order_type)\n", 277 | " print(app_z_transaction_type)\n", 278 | " print(data)\n", 279 | " kws.on_connect = on_connect\n", 280 | " kws.on_close = on_close\n", 281 | " kws.on_error = on_error\n", 282 | " kws.on_noreconnect = on_noreconnect\n", 283 | " kws.on_reconnect = on_reconnect\n", 284 | " kws.on_order_update = on_order_update\n", 285 | " kws.connect()\n", 286 | "def ashi():\n", 287 | " while(True):\n", 288 | " InteractiveShell.ast_node_interactivity = \"all\"\n", 289 | " km=datetime.now().minute\n", 290 | " ks=datetime.now().second\n", 291 | " if km%1==0 and ks==1:\n", 292 | " clear_output(wait=True)\n", 293 | " z=kite.historical_data(ttoken, sdate, todate,time_frame,0)\n", 294 | " za=pd.DataFrame(z)\n", 295 | " rsi_period=14\n", 296 | " chg=za[\"close\"].diff(1)\n", 297 | " gain=chg.mask(chg<0,0) \n", 298 | " loss=chg.mask(chg>0,0)\n", 299 | " avg_gain=gain.ewm(com=rsi_period-1,min_periods=rsi_period).mean()\n", 300 | " avg_loss=loss.ewm(com=rsi_period-1,min_periods=rsi_period).mean()\n", 301 | " rs =abs(avg_gain / avg_loss)\n", 302 | " rsi =100 -(100/(1+rs))\n", 303 | " za['rsi']=rsi\n", 304 | " def bidatrema(df,period):\n", 305 | " df['hl']=abs(df['high']-df['low'])\n", 306 | " df['hpc']=abs(df['high']-df['close'].shift())\n", 307 | " df['lpc']=abs(df['low']-df['close'].shift())\n", 308 | " df['tr']=df[['hl','hpc','lpc']].max(axis=1)\n", 309 | " df['ATR']=pd.DataFrame.ewm(df[\"tr\"], span=period,min_periods=period).mean()\n", 310 | " df.drop([\"hl\",\"hpc\",\"lpc\",\"tr\"],axis = 1 , inplace =True)\n", 311 | " bidatrema(za,14)\n", 312 | " p=za.iloc[-1,6]\n", 313 | " o=za.iloc[-2,2]\n", 314 | " a.append(p)\n", 315 | " b.append(o)\n", 316 | " def avg_ha(x):\n", 317 | " a=list(modf(round(x,3)))\n", 318 | " d=str(a[0])\n", 319 | " aa=str(d[0:2])\n", 320 | " try:\n", 321 | " ab=str(d[2])\n", 322 | " except:\n", 323 | " ab='0'\n", 324 | " try:\n", 325 | " ac=str(d[3])\n", 326 | " except:\n", 327 | " ac='0'\n", 328 | " try:\n", 329 | " ad=str(d[4])\n", 330 | " except:\n", 331 | " ad='0'\n", 332 | " c=aa+ab+ac+ad\n", 333 | " b=0\n", 334 | " if a[0]!=0:\n", 335 | " astr=c\n", 336 | " a0=astr[0]\n", 337 | " a1=astr[1]\n", 338 | " a3=int(astr[2])\n", 339 | " a2=int(astr[3:5])\n", 340 | " if a2>=0 and a2<25:\n", 341 | " a2=0\n", 342 | " elif a2>=25 and a2<75:\n", 343 | " a2=5\n", 344 | " elif a2>=75 and a2<=99:\n", 345 | " a3+=1\n", 346 | " a2=0\n", 347 | " aint=a0+a1+str(a3)+str(a2)\n", 348 | " a[0]=float(aint)\n", 349 | " for k in a:\n", 350 | " b+=k\n", 351 | " return b\n", 352 | " InteractiveShell.ast_node_interactivity = \"all\"\n", 353 | " print(\" \\n \\t \\t \\t \\t ZERODHA RSI 'Relative Strength Index BOT'\",changesver)\n", 354 | " print(\" \\n \\t \\t \\t \\t ZERODHA RSI 'Relative Strength Index BOT'\")\n", 355 | " now_utc = datetime.now(timezone('UTC'))\n", 356 | " now_asia = now_utc.astimezone(timezone('Asia/Kolkata'))\n", 357 | " now_asia = now_asia.strftime(\"%Y-%m-%d _ %H:%M:%S \")\n", 358 | " klp1=now_asia\n", 359 | " prog_starts.append(klp1)\n", 360 | " print(\"\\n \")\n", 361 | " print(\"Zerodha RSI BOT start Time \" , prog_starts[0])\n", 362 | " print(\"\\n \")\n", 363 | " print(\"BOT working succeesfully on time: \" , now_asia )\n", 364 | " print(\"\\n \")\n", 365 | " print(\"Trading symbol is\",ttradingsymbol,\"Token is\",ttoken,\"Exchange is\",eexchange,\"RSI condition is\",RSI_VAL,RSI_VAL2,\"and profit booking RSI level is\",profit_booking,\" \\t \\n and product type is\",productt,\"Quantity is\",qu,\"time frame for Historical Data is\",time_frame,\"Starting and Ending Date of Historical Data is\",sdate,todate)\n", 366 | " print(\"\\n \")\n", 367 | " #display(data)\n", 368 | " print(\"RSI condition Occured but high Not break\" , len(progrun))\n", 369 | " if 2 in rsichec:\n", 370 | " print(\"RSI condition's occured at time \" ,*rsi_condition,sep='\\n')\n", 371 | " print(\"\\n \")\n", 372 | " print(\"\\n \")\n", 373 | " if 2 in high_to_high:\n", 374 | " print(\"High-High breaks condition time \" ,*beak_high,sep='\\n')\n", 375 | " print(\"\\n \")\n", 376 | " print(\"Current ATR status is \" , za.iloc[-1,7])\n", 377 | " print(\"Current RSI status is \" , a[-1])\n", 378 | " print(\"Previous RSI status is \" , a[-2])\n", 379 | " print(\"Current HIGH is \" , b[-1])\n", 380 | " print(\"Previous HIGH satus is \" , b[-2])\n", 381 | " \n", 382 | " print(\"Lowest-Low when enter in RSI Condition \" , datalow)\n", 383 | " print(\"RSI at ENTRY in Condition Rough detail \" , datarsi[-1])\n", 384 | " print(\"\\n \")\n", 385 | " print(\"\\t \\t \\t BUY & SELL details and Number of Order Placed \\n \")\n", 386 | " print(\"Orignal Price while placing order is \" , orprice)\n", 387 | " print(\"BUY bid trigger price is \" , buyprice)\n", 388 | " print(\"BUY bid price is \" , limitprice)\n", 389 | " print(\"SELL SL trigger price is \" , sellorder)\n", 390 | " print(\"SELL stoploss is price is \" , sellorder1)\n", 391 | " print(\"Number of BUY/SELL order till now is \" , len(orderplaced))\n", 392 | " print(\"Number of time BOT complete RSI profit booking strategy\" , len(rsi_strategy))\n", 393 | " print(\"Number of time BOT complet with execution of sell bid \" , len(sell_strategy))\n", 394 | " if 0 in check:\n", 395 | " print(\" BUY bid Order Symbol \" , buy_order_info_buy_symbol)\n", 396 | " print(\" BUY bid Order Status \" , buy_order_info_buy_status)\n", 397 | " print(\" BUY bid Order id \" , buy_order_info_buy_orid)\n", 398 | " print(\" BUY bid placed of \" , buy_order_info_buy_symbol, \"and order id is\", buy_order_info_buy_orid)\n", 399 | " if 0 in sellbit_execute_limit:\n", 400 | " print(\" SELL Order Status \" , buy_order_sell_status)\n", 401 | " print(\" SELL Order id \" , buy_order_info_sell_orderid)\n", 402 | " print(\" BUY Order executed on zerodha and SELL bid placed Order of\",ttradingsymbol,\"status is\",buy_order_sell_status,\"order id is\",buy_order_info_sell_orderid)\n", 403 | " print(\"\\n \")\n", 404 | " if 0 in check:\n", 405 | " if 0 in sellbit_execute_limit:\n", 406 | " lll=str(buy_order_info_sell_orderid[0])\n", 407 | " kneww8=pd.DataFrame(kite.orders())[[\"tradingsymbol\",\"status\",\"order_id\",\"quantity\"]]\n", 408 | " zc898,=kneww8[kneww8.order_id== lll].index\n", 409 | " in898=int(zc898)\n", 410 | " if \"COMPLETE\" in kneww8.loc[in898,\"status\"]:\n", 411 | " print(\"YES Order Already Executed of SL \")\n", 412 | " a.clear()\n", 413 | " b.clear()\n", 414 | " hh.clear()\n", 415 | " ii.clear()\n", 416 | " ggg.clear()\n", 417 | " ame.clear()\n", 418 | " a.append(0)\n", 419 | " b.append(0)\n", 420 | " firs.clear()\n", 421 | " inpu.clear()\n", 422 | " glkk.clear()\n", 423 | " akki.clear()\n", 424 | " check.clear()\n", 425 | " bii_by.clear()\n", 426 | " orprice.clear()\n", 427 | " datarsi.clear()\n", 428 | " datalow.clear()\n", 429 | " buyprice.clear()\n", 430 | " datarsi.append(0)\n", 431 | " sellorder.clear()\n", 432 | " org_price.clear()\n", 433 | " limitprice.clear()\n", 434 | " sellorder1.clear()\n", 435 | " sellbit_execute_limit.clear()\n", 436 | " buy_order_sell_status.clear()\n", 437 | " buy_order_info_buy_orid.clear()\n", 438 | " buy_order_info_buy_symbol.clear()\n", 439 | " buy_order_info_buy_status.clear()\n", 440 | " buy_order_info_buy_status.clear()\n", 441 | " buy_order_info_buy_orderid.clear()\n", 442 | " buy_order_info_sell_orderid.clear()\n", 443 | " print(\" Cycle complete with execution of SELL bid \")\n", 444 | " orderplaced.append(1)\n", 445 | " sell_strategy.append(1)\n", 446 | " ashi()\n", 447 | " if za.iloc[-1,6]>=profit_booking:\n", 448 | " lll=buy_order_info_sell_orderid[0]\n", 449 | " print(\"SL order Cancel id is : \",buy_order_info_sell_orderid,kite.cancel_order(\"regular\",order_id=lll, parent_order_id=None))\n", 450 | " print(\"Market order placed and BOT starts again\",kite.place_order(variety=\"regular\",tradingsymbol=ttradingsymbol,quantity=qu,exchange=eexchange,order_type='MARKET',transaction_type='SELL',product=productt,tag=\"RSImkt\")) \n", 451 | " a.clear()\n", 452 | " b.clear()\n", 453 | " hh.clear()\n", 454 | " ii.clear()\n", 455 | " ggg.clear()\n", 456 | " ame.clear()\n", 457 | " a.append(0)\n", 458 | " b.append(0)\n", 459 | " firs.clear()\n", 460 | " inpu.clear()\n", 461 | " glkk.clear()\n", 462 | " akki.clear()\n", 463 | " check.clear()\n", 464 | " bii_by.clear()\n", 465 | " datarsi.clear()\n", 466 | " orprice.clear()\n", 467 | " datalow.clear()\n", 468 | " buyprice.clear()\n", 469 | " sellorder.clear()\n", 470 | " org_price.clear()\n", 471 | " datarsi.append(0)\n", 472 | " limitprice.clear()\n", 473 | " sellorder1.clear()\n", 474 | " sellbit_execute_limit.clear()\n", 475 | " buy_order_sell_status.clear()\n", 476 | " buy_order_info_buy_orid.clear()\n", 477 | " buy_order_info_buy_symbol.clear()\n", 478 | " buy_order_info_buy_status.clear()\n", 479 | " buy_order_info_buy_status.clear()\n", 480 | " buy_order_info_buy_orderid.clear()\n", 481 | " buy_order_info_sell_orderid.clear()\n", 482 | " print(\" This Cycle completed with RSI strategy \")\n", 483 | " rsi_strategy.append(1)\n", 484 | " orderplaced.append(1)\n", 485 | " ashi()\n", 486 | " else:\n", 487 | " pass\n", 488 | " if 0 not in glkk:\n", 489 | " kkkstr=str(buy_order_info_buy_orid[0])\n", 490 | " kneww=pd.DataFrame(kite.orders())[[\"tradingsymbol\",\"status\",\"order_id\",\"quantity\"]]\n", 491 | " zc89,=kneww[kneww.order_id== kkkstr].index\n", 492 | " in899=int(zc89)\n", 493 | " if \"COMPLETE\" in kneww.loc[in899,\"status\"]:\n", 494 | " print(\" YES! BUY bid Executed \")\n", 495 | " xel=min(datalow)-(za.iloc[-1,7]*.25)\n", 496 | " triggerpriceis=avg_ha(xel)\n", 497 | " sellorder.append(triggerpriceis)\n", 498 | " xelm=triggerpriceis-sell_add\n", 499 | " stoplossis=avg_ha(xelm)\n", 500 | " sellorder1.append(stoplossis)\n", 501 | " print(\"Buy Limit order executed on zerodha and now SELL order placed\") \n", 502 | " print(\"Sell Stoploss order placed and SL trigger price is \", triggerpriceis,\"and stoploss is\",stoplossis)\n", 503 | " listt1=int(kite.place_order( variety=\"regular\",tradingsymbol=ttradingsymbol,quantity=qu,exchange=eexchange,order_type='SL',stoploss= triggerpriceis,trigger_price=stoplossis,transaction_type='SELL',product=productt,tag=\"RSIsel\"))\n", 504 | " listtt2=\"STATUS\"\n", 505 | " buy_order_sell_status.append(listtt2)\n", 506 | " zkak=int(listt1)\n", 507 | " buy_order_info_sell_orderid.append(zkak)\n", 508 | " print(buy_order_sell_status)\n", 509 | " print(buy_order_info_sell_orderid)\n", 510 | " sellbit_execute=secrets.randbelow(1)\n", 511 | " sellbit_execute_limit.append(sellbit_execute)\n", 512 | " glk=secrets.randbelow(1)\n", 513 | " glkk.append(glk)\n", 514 | " else:\n", 515 | " pass \n", 516 | " else:\n", 517 | " pass\n", 518 | " if 0 in bii_by:\n", 519 | " if 0 not in akki:\n", 520 | " if 21 in ame:\n", 521 | " orgprice=za.iloc[-1,2]\n", 522 | " orprice.append(orgprice)\n", 523 | " exx=(za.iloc[-1,2]+(za.iloc[-1,7]*.25))\n", 524 | " cur_high=avg_ha(exx)\n", 525 | " exxx=cur_high+buy_add\n", 526 | " lim_price=avg_ha(exxx)\n", 527 | " buyprice.append(cur_high) \n", 528 | " limitprice.append(lim_price)\n", 529 | " org_price.append(orgprice)\n", 530 | " print(\"Congrats ! One Limit BUY Order placed Price is\", lim_price , \"and trigger_price is \", cur_high)\n", 531 | " list_buy_symbol=ttradingsymbol\n", 532 | " list_buy_status=\"STATUS\"\n", 533 | " buy_order_info_buy_symbol.append(list_buy_symbol)\n", 534 | " buy_order_info_buy_status.append(list_buy_status)\n", 535 | " buy_order_info_buy_orid.append(kite.place_order(variety=\"regular\",tradingsymbol=ttradingsymbol,quantity=qu,exchange=eexchange,order_type='SL',price=lim_price ,trigger_price=cur_high,transaction_type='BUY',product=productt,tag=\"RSIbuy\")) \n", 536 | " print(\" BUY ORDER SYMBOL \",buy_order_info_buy_symbol)\n", 537 | " print(\" BUY ORDER STATUS \",buy_order_info_buy_status)\n", 538 | " print(\" BUY ORDER ID IS \",buy_order_info_buy_orid)\n", 539 | " biiiii=secrets.randbelow(1)\n", 540 | " akki.append(biiiii)\n", 541 | " p=secrets.randbelow(1)\n", 542 | " check.append(p)\n", 543 | " else:\n", 544 | " pass\n", 545 | " else:\n", 546 | " pass\n", 547 | " if 0 not in bii_by:\n", 548 | " if 2 not in firs:\n", 549 | " if za.iloc[-2,6]>= RSI_VAL and za.iloc[-1,6]<=RSI_VAL:\n", 550 | " rsichec.append(2)\n", 551 | " rsi_condition.append(now_asia)\n", 552 | " fiss=2\n", 553 | " apa=1\n", 554 | " firs.append(fiss)\n", 555 | " inpu.append(apa)\n", 556 | " else:\n", 557 | " pass\n", 558 | " if 1 in inpu:\n", 559 | " datalow.append(za.iloc[-1,3])\n", 560 | " datarsi.append(za.iloc[-1,6])\n", 561 | " if za.iloc[-1,6]>=RSI_VAL2:\n", 562 | " print(\" Programe satisfied RSI condition \")\n", 563 | " if za.iloc[-2,2] < za.iloc[-1,2]:\n", 564 | " print(\"High breaks High condition satisfy\")\n", 565 | " high_to_high.append(2)\n", 566 | " beak_high.append(now_asia)\n", 567 | " bii = secrets.randbelow(1)\n", 568 | " bii_by.append(bii) \n", 569 | " else:\n", 570 | " progrun.append(1)\n", 571 | " bii_by.clear()\n", 572 | " firs.clear()\n", 573 | " inpu.clear()\n", 574 | " datalow.clear()\n", 575 | " datarsi.clear()\n", 576 | " datarsi.append(0)\n", 577 | " else:\n", 578 | " pass\n", 579 | " else:\n", 580 | " pass\n", 581 | " else:\n", 582 | " pass\n", 583 | "ashi()" 584 | ] 585 | }, 586 | { 587 | "cell_type": "code", 588 | "execution_count": 14, 589 | "metadata": {}, 590 | "outputs": [ 591 | { 592 | "name": "stdout", 593 | "output_type": "stream", 594 | "text": [ 595 | " \n", 596 | " \t \t \t \t \t ---- ALL THE HOLDINGS YOU HAVE ON THIS ACCOUNT---- \n" 597 | ] 598 | }, 599 | { 600 | "data": { 601 | "text/html": [ 602 | "
\n", 603 | "\n", 616 | "\n", 617 | " \n", 618 | " \n", 619 | " \n", 620 | " \n", 621 | " \n", 622 | " \n", 623 | " \n", 624 | " \n", 625 | " \n", 626 | " \n", 627 | " \n", 628 | " \n", 629 | " \n", 630 | " \n", 631 | " \n", 632 | " \n", 633 | " \n", 634 | " \n", 635 | " \n", 636 | " \n", 637 | " \n", 638 | " \n", 639 | " \n", 640 | " \n", 641 | " \n", 642 | " \n", 643 | " \n", 644 | " \n", 645 | " \n", 646 | " \n", 647 | " \n", 648 | " \n", 649 | " \n", 650 | " \n", 651 | " \n", 652 | " \n", 653 | " \n", 654 | " \n", 655 | " \n", 656 | " \n", 657 | " \n", 658 | " \n", 659 | " \n", 660 | " \n", 661 | " \n", 662 | " \n", 663 | " \n", 664 | " \n", 665 | " \n", 666 | " \n", 667 | " \n", 668 | " \n", 669 | " \n", 670 | " \n", 671 | " \n", 672 | " \n", 673 | " \n", 674 | " \n", 675 | " \n", 676 | " \n", 677 | " \n", 678 | " \n", 679 | " \n", 680 | " \n", 681 | "
tradingsymbolinstrument_tokenpnlaverage_priceclose_pricecollateral_quantitycollateral_typeday_changeday_change_percentageexchangelast_priceisinpriceproductquantityrealised_quantityt1_quantity
0CRESSAN1311690280.0000000.2000000.200.000.000000BSE0.20INE716D010330CNC050
1IDEA3677697-2.51000111.78666711.30-0.35-3.097345NSE10.95INE669E010160CNC221
\n", 682 | "
" 683 | ], 684 | "text/plain": [ 685 | " tradingsymbol instrument_token pnl average_price close_price \\\n", 686 | "0 CRESSAN 131169028 0.000000 0.200000 0.2 \n", 687 | "1 IDEA 3677697 -2.510001 11.786667 11.3 \n", 688 | "\n", 689 | " collateral_quantity collateral_type day_change day_change_percentage \\\n", 690 | "0 0 0.00 0.000000 \n", 691 | "1 0 -0.35 -3.097345 \n", 692 | "\n", 693 | " exchange last_price isin price product quantity \\\n", 694 | "0 BSE 0.20 INE716D01033 0 CNC 0 \n", 695 | "1 NSE 10.95 INE669E01016 0 CNC 2 \n", 696 | "\n", 697 | " realised_quantity t1_quantity \n", 698 | "0 5 0 \n", 699 | "1 2 1 " 700 | ] 701 | }, 702 | "metadata": {}, 703 | "output_type": "display_data" 704 | }, 705 | { 706 | "name": "stdout", 707 | "output_type": "stream", 708 | "text": [ 709 | " \n", 710 | " \t \t \t \t \t ---- ALL THE POSITONS YOU HAVE ON THIS ACCOUNT---- \n" 711 | ] 712 | }, 713 | { 714 | "data": { 715 | "text/html": [ 716 | "
\n", 717 | "\n", 730 | "\n", 731 | " \n", 732 | " \n", 733 | " \n", 734 | " \n", 735 | " \n", 736 | " \n", 737 | " \n", 738 | " \n", 739 | " \n", 740 | " \n", 741 | " \n", 742 | " \n", 743 | " \n", 744 | " \n", 745 | " \n", 746 | " \n", 747 | " \n", 748 | " \n", 749 | " \n", 750 | " \n", 751 | " \n", 752 | " \n", 753 | " \n", 754 | " \n", 755 | " \n", 756 | " \n", 757 | " \n", 758 | " \n", 759 | " \n", 760 | " \n", 761 | " \n", 762 | " \n", 763 | " \n", 764 | " \n", 765 | " \n", 766 | " \n", 767 | " \n", 768 | " \n", 769 | " \n", 770 | " \n", 771 | " \n", 772 | " \n", 773 | " \n", 774 | " \n", 775 | " \n", 776 | " \n", 777 | " \n", 778 | " \n", 779 | " \n", 780 | " \n", 781 | " \n", 782 | " \n", 783 | " \n", 784 | " \n", 785 | " \n", 786 | " \n", 787 | " \n", 788 | " \n", 789 | " \n", 790 | " \n", 791 | " \n", 792 | " \n", 793 | " \n", 794 | " \n", 795 | " \n", 796 | " \n", 797 | " \n", 798 | " \n", 799 | " \n", 800 | " \n", 801 | " \n", 802 | " \n", 803 | " \n", 804 | " \n", 805 | " \n", 806 | " \n", 807 | " \n", 808 | " \n", 809 | " \n", 810 | " \n", 811 | " \n", 812 | " \n", 813 | " \n", 814 | " \n", 815 | " \n", 816 | " \n", 817 | " \n", 818 | " \n", 819 | " \n", 820 | " \n", 821 | " \n", 822 | " \n", 823 | " \n", 824 | " \n", 825 | " \n", 826 | " \n", 827 | " \n", 828 | " \n", 829 | " \n", 830 | " \n", 831 | " \n", 832 | " \n", 833 | " \n", 834 | " \n", 835 | " \n", 836 | " \n", 837 | " \n", 838 | " \n", 839 | " \n", 840 | " \n", 841 | " \n", 842 | " \n", 843 | " \n", 844 | " \n", 845 | " \n", 846 | " \n", 847 | " \n", 848 | " \n", 849 | " \n", 850 | " \n", 851 | " \n", 852 | " \n", 853 | " \n", 854 | " \n", 855 | " \n", 856 | " \n", 857 | " \n", 858 | " \n", 859 | " \n", 860 | " \n", 861 | " \n", 862 | " \n", 863 | " \n", 864 | " \n", 865 | " \n", 866 | " \n", 867 | " \n", 868 | " \n", 869 | " \n", 870 | " \n", 871 | " \n", 872 | " \n", 873 | "
tradingsymbolaverage_pricebuy_m2mbuy_pricebuy_quantitybuy_valueclose_pricesell_m2msell_pricesell_quantitysell_valueunrealisedvalueday_buy_priceday_buy_quantityday_buy_valueday_sell_priceproductquantityrealised
0IDEA11.210011.2111.2100111.210.00000.00.000.0-0.31-11.2111.21111.210.0CNC10
1EURINR19JULFUT77.5100464955.0077.51006465060.0077.49250.00.000.0780.00-465060.000.0000.000.0NRML60
2GBPINR19JULFUT85.7900517215.0085.79006514740.0086.20250.00.000.03990.00-514740.000.0000.000.0NRML60
3JPYINR19JULFUT64.0100384060.0064.01006384060.0064.01000.00.000.0765.00-384060.000.0000.000.0NRML60
4USDINR19JULFUT68.6475207015.0068.64753205942.5069.00500.00.000.01035.00-205942.500.0000.000.0NRML30
\n", 874 | "
" 875 | ], 876 | "text/plain": [ 877 | " tradingsymbol average_price buy_m2m buy_price buy_quantity \\\n", 878 | "0 IDEA 11.2100 11.21 11.2100 1 \n", 879 | "1 EURINR19JULFUT 77.5100 464955.00 77.5100 6 \n", 880 | "2 GBPINR19JULFUT 85.7900 517215.00 85.7900 6 \n", 881 | "3 JPYINR19JULFUT 64.0100 384060.00 64.0100 6 \n", 882 | "4 USDINR19JULFUT 68.6475 207015.00 68.6475 3 \n", 883 | "\n", 884 | " buy_value close_price sell_m2m sell_price sell_quantity sell_value \\\n", 885 | "0 11.21 0.0000 0.0 0.0 0 0.0 \n", 886 | "1 465060.00 77.4925 0.0 0.0 0 0.0 \n", 887 | "2 514740.00 86.2025 0.0 0.0 0 0.0 \n", 888 | "3 384060.00 64.0100 0.0 0.0 0 0.0 \n", 889 | "4 205942.50 69.0050 0.0 0.0 0 0.0 \n", 890 | "\n", 891 | " unrealised value day_buy_price day_buy_quantity day_buy_value \\\n", 892 | "0 -0.31 -11.21 11.21 1 11.21 \n", 893 | "1 780.00 -465060.00 0.00 0 0.00 \n", 894 | "2 3990.00 -514740.00 0.00 0 0.00 \n", 895 | "3 765.00 -384060.00 0.00 0 0.00 \n", 896 | "4 1035.00 -205942.50 0.00 0 0.00 \n", 897 | "\n", 898 | " day_sell_price product quantity realised \n", 899 | "0 0.0 CNC 1 0 \n", 900 | "1 0.0 NRML 6 0 \n", 901 | "2 0.0 NRML 6 0 \n", 902 | "3 0.0 NRML 6 0 \n", 903 | "4 0.0 NRML 3 0 " 904 | ] 905 | }, 906 | "metadata": {}, 907 | "output_type": "display_data" 908 | }, 909 | { 910 | "name": "stdout", 911 | "output_type": "stream", 912 | "text": [ 913 | " \n", 914 | " \t \t \t \t \t ---- ALL THE ORDERS YOU HAVE ON THIS ACCOUNT---- \n" 915 | ] 916 | }, 917 | { 918 | "data": { 919 | "text/html": [ 920 | "
\n", 921 | "\n", 934 | "\n", 935 | " \n", 936 | " \n", 937 | " \n", 938 | " \n", 939 | " \n", 940 | " \n", 941 | " \n", 942 | " \n", 943 | " \n", 944 | " \n", 945 | " \n", 946 | " \n", 947 | " \n", 948 | " \n", 949 | " \n", 950 | " \n", 951 | " \n", 952 | " \n", 953 | " \n", 954 | " \n", 955 | " \n", 956 | " \n", 957 | " \n", 958 | " \n", 959 | " \n", 960 | " \n", 961 | " \n", 962 | " \n", 963 | " \n", 964 | " \n", 965 | " \n", 966 | " \n", 967 | " \n", 968 | " \n", 969 | " \n", 970 | " \n", 971 | " \n", 972 | " \n", 973 | " \n", 974 | " \n", 975 | " \n", 976 | " \n", 977 | " \n", 978 | " \n", 979 | " \n", 980 | " \n", 981 | " \n", 982 | " \n", 983 | " \n", 984 | " \n", 985 | " \n", 986 | " \n", 987 | " \n", 988 | " \n", 989 | " \n", 990 | " \n", 991 | " \n", 992 | " \n", 993 | " \n", 994 | " \n", 995 | " \n", 996 | " \n", 997 | " \n", 998 | " \n", 999 | " \n", 1000 | " \n", 1001 | " \n", 1002 | " \n", 1003 | " \n", 1004 | " \n", 1005 | " \n", 1006 | " \n", 1007 | " \n", 1008 | " \n", 1009 | " \n", 1010 | " \n", 1011 | " \n", 1012 | " \n", 1013 | " \n", 1014 | " \n", 1015 | " \n", 1016 | " \n", 1017 | " \n", 1018 | " \n", 1019 | " \n", 1020 | " \n", 1021 | " \n", 1022 | " \n", 1023 | " \n", 1024 | " \n", 1025 | " \n", 1026 | " \n", 1027 | " \n", 1028 | " \n", 1029 | " \n", 1030 | " \n", 1031 | " \n", 1032 | " \n", 1033 | " \n", 1034 | " \n", 1035 | " \n", 1036 | " \n", 1037 | " \n", 1038 | " \n", 1039 | " \n", 1040 | " \n", 1041 | " \n", 1042 | " \n", 1043 | " \n", 1044 | " \n", 1045 | " \n", 1046 | " \n", 1047 | " \n", 1048 | " \n", 1049 | " \n", 1050 | " \n", 1051 | " \n", 1052 | " \n", 1053 | " \n", 1054 | " \n", 1055 | " \n", 1056 | " \n", 1057 | " \n", 1058 | " \n", 1059 | " \n", 1060 | " \n", 1061 | " \n", 1062 | " \n", 1063 | " \n", 1064 | " \n", 1065 | " \n", 1066 | " \n", 1067 | " \n", 1068 | " \n", 1069 | " \n", 1070 | " \n", 1071 | " \n", 1072 | " \n", 1073 | " \n", 1074 | " \n", 1075 | " \n", 1076 | " \n", 1077 | " \n", 1078 | " \n", 1079 | " \n", 1080 | " \n", 1081 | " \n", 1082 | " \n", 1083 | " \n", 1084 | " \n", 1085 | " \n", 1086 | " \n", 1087 | " \n", 1088 | " \n", 1089 | " \n", 1090 | " \n", 1091 | " \n", 1092 | " \n", 1093 | " \n", 1094 | " \n", 1095 | " \n", 1096 | " \n", 1097 | " \n", 1098 | " \n", 1099 | " \n", 1100 | " \n", 1101 | " \n", 1102 | " \n", 1103 | " \n", 1104 | " \n", 1105 | " \n", 1106 | " \n", 1107 | " \n", 1108 | " \n", 1109 | " \n", 1110 | " \n", 1111 | " \n", 1112 | " \n", 1113 | " \n", 1114 | " \n", 1115 | " \n", 1116 | " \n", 1117 | " \n", 1118 | " \n", 1119 | " \n", 1120 | " \n", 1121 | " \n", 1122 | " \n", 1123 | " \n", 1124 | " \n", 1125 | " \n", 1126 | " \n", 1127 | " \n", 1128 | " \n", 1129 | " \n", 1130 | " \n", 1131 | " \n", 1132 | " \n", 1133 | " \n", 1134 | " \n", 1135 | " \n", 1136 | " \n", 1137 | " \n", 1138 | " \n", 1139 | " \n", 1140 | " \n", 1141 | " \n", 1142 | " \n", 1143 | " \n", 1144 | " \n", 1145 | " \n", 1146 | " \n", 1147 | " \n", 1148 | " \n", 1149 | " \n", 1150 | " \n", 1151 | " \n", 1152 | " \n", 1153 | " \n", 1154 | " \n", 1155 | " \n", 1156 | " \n", 1157 | " \n", 1158 | " \n", 1159 | " \n", 1160 | " \n", 1161 | " \n", 1162 | " \n", 1163 | " \n", 1164 | " \n", 1165 | " \n", 1166 | " \n", 1167 | " \n", 1168 | " \n", 1169 | " \n", 1170 | " \n", 1171 | " \n", 1172 | " \n", 1173 | " \n", 1174 | " \n", 1175 | " \n", 1176 | " \n", 1177 | " \n", 1178 | " \n", 1179 | " \n", 1180 | " \n", 1181 | " \n", 1182 | " \n", 1183 | " \n", 1184 | " \n", 1185 | " \n", 1186 | " \n", 1187 | " \n", 1188 | " \n", 1189 | " \n", 1190 | " \n", 1191 | " \n", 1192 | " \n", 1193 | " \n", 1194 | " \n", 1195 | " \n", 1196 | " \n", 1197 | " \n", 1198 | " \n", 1199 | " \n", 1200 | " \n", 1201 | " \n", 1202 | " \n", 1203 | " \n", 1204 | " \n", 1205 | " \n", 1206 | " \n", 1207 | " \n", 1208 | " \n", 1209 | " \n", 1210 | " \n", 1211 | " \n", 1212 | " \n", 1213 | " \n", 1214 | " \n", 1215 | " \n", 1216 | " \n", 1217 | " \n", 1218 | " \n", 1219 | " \n", 1220 | " \n", 1221 | " \n", 1222 | " \n", 1223 | " \n", 1224 | " \n", 1225 | " \n", 1226 | " \n", 1227 | " \n", 1228 | " \n", 1229 | " \n", 1230 | " \n", 1231 | " \n", 1232 | " \n", 1233 | " \n", 1234 | " \n", 1235 | " \n", 1236 | " \n", 1237 | " \n", 1238 | " \n", 1239 | "
tradingsymboltransaction_typeorder_idquantityorder_typeexchange_update_timestamptagquantitystatus
0SBINBUY1907190011311731SLNoneRSIbuy1REJECTED
1SBINBUY1907190011368381SLNoneRSIbuy1REJECTED
2SBINBUY1907190011433721SLNoneRSIbuy1REJECTED
3SBINBUY1907190011912091SLNoneRSIbuy1REJECTED
4IDEABUY1907190012786421SL2019-07-19 11:24:04RSIbuy1REJECTED
5ADANIPOWERBUY1907190012791991SL2019-07-19 11:24:10RSIbuy1REJECTED
6IDEABUY1907190013223671LIMIT2019-07-19 11:32:24RSIbuy1COMPLETE
7SBINBUY1907190007302481SL2019-07-19 11:44:36RSIbuy1CANCELLED
8IDEABUY1907190013688361SL2019-07-19 11:44:36None1CANCELLED
9ADANIPOWERBUY1907190013881941SL2019-07-19 11:44:36None1CANCELLED
10ADANIPOWERBUY1907190013935181SL2019-07-19 11:44:36None1CANCELLED
11IDEABUY1907190016910921SLNoneNone1REJECTED
12IDEABUY1907190017003011SLNoneNone1REJECTED
13IDEABUY1907190016976241SL2019-07-19 12:54:45None1CANCELLED
14ADANIPOWERBUY1907190017061151SL2019-07-19 12:54:45None1CANCELLED
15ADANIPOWERBUY1907190017091081SL2019-07-19 12:54:45None1CANCELLED
16ADANIPOWERBUY1907190017119141SL2019-07-19 12:54:45None1CANCELLED
17ADANIPOWERBUY1907190017258121SL2019-07-19 12:54:45None1CANCELLED
18IDEABUY1907190017598931SLNoneNone1REJECTED
19IDEABUY1907190017824601SLNoneNone1REJECTED
20CRESSANSELL1907190017318605LIMIT2019-07-19 12:55:01None5OPEN
21ADANIPOWERBUY1907190025059711MARKET2019-07-19 15:12:10None1COMPLETE
22ADANIPOWERBUY1907190021507511SL2019-07-19 15:12:35None1CANCELLED
23ADANIPOWERSELL1907190025216451MARKET2019-07-19 15:14:33None1COMPLETE
\n", 1240 | "
" 1241 | ], 1242 | "text/plain": [ 1243 | " tradingsymbol transaction_type order_id quantity order_type \\\n", 1244 | "0 SBIN BUY 190719001131173 1 SL \n", 1245 | "1 SBIN BUY 190719001136838 1 SL \n", 1246 | "2 SBIN BUY 190719001143372 1 SL \n", 1247 | "3 SBIN BUY 190719001191209 1 SL \n", 1248 | "4 IDEA BUY 190719001278642 1 SL \n", 1249 | "5 ADANIPOWER BUY 190719001279199 1 SL \n", 1250 | "6 IDEA BUY 190719001322367 1 LIMIT \n", 1251 | "7 SBIN BUY 190719000730248 1 SL \n", 1252 | "8 IDEA BUY 190719001368836 1 SL \n", 1253 | "9 ADANIPOWER BUY 190719001388194 1 SL \n", 1254 | "10 ADANIPOWER BUY 190719001393518 1 SL \n", 1255 | "11 IDEA BUY 190719001691092 1 SL \n", 1256 | "12 IDEA BUY 190719001700301 1 SL \n", 1257 | "13 IDEA BUY 190719001697624 1 SL \n", 1258 | "14 ADANIPOWER BUY 190719001706115 1 SL \n", 1259 | "15 ADANIPOWER BUY 190719001709108 1 SL \n", 1260 | "16 ADANIPOWER BUY 190719001711914 1 SL \n", 1261 | "17 ADANIPOWER BUY 190719001725812 1 SL \n", 1262 | "18 IDEA BUY 190719001759893 1 SL \n", 1263 | "19 IDEA BUY 190719001782460 1 SL \n", 1264 | "20 CRESSAN SELL 190719001731860 5 LIMIT \n", 1265 | "21 ADANIPOWER BUY 190719002505971 1 MARKET \n", 1266 | "22 ADANIPOWER BUY 190719002150751 1 SL \n", 1267 | "23 ADANIPOWER SELL 190719002521645 1 MARKET \n", 1268 | "\n", 1269 | " exchange_update_timestamp tag quantity status \n", 1270 | "0 None RSIbuy 1 REJECTED \n", 1271 | "1 None RSIbuy 1 REJECTED \n", 1272 | "2 None RSIbuy 1 REJECTED \n", 1273 | "3 None RSIbuy 1 REJECTED \n", 1274 | "4 2019-07-19 11:24:04 RSIbuy 1 REJECTED \n", 1275 | "5 2019-07-19 11:24:10 RSIbuy 1 REJECTED \n", 1276 | "6 2019-07-19 11:32:24 RSIbuy 1 COMPLETE \n", 1277 | "7 2019-07-19 11:44:36 RSIbuy 1 CANCELLED \n", 1278 | "8 2019-07-19 11:44:36 None 1 CANCELLED \n", 1279 | "9 2019-07-19 11:44:36 None 1 CANCELLED \n", 1280 | "10 2019-07-19 11:44:36 None 1 CANCELLED \n", 1281 | "11 None None 1 REJECTED \n", 1282 | "12 None None 1 REJECTED \n", 1283 | "13 2019-07-19 12:54:45 None 1 CANCELLED \n", 1284 | "14 2019-07-19 12:54:45 None 1 CANCELLED \n", 1285 | "15 2019-07-19 12:54:45 None 1 CANCELLED \n", 1286 | "16 2019-07-19 12:54:45 None 1 CANCELLED \n", 1287 | "17 2019-07-19 12:54:45 None 1 CANCELLED \n", 1288 | "18 None None 1 REJECTED \n", 1289 | "19 None None 1 REJECTED \n", 1290 | "20 2019-07-19 12:55:01 None 5 OPEN \n", 1291 | "21 2019-07-19 15:12:10 None 1 COMPLETE \n", 1292 | "22 2019-07-19 15:12:35 None 1 CANCELLED \n", 1293 | "23 2019-07-19 15:14:33 None 1 COMPLETE " 1294 | ] 1295 | }, 1296 | "metadata": {}, 1297 | "output_type": "display_data" 1298 | } 1299 | ], 1300 | "source": [ 1301 | "print(\" \\n \\t \\t \\t \\t \\t ---- ALL THE HOLDINGS YOU HAVE ON THIS ACCOUNT---- \")\n", 1302 | "display(pd.DataFrame(kite.holdings())[[\"tradingsymbol\",\"instrument_token\",\"pnl\",\"average_price\",\"close_price\",\"collateral_quantity\",\"collateral_type\",\"day_change\",\"day_change_percentage\",\"exchange\",\"last_price\",\"isin\",\"price\",\"product\",\"quantity\",\"realised_quantity\",\"t1_quantity\" ]]) \n", 1303 | "print(\" \\n \\t \\t \\t \\t \\t ---- ALL THE POSITONS YOU HAVE ON THIS ACCOUNT---- \")\n", 1304 | "display(pd.DataFrame(kite.positions()[\"net\"])[[\"tradingsymbol\",\"average_price\",\"buy_m2m\",\"buy_price\",\"buy_quantity\",\"buy_value\",\"close_price\",\"sell_m2m\",\"sell_price\",\"sell_quantity\",\"sell_value\",\"unrealised\",\"value\",\"day_buy_price\",\"day_buy_quantity\",\"day_buy_value\",\"day_sell_price\",\"product\",\"quantity\",\"realised\"]].head())\n", 1305 | "print(\" \\n \\t \\t \\t \\t \\t ---- ALL THE ORDERS YOU HAVE ON THIS ACCOUNT---- \")\n", 1306 | "display(pd.DataFrame(kite.orders())[[\"tradingsymbol\",\"transaction_type\",\"order_id\",\"quantity\",\"order_type\",\"exchange_update_timestamp\",'tag',\"quantity\",\"status\"]])" 1307 | ] 1308 | }, 1309 | { 1310 | "cell_type": "code", 1311 | "execution_count": null, 1312 | "metadata": {}, 1313 | "outputs": [], 1314 | "source": [] 1315 | } 1316 | ], 1317 | "metadata": { 1318 | "colab": { 1319 | "collapsed_sections": [], 1320 | "name": "RSI BOT 2.0.ipynb", 1321 | "provenance": [], 1322 | "version": "0.3.2" 1323 | }, 1324 | "kernelspec": { 1325 | "display_name": "Python 3 (ipykernel)", 1326 | "language": "python", 1327 | "name": "python3" 1328 | }, 1329 | "language_info": { 1330 | "codemirror_mode": { 1331 | "name": "ipython", 1332 | "version": 3 1333 | }, 1334 | "file_extension": ".py", 1335 | "mimetype": "text/x-python", 1336 | "name": "python", 1337 | "nbconvert_exporter": "python", 1338 | "pygments_lexer": "ipython3", 1339 | "version": "3.10.12" 1340 | } 1341 | }, 1342 | "nbformat": 4, 1343 | "nbformat_minor": 1 1344 | } 1345 | -------------------------------------------------------------------------------- /2.8 - Backtesting Guppy Multiple Moving Average (GMMA) with Python using Zerodha API.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "#https://unofficed.com/lessons/backtesting-guppy-multiple-moving-average-gmma-with-python-using-zerodha-api/" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": {}, 16 | "outputs": [], 17 | "source": [ 18 | "import logging\n", 19 | "from kiteconnect import KiteConnect\n", 20 | "\n", 21 | "logging.basicConfig(level=logging.DEBUG)\n", 22 | "\n", 23 | "kite = KiteConnect(api_key=\"your_api_key\")\n", 24 | "\n", 25 | "# Redirect the user to the login url obtained\n", 26 | "# from kite.login_url(), and receive the request_token\n", 27 | "# from the registered redirect url after the login flow.\n", 28 | "# Once you have the request_token, obtain the access_token\n", 29 | "# as follows.\n", 30 | "\n", 31 | "data = kite.generate_session(\"request_token_here\", api_secret=\"your_secret\")\n", 32 | "kite.set_access_token(data[\"access_token\"])" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 6, 38 | "metadata": {}, 39 | "outputs": [], 40 | "source": [ 41 | "from datetime import date, timedelta\n", 42 | "import pandas as pd\n", 43 | "from pytz import timezone" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "# Backtesting for BUY " 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 4, 56 | "metadata": {}, 57 | "outputs": [ 58 | { 59 | "name": "stdout", 60 | "output_type": "stream", 61 | "text": [ 62 | "SCANNING START\n", 63 | "complete\n" 64 | ] 65 | } 66 | ], 67 | "source": [ 68 | "tokenall=[5633, 6401, 3861249, 4451329, 2760193, 20993, 325121, 2524673, 41729, 49409, 54273, 60417, 70401, 1510401, 1195009, 1214721, 94977, 108033, 2714625, 2911489, 2763265, 3812865, 160001, 160769, 163073, 177665, 5215745, 3876097, 197633, 3771393, 225537, 173057, 261889, 1207553, 3463169, 2796801, 315393, 3378433, 2513665, 1850625, 340481, 341249, 3789569, 345089, 2747905, 348929, 359937, 356865, 364545, 3699201, 1270529, 377857, 3677697, 3060993, 381697, 2883073, 387073, 387841, 1346049, 408065, 2393089, 415745, 3920129, 424961, 1723649, 2661633, 2933761, 3011329, 4574465, 3001089, 4632577, 492033, 2061825, 511233, 2939649, 2672641, 519937, 2815745, 2674433, 582913, 593665, 3924993, 2977281, 2748929, 633601, 2819073, 636673, 2730497, 3834113, 2906881, 3364353, 731905, 3375873, 3930881, 737793, 738561, 141569, 3078657, 779521, 780289, 1492737, 1522689, 1102337, 857857, 3431425, 3076609, 1837825, 871681, 952577, 878593, 884737, 4343041, 877057, 895745, 2953217, 3465729, 897537, 2873089, 2952193, 2752769, 920065, 951809, 3026177, 969473, 3050241, 112129, 134657, 3721473, 2800641, 3385857, 4454401, 1152769, 806401, 617473, 2905857, 3660545, 3906305, 758529, 975873]\n", 69 | "ss=['ACC', 'ADANIENT', 'ADANIPORTS', 'ADANIPOWER', 'ALBK', 'ALOKTEXT', 'AMBUJACEM', 'ANDHRABANK', 'APOLLOTYRE', 'ARVIND', 'ASHOKLEY', 'ASIANPAINT', 'AUROPHARMA', 'AXISBANK', 'BANKBARODA', 'BANKINDIA', 'BATAINDIA', 'BHARATFORG', 'BHARTIARTL', 'BIOCON', 'CANBK', 'CENTRALBK', 'CENTURYTEX', 'CESC', 'CHAMBLFERT', 'CIPLA', 'COALINDIA', 'COLPAL', 'DABUR', 'DLF', 'DRREDDY', 'EXIDEIND', 'FEDERALBNK', 'GAIL', 'GMRINFRA', 'GODREJIND', 'GRASIM', 'GSPL', 'HAVELLS', 'HCLTECH', 'HDFC', 'HDFCBANK', 'HDIL', 'HEROMOTOCO', 'HEXAWARE', 'HINDALCO', 'HINDPETRO', 'HINDUNILVR', 'HINDZINC', 'IBREALEST', 'ICICIBANK', 'IDBI', 'IDEA', 'IDFC', 'IFCI', 'IGL', 'INDHOTEL', 'INDIACEM', 'INDUSINDBK', 'INFY', 'IOB', 'IOC', 'IRB', 'ITC', 'JINDALSTEL', 'JISLJALEQS', 'JPASSOCIAT', 'JPPOWER', 'JSWENERGY', 'JSWSTEEL', 'JUBLFOOD', 'KOTAKBANK', 'KTKBANK', 'LICHSGFIN', 'LT', 'LUPIN', 'M&M', 'MARUTI', 'MCDOWELL-N', 'MRF', 'NCC', 'NMDC', 'NTPC', 'OFSS', 'ONGC', 'OPTOCIRCUI', 'ORIENTBANK', 'PNB', 'POWERGRID', 'PTC', 'PUNJLLOYD', 'RAYMOND', 'RCOM', 'RECLTD', 'RELCAPITAL', 'RELIANCE', 'RELINFRA', 'RENUKA', 'SBIN', 'SCI', 'SINTEX', 'SOUTHBANK', 'SRTRANSFIN', 'SUNPHARMA', 'SUNTV', 'SUZLON', 'SYNDIBANK', 'TATACHEM', 'TATACOMM', 'TATAGLOBAL', 'TATAMOTORS', 'TATAMTRDVR', 'TATAPOWER', 'TATASTEEL', 'TCS', 'TECHM', 'TITAN', 'UCOBANK', 'ULTRACEMCO', 'UNIONBANK', 'UNITECH', 'VOLTAS', 'WELCORP', 'WIPRO', 'YESBANK', 'BHEL', 'BPCL', 'DISHTV', 'DIVISLAB', 'GVKPIL', 'NHPC', 'MPHASIS', 'SIEMENS', 'PEL', 'PETRONET', 'PFC', 'RPOWER', 'SAIL', 'ZEEL']\n", 70 | "\n", 71 | "# inputs for backtesting \n", 72 | "\n", 73 | "sdate =\"2018-08-20 00:00:00\"\n", 74 | "todate =\"2019-09-16 09:16:00\"\n", 75 | "#sdate =\"2019-08-19\"\n", 76 | "#todate =\"2019-10-01\"\n", 77 | "time_frame =\"minute\"\n", 78 | "quu=50000\n", 79 | "eexchange=\"NSE\"\n", 80 | "productt=\"MIS\"\n", 81 | "qu=int(quu)\n", 82 | "\n", 83 | "# baktesting starting and ending date \n", 84 | "\n", 85 | "sdate_backtest =\"2018-08-14\"\n", 86 | "todate_backtest =\"2019-10-02\"\n", 87 | "##############\n", 88 | "print(\"SCANNING START\")\n", 89 | "ttoken=5633\n", 90 | "ttradingsymbol=\"ACC\"\n", 91 | "\n", 92 | "strike_rate = 0\n", 93 | "strike_rate_list = []\n", 94 | "timeframe = []\n", 95 | "timeframe_list = []\n", 96 | "pnl = 0\n", 97 | "pnl_list = []\n", 98 | "\n", 99 | "def time_con(time,df):\n", 100 | " z = 0\n", 101 | " chg = []\n", 102 | " chg_index = 0\n", 103 | " timeframe = time\n", 104 | " date = np.array([])\n", 105 | " for i in range(len(df)):\n", 106 | " date = np.hstack((date,str(df.loc[i,\"date\"])))\n", 107 | " open_np = df.open.values\n", 108 | " high_np = df.high.values\n", 109 | " low_np = df.low.values\n", 110 | " close_np = df.close.values\n", 111 | "# volume_np = df.volume.values\n", 112 | "# print(date)\n", 113 | " for j in range(len(date)):\n", 114 | " if date[j][11:16] == \"09:15\":\n", 115 | " chg.append(j)\n", 116 | " np_date=np.array([])\n", 117 | " np_open = np.array([])\n", 118 | " np_high = np.array([])\n", 119 | " np_low = np.array([])\n", 120 | " np_close = np.array([])\n", 121 | "# np_volume = np.array([])\n", 122 | " for i in range(len(date)-timeframe+1):\n", 123 | " if i>=z:\n", 124 | " chg_index+=1\n", 125 | " if (chg_index < len(chg)):\n", 126 | " for k in range(i,chg[chg_index],timeframe):\n", 127 | "# print(date[k])\n", 128 | "# print(k,k+timeframe)\n", 129 | " np_date = np.hstack((np_date,date[k]))\n", 130 | " np_open = np.hstack((np_open,open_np[k]))\n", 131 | " np_high = np.hstack((np_high,np.amax(high_np[k:k+timeframe])))\n", 132 | "# print(high_np[k:k+timeframe])\n", 133 | " np_low = np.hstack((np_low,np.amin(low_np[k:k+timeframe])))\n", 134 | "# print(low_np[k:k+timeframe])\n", 135 | " np_close = np.hstack((np_close,close_np[k+timeframe-1]))\n", 136 | "# np_volume = np.hstack((np_volume,np.sum(volume_np[k:k+timeframe])))\n", 137 | " z = i+timeframe\n", 138 | " z=chg[chg_index]\n", 139 | " else:\n", 140 | " con_df = pd.DataFrame({\"date\":np_date,\"open\":np_open,\"high\":np_high,\"low\":np_low,\"close\":np_close})\n", 141 | " return con_df\n", 142 | "\n", 143 | "\n", 144 | "for t in range(1,2):\n", 145 | " dfw=kite.historical_data(ttoken,sdate_backtest,todate_backtest,time_frame,0)\n", 146 | " dfw=pd.DataFrame(dfw)\n", 147 | " df=pd.DataFrame(dfw[['date','open','high','low','close']])\n", 148 | " df = time_con(t,df)\n", 149 | " slow_ema = [3,5,7,9,11,13,15,17,19,21,23]\n", 150 | " fast_ema = [25,28,31,34,37,40,43,46,49,52,55,58,61,64,67,70,200]\n", 151 | " def EMA(df, base, target, period, alpha=False):\n", 152 | " con = pd.concat([df[:period][base].rolling(window=period).mean(), df[period:][base]])\n", 153 | " if (alpha == True):\n", 154 | " # (1 - alpha) * previous_val + alpha * current_val where alpha = 1 / period\n", 155 | " df[target] = con.ewm(alpha=1 / period, adjust=False).mean()\n", 156 | " else:\n", 157 | " # ((current_val - previous_val) * coeff) + previous_val where coeff = 2 / (period + 1)\n", 158 | " df[target] = con.ewm(span=period, adjust=False).mean()\n", 159 | " df.fillna(0,inplace = True)\n", 160 | " # return df\n", 161 | " for j in slow_ema:\n", 162 | " val = \"ema\"+\"_\"+str(j)\n", 163 | " EMA(df,\"close\",val,j)\n", 164 | " for k in fast_ema:\n", 165 | " val = \"ema\"+\"_\"+str(k)\n", 166 | " EMA(df,\"close\",val,k)\n", 167 | " def super_guppy(interval,df,anchor=0):\n", 168 | " anchor = 0\n", 169 | " ShowBreak = True\n", 170 | " ShowSwing = True\n", 171 | " ShowCon = False\n", 172 | " uOCCswing = False\n", 173 | " Lookback = 6\n", 174 | " emaFilter = False\n", 175 | " mult = 0\n", 176 | " buybreak = 0\n", 177 | " sellbreak = 0\n", 178 | " buy_barssince_var = 0\n", 179 | " sell_barssince_var = 0\n", 180 | " buybreak_barssince_var = 0\n", 181 | " sellbreak_barssince_var = 0\n", 182 | " barssince_lst = list()\n", 183 | " barssince_var = 0\n", 184 | " bar_count_var = 0\n", 185 | " buy1 = list()\n", 186 | " sell1 = list()\n", 187 | " buy2 = list()\n", 188 | " sell2 = list()\n", 189 | " buybreak1 = list()\n", 190 | " sellbreak1 = list()\n", 191 | " def barssince(b,barssince_var):\n", 192 | " barssince_lst = []\n", 193 | " barssince_var = 0 \n", 194 | " new_var = len(b)\n", 195 | " for i in b[::-1]:\n", 196 | " if i == 1:\n", 197 | " break\n", 198 | " barssince_lst.append(i)\n", 199 | " barssince_var = len(barssince_lst)\n", 200 | " return barssince_var\n", 201 | " barssince_lst.clear()\n", 202 | " if interval < 1441 :\n", 203 | " if (anchor==0 or interval <= 0 or interval >= anchor or anchor > 1441 ):\n", 204 | " mult = 1\n", 205 | " else:\n", 206 | " if round(anchor/interval) > 1:\n", 207 | " mult = round(anchor/interval)\n", 208 | " else:\n", 209 | " mult = 1\n", 210 | " else:\n", 211 | " mult = 1\n", 212 | " #isIntraday Not\n", 213 | " if interval > 1441:\n", 214 | " if (anchor==0 or interval <= 0 or interval >= anchor or anchor < 52 ):\n", 215 | " mult = mult\n", 216 | " else:\n", 217 | " if round(anchor/interval) > 1:\n", 218 | " mult = round(anchor/interval)\n", 219 | " else:\n", 220 | " mult = 1\n", 221 | " else:\n", 222 | " mult = mult\n", 223 | " mult = 1\n", 224 | " for i in range(len(df)):\n", 225 | " emaF1 = df.loc[i,'ema_3']\n", 226 | " emaF2 = df.loc[i,'ema_5']\n", 227 | " emaF3 = df.loc[i,'ema_7']\n", 228 | " emaF4 = df.loc[i,'ema_9']\n", 229 | " emaF5 = df.loc[i,'ema_11']\n", 230 | " emaF6 = df.loc[i,'ema_13']\n", 231 | " emaF7 = df.loc[i,'ema_15']\n", 232 | " emaF8 = df.loc[i,'ema_17']\n", 233 | " emaF9 = df.loc[i,'ema_19']\n", 234 | " emaF10 = df.loc[i,'ema_21']\n", 235 | " emaF11 = df.loc[i,'ema_23']\n", 236 | " emaS1 = df.loc[i,'ema_25']\n", 237 | " emaS2 = df.loc[i,'ema_28']\n", 238 | " emaS3 = df.loc[i,'ema_31']\n", 239 | " emaS4 = df.loc[i,'ema_34']\n", 240 | " emaS5 = df.loc[i,'ema_37']\n", 241 | " emaS6 = df.loc[i,'ema_40']\n", 242 | " emaS7 = df.loc[i,'ema_43']\n", 243 | " emaS8 = df.loc[i,'ema_46']\n", 244 | " emaS9 = df.loc[i,'ema_49']\n", 245 | " emaS10 = df.loc[i,'ema_52']\n", 246 | " emaS11 = df.loc[i,'ema_55']\n", 247 | " emaS12 = df.loc[i,'ema_58']\n", 248 | " emaS13 = df.loc[i,'ema_61']\n", 249 | " emaS14 = df.loc[i,'ema_64']\n", 250 | " emaS15 = df.loc[i,'ema_67']\n", 251 | " emaS16 = df.loc[i,'ema_70']\n", 252 | " ema200 = df.loc[i,'ema_200'] \n", 253 | " emafast = (emaF1 + emaF2 + emaF3 + emaF4 + emaF5 + emaF6 + emaF7 + emaF8 + emaF9 + emaF10 + emaF11)/11\n", 254 | " emaslow = (emaS1 + emaS2 + emaS3 + emaS4 + emaS5 + emaS6 + emaS7 + emaS8 + emaS9 + emaS10 + emaS11 + emaS12 + emaS13 + emaS14 + emaS15 + emaS16)/16\n", 255 | " #Fast EMA Color Rules\n", 256 | " colfastL = (emaF1>emaF2 and emaF2>emaF3 and emaF3>emaF4 and emaF4>emaF5 and emaF5>emaF6 and emaF6>emaF7 and emaF7>emaF8 and emaF8>emaF9 and emaF9>emaF10 and emaF10>emaF11)\n", 257 | " colfastS = (emaF1emaS2 and emaS2>emaS3 and emaS3>emaS4 and emaS4>emaS5 and emaS5>emaS6 and emaS6>emaS7 and emaS7>emaS8) and (emaS8>emaS9 and emaS9>emaS10 and emaS10>emaS11 and emaS11>emaS12 and emaS12>emaS13 and emaS13>emaS14 and emaS14>emaS15 and emaS15>emaS16)\n", 260 | " colslowS = (emaS1 emaslow and not colslowS and colfastL and (not ShowCon or colslowL) and (not emaFilter or emafast>ema200):\n", 262 | " if int(buy1[-1]) > 0:\n", 263 | " buy = buy1[-1] + 1\n", 264 | " else:\n", 265 | " buy = 1\n", 266 | " else:\n", 267 | " buy = 0\n", 268 | " buy1.append(buy)\n", 269 | " if emafast < emaslow and not colslowL and colfastS and (not ShowCon or colslowS) and (not emaFilter or emafast 0:\n", 271 | " sell = sell1[-1] + 1\n", 272 | " else:\n", 273 | " sell = 1\n", 274 | " else:\n", 275 | " sell = 0\n", 276 | " sell1.append(sell)\n", 277 | " #buy\n", 278 | " if buy>1 and colfastL and (uOCCswing and ((df.loc[i-1,'close']df.loc[i,'open']))):\n", 279 | " buy3 = 1\n", 280 | " else:\n", 281 | " buy3 = buy\n", 282 | " buy2.append(buy3)\n", 283 | " #sell \n", 284 | " if sell>1 and colfastS and (uOCCswing and ((df.loc[i-1,'close']df.loc[i,'open']))):\n", 285 | " sell3 = 1\n", 286 | " else:\n", 287 | " sell3 = sell\n", 288 | " sell2.append(sell3)\n", 289 | " #buybreak\n", 290 | " if emafast > emaslow and not colslowS and (not emaFilter or emafast>ema200):\n", 291 | " if buybreak1[-1] > 0:\n", 292 | " buybreak = buybreak1[-1] + 1\n", 293 | " else:\n", 294 | " buybreak = 1\n", 295 | " else:\n", 296 | " buybreak = 0\n", 297 | " buybreak1.append(buybreak)\n", 298 | " if emafast < emaslow and not colslowL and (not emaFilter or emafast 0:\n", 300 | " sellbreak = sellbreak1[-1]+1\n", 301 | " else:\n", 302 | " sellbreak = 1\n", 303 | " else:\n", 304 | " sellbreak = 0\n", 305 | " sellbreak1.append(sellbreak)\n", 306 | " #arrow plotting\n", 307 | " #buy_arrow\n", 308 | " buy_barssince_var = barssince(buy2[:-1],barssince_var)\n", 309 | " if (ShowSwing and buy3==1)and buy_barssince_var > 6:\n", 310 | " buy_arrow = 1\n", 311 | " else:\n", 312 | " buy_arrow = 0\n", 313 | " #sell arrow\n", 314 | " sell_barssince_var = barssince(sell2[:-1],barssince_var)\n", 315 | " if ShowSwing and (sell3==1 and sell_barssince_var > 6):\n", 316 | " sell_arrow = 1\n", 317 | " else:\n", 318 | " sell_arrow = 0\n", 319 | " #buybreak_arrow\n", 320 | " buybreak_barssince_var = barssince(buybreak1[:-1],barssince_var)\n", 321 | " sellbreak_barssince_var = barssince(sellbreak1[:-1],barssince_var)\n", 322 | " if ShowBreak and buybreak==1 and (sellbreak_barssince_var>Lookback) and (buybreak_barssince_var>Lookback):\n", 323 | " buybreak_arrow = 1\n", 324 | " else:\n", 325 | " buybreak_arrow = 0\n", 326 | " #sellbreak_arrow\n", 327 | " if ShowBreak and sellbreak==1 and (buybreak_barssince_var>Lookback) and (sellbreak_barssince_var>Lookback):\n", 328 | " sellbreak_arrow = 1\n", 329 | " else:\n", 330 | " sellbreak_arrow = 0\n", 331 | " if buy_arrow==1 and sell_arrow==0 and buybreak_arrow==0 and sellbreak_arrow==0:\n", 332 | " arrow_color = 'green'\n", 333 | " elif buy_arrow==0 and sell_arrow==1 and buybreak_arrow==0 and sellbreak_arrow==0:\n", 334 | " arrow_color = 'red'\n", 335 | " elif sell_arrow==0 and (buy_arrow==0 or buy_arrow==1) and buybreak_arrow==1 and sellbreak_arrow==0:\n", 336 | " arrow_color = 'aqua'\n", 337 | " elif buy_arrow==0 and (sell_arrow==1 or sell_arrow==0) and buybreak_arrow==0 and sellbreak_arrow==1:\n", 338 | " arrow_color = 'blue'\n", 339 | " else:\n", 340 | " arrow_color = 'none'\n", 341 | " df.loc[i,'arrow_color'] = arrow_color\n", 342 | " df = df[['date','open','high','low','close','arrow_color']]\n", 343 | " return df\n", 344 | " df=super_guppy(15,df)\n", 345 | " gup=df\n", 346 | " def bidatrema(df,period):\n", 347 | " df['hl']=abs(df['high']-df['low'])\n", 348 | " df['hpc']=abs(df['high']-df['close'].shift())\n", 349 | " df['lpc']=abs(df['low']-df['close'].shift())\n", 350 | " df['tr']=df[['hl','hpc','lpc']].max(axis=1)\n", 351 | " df['ATR']=pd.DataFrame.ewm(df[\"tr\"], span=period,min_periods=period).mean()\n", 352 | " df.drop([\"hl\",\"hpc\",\"lpc\",\"tr\"],axis = 1 , inplace =True)\n", 353 | " bidatrema(gup,14)\n", 354 | " df['bid_value'] = 0\n", 355 | " df['executed_value'] = 0\n", 356 | " df['diff'] = 0\n", 357 | " df['pnl'] = 0\n", 358 | " qty = int ( qu/ (gup.iloc[-1,2]) )\n", 359 | " var = False\n", 360 | " var_2 = False\n", 361 | " var_1 = False\n", 362 | " for i in range(len(df)):\n", 363 | " zz=df.loc[i,\"date\"]\n", 364 | " za=str(zz)[11:13]\n", 365 | " if za!=\"15\":\n", 366 | " if var==True:\n", 367 | " df.loc[i,'diff'] = df.loc[i,'close'] - bid_value\n", 368 | " df.loc[i,'pnl'] = ( bid_value /bid_value)*df.loc[i,'diff']\n", 369 | " var=False\n", 370 | " var1=False \n", 371 | " var_2=True \n", 372 | " if za!=\"15\":\n", 373 | " var_2=False\n", 374 | " if var==False and var_2==False:\n", 375 | " if df.loc[i,'arrow_color']=='green':\n", 376 | " bid_value = (df.loc[i,'high']+df.loc[i,'ATR']*.25)+(df.loc[i,'ATR']*.1)\n", 377 | " df.loc[i,'bid_value'] =bid_value\n", 378 | " var=True\n", 379 | " var1=False\n", 380 | " if var==True and var1==False and var_2==False:\n", 381 | " if df.loc[i,'high'] > bid_value and df.loc[i,'low'] < bid_value:\n", 382 | " df.loc[i,'executed_value']=bid_value\n", 383 | " var1=True\n", 384 | " if var==True and var1==True and var_2==False:\n", 385 | " if df.loc[i,'arrow_color']=='red':\n", 386 | " df.loc[i,'diff'] = df.loc[i,'close'] - bid_value\n", 387 | " df.loc[i,'pnl'] = ( bid_value /bid_value)*df.loc[i,'diff']\n", 388 | " var=False\n", 389 | " var1=False\n", 390 | " list_1 = df['pnl']\n", 391 | " pos_count = len(list(filter(lambda x: (x>0),list_1)))\n", 392 | " neg_count = len(list(filter(lambda x: (x<0),list_1)))\n", 393 | " total_trade = pos_count + neg_count\n", 394 | " try :\n", 395 | " strike_rate = pos_count/total_trade\n", 396 | " except:\n", 397 | " strike_rate = 0\n", 398 | " pnl = df['pnl'].sum()\n", 399 | " pnl_list.append(pnl)\n", 400 | " timeframe_list.append(t)\n", 401 | " strike_rate_list.append(strike_rate)\n", 402 | "main_df = pd.DataFrame({'timeframe':timeframe_list,'pnl':pnl_list,'strike_rate':strike_rate_list})\n", 403 | "main_df.to_csv('pnlbuy.csv')\n", 404 | "print(\"complete\")" 405 | ] 406 | }, 407 | { 408 | "cell_type": "code", 409 | "execution_count": 6, 410 | "metadata": {}, 411 | "outputs": [ 412 | { 413 | "data": { 414 | "text/html": [ 415 | "
\n", 416 | "\n", 429 | "\n", 430 | " \n", 431 | " \n", 432 | " \n", 433 | " \n", 434 | " \n", 435 | " \n", 436 | " \n", 437 | " \n", 438 | " \n", 439 | " \n", 440 | " \n", 441 | " \n", 442 | " \n", 443 | " \n", 444 | " \n", 445 | " \n", 446 | "
timeframepnlstrike_rate
01-73.2699110.214286
\n", 447 | "
" 448 | ], 449 | "text/plain": [ 450 | " timeframe pnl strike_rate\n", 451 | "0 1 -73.269911 0.214286" 452 | ] 453 | }, 454 | "metadata": {}, 455 | "output_type": "display_data" 456 | } 457 | ], 458 | "source": [ 459 | "display(main_df)" 460 | ] 461 | }, 462 | { 463 | "cell_type": "markdown", 464 | "metadata": {}, 465 | "source": [ 466 | "* * * * " 467 | ] 468 | }, 469 | { 470 | "cell_type": "markdown", 471 | "metadata": {}, 472 | "source": [ 473 | "# BACKTESTING for SELL" 474 | ] 475 | }, 476 | { 477 | "cell_type": "code", 478 | "execution_count": 8, 479 | "metadata": {}, 480 | "outputs": [ 481 | { 482 | "name": "stdout", 483 | "output_type": "stream", 484 | "text": [ 485 | "SCANNING START\n", 486 | "complete\n" 487 | ] 488 | } 489 | ], 490 | "source": [ 491 | "## fno\n", 492 | "tokenall=[5633, 6401, 3861249, 4451329, 2760193, 20993, 325121, 2524673, 41729, 49409, 54273, 60417, 70401, 1510401, 1195009, 1214721, 94977, 108033, 2714625, 2911489, 2763265, 3812865, 160001, 160769, 163073, 177665, 5215745, 3876097, 197633, 3771393, 225537, 173057, 261889, 1207553, 3463169, 2796801, 315393, 3378433, 2513665, 1850625, 340481, 341249, 3789569, 345089, 2747905, 348929, 359937, 356865, 364545, 3699201, 1270529, 377857, 3677697, 3060993, 381697, 2883073, 387073, 387841, 1346049, 408065, 2393089, 415745, 3920129, 424961, 1723649, 2661633, 2933761, 3011329, 4574465, 3001089, 4632577, 492033, 2061825, 511233, 2939649, 2672641, 519937, 2815745, 2674433, 582913, 593665, 3924993, 2977281, 2748929, 633601, 2819073, 636673, 2730497, 3834113, 2906881, 3364353, 731905, 3375873, 3930881, 737793, 738561, 141569, 3078657, 779521, 780289, 1492737, 1522689, 1102337, 857857, 3431425, 3076609, 1837825, 871681, 952577, 878593, 884737, 4343041, 877057, 895745, 2953217, 3465729, 897537, 2873089, 2952193, 2752769, 920065, 951809, 3026177, 969473, 3050241, 112129, 134657, 3721473, 2800641, 3385857, 4454401, 1152769, 806401, 617473, 2905857, 3660545, 3906305, 758529, 975873]\n", 493 | "ss=['ACC', 'ADANIENT', 'ADANIPORTS', 'ADANIPOWER', 'ALBK', 'ALOKTEXT', 'AMBUJACEM', 'ANDHRABANK', 'APOLLOTYRE', 'ARVIND', 'ASHOKLEY', 'ASIANPAINT', 'AUROPHARMA', 'AXISBANK', 'BANKBARODA', 'BANKINDIA', 'BATAINDIA', 'BHARATFORG', 'BHARTIARTL', 'BIOCON', 'CANBK', 'CENTRALBK', 'CENTURYTEX', 'CESC', 'CHAMBLFERT', 'CIPLA', 'COALINDIA', 'COLPAL', 'DABUR', 'DLF', 'DRREDDY', 'EXIDEIND', 'FEDERALBNK', 'GAIL', 'GMRINFRA', 'GODREJIND', 'GRASIM', 'GSPL', 'HAVELLS', 'HCLTECH', 'HDFC', 'HDFCBANK', 'HDIL', 'HEROMOTOCO', 'HEXAWARE', 'HINDALCO', 'HINDPETRO', 'HINDUNILVR', 'HINDZINC', 'IBREALEST', 'ICICIBANK', 'IDBI', 'IDEA', 'IDFC', 'IFCI', 'IGL', 'INDHOTEL', 'INDIACEM', 'INDUSINDBK', 'INFY', 'IOB', 'IOC', 'IRB', 'ITC', 'JINDALSTEL', 'JISLJALEQS', 'JPASSOCIAT', 'JPPOWER', 'JSWENERGY', 'JSWSTEEL', 'JUBLFOOD', 'KOTAKBANK', 'KTKBANK', 'LICHSGFIN', 'LT', 'LUPIN', 'M&M', 'MARUTI', 'MCDOWELL-N', 'MRF', 'NCC', 'NMDC', 'NTPC', 'OFSS', 'ONGC', 'OPTOCIRCUI', 'ORIENTBANK', 'PNB', 'POWERGRID', 'PTC', 'PUNJLLOYD', 'RAYMOND', 'RCOM', 'RECLTD', 'RELCAPITAL', 'RELIANCE', 'RELINFRA', 'RENUKA', 'SBIN', 'SCI', 'SINTEX', 'SOUTHBANK', 'SRTRANSFIN', 'SUNPHARMA', 'SUNTV', 'SUZLON', 'SYNDIBANK', 'TATACHEM', 'TATACOMM', 'TATAGLOBAL', 'TATAMOTORS', 'TATAMTRDVR', 'TATAPOWER', 'TATASTEEL', 'TCS', 'TECHM', 'TITAN', 'UCOBANK', 'ULTRACEMCO', 'UNIONBANK', 'UNITECH', 'VOLTAS', 'WELCORP', 'WIPRO', 'YESBANK', 'BHEL', 'BPCL', 'DISHTV', 'DIVISLAB', 'GVKPIL', 'NHPC', 'MPHASIS', 'SIEMENS', 'PEL', 'PETRONET', 'PFC', 'RPOWER', 'SAIL', 'ZEEL']\n", 494 | "\n", 495 | "\n", 496 | "sdate =\"2019-08-20 00:00:00\"\n", 497 | "todate =\"2019-09-16 09:16:00\"\n", 498 | "#sdate =\"2019-08-19\"\n", 499 | "#todate =\"2019-10-01\"\n", 500 | "\n", 501 | "# inputs for backtesting\n", 502 | "\n", 503 | "time_frame =\"minute\"\n", 504 | "quu=50000\n", 505 | "eexchange=\"NSE\"\n", 506 | "productt=\"MIS\"\n", 507 | "qu=int(quu)\n", 508 | "\n", 509 | "# Backtesting Dates\n", 510 | "sdate_backtest =\"2019-08-14\"\n", 511 | "todate_backtest =\"2019-10-02\"\n", 512 | "\n", 513 | "print(\"SCANNING START\")\n", 514 | "ttoken=5633\n", 515 | "ttradingsymbol=\"ACC\"\n", 516 | "\n", 517 | "\n", 518 | "def time_con(time,df):\n", 519 | " z = 0\n", 520 | " chg = []\n", 521 | " chg_index = 0\n", 522 | " timeframe = time\n", 523 | " date = np.array([])\n", 524 | " for i in range(len(df)):\n", 525 | " date = np.hstack((date,str(df.loc[i,\"date\"])))\n", 526 | " open_np = df.open.values\n", 527 | " high_np = df.high.values\n", 528 | " low_np = df.low.values\n", 529 | " close_np = df.close.values\n", 530 | "# volume_np = df.volume.values\n", 531 | "# print(date)\n", 532 | " for j in range(len(date)):\n", 533 | " if date[j][11:16] == \"09:15\":\n", 534 | " chg.append(j)\n", 535 | " np_date=np.array([])\n", 536 | " np_open = np.array([])\n", 537 | " np_high = np.array([])\n", 538 | " np_low = np.array([])\n", 539 | " np_close = np.array([])\n", 540 | "# np_volume = np.array([])\n", 541 | " for i in range(len(date)-timeframe+1):\n", 542 | " if i>=z:\n", 543 | " chg_index+=1\n", 544 | " if (chg_index < len(chg)):\n", 545 | " for k in range(i,chg[chg_index],timeframe):\n", 546 | "# print(date[k])\n", 547 | "# print(k,k+timeframe)\n", 548 | " np_date = np.hstack((np_date,date[k]))\n", 549 | " np_open = np.hstack((np_open,open_np[k]))\n", 550 | " np_high = np.hstack((np_high,np.amax(high_np[k:k+timeframe])))\n", 551 | "# print(high_np[k:k+timeframe])\n", 552 | " np_low = np.hstack((np_low,np.amin(low_np[k:k+timeframe])))\n", 553 | "# print(low_np[k:k+timeframe])\n", 554 | " np_close = np.hstack((np_close,close_np[k+timeframe-1]))\n", 555 | "# np_volume = np.hstack((np_volume,np.sum(volume_np[k:k+timeframe])))\n", 556 | " z = i+timeframe\n", 557 | " z=chg[chg_index]\n", 558 | " else:\n", 559 | " con_df = pd.DataFrame({\"date\":np_date,\"open\":np_open,\"high\":np_high,\"low\":np_low,\"close\":np_close})\n", 560 | " return con_df\n", 561 | "\n", 562 | "strike_rate = 0\n", 563 | "strike_rate_list = []\n", 564 | "timeframe = []\n", 565 | "timeframe_list = []\n", 566 | "pnl = 0\n", 567 | "pnl_list = []\n", 568 | "\n", 569 | "for t in range(1,2):\n", 570 | " dfw=kite.historical_data(ttoken,sdate_backtest,todate_backtest,time_frame,0)\n", 571 | " dfw=pd.DataFrame(dfw)\n", 572 | " df=pd.DataFrame(dfw[['date','open','high','low','close']])\n", 573 | " df= time_con(t,df)\n", 574 | " slow_ema = [3,5,7,9,11,13,15,17,19,21,23]\n", 575 | " fast_ema = [25,28,31,34,37,40,43,46,49,52,55,58,61,64,67,70,200]\n", 576 | " def EMA(df, base, target, period, alpha=False):\n", 577 | " con = pd.concat([df[:period][base].rolling(window=period).mean(), df[period:][base]])\n", 578 | " if (alpha == True):\n", 579 | " # (1 - alpha) * previous_val + alpha * current_val where alpha = 1 / period\n", 580 | " df[target] = con.ewm(alpha=1 / period, adjust=False).mean()\n", 581 | " else:\n", 582 | " # ((current_val - previous_val) * coeff) + previous_val where coeff = 2 / (period + 1)\n", 583 | " df[target] = con.ewm(span=period, adjust=False).mean()\n", 584 | " df.fillna(0,inplace = True)\n", 585 | " # return df\n", 586 | " for j in slow_ema:\n", 587 | " val = \"ema\"+\"_\"+str(j)\n", 588 | " EMA(df,\"close\",val,j)\n", 589 | " for k in fast_ema:\n", 590 | " val = \"ema\"+\"_\"+str(k)\n", 591 | " EMA(df,\"close\",val,k)\n", 592 | " def super_guppy(interval,df,anchor=0):\n", 593 | " anchor = 0\n", 594 | " ShowBreak = True\n", 595 | " ShowSwing = True\n", 596 | " ShowCon = False\n", 597 | " uOCCswing = False\n", 598 | " Lookback = 6\n", 599 | " emaFilter = False\n", 600 | " mult = 0\n", 601 | " buybreak = 0\n", 602 | " sellbreak = 0\n", 603 | " buy_barssince_var = 0\n", 604 | " sell_barssince_var = 0\n", 605 | " buybreak_barssince_var = 0\n", 606 | " sellbreak_barssince_var = 0\n", 607 | " barssince_lst = list()\n", 608 | " barssince_var = 0\n", 609 | " bar_count_var = 0\n", 610 | " buy1 = list()\n", 611 | " sell1 = list()\n", 612 | " buy2 = list()\n", 613 | " sell2 = list()\n", 614 | " buybreak1 = list()\n", 615 | " sellbreak1 = list()\n", 616 | " def barssince(b,barssince_var):\n", 617 | " barssince_lst = []\n", 618 | " barssince_var = 0 \n", 619 | " new_var = len(b)\n", 620 | " for i in b[::-1]:\n", 621 | " if i == 1:\n", 622 | " break\n", 623 | " barssince_lst.append(i)\n", 624 | " barssince_var = len(barssince_lst)\n", 625 | " return barssince_var\n", 626 | " barssince_lst.clear()\n", 627 | " if interval < 1441 :\n", 628 | " if (anchor==0 or interval <= 0 or interval >= anchor or anchor > 1441 ):\n", 629 | " mult = 1\n", 630 | " else:\n", 631 | " if round(anchor/interval) > 1:\n", 632 | " mult = round(anchor/interval)\n", 633 | " else:\n", 634 | " mult = 1\n", 635 | " else:\n", 636 | " mult = 1\n", 637 | " #isIntraday Not\n", 638 | " if interval > 1441:\n", 639 | " if (anchor==0 or interval <= 0 or interval >= anchor or anchor < 52 ):\n", 640 | " mult = mult\n", 641 | " else:\n", 642 | " if round(anchor/interval) > 1:\n", 643 | " mult = round(anchor/interval)\n", 644 | " else:\n", 645 | " mult = 1\n", 646 | " else:\n", 647 | " mult = mult\n", 648 | " mult = 1\n", 649 | " for i in range(len(df)):\n", 650 | " emaF1 = df.loc[i,'ema_3']\n", 651 | " emaF2 = df.loc[i,'ema_5']\n", 652 | " emaF3 = df.loc[i,'ema_7']\n", 653 | " emaF4 = df.loc[i,'ema_9']\n", 654 | " emaF5 = df.loc[i,'ema_11']\n", 655 | " emaF6 = df.loc[i,'ema_13']\n", 656 | " emaF7 = df.loc[i,'ema_15']\n", 657 | " emaF8 = df.loc[i,'ema_17']\n", 658 | " emaF9 = df.loc[i,'ema_19']\n", 659 | " emaF10 = df.loc[i,'ema_21']\n", 660 | " emaF11 = df.loc[i,'ema_23']\n", 661 | " emaS1 = df.loc[i,'ema_25']\n", 662 | " emaS2 = df.loc[i,'ema_28']\n", 663 | " emaS3 = df.loc[i,'ema_31']\n", 664 | " emaS4 = df.loc[i,'ema_34']\n", 665 | " emaS5 = df.loc[i,'ema_37']\n", 666 | " emaS6 = df.loc[i,'ema_40']\n", 667 | " emaS7 = df.loc[i,'ema_43']\n", 668 | " emaS8 = df.loc[i,'ema_46']\n", 669 | " emaS9 = df.loc[i,'ema_49']\n", 670 | " emaS10 = df.loc[i,'ema_52']\n", 671 | " emaS11 = df.loc[i,'ema_55']\n", 672 | " emaS12 = df.loc[i,'ema_58']\n", 673 | " emaS13 = df.loc[i,'ema_61']\n", 674 | " emaS14 = df.loc[i,'ema_64']\n", 675 | " emaS15 = df.loc[i,'ema_67']\n", 676 | " emaS16 = df.loc[i,'ema_70']\n", 677 | " ema200 = df.loc[i,'ema_200'] \n", 678 | " emafast = (emaF1 + emaF2 + emaF3 + emaF4 + emaF5 + emaF6 + emaF7 + emaF8 + emaF9 + emaF10 + emaF11)/11\n", 679 | " emaslow = (emaS1 + emaS2 + emaS3 + emaS4 + emaS5 + emaS6 + emaS7 + emaS8 + emaS9 + emaS10 + emaS11 + emaS12 + emaS13 + emaS14 + emaS15 + emaS16)/16\n", 680 | " #Fast EMA Color Rules\n", 681 | " colfastL = (emaF1>emaF2 and emaF2>emaF3 and emaF3>emaF4 and emaF4>emaF5 and emaF5>emaF6 and emaF6>emaF7 and emaF7>emaF8 and emaF8>emaF9 and emaF9>emaF10 and emaF10>emaF11)\n", 682 | " colfastS = (emaF1emaS2 and emaS2>emaS3 and emaS3>emaS4 and emaS4>emaS5 and emaS5>emaS6 and emaS6>emaS7 and emaS7>emaS8) and (emaS8>emaS9 and emaS9>emaS10 and emaS10>emaS11 and emaS11>emaS12 and emaS12>emaS13 and emaS13>emaS14 and emaS14>emaS15 and emaS15>emaS16)\n", 685 | " colslowS = (emaS1 emaslow and not colslowS and colfastL and (not ShowCon or colslowL) and (not emaFilter or emafast>ema200):\n", 687 | " if int(buy1[-1]) > 0:\n", 688 | " buy = buy1[-1] + 1\n", 689 | " else:\n", 690 | " buy = 1\n", 691 | " else:\n", 692 | " buy = 0\n", 693 | " buy1.append(buy)\n", 694 | " if emafast < emaslow and not colslowL and colfastS and (not ShowCon or colslowS) and (not emaFilter or emafast 0:\n", 696 | " sell = sell1[-1] + 1\n", 697 | " else:\n", 698 | " sell = 1\n", 699 | " else:\n", 700 | " sell = 0\n", 701 | " sell1.append(sell)\n", 702 | " #buy\n", 703 | " if buy>1 and colfastL and (uOCCswing and ((df.loc[i-1,'close']df.loc[i,'open']))):\n", 704 | " buy3 = 1\n", 705 | " else:\n", 706 | " buy3 = buy\n", 707 | " buy2.append(buy3)\n", 708 | " #sell \n", 709 | " if sell>1 and colfastS and (uOCCswing and ((df.loc[i-1,'close']df.loc[i,'open']))):\n", 710 | " sell3 = 1\n", 711 | " else:\n", 712 | " sell3 = sell\n", 713 | " sell2.append(sell3)\n", 714 | " #buybreak\n", 715 | " if emafast > emaslow and not colslowS and (not emaFilter or emafast>ema200):\n", 716 | " if buybreak1[-1] > 0:\n", 717 | " buybreak = buybreak1[-1] + 1\n", 718 | " else:\n", 719 | " buybreak = 1\n", 720 | " else:\n", 721 | " buybreak = 0\n", 722 | " buybreak1.append(buybreak)\n", 723 | " if emafast < emaslow and not colslowL and (not emaFilter or emafast 0:\n", 725 | " sellbreak = sellbreak1[-1]+1\n", 726 | " else:\n", 727 | " sellbreak = 1\n", 728 | " else:\n", 729 | " sellbreak = 0\n", 730 | " sellbreak1.append(sellbreak)\n", 731 | " #arrow plotting\n", 732 | " #buy_arrow\n", 733 | " buy_barssince_var = barssince(buy2[:-1],barssince_var)\n", 734 | " if (ShowSwing and buy3==1)and buy_barssince_var > 6:\n", 735 | " buy_arrow = 1\n", 736 | " else:\n", 737 | " buy_arrow = 0\n", 738 | " #sell arrow\n", 739 | " sell_barssince_var = barssince(sell2[:-1],barssince_var)\n", 740 | " if ShowSwing and (sell3==1 and sell_barssince_var > 6):\n", 741 | " sell_arrow = 1\n", 742 | " else:\n", 743 | " sell_arrow = 0\n", 744 | " #buybreak_arrow\n", 745 | " buybreak_barssince_var = barssince(buybreak1[:-1],barssince_var)\n", 746 | " sellbreak_barssince_var = barssince(sellbreak1[:-1],barssince_var)\n", 747 | " if ShowBreak and buybreak==1 and (sellbreak_barssince_var>Lookback) and (buybreak_barssince_var>Lookback):\n", 748 | " buybreak_arrow = 1\n", 749 | " else:\n", 750 | " buybreak_arrow = 0\n", 751 | " #sellbreak_arrow\n", 752 | " if ShowBreak and sellbreak==1 and (buybreak_barssince_var>Lookback) and (sellbreak_barssince_var>Lookback):\n", 753 | " sellbreak_arrow = 1\n", 754 | " else:\n", 755 | " sellbreak_arrow = 0\n", 756 | " if buy_arrow==1 and sell_arrow==0 and buybreak_arrow==0 and sellbreak_arrow==0:\n", 757 | " arrow_color = 'green'\n", 758 | " elif buy_arrow==0 and sell_arrow==1 and buybreak_arrow==0 and sellbreak_arrow==0:\n", 759 | " arrow_color = 'red'\n", 760 | " elif sell_arrow==0 and (buy_arrow==0 or buy_arrow==1) and buybreak_arrow==1 and sellbreak_arrow==0:\n", 761 | " arrow_color = 'aqua'\n", 762 | " elif buy_arrow==0 and (sell_arrow==1 or sell_arrow==0) and buybreak_arrow==0 and sellbreak_arrow==1:\n", 763 | " arrow_color = 'blue'\n", 764 | " else:\n", 765 | " arrow_color = 'none'\n", 766 | " df.loc[i,'arrow_color'] = arrow_color\n", 767 | " df = df[['date','open','high','low','close','arrow_color']]\n", 768 | " return df\n", 769 | " df=super_guppy(15,df)\n", 770 | " gup=df\n", 771 | " def bidatrema(df,period):\n", 772 | " df['hl']=abs(df['high']-df['low'])\n", 773 | " df['hpc']=abs(df['high']-df['close'].shift())\n", 774 | " df['lpc']=abs(df['low']-df['close'].shift())\n", 775 | " df['tr']=df[['hl','hpc','lpc']].max(axis=1)\n", 776 | " df['ATR']=pd.DataFrame.ewm(df[\"tr\"], span=period,min_periods=period).mean()\n", 777 | " df.drop([\"hl\",\"hpc\",\"lpc\",\"tr\"],axis = 1 , inplace =True)\n", 778 | " bidatrema(gup,14)\n", 779 | " df['bid_value'] = 0\n", 780 | " df['executed_value'] = 0\n", 781 | " df['diff'] = 0\n", 782 | " df['pnl'] = 0\n", 783 | " qty = int ( qu/ (gup.iloc[-1,2]) )\n", 784 | " var = False\n", 785 | " var_2 = False\n", 786 | " var_1 = False\n", 787 | " for i in range(len(df)):\n", 788 | " zz=df.loc[i,\"date\"]\n", 789 | " za=str(zz)[11:13]\n", 790 | " if za==\"15\":\n", 791 | " if var==True:\n", 792 | " df.loc[i,'diff'] = bid_value - df.loc[i,'close']\n", 793 | " df.loc[i,'pnl'] = ( bid_value /bid_value)*df.loc[i,'diff']\n", 794 | " ##\n", 795 | " var=False\n", 796 | " var1=False \n", 797 | " var_2=True\n", 798 | " if za!=\"15\":\n", 799 | " var_2=False\n", 800 | " if var==False and var_2==False:\n", 801 | " if df.loc[i,'arrow_color']=='red':\n", 802 | " bid_value = (df.loc[i,'low']-df.loc[i,'ATR']*.25)-(df.loc[i,'ATR']*.1)\n", 803 | " df.loc[i,'bid_value'] =bid_value\n", 804 | " var=True\n", 805 | " var1=False\n", 806 | " if var==True and var1==False and var_2==False:\n", 807 | " if df.loc[i,'high'] > bid_value and df.loc[i,'low'] < bid_value:\n", 808 | " df.loc[i,'executed_value']=bid_value\n", 809 | " var1=True\n", 810 | " if var==True and var1==True and var_2==False:\n", 811 | " if df.loc[i,'arrow_color']=='green':\n", 812 | " df.loc[i,'diff'] = bid_value - df.loc[i,'close'] \n", 813 | " df.loc[i,'pnl'] = ( bid_value /bid_value)*df.loc[i,'diff']\n", 814 | " var=False\n", 815 | " var1=False\n", 816 | " list_1 = df['pnl']\n", 817 | " pos_count = len(list(filter(lambda x: (x>0),list_1)))\n", 818 | " neg_count = len(list(filter(lambda x: (x<0),list_1)))\n", 819 | " total_trade = pos_count + neg_count\n", 820 | " try :\n", 821 | " strike_rate = pos_count/total_trade\n", 822 | " except:\n", 823 | " strike_rate = 0\n", 824 | " #print(strike_rate)\n", 825 | " pnl = df['pnl'].sum()\n", 826 | " pnl_list.append(pnl)\n", 827 | " timeframe_list.append(t)\n", 828 | " strike_rate_list.append(strike_rate)\n", 829 | "main_df = pd.DataFrame({'timeframe':timeframe_list,'pnl':pnl_list,'strike_rate':strike_rate_list})\n", 830 | "main_df.to_csv('pnlsell.csv')\n", 831 | "print(\"complete\")" 832 | ] 833 | }, 834 | { 835 | "cell_type": "code", 836 | "execution_count": 9, 837 | "metadata": {}, 838 | "outputs": [ 839 | { 840 | "data": { 841 | "text/html": [ 842 | "
\n", 843 | "\n", 856 | "\n", 857 | " \n", 858 | " \n", 859 | " \n", 860 | " \n", 861 | " \n", 862 | " \n", 863 | " \n", 864 | " \n", 865 | " \n", 866 | " \n", 867 | " \n", 868 | " \n", 869 | " \n", 870 | " \n", 871 | " \n", 872 | " \n", 873 | "
timeframepnlstrike_rate
01-11.927320.333333
\n", 874 | "
" 875 | ], 876 | "text/plain": [ 877 | " timeframe pnl strike_rate\n", 878 | "0 1 -11.92732 0.333333" 879 | ] 880 | }, 881 | "metadata": {}, 882 | "output_type": "display_data" 883 | } 884 | ], 885 | "source": [ 886 | "display(main_df)" 887 | ] 888 | } 889 | ], 890 | "metadata": { 891 | "kernelspec": { 892 | "display_name": "Python 3 (ipykernel)", 893 | "language": "python", 894 | "name": "python3" 895 | }, 896 | "language_info": { 897 | "codemirror_mode": { 898 | "name": "ipython", 899 | "version": 3 900 | }, 901 | "file_extension": ".py", 902 | "mimetype": "text/x-python", 903 | "name": "python", 904 | "nbconvert_exporter": "python", 905 | "pygments_lexer": "ipython3", 906 | "version": "3.10.12" 907 | } 908 | }, 909 | "nbformat": 4, 910 | "nbformat_minor": 2 911 | } 912 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🌟 Mastering AlgoTrading: A Beginner's Guide using KiteConnect API 🌟 2 | 3 | > 🚀 Launch into the realm of algorithmic trading with this beginner's guide. Explore the intricacies of the Indian Stock Market through practical examples and hands-on experience. This guide is tailored for those looking to harness the power of Zerodha's KiteConnect API for effective and efficient trading strategies. 4 | 5 | ## 🌐 Overview 6 | 7 | - **Zerodha KiteConnect** 8 | - 🤖 Immerse yourself in the world of algorithmic trading with Zerodha's feature-rich trading platform, offering comprehensive functionalities for an enhanced trading experience. 9 | - **Complete Course** 10 | - 📘 Dive deeper into algorithmic trading by exploring the full course content [here](https://unofficed.com/courses/mastering-algotrading-a-beginners-guide-using-kiteconnect-api/). 11 | 12 | ## 📚 Table of Contents 13 | 14 | 1. **Getting Instrument Token of a Scrip Using Python and Zerodha API** 15 | 2. **Getting Instrument Token of a Scrip Using Python and Zerodha Kite LTP Method** 16 | 3. **Getting Historical Data of Reliance Using Python and Zerodha API** 17 | 4. **Getting Historical Data of Reliance In Pandas Using Python and Zerodha API** 18 | 5. **Organizing Expiry Dates of Financial Instruments with Python** 19 | 6. **Buy on RSI Strategy Coding in Python using Zerodha** 20 | 7. **Guppy Strategy Screener Using Python and Zerodha** 21 | 8. **Guppy Strategy Trading Bot Using Python and Zerodha** 22 | 9. **Guppy Indicator Trading Bot Using Python and Zerodha** 23 | 10. **Plotting OHLC to Candlestick Chart** 24 | 11. **Plotting Zerodha OHLC to Candlestick Chart** 25 | 12. **Converting Candles to Heikin Ashi Using Zerodha KiteConnect** 26 | 13. **Creating TimeFrames from Minute-Based Data Using Python** 27 | 14. **Multi-Stock Bot Using Guppy Strategy with Screener and Backtesting** 28 | 15. **RSI Based Trading Bot with Python using Zerodha API** 29 | 16. **Multi-Timeframe Bot Using Guppy Strategy and Screener** 30 | 17. **Backtesting Guppy Multiple Moving Average (GMMA) with Python using Zerodha API** 31 | 18. **Exploring Technical Indicators in the Indian Stock Market with Zerodha API and Python** 32 | 33 | ## 📜 Descriptions 34 | 35 | ### 1. Getting Instrument Token of a Scrip Using Python and Zerodha API 36 | - Learn how to retrieve the instrument token for a specific scrip using Python in conjunction with the Zerodha API. 37 | 38 | ### 2. Getting Instrument Token of a Scrip Using Python and Zerodha Kite LTP Method 39 | - A guide to acquiring instrument tokens using the Kite LTP (Last Traded Price) method in Python with Zerodha's API. 40 | 41 | ### 3. Getting Historical Data of Reliance Using Python and Zerodha API 42 | - Discover the method to fetch historical data for Reliance stocks using Python through the Zerodha API. 43 | 44 | ### 4. Getting Historical Data of Reliance In Pandas Using Python and Zerodha API 45 | - Explore how to process and analyze Reliance's historical data using Pandas in Python, leveraging Zerodha's API. 46 | 47 | ### 5. Organizing Expiry Dates of Financial Instruments with Python 48 | - Learn to organize and manage the expiry dates of various financial instruments using Python. 49 | 50 | ### 6. Buy on RSI Strategy Coding in Python using Zerodha 51 | - Implement the RSI (Relative Strength Index) strategy in Python for trading decisions on the Zerodha platform. 52 | 53 | ### 7. Guppy Strategy Screener Using Python and Zerodha 54 | - A comprehensive guide to using the Guppy strategy for screening stocks with Python on Zerodha. 55 | 56 | ### 8. Guppy Strategy Trading Bot Using Python and Zerodha 57 | - Develop an automated trading bot that employs the Guppy strategy in Python with Zerodha's API. 58 | 59 | ### 9. Guppy Indicator Trading Bot Using Python and Zerodha 60 | - Create a trading bot focused on the Guppy indicator, utilizing Python and Zerodha's tools for automated trading. 61 | 62 | ### 10. Plotting OHLC to Candlestick Chart 63 | - Understand the process of transforming OHLC (Open, High, Low, Close) data into intuitive candlestick charts. 64 | 65 | ### 11. Plotting Zerodha OHLC to Candlestick Chart 66 | - Learn to convert Zerodha's OHLC data into candlestick charts for better visualization and analysis. 67 | 68 | ### 12. Converting Candles to Heikin Ashi Using Zerodha KiteConnect 69 | - Master the technique of converting candlestick charts to Heikin Ashi format using Zerodha KiteConnect. 70 | 71 | ### 13. Creating TimeFrames from Minute-Based Data Using Python 72 | - A guide to creating various timeframes from minute-based data for comprehensive market analysis using Python. 73 | 74 | ### 14. Multi-Stock Bot Using Guppy Strategy with Screener and Backtesting 75 | - Build a multi-stock trading bot that incorporates the Guppy strategy, complete with screening and backtesting functionalities. 76 | 77 | ### 15. RSI Based Trading Bot with Python using Zerodha API 78 | - Develop a trading bot in Python that operates based on the RSI strategy, integrated with Zerodha's API. 79 | 80 | ### 16. Multi-Timeframe Bot Using Guppy Strategy and Screener 81 | - Create an advanced bot that operates across multiple timeframes using the Guppy strategy, along with stock screening. 82 | 83 | ### 17. Backtesting Guppy Multiple Moving Average (GMMA) with Python using Zerodha API 84 | - Learn the nuances of backtesting the Guppy Multiple Moving Average strategy using Python with Zerodha's API. 85 | 86 | ### 18. Exploring Technical Indicators in the Indian Stock Market with Zerodha API and Python 87 | - Dive into various technical indicators and their application in the Indian stock market using Python and Zerodha's API. 88 | 89 | 90 | ## 🚀 Conclusion 91 | 92 | Step into the exciting world of Algo Trading. This guide is your gateway to understanding and applying effective trading strategies in the Indian stock market, leveraging the technological prowess of Zerodha’s KiteConnect API. 93 | --------------------------------------------------------------------------------