├── 0). All_codes_download.zip
├── 1)_Getting_Started_with_Zerodha_.ipynb
├── BACKTESTIG_PROGRAM_.ipynb
├── Buy on RSI when the current high of candle is more then previous high of candle .ipynb
├── Hisorical_Data_Download_of_stocks.ipynb
├── Live_BOT_(1)_on_RSI_.ipynb
├── Live_BOT_(2)_on_GUPPY_with_screener.ipynb
├── Live_BOT_(3)_Guppy_Automated_.ipynb
├── Live_BOT_(4),_advance_bot_,multiple_bot_working_in_single_bot_.ipynb
├── Live_BOT_(5)_.ipynb
├── Stock_Screener_(GUPPY)_.ipynb
├── Technical_Indicator's_of_Indian_Stock_market.ipynb
├── change time frame.ipynb
├── conversion code of Candles to hikenashi.ipynb
├── order_information.jpg
└── readme.md
/0). All_codes_download.zip:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ashishkumar30/Stock_Market_Live_Trading_using_AI/d59f8ce1cbefc000e5eb02b90dfdbf4159aad406/0). All_codes_download.zip
--------------------------------------------------------------------------------
/1)_Getting_Started_with_Zerodha_.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "kernelspec": {
6 | "display_name": "Python 3",
7 | "language": "python",
8 | "name": "python3"
9 | },
10 | "language_info": {
11 | "codemirror_mode": {
12 | "name": "ipython",
13 | "version": 3
14 | },
15 | "file_extension": ".py",
16 | "mimetype": "text/x-python",
17 | "name": "python",
18 | "nbconvert_exporter": "python",
19 | "pygments_lexer": "ipython3",
20 | "version": "3.7.3"
21 | },
22 | "colab": {
23 | "name": "1). Getting Started with Zerodha .ipynb",
24 | "provenance": [],
25 | "collapsed_sections": [],
26 | "include_colab_link": true
27 | }
28 | },
29 | "cells": [
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {
33 | "id": "view-in-github",
34 | "colab_type": "text"
35 | },
36 | "source": [
37 | "
"
38 | ]
39 | },
40 | {
41 | "cell_type": "markdown",
42 | "metadata": {
43 | "id": "kXWQClNAL7Mm",
44 | "colab_type": "text"
45 | },
46 | "source": [
47 | "# Starting new project with zerodha"
48 | ]
49 | },
50 | {
51 | "cell_type": "markdown",
52 | "metadata": {
53 | "id": "_jiHn21EL7Mq",
54 | "colab_type": "text"
55 | },
56 | "source": [
57 | "install Kiteconnect\n",
58 | "* pip install kiteconnect "
59 | ]
60 | },
61 | {
62 | "cell_type": "markdown",
63 | "metadata": {
64 | "id": "ucuJO79BL7Ms",
65 | "colab_type": "text"
66 | },
67 | "source": [
68 | "# Import All libraries required"
69 | ]
70 | },
71 | {
72 | "cell_type": "code",
73 | "metadata": {
74 | "id": "C0OaOAMUL7Mu",
75 | "colab_type": "code",
76 | "colab": {}
77 | },
78 | "source": [
79 | "import time\n",
80 | "import secrets \n",
81 | "import threading\n",
82 | "import webbrowser\n",
83 | "import numpy as np\n",
84 | "import pandas as pd \n",
85 | "from math import modf\n",
86 | "from pprint import pprint\n",
87 | "from pytz import timezone\n",
88 | "from datetime import datetime \n",
89 | "from time import gmtime, strftime\n",
90 | "from kiteconnect import KiteTicker \n",
91 | "from kiteconnect import KiteConnect\n",
92 | "from IPython.display import clear_output"
93 | ],
94 | "execution_count": 0,
95 | "outputs": []
96 | },
97 | {
98 | "cell_type": "markdown",
99 | "metadata": {
100 | "id": "IQ5RKDp2L7M1",
101 | "colab_type": "text"
102 | },
103 | "source": [
104 | "# Login to zerodha "
105 | ]
106 | },
107 | {
108 | "cell_type": "markdown",
109 | "metadata": {
110 | "id": "4jgYunT5L7M3",
111 | "colab_type": "text"
112 | },
113 | "source": [
114 | "enter your api key and api secret"
115 | ]
116 | },
117 | {
118 | "cell_type": "code",
119 | "metadata": {
120 | "id": "CMnS0c19L7M4",
121 | "colab_type": "code",
122 | "colab": {}
123 | },
124 | "source": [
125 | "api_key='******************* @@enter your api key here@@@**************' \n",
126 | "api_secret='**********************@@enter your api key here****************'\n",
127 | "print(KiteConnect(api_key,api_secret).login_url())\n",
128 | "webbrowser.open_new_tab(KiteConnect(api_key,api_secret).login_url())\n",
129 | "\n",
130 | "# make object \n",
131 | "\n",
132 | "kite = KiteConnect(api_key=api_key)\n",
133 | "data = kite.generate_session(((str(input(\"ENTER full link generated in URL :- \")).split(\"request_token=\")[1]).split(\"&action\"))[0],api_secret)\n",
134 | "kite.set_access_token(data[\"access_token\"])\n",
135 | "print(\"success\")"
136 | ],
137 | "execution_count": 0,
138 | "outputs": []
139 | },
140 | {
141 | "cell_type": "markdown",
142 | "metadata": {
143 | "id": "_hzNKYX5L7M-",
144 | "colab_type": "text"
145 | },
146 | "source": [
147 | "# Finding \" Instrument Token list of zerodha\" "
148 | ]
149 | },
150 | {
151 | "cell_type": "code",
152 | "metadata": {
153 | "id": "-6ciKl6pL7M_",
154 | "colab_type": "code",
155 | "colab": {},
156 | "outputId": "f68ea1a0-6fba-445e-f1cd-ebde5ac38dc4"
157 | },
158 | "source": [
159 | "pd.DataFrame(kite.instruments()).tail(20)"
160 | ],
161 | "execution_count": 0,
162 | "outputs": [
163 | {
164 | "output_type": "execute_result",
165 | "data": {
166 | "text/html": [
167 | "
\n",
168 | "\n",
181 | "
\n",
182 | " \n",
183 | " \n",
184 | " | \n",
185 | " instrument_token | \n",
186 | " exchange_token | \n",
187 | " tradingsymbol | \n",
188 | " name | \n",
189 | " last_price | \n",
190 | " expiry | \n",
191 | " strike | \n",
192 | " tick_size | \n",
193 | " lot_size | \n",
194 | " instrument_type | \n",
195 | " segment | \n",
196 | " exchange | \n",
197 | "
\n",
198 | " \n",
199 | " \n",
200 | " \n",
201 | " 58709 | \n",
202 | " 1045249 | \n",
203 | " 4083 | \n",
204 | " AMBANIORG-SM | \n",
205 | " | \n",
206 | " 0.0 | \n",
207 | " | \n",
208 | " 0.0 | \n",
209 | " 0.05 | \n",
210 | " 2000 | \n",
211 | " EQ | \n",
212 | " NSE | \n",
213 | " NSE | \n",
214 | "
\n",
215 | " \n",
216 | " 58710 | \n",
217 | " 1823233 | \n",
218 | " 7122 | \n",
219 | " 788GS2030-GS | \n",
220 | " | \n",
221 | " 0.0 | \n",
222 | " | \n",
223 | " 0.0 | \n",
224 | " 0.01 | \n",
225 | " 1 | \n",
226 | " EQ | \n",
227 | " NSE | \n",
228 | " NSE | \n",
229 | "
\n",
230 | " \n",
231 | " 58711 | \n",
232 | " 5181953 | \n",
233 | " 20242 | \n",
234 | " OBEROIRLTY | \n",
235 | " OBEROI REALTY | \n",
236 | " 0.0 | \n",
237 | " | \n",
238 | " 0.0 | \n",
239 | " 0.05 | \n",
240 | " 1 | \n",
241 | " EQ | \n",
242 | " NSE | \n",
243 | " NSE | \n",
244 | "
\n",
245 | " \n",
246 | " 58712 | \n",
247 | " 2762497 | \n",
248 | " 10791 | \n",
249 | " NUCLEUS | \n",
250 | " NUCLEUS SOFTWARE EXPORTS | \n",
251 | " 0.0 | \n",
252 | " | \n",
253 | " 0.0 | \n",
254 | " 0.05 | \n",
255 | " 1 | \n",
256 | " EQ | \n",
257 | " NSE | \n",
258 | " NSE | \n",
259 | "
\n",
260 | " \n",
261 | " 58713 | \n",
262 | " 2752257 | \n",
263 | " 10751 | \n",
264 | " NTPC-ND | \n",
265 | " TFB 7.62% 2035 SR. 3B | \n",
266 | " 0.0 | \n",
267 | " | \n",
268 | " 0.0 | \n",
269 | " 0.01 | \n",
270 | " 1 | \n",
271 | " EQ | \n",
272 | " NSE | \n",
273 | " NSE | \n",
274 | "
\n",
275 | " \n",
276 | " 58714 | \n",
277 | " 2751745 | \n",
278 | " 10749 | \n",
279 | " NTPC-NC | \n",
280 | " TFB 7.53% 2030 SR. 2B | \n",
281 | " 0.0 | \n",
282 | " | \n",
283 | " 0.0 | \n",
284 | " 0.01 | \n",
285 | " 1 | \n",
286 | " EQ | \n",
287 | " NSE | \n",
288 | " NSE | \n",
289 | "
\n",
290 | " \n",
291 | " 58715 | \n",
292 | " 2750977 | \n",
293 | " 10746 | \n",
294 | " NTPC-NB | \n",
295 | " TFB 7.36% 2025 SR. 1B | \n",
296 | " 0.0 | \n",
297 | " | \n",
298 | " 0.0 | \n",
299 | " 0.01 | \n",
300 | " 1 | \n",
301 | " EQ | \n",
302 | " NSE | \n",
303 | " NSE | \n",
304 | "
\n",
305 | " \n",
306 | " 58716 | \n",
307 | " 2750465 | \n",
308 | " 10744 | \n",
309 | " NTPC-NA | \n",
310 | " TFB 7.37% 2035 SR. 3A | \n",
311 | " 0.0 | \n",
312 | " | \n",
313 | " 0.0 | \n",
314 | " 0.01 | \n",
315 | " 1 | \n",
316 | " EQ | \n",
317 | " NSE | \n",
318 | " NSE | \n",
319 | "
\n",
320 | " \n",
321 | " 58717 | \n",
322 | " 2749697 | \n",
323 | " 10741 | \n",
324 | " NTPC-N9 | \n",
325 | " TFB 7.28% 2030 SR. 2A | \n",
326 | " 0.0 | \n",
327 | " | \n",
328 | " 0.0 | \n",
329 | " 0.01 | \n",
330 | " 1 | \n",
331 | " EQ | \n",
332 | " NSE | \n",
333 | " NSE | \n",
334 | "
\n",
335 | " \n",
336 | " 58718 | \n",
337 | " 2748673 | \n",
338 | " 10737 | \n",
339 | " NTPC-N8 | \n",
340 | " TFB 7.11% 2025 SR. 1A | \n",
341 | " 0.0 | \n",
342 | " | \n",
343 | " 0.0 | \n",
344 | " 0.01 | \n",
345 | " 1 | \n",
346 | " EQ | \n",
347 | " NSE | \n",
348 | " NSE | \n",
349 | "
\n",
350 | " \n",
351 | " 58719 | \n",
352 | " 1888513 | \n",
353 | " 7377 | \n",
354 | " NTPC-N7 | \n",
355 | " 8.49% SEC NON-CUM RED NCD | \n",
356 | " 0.0 | \n",
357 | " | \n",
358 | " 0.0 | \n",
359 | " 0.01 | \n",
360 | " 1 | \n",
361 | " EQ | \n",
362 | " NSE | \n",
363 | " NSE | \n",
364 | "
\n",
365 | " \n",
366 | " 58720 | \n",
367 | " 3730433 | \n",
368 | " 14572 | \n",
369 | " GLOBOFFS | \n",
370 | " | \n",
371 | " 0.0 | \n",
372 | " | \n",
373 | " 0.0 | \n",
374 | " 0.05 | \n",
375 | " 1 | \n",
376 | " EQ | \n",
377 | " NSE | \n",
378 | " NSE | \n",
379 | "
\n",
380 | " \n",
381 | " 58721 | \n",
382 | " 5052929 | \n",
383 | " 19738 | \n",
384 | " GLOBAL-SM | \n",
385 | " GLOBAL EDUCATION | \n",
386 | " 0.0 | \n",
387 | " | \n",
388 | " 0.0 | \n",
389 | " 0.05 | \n",
390 | " 1000 | \n",
391 | " EQ | \n",
392 | " NSE | \n",
393 | " NSE | \n",
394 | "
\n",
395 | " \n",
396 | " 58722 | \n",
397 | " 1895937 | \n",
398 | " 7406 | \n",
399 | " GLENMARK | \n",
400 | " GLENMARK PHARMACEUTICALS | \n",
401 | " 0.0 | \n",
402 | " | \n",
403 | " 0.0 | \n",
404 | " 0.05 | \n",
405 | " 1 | \n",
406 | " EQ | \n",
407 | " NSE | \n",
408 | " NSE | \n",
409 | "
\n",
410 | " \n",
411 | " 58723 | \n",
412 | " 295169 | \n",
413 | " 1153 | \n",
414 | " GLAXO | \n",
415 | " GLAXOSMITHKLINE PHARMA LT | \n",
416 | " 0.0 | \n",
417 | " | \n",
418 | " 0.0 | \n",
419 | " 0.05 | \n",
420 | " 1 | \n",
421 | " EQ | \n",
422 | " NSE | \n",
423 | " NSE | \n",
424 | "
\n",
425 | " \n",
426 | " 58724 | \n",
427 | " 1225217 | \n",
428 | " 4786 | \n",
429 | " AKG-SM | \n",
430 | " | \n",
431 | " 0.0 | \n",
432 | " | \n",
433 | " 0.0 | \n",
434 | " 0.05 | \n",
435 | " 4000 | \n",
436 | " EQ | \n",
437 | " NSE | \n",
438 | " NSE | \n",
439 | "
\n",
440 | " \n",
441 | " 58725 | \n",
442 | " 6953217 | \n",
443 | " 27161 | \n",
444 | " CINELINE | \n",
445 | " CINELINE INDIA | \n",
446 | " 0.0 | \n",
447 | " | \n",
448 | " 0.0 | \n",
449 | " 0.05 | \n",
450 | " 1 | \n",
451 | " EQ | \n",
452 | " NSE | \n",
453 | " NSE | \n",
454 | "
\n",
455 | " \n",
456 | " 58726 | \n",
457 | " 6447105 | \n",
458 | " 25184 | \n",
459 | " CIMMCO | \n",
460 | " CIMMCO | \n",
461 | " 0.0 | \n",
462 | " | \n",
463 | " 0.0 | \n",
464 | " 0.05 | \n",
465 | " 1 | \n",
466 | " EQ | \n",
467 | " NSE | \n",
468 | " NSE | \n",
469 | "
\n",
470 | " \n",
471 | " 58727 | \n",
472 | " 1316353 | \n",
473 | " 5142 | \n",
474 | " CIGNITITEC | \n",
475 | " | \n",
476 | " 0.0 | \n",
477 | " | \n",
478 | " 0.0 | \n",
479 | " 0.05 | \n",
480 | " 1 | \n",
481 | " EQ | \n",
482 | " NSE | \n",
483 | " NSE | \n",
484 | "
\n",
485 | " \n",
486 | " 58728 | \n",
487 | " 175361 | \n",
488 | " 685 | \n",
489 | " CHOLAFIN | \n",
490 | " CHOLAMANDALAM IN & FIN CO | \n",
491 | " 0.0 | \n",
492 | " | \n",
493 | " 0.0 | \n",
494 | " 0.05 | \n",
495 | " 1 | \n",
496 | " EQ | \n",
497 | " NSE | \n",
498 | " NSE | \n",
499 | "
\n",
500 | " \n",
501 | "
\n",
502 | "
"
503 | ],
504 | "text/plain": [
505 | " instrument_token exchange_token tradingsymbol \\\n",
506 | "58709 1045249 4083 AMBANIORG-SM \n",
507 | "58710 1823233 7122 788GS2030-GS \n",
508 | "58711 5181953 20242 OBEROIRLTY \n",
509 | "58712 2762497 10791 NUCLEUS \n",
510 | "58713 2752257 10751 NTPC-ND \n",
511 | "58714 2751745 10749 NTPC-NC \n",
512 | "58715 2750977 10746 NTPC-NB \n",
513 | "58716 2750465 10744 NTPC-NA \n",
514 | "58717 2749697 10741 NTPC-N9 \n",
515 | "58718 2748673 10737 NTPC-N8 \n",
516 | "58719 1888513 7377 NTPC-N7 \n",
517 | "58720 3730433 14572 GLOBOFFS \n",
518 | "58721 5052929 19738 GLOBAL-SM \n",
519 | "58722 1895937 7406 GLENMARK \n",
520 | "58723 295169 1153 GLAXO \n",
521 | "58724 1225217 4786 AKG-SM \n",
522 | "58725 6953217 27161 CINELINE \n",
523 | "58726 6447105 25184 CIMMCO \n",
524 | "58727 1316353 5142 CIGNITITEC \n",
525 | "58728 175361 685 CHOLAFIN \n",
526 | "\n",
527 | " name last_price expiry strike tick_size \\\n",
528 | "58709 0.0 0.0 0.05 \n",
529 | "58710 0.0 0.0 0.01 \n",
530 | "58711 OBEROI REALTY 0.0 0.0 0.05 \n",
531 | "58712 NUCLEUS SOFTWARE EXPORTS 0.0 0.0 0.05 \n",
532 | "58713 TFB 7.62% 2035 SR. 3B 0.0 0.0 0.01 \n",
533 | "58714 TFB 7.53% 2030 SR. 2B 0.0 0.0 0.01 \n",
534 | "58715 TFB 7.36% 2025 SR. 1B 0.0 0.0 0.01 \n",
535 | "58716 TFB 7.37% 2035 SR. 3A 0.0 0.0 0.01 \n",
536 | "58717 TFB 7.28% 2030 SR. 2A 0.0 0.0 0.01 \n",
537 | "58718 TFB 7.11% 2025 SR. 1A 0.0 0.0 0.01 \n",
538 | "58719 8.49% SEC NON-CUM RED NCD 0.0 0.0 0.01 \n",
539 | "58720 0.0 0.0 0.05 \n",
540 | "58721 GLOBAL EDUCATION 0.0 0.0 0.05 \n",
541 | "58722 GLENMARK PHARMACEUTICALS 0.0 0.0 0.05 \n",
542 | "58723 GLAXOSMITHKLINE PHARMA LT 0.0 0.0 0.05 \n",
543 | "58724 0.0 0.0 0.05 \n",
544 | "58725 CINELINE INDIA 0.0 0.0 0.05 \n",
545 | "58726 CIMMCO 0.0 0.0 0.05 \n",
546 | "58727 0.0 0.0 0.05 \n",
547 | "58728 CHOLAMANDALAM IN & FIN CO 0.0 0.0 0.05 \n",
548 | "\n",
549 | " lot_size instrument_type segment exchange \n",
550 | "58709 2000 EQ NSE NSE \n",
551 | "58710 1 EQ NSE NSE \n",
552 | "58711 1 EQ NSE NSE \n",
553 | "58712 1 EQ NSE NSE \n",
554 | "58713 1 EQ NSE NSE \n",
555 | "58714 1 EQ NSE NSE \n",
556 | "58715 1 EQ NSE NSE \n",
557 | "58716 1 EQ NSE NSE \n",
558 | "58717 1 EQ NSE NSE \n",
559 | "58718 1 EQ NSE NSE \n",
560 | "58719 1 EQ NSE NSE \n",
561 | "58720 1 EQ NSE NSE \n",
562 | "58721 1000 EQ NSE NSE \n",
563 | "58722 1 EQ NSE NSE \n",
564 | "58723 1 EQ NSE NSE \n",
565 | "58724 4000 EQ NSE NSE \n",
566 | "58725 1 EQ NSE NSE \n",
567 | "58726 1 EQ NSE NSE \n",
568 | "58727 1 EQ NSE NSE \n",
569 | "58728 1 EQ NSE NSE "
570 | ]
571 | },
572 | "metadata": {
573 | "tags": []
574 | },
575 | "execution_count": 29
576 | }
577 | ]
578 | },
579 | {
580 | "cell_type": "markdown",
581 | "metadata": {
582 | "id": "wupBzeMsL7NG",
583 | "colab_type": "text"
584 | },
585 | "source": [
586 | "# Finding a token from symbol"
587 | ]
588 | },
589 | {
590 | "cell_type": "code",
591 | "metadata": {
592 | "id": "Koy2VLajL7NI",
593 | "colab_type": "code",
594 | "colab": {},
595 | "outputId": "1debe635-8a35-408f-c228-67fec9935f7a"
596 | },
597 | "source": [
598 | "print(\"input token name\")\n",
599 | "token_n=str(input())\n",
600 | "print(\"enter exchange\")\n",
601 | "exc=str(input())\n",
602 | "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)]"
603 | ],
604 | "execution_count": 0,
605 | "outputs": [
606 | {
607 | "output_type": "stream",
608 | "text": [
609 | "input token name\n",
610 | "SUNPHARMA\n",
611 | "enter exchange\n",
612 | "NSE\n"
613 | ],
614 | "name": "stdout"
615 | },
616 | {
617 | "output_type": "execute_result",
618 | "data": {
619 | "text/html": [
620 | "\n",
621 | "\n",
634 | "
\n",
635 | " \n",
636 | " \n",
637 | " | \n",
638 | " tradingsymbol | \n",
639 | " instrument_token | \n",
640 | " name | \n",
641 | " exchange | \n",
642 | "
\n",
643 | " \n",
644 | " \n",
645 | " \n",
646 | " 378 | \n",
647 | " SUNPHARMA | \n",
648 | " 857857 | \n",
649 | " SUN PHARMACEUTICAL IND L | \n",
650 | " NSE | \n",
651 | "
\n",
652 | " \n",
653 | "
\n",
654 | "
"
655 | ],
656 | "text/plain": [
657 | " tradingsymbol instrument_token name exchange\n",
658 | "378 SUNPHARMA 857857 SUN PHARMACEUTICAL IND L NSE"
659 | ]
660 | },
661 | "metadata": {
662 | "tags": []
663 | },
664 | "execution_count": 8
665 | }
666 | ]
667 | },
668 | {
669 | "cell_type": "markdown",
670 | "metadata": {
671 | "id": "1fRSYtDGL7NO",
672 | "colab_type": "text"
673 | },
674 | "source": [
675 | "# INPUTS"
676 | ]
677 | },
678 | {
679 | "cell_type": "markdown",
680 | "metadata": {
681 | "id": "RMBqa3gXL7NQ",
682 | "colab_type": "text"
683 | },
684 | "source": [
685 | "Example: ( Inputs are case sensitive )\n",
686 | "1. Trading symbol ... ( \"SUNPHARMA \")\n",
687 | "2. exchange ... (\" NSE / BSE \" / \" CDS \")\n",
688 | "3. productt ... (\" CNC\" / \" NRML \" )\n",
689 | "4. Quantity ... (1 , 2 , 3 etc ! how much u want)\n",
690 | "5. Instrument token ... ( Ex SUNPHARMA instrument token is 857857 )\n",
691 | "6. RSI in condition ... ( 30 )\n",
692 | "7. RSI out condition ... ( 31 )\n",
693 | "8. rofit booking RSI condition... ( 70 )\n",
694 | "9. Time frame of candels ... ( \"minute\"/ \"10 minute\" )\n",
695 | "10. starting data of Data ... ( \"2019-06-05\" )\n",
696 | "11. Ending data Of Data ... ( \"2019-07-02\" )"
697 | ]
698 | },
699 | {
700 | "cell_type": "markdown",
701 | "metadata": {
702 | "id": "MGovyvrHL7NS",
703 | "colab_type": "text"
704 | },
705 | "source": [
706 | "# Getting HISTORICAL DATA"
707 | ]
708 | },
709 | {
710 | "cell_type": "code",
711 | "metadata": {
712 | "id": "et9R_RZnL7NT",
713 | "colab_type": "code",
714 | "colab": {},
715 | "outputId": "6b85af02-315d-4c0e-9792-bb3404ac8802"
716 | },
717 | "source": [
718 | "token=857857 # this is the token of sunpharma \n",
719 | "starting_date=\"2019-05-10\"\n",
720 | "ending_date=\"2020-08-16\"\n",
721 | "time_period=\"day\"\n",
722 | "\n",
723 | "zap=kite.historical_data(token,starting_date,ending_date,time_period,0)\n",
724 | "zapp=pd.DataFrame(zap).tail(10)\n",
725 | "\n",
726 | "zapp"
727 | ],
728 | "execution_count": 0,
729 | "outputs": [
730 | {
731 | "output_type": "execute_result",
732 | "data": {
733 | "text/html": [
734 | "\n",
735 | "\n",
748 | "
\n",
749 | " \n",
750 | " \n",
751 | " | \n",
752 | " close | \n",
753 | " date | \n",
754 | " high | \n",
755 | " low | \n",
756 | " open | \n",
757 | " volume | \n",
758 | "
\n",
759 | " \n",
760 | " \n",
761 | " \n",
762 | " 68 | \n",
763 | " 426.00 | \n",
764 | " 2019-08-19 00:00:00+05:30 | \n",
765 | " 434.20 | \n",
766 | " 421.00 | \n",
767 | " 424.75 | \n",
768 | " 11584821 | \n",
769 | "
\n",
770 | " \n",
771 | " 69 | \n",
772 | " 420.90 | \n",
773 | " 2019-08-20 00:00:00+05:30 | \n",
774 | " 426.50 | \n",
775 | " 417.30 | \n",
776 | " 426.00 | \n",
777 | " 5151625 | \n",
778 | "
\n",
779 | " \n",
780 | " 70 | \n",
781 | " 417.00 | \n",
782 | " 2019-08-21 00:00:00+05:30 | \n",
783 | " 425.85 | \n",
784 | " 413.60 | \n",
785 | " 421.00 | \n",
786 | " 5120294 | \n",
787 | "
\n",
788 | " \n",
789 | " 71 | \n",
790 | " 414.55 | \n",
791 | " 2019-08-22 00:00:00+05:30 | \n",
792 | " 420.75 | \n",
793 | " 408.50 | \n",
794 | " 416.70 | \n",
795 | " 5299316 | \n",
796 | "
\n",
797 | " \n",
798 | " 72 | \n",
799 | " 427.80 | \n",
800 | " 2019-08-23 00:00:00+05:30 | \n",
801 | " 429.95 | \n",
802 | " 408.90 | \n",
803 | " 415.00 | \n",
804 | " 5479802 | \n",
805 | "
\n",
806 | " \n",
807 | " 73 | \n",
808 | " 419.35 | \n",
809 | " 2019-08-26 00:00:00+05:30 | \n",
810 | " 429.80 | \n",
811 | " 415.10 | \n",
812 | " 428.00 | \n",
813 | " 6258106 | \n",
814 | "
\n",
815 | " \n",
816 | " 74 | \n",
817 | " 416.75 | \n",
818 | " 2019-08-27 00:00:00+05:30 | \n",
819 | " 425.45 | \n",
820 | " 415.10 | \n",
821 | " 424.05 | \n",
822 | " 6738413 | \n",
823 | "
\n",
824 | " \n",
825 | " 75 | \n",
826 | " 413.10 | \n",
827 | " 2019-08-28 00:00:00+05:30 | \n",
828 | " 421.70 | \n",
829 | " 411.50 | \n",
830 | " 416.90 | \n",
831 | " 3441725 | \n",
832 | "
\n",
833 | " \n",
834 | " 76 | \n",
835 | " 434.65 | \n",
836 | " 2019-08-29 00:00:00+05:30 | \n",
837 | " 438.60 | \n",
838 | " 421.10 | \n",
839 | " 428.00 | \n",
840 | " 19967263 | \n",
841 | "
\n",
842 | " \n",
843 | " 77 | \n",
844 | " 450.40 | \n",
845 | " 2019-08-30 00:00:00+05:30 | \n",
846 | " 453.40 | \n",
847 | " 429.75 | \n",
848 | " 432.95 | \n",
849 | " 12308149 | \n",
850 | "
\n",
851 | " \n",
852 | "
\n",
853 | "
"
854 | ],
855 | "text/plain": [
856 | " close date high low open volume\n",
857 | "68 426.00 2019-08-19 00:00:00+05:30 434.20 421.00 424.75 11584821\n",
858 | "69 420.90 2019-08-20 00:00:00+05:30 426.50 417.30 426.00 5151625\n",
859 | "70 417.00 2019-08-21 00:00:00+05:30 425.85 413.60 421.00 5120294\n",
860 | "71 414.55 2019-08-22 00:00:00+05:30 420.75 408.50 416.70 5299316\n",
861 | "72 427.80 2019-08-23 00:00:00+05:30 429.95 408.90 415.00 5479802\n",
862 | "73 419.35 2019-08-26 00:00:00+05:30 429.80 415.10 428.00 6258106\n",
863 | "74 416.75 2019-08-27 00:00:00+05:30 425.45 415.10 424.05 6738413\n",
864 | "75 413.10 2019-08-28 00:00:00+05:30 421.70 411.50 416.90 3441725\n",
865 | "76 434.65 2019-08-29 00:00:00+05:30 438.60 421.10 428.00 19967263\n",
866 | "77 450.40 2019-08-30 00:00:00+05:30 453.40 429.75 432.95 12308149"
867 | ]
868 | },
869 | "metadata": {
870 | "tags": []
871 | },
872 | "execution_count": 11
873 | }
874 | ]
875 | },
876 | {
877 | "cell_type": "markdown",
878 | "metadata": {
879 | "id": "KIx98xOHL7NY",
880 | "colab_type": "text"
881 | },
882 | "source": [
883 | "# Getting continous data of stock "
884 | ]
885 | },
886 | {
887 | "cell_type": "code",
888 | "metadata": {
889 | "id": "demMQQvwL7Na",
890 | "colab_type": "code",
891 | "colab": {},
892 | "outputId": "8e8fb3f5-015d-4281-9c32-d82b4dfe756a"
893 | },
894 | "source": [
895 | "token=857857 # this is the token of sunpharma \n",
896 | "starting_date=\"2019-05-10\"\n",
897 | "ending_date=\"2020-08-16\"\n",
898 | "time_period=\"minute\"\n",
899 | "\n",
900 | "def livedata():\n",
901 | " while (True):\n",
902 | " clear_output(wait=True)\n",
903 | " z=kite.historical_data(token,starting_date,ending_date,time_period,0)\n",
904 | " za=pd.DataFrame(z)\n",
905 | " display(za.tail())\n",
906 | " time.sleep(1)\n",
907 | "livedata()"
908 | ],
909 | "execution_count": 0,
910 | "outputs": [
911 | {
912 | "output_type": "display_data",
913 | "data": {
914 | "text/html": [
915 | "\n",
916 | "\n",
929 | "
\n",
930 | " \n",
931 | " \n",
932 | " | \n",
933 | " close | \n",
934 | " date | \n",
935 | " high | \n",
936 | " low | \n",
937 | " open | \n",
938 | " volume | \n",
939 | "
\n",
940 | " \n",
941 | " \n",
942 | " \n",
943 | " 29244 | \n",
944 | " 452.45 | \n",
945 | " 2019-08-30 15:25:00+05:30 | \n",
946 | " 452.85 | \n",
947 | " 452.35 | \n",
948 | " 452.80 | \n",
949 | " 36323 | \n",
950 | "
\n",
951 | " \n",
952 | " 29245 | \n",
953 | " 452.05 | \n",
954 | " 2019-08-30 15:26:00+05:30 | \n",
955 | " 452.55 | \n",
956 | " 452.05 | \n",
957 | " 452.45 | \n",
958 | " 16843 | \n",
959 | "
\n",
960 | " \n",
961 | " 29246 | \n",
962 | " 452.20 | \n",
963 | " 2019-08-30 15:27:00+05:30 | \n",
964 | " 452.25 | \n",
965 | " 452.00 | \n",
966 | " 452.05 | \n",
967 | " 30296 | \n",
968 | "
\n",
969 | " \n",
970 | " 29247 | \n",
971 | " 452.95 | \n",
972 | " 2019-08-30 15:28:00+05:30 | \n",
973 | " 452.95 | \n",
974 | " 452.05 | \n",
975 | " 452.20 | \n",
976 | " 49371 | \n",
977 | "
\n",
978 | " \n",
979 | " 29248 | \n",
980 | " 452.15 | \n",
981 | " 2019-08-30 15:29:00+05:30 | \n",
982 | " 453.00 | \n",
983 | " 452.00 | \n",
984 | " 452.45 | \n",
985 | " 34857 | \n",
986 | "
\n",
987 | " \n",
988 | "
\n",
989 | "
"
990 | ],
991 | "text/plain": [
992 | " close date high low open volume\n",
993 | "29244 452.45 2019-08-30 15:25:00+05:30 452.85 452.35 452.80 36323\n",
994 | "29245 452.05 2019-08-30 15:26:00+05:30 452.55 452.05 452.45 16843\n",
995 | "29246 452.20 2019-08-30 15:27:00+05:30 452.25 452.00 452.05 30296\n",
996 | "29247 452.95 2019-08-30 15:28:00+05:30 452.95 452.05 452.20 49371\n",
997 | "29248 452.15 2019-08-30 15:29:00+05:30 453.00 452.00 452.45 34857"
998 | ]
999 | },
1000 | "metadata": {
1001 | "tags": []
1002 | }
1003 | }
1004 | ]
1005 | },
1006 | {
1007 | "cell_type": "markdown",
1008 | "metadata": {
1009 | "id": "GSjcvC7aL7Nf",
1010 | "colab_type": "text"
1011 | },
1012 | "source": [
1013 | "# for last trade price of stock"
1014 | ]
1015 | },
1016 | {
1017 | "cell_type": "code",
1018 | "metadata": {
1019 | "id": "Iu86itHNL7Ni",
1020 | "colab_type": "code",
1021 | "colab": {},
1022 | "outputId": "239d51c3-2644-49a9-9460-30cbe0580830"
1023 | },
1024 | "source": [
1025 | "# GAIL\n",
1026 | "kite.ltp(1207553)"
1027 | ],
1028 | "execution_count": 0,
1029 | "outputs": [
1030 | {
1031 | "output_type": "execute_result",
1032 | "data": {
1033 | "text/plain": [
1034 | "{'1207553': {'instrument_token': 1207553, 'last_price': 129.8}}"
1035 | ]
1036 | },
1037 | "metadata": {
1038 | "tags": []
1039 | },
1040 | "execution_count": 46
1041 | }
1042 | ]
1043 | },
1044 | {
1045 | "cell_type": "markdown",
1046 | "metadata": {
1047 | "id": "LrZ8NJ18L7No",
1048 | "colab_type": "text"
1049 | },
1050 | "source": [
1051 | "# for last ohlc of stock"
1052 | ]
1053 | },
1054 | {
1055 | "cell_type": "code",
1056 | "metadata": {
1057 | "id": "ZsVWbdSZL7Nq",
1058 | "colab_type": "code",
1059 | "colab": {},
1060 | "outputId": "652b0e2c-db2a-47b7-e42e-ede1ccec1c7f"
1061 | },
1062 | "source": [
1063 | "# GAIL\n",
1064 | "kite.ohlc(1207553)"
1065 | ],
1066 | "execution_count": 0,
1067 | "outputs": [
1068 | {
1069 | "output_type": "execute_result",
1070 | "data": {
1071 | "text/plain": [
1072 | "{'1207553': {'instrument_token': 1207553,\n",
1073 | " 'last_price': 129.8,\n",
1074 | " 'ohlc': {'open': 129.65, 'high': 133.1, 'low': 125.2, 'close': 128.5}}}"
1075 | ]
1076 | },
1077 | "metadata": {
1078 | "tags": []
1079 | },
1080 | "execution_count": 31
1081 | }
1082 | ]
1083 | },
1084 | {
1085 | "cell_type": "markdown",
1086 | "metadata": {
1087 | "id": "2Pw6DhmaL7Nu",
1088 | "colab_type": "text"
1089 | },
1090 | "source": [
1091 | "# FOR PLACING AN ORDER :-"
1092 | ]
1093 | },
1094 | {
1095 | "cell_type": "markdown",
1096 | "metadata": {
1097 | "id": "814BeUu5L7Nw",
1098 | "colab_type": "text"
1099 | },
1100 | "source": [
1101 | "* market order place"
1102 | ]
1103 | },
1104 | {
1105 | "cell_type": "code",
1106 | "metadata": {
1107 | "id": "78WtFpA1L7Nx",
1108 | "colab_type": "code",
1109 | "colab": {},
1110 | "outputId": "ecc33c8a-e35c-4e3a-f8a0-1048f73375ab"
1111 | },
1112 | "source": [
1113 | "kite.place_order(variety=\"regular\",tradingsymbol='IDEA',quantity=1,exchange='NSE',order_type='MARKET',transaction_type='SELL',product='MIS',tag='test')"
1114 | ],
1115 | "execution_count": 0,
1116 | "outputs": [
1117 | {
1118 | "output_type": "execute_result",
1119 | "data": {
1120 | "text/plain": [
1121 | "'190901000000678'"
1122 | ]
1123 | },
1124 | "metadata": {
1125 | "tags": []
1126 | },
1127 | "execution_count": 32
1128 | }
1129 | ]
1130 | },
1131 | {
1132 | "cell_type": "markdown",
1133 | "metadata": {
1134 | "id": "g3wDh1s3L7N2",
1135 | "colab_type": "text"
1136 | },
1137 | "source": [
1138 | "* BUY limit order place"
1139 | ]
1140 | },
1141 | {
1142 | "cell_type": "code",
1143 | "metadata": {
1144 | "id": "JY9r4wBBL7N4",
1145 | "colab_type": "code",
1146 | "colab": {},
1147 | "outputId": "9e58b2dd-4733-41b0-9290-311b80c7080b"
1148 | },
1149 | "source": [
1150 | "kite.place_order( variety=\"regular\",tradingsymbol=\"ADANIPOWER\",quantity=1,exchange=\"BSE\",order_type='LIMIT',price=54,trigger_price=55,transaction_type='SELL',product='CNC',tag=\"te\")"
1151 | ],
1152 | "execution_count": 0,
1153 | "outputs": [
1154 | {
1155 | "output_type": "execute_result",
1156 | "data": {
1157 | "text/plain": [
1158 | "'190901000000681'"
1159 | ]
1160 | },
1161 | "metadata": {
1162 | "tags": []
1163 | },
1164 | "execution_count": 35
1165 | }
1166 | ]
1167 | },
1168 | {
1169 | "cell_type": "markdown",
1170 | "metadata": {
1171 | "id": "CAF4z5X0L7N9",
1172 | "colab_type": "text"
1173 | },
1174 | "source": [
1175 | "* SELL limit order place"
1176 | ]
1177 | },
1178 | {
1179 | "cell_type": "code",
1180 | "metadata": {
1181 | "id": "xpki0GzjL7N_",
1182 | "colab_type": "code",
1183 | "colab": {},
1184 | "outputId": "75af1d7e-b018-4088-a3da-e4a82ff2de88"
1185 | },
1186 | "source": [
1187 | "kite.place_order( variety=\"regular\",tradingsymbol=\"ADANIPOWER\",quantity=1,exchange=\"BSE\",order_type='LIMIT',price=54 ,trigger_price=52,transaction_type='SELL',product='CNC',tag=\"test\")"
1188 | ],
1189 | "execution_count": 0,
1190 | "outputs": [
1191 | {
1192 | "output_type": "execute_result",
1193 | "data": {
1194 | "text/plain": [
1195 | "'190901000000682'"
1196 | ]
1197 | },
1198 | "metadata": {
1199 | "tags": []
1200 | },
1201 | "execution_count": 37
1202 | }
1203 | ]
1204 | },
1205 | {
1206 | "cell_type": "markdown",
1207 | "metadata": {
1208 | "id": "zrSUAWOLL7OE",
1209 | "colab_type": "text"
1210 | },
1211 | "source": [
1212 | "# Cancel an order "
1213 | ]
1214 | },
1215 | {
1216 | "cell_type": "code",
1217 | "metadata": {
1218 | "id": "s8D5SrIwL7OG",
1219 | "colab_type": "code",
1220 | "colab": {},
1221 | "outputId": "25002b6f-33db-4a19-acda-561b989e35cb"
1222 | },
1223 | "source": [
1224 | "kite.cancel_order(\"regular\",order_id=\"190901000000682\", parent_order_id=None)"
1225 | ],
1226 | "execution_count": 0,
1227 | "outputs": [
1228 | {
1229 | "output_type": "execute_result",
1230 | "data": {
1231 | "text/plain": [
1232 | "'190901000000682'"
1233 | ]
1234 | },
1235 | "metadata": {
1236 | "tags": []
1237 | },
1238 | "execution_count": 40
1239 | }
1240 | ]
1241 | },
1242 | {
1243 | "cell_type": "markdown",
1244 | "metadata": {
1245 | "id": "jHXZewPOL7OK",
1246 | "colab_type": "text"
1247 | },
1248 | "source": [
1249 | "# Exit from an order"
1250 | ]
1251 | },
1252 | {
1253 | "cell_type": "code",
1254 | "metadata": {
1255 | "id": "q1wtTkp1L7OL",
1256 | "colab_type": "code",
1257 | "colab": {}
1258 | },
1259 | "source": [
1260 | "kite.exit_order(variety=\"regular\",order_id='190901000000682')"
1261 | ],
1262 | "execution_count": 0,
1263 | "outputs": []
1264 | },
1265 | {
1266 | "cell_type": "markdown",
1267 | "metadata": {
1268 | "id": "jr5t0McRL7OR",
1269 | "colab_type": "text"
1270 | },
1271 | "source": [
1272 | "# Order Placed information"
1273 | ]
1274 | },
1275 | {
1276 | "cell_type": "code",
1277 | "metadata": {
1278 | "id": "EJxLX0ggL7OT",
1279 | "colab_type": "code",
1280 | "colab": {},
1281 | "outputId": "8f2077fd-1ed0-4891-c7c4-9c7acc24105b"
1282 | },
1283 | "source": [
1284 | "display(pd.DataFrame(kite.orders())[[\"tradingsymbol\",\"instrument_token\",\"status\",\"order_id\",\"quantity\",\"price\",\"trigger_price\",\"order_type\",\"exchange_update_timestamp\",\"exchange\",'tag',\"validity\", \"average_price\",\"pending_quantity\"]])"
1285 | ],
1286 | "execution_count": 0,
1287 | "outputs": [
1288 | {
1289 | "output_type": "display_data",
1290 | "data": {
1291 | "text/html": [
1292 | "\n",
1293 | "\n",
1306 | "
\n",
1307 | " \n",
1308 | " \n",
1309 | " | \n",
1310 | " tradingsymbol | \n",
1311 | " instrument_token | \n",
1312 | " status | \n",
1313 | " order_id | \n",
1314 | " quantity | \n",
1315 | " price | \n",
1316 | " trigger_price | \n",
1317 | " order_type | \n",
1318 | " exchange_update_timestamp | \n",
1319 | " exchange | \n",
1320 | " tag | \n",
1321 | " validity | \n",
1322 | " average_price | \n",
1323 | " pending_quantity | \n",
1324 | "
\n",
1325 | " \n",
1326 | " \n",
1327 | " \n",
1328 | " 0 | \n",
1329 | " GAIL | \n",
1330 | " 1207553 | \n",
1331 | " REJECTED | \n",
1332 | " 190901000000537 | \n",
1333 | " 765 | \n",
1334 | " 131.00 | \n",
1335 | " 130.90 | \n",
1336 | " SL | \n",
1337 | " None | \n",
1338 | " NSE | \n",
1339 | " advG | \n",
1340 | " DAY | \n",
1341 | " 0 | \n",
1342 | " 0 | \n",
1343 | "
\n",
1344 | " \n",
1345 | " 1 | \n",
1346 | " NTPC | \n",
1347 | " 2977281 | \n",
1348 | " REJECTED | \n",
1349 | " 190901000000538 | \n",
1350 | " 819 | \n",
1351 | " 122.35 | \n",
1352 | " 122.25 | \n",
1353 | " SL | \n",
1354 | " None | \n",
1355 | " NSE | \n",
1356 | " advG | \n",
1357 | " DAY | \n",
1358 | " 0 | \n",
1359 | " 0 | \n",
1360 | "
\n",
1361 | " \n",
1362 | " 2 | \n",
1363 | " VEDL | \n",
1364 | " 784129 | \n",
1365 | " REJECTED | \n",
1366 | " 190901000000539 | \n",
1367 | " 711 | \n",
1368 | " 140.90 | \n",
1369 | " 140.80 | \n",
1370 | " SL | \n",
1371 | " None | \n",
1372 | " NSE | \n",
1373 | " advG | \n",
1374 | " DAY | \n",
1375 | " 0 | \n",
1376 | " 0 | \n",
1377 | "
\n",
1378 | " \n",
1379 | " 3 | \n",
1380 | " HDFC | \n",
1381 | " 340481 | \n",
1382 | " REJECTED | \n",
1383 | " 190901000000549 | \n",
1384 | " 13 | \n",
1385 | " 2171.95 | \n",
1386 | " 2170.70 | \n",
1387 | " SL | \n",
1388 | " None | \n",
1389 | " NSE | \n",
1390 | " advG | \n",
1391 | " DAY | \n",
1392 | " 0 | \n",
1393 | " 0 | \n",
1394 | "
\n",
1395 | " \n",
1396 | " 4 | \n",
1397 | " IDEA | \n",
1398 | " 3677697 | \n",
1399 | " REJECTED | \n",
1400 | " 190901000000678 | \n",
1401 | " 1 | \n",
1402 | " 0.00 | \n",
1403 | " 0.00 | \n",
1404 | " MARKET | \n",
1405 | " None | \n",
1406 | " NSE | \n",
1407 | " test | \n",
1408 | " DAY | \n",
1409 | " 0 | \n",
1410 | " 0 | \n",
1411 | "
\n",
1412 | " \n",
1413 | " 5 | \n",
1414 | " ADANIPOWER | \n",
1415 | " 136472580 | \n",
1416 | " REJECTED | \n",
1417 | " 190901000000681 | \n",
1418 | " 1 | \n",
1419 | " 54.00 | \n",
1420 | " 0.00 | \n",
1421 | " LIMIT | \n",
1422 | " None | \n",
1423 | " BSE | \n",
1424 | " te | \n",
1425 | " DAY | \n",
1426 | " 0 | \n",
1427 | " 0 | \n",
1428 | "
\n",
1429 | " \n",
1430 | " 6 | \n",
1431 | " ADANIPOWER | \n",
1432 | " 136472580 | \n",
1433 | " REJECTED | \n",
1434 | " 190901000000682 | \n",
1435 | " 1 | \n",
1436 | " 54.00 | \n",
1437 | " 0.00 | \n",
1438 | " LIMIT | \n",
1439 | " None | \n",
1440 | " BSE | \n",
1441 | " test | \n",
1442 | " DAY | \n",
1443 | " 0 | \n",
1444 | " 0 | \n",
1445 | "
\n",
1446 | " \n",
1447 | "
\n",
1448 | "
"
1449 | ],
1450 | "text/plain": [
1451 | " tradingsymbol instrument_token status order_id quantity \\\n",
1452 | "0 GAIL 1207553 REJECTED 190901000000537 765 \n",
1453 | "1 NTPC 2977281 REJECTED 190901000000538 819 \n",
1454 | "2 VEDL 784129 REJECTED 190901000000539 711 \n",
1455 | "3 HDFC 340481 REJECTED 190901000000549 13 \n",
1456 | "4 IDEA 3677697 REJECTED 190901000000678 1 \n",
1457 | "5 ADANIPOWER 136472580 REJECTED 190901000000681 1 \n",
1458 | "6 ADANIPOWER 136472580 REJECTED 190901000000682 1 \n",
1459 | "\n",
1460 | " price trigger_price order_type exchange_update_timestamp exchange tag \\\n",
1461 | "0 131.00 130.90 SL None NSE advG \n",
1462 | "1 122.35 122.25 SL None NSE advG \n",
1463 | "2 140.90 140.80 SL None NSE advG \n",
1464 | "3 2171.95 2170.70 SL None NSE advG \n",
1465 | "4 0.00 0.00 MARKET None NSE test \n",
1466 | "5 54.00 0.00 LIMIT None BSE te \n",
1467 | "6 54.00 0.00 LIMIT None BSE test \n",
1468 | "\n",
1469 | " validity average_price pending_quantity \n",
1470 | "0 DAY 0 0 \n",
1471 | "1 DAY 0 0 \n",
1472 | "2 DAY 0 0 \n",
1473 | "3 DAY 0 0 \n",
1474 | "4 DAY 0 0 \n",
1475 | "5 DAY 0 0 \n",
1476 | "6 DAY 0 0 "
1477 | ]
1478 | },
1479 | "metadata": {
1480 | "tags": []
1481 | }
1482 | }
1483 | ]
1484 | },
1485 | {
1486 | "cell_type": "markdown",
1487 | "metadata": {
1488 | "id": "DtpoIoTQL7OY",
1489 | "colab_type": "text"
1490 | },
1491 | "source": [
1492 | "# Open positions "
1493 | ]
1494 | },
1495 | {
1496 | "cell_type": "code",
1497 | "metadata": {
1498 | "id": "4EiG5wOFL7Oa",
1499 | "colab_type": "code",
1500 | "colab": {},
1501 | "outputId": "54404645-158a-437f-8177-09ec9ea616be"
1502 | },
1503 | "source": [
1504 | "pd.DataFrame(kite.positions())"
1505 | ],
1506 | "execution_count": 0,
1507 | "outputs": [
1508 | {
1509 | "output_type": "execute_result",
1510 | "data": {
1511 | "text/html": [
1512 | "\n",
1513 | "\n",
1526 | "
\n",
1527 | " \n",
1528 | " \n",
1529 | " | \n",
1530 | " net | \n",
1531 | " day | \n",
1532 | "
\n",
1533 | " \n",
1534 | " \n",
1535 | " \n",
1536 | "
\n",
1537 | "
"
1538 | ],
1539 | "text/plain": [
1540 | "Empty DataFrame\n",
1541 | "Columns: [net, day]\n",
1542 | "Index: []"
1543 | ]
1544 | },
1545 | "metadata": {
1546 | "tags": []
1547 | },
1548 | "execution_count": 44
1549 | }
1550 | ]
1551 | },
1552 | {
1553 | "cell_type": "markdown",
1554 | "metadata": {
1555 | "id": "VlRSy_tUL7Oe",
1556 | "colab_type": "text"
1557 | },
1558 | "source": [
1559 | "# Holdings you have "
1560 | ]
1561 | },
1562 | {
1563 | "cell_type": "code",
1564 | "metadata": {
1565 | "id": "50-CsWRZL7Og",
1566 | "colab_type": "code",
1567 | "colab": {},
1568 | "outputId": "16481f88-38a1-40f1-b700-879ebac4f9db"
1569 | },
1570 | "source": [
1571 | "display(pd.DataFrame(kite.holdings())[[\"tradingsymbol\",\"instrument_token\",\"pnl\",\"average_price\",\"close_price\",\"collateral_quantity\",\"collateral_type\",\"day_change\",\"day_change_percentage\",\"exchange\",\"isin\",\"last_price\",\"price\",\"product\",\"quantity\",\"realised_quantity\",\"t1_quantity\" ]]) "
1572 | ],
1573 | "execution_count": 0,
1574 | "outputs": [
1575 | {
1576 | "output_type": "display_data",
1577 | "data": {
1578 | "text/html": [
1579 | "\n",
1580 | "\n",
1593 | "
\n",
1594 | " \n",
1595 | " \n",
1596 | " | \n",
1597 | " tradingsymbol | \n",
1598 | " instrument_token | \n",
1599 | " pnl | \n",
1600 | " average_price | \n",
1601 | " close_price | \n",
1602 | " collateral_quantity | \n",
1603 | " collateral_type | \n",
1604 | " day_change | \n",
1605 | " day_change_percentage | \n",
1606 | " exchange | \n",
1607 | " isin | \n",
1608 | " last_price | \n",
1609 | " price | \n",
1610 | " product | \n",
1611 | " quantity | \n",
1612 | " realised_quantity | \n",
1613 | " t1_quantity | \n",
1614 | "
\n",
1615 | " \n",
1616 | " \n",
1617 | " \n",
1618 | " 0 | \n",
1619 | " ADANIPOWER | \n",
1620 | " 4451329 | \n",
1621 | " -2.95 | \n",
1622 | " 61.350 | \n",
1623 | " 58.40 | \n",
1624 | " 0 | \n",
1625 | " | \n",
1626 | " 0.00 | \n",
1627 | " 0.000000 | \n",
1628 | " NSE | \n",
1629 | " INE814H01011 | \n",
1630 | " 58.40 | \n",
1631 | " 0 | \n",
1632 | " CNC | \n",
1633 | " 1 | \n",
1634 | " 1 | \n",
1635 | " 0 | \n",
1636 | "
\n",
1637 | " \n",
1638 | " 1 | \n",
1639 | " CRESSAN | \n",
1640 | " 131169028 | \n",
1641 | " -0.05 | \n",
1642 | " 0.200 | \n",
1643 | " 0.19 | \n",
1644 | " 0 | \n",
1645 | " | \n",
1646 | " 0.00 | \n",
1647 | " 0.000000 | \n",
1648 | " BSE | \n",
1649 | " INE716D01033 | \n",
1650 | " 0.19 | \n",
1651 | " 0 | \n",
1652 | " CNC | \n",
1653 | " 5 | \n",
1654 | " 5 | \n",
1655 | " 0 | \n",
1656 | "
\n",
1657 | " \n",
1658 | " 2 | \n",
1659 | " IDEA | \n",
1660 | " 3677697 | \n",
1661 | " -26.72 | \n",
1662 | " 10.694 | \n",
1663 | " 5.37 | \n",
1664 | " 0 | \n",
1665 | " | \n",
1666 | " -0.02 | \n",
1667 | " -0.372439 | \n",
1668 | " NSE | \n",
1669 | " INE669E01016 | \n",
1670 | " 5.35 | \n",
1671 | " 0 | \n",
1672 | " CNC | \n",
1673 | " 5 | \n",
1674 | " 5 | \n",
1675 | " 0 | \n",
1676 | "
\n",
1677 | " \n",
1678 | " 3 | \n",
1679 | " VIKASPROP | \n",
1680 | " 3109889 | \n",
1681 | " -0.25 | \n",
1682 | " 4.300 | \n",
1683 | " 4.05 | \n",
1684 | " 0 | \n",
1685 | " | \n",
1686 | " 0.00 | \n",
1687 | " 0.000000 | \n",
1688 | " NSE | \n",
1689 | " INE767B01022 | \n",
1690 | " 4.05 | \n",
1691 | " 0 | \n",
1692 | " CNC | \n",
1693 | " 1 | \n",
1694 | " 1 | \n",
1695 | " 0 | \n",
1696 | "
\n",
1697 | " \n",
1698 | "
\n",
1699 | "
"
1700 | ],
1701 | "text/plain": [
1702 | " tradingsymbol instrument_token pnl average_price close_price \\\n",
1703 | "0 ADANIPOWER 4451329 -2.95 61.350 58.40 \n",
1704 | "1 CRESSAN 131169028 -0.05 0.200 0.19 \n",
1705 | "2 IDEA 3677697 -26.72 10.694 5.37 \n",
1706 | "3 VIKASPROP 3109889 -0.25 4.300 4.05 \n",
1707 | "\n",
1708 | " collateral_quantity collateral_type day_change day_change_percentage \\\n",
1709 | "0 0 0.00 0.000000 \n",
1710 | "1 0 0.00 0.000000 \n",
1711 | "2 0 -0.02 -0.372439 \n",
1712 | "3 0 0.00 0.000000 \n",
1713 | "\n",
1714 | " exchange isin last_price price product quantity \\\n",
1715 | "0 NSE INE814H01011 58.40 0 CNC 1 \n",
1716 | "1 BSE INE716D01033 0.19 0 CNC 5 \n",
1717 | "2 NSE INE669E01016 5.35 0 CNC 5 \n",
1718 | "3 NSE INE767B01022 4.05 0 CNC 1 \n",
1719 | "\n",
1720 | " realised_quantity t1_quantity \n",
1721 | "0 1 0 \n",
1722 | "1 5 0 \n",
1723 | "2 5 0 \n",
1724 | "3 1 0 "
1725 | ]
1726 | },
1727 | "metadata": {
1728 | "tags": []
1729 | }
1730 | }
1731 | ]
1732 | },
1733 | {
1734 | "cell_type": "markdown",
1735 | "metadata": {
1736 | "id": "DpKZz6BBL7Ok",
1737 | "colab_type": "text"
1738 | },
1739 | "source": [
1740 | "# Websocket"
1741 | ]
1742 | },
1743 | {
1744 | "cell_type": "code",
1745 | "metadata": {
1746 | "id": "tyTOWvvhL7Om",
1747 | "colab_type": "code",
1748 | "colab": {}
1749 | },
1750 | "source": [
1751 | "import logging\n",
1752 | "from kiteconnect import KiteTicker\n",
1753 | "logging.basicConfig(level=logging.DEBUG)\n",
1754 | "kws = KiteTicker(\"inx2yhl01h5udgec\", \"R46Cl90i4SQakKZqFvC3g130gQAzj31J\")\n",
1755 | "def on_ticks(ws, ticks):\n",
1756 | " logging.debug(\"Ticks: {}\".format(ticks))\n",
1757 | "def on_connect(ws, response):\n",
1758 | " ws.subscribe([256265])\n",
1759 | " ws.set_mode(ws.MODE_FULL, [256265])\n",
1760 | "def on_close(ws, code, reason):\n",
1761 | " ws.stop()\n",
1762 | "kws.on_ticks = on_ticks\n",
1763 | "kws.on_connect = on_connect\n",
1764 | "kws.on_close = on_close\n",
1765 | "kws.connect()"
1766 | ],
1767 | "execution_count": 0,
1768 | "outputs": []
1769 | },
1770 | {
1771 | "cell_type": "markdown",
1772 | "metadata": {
1773 | "id": "QAXvT4H7L7Ou",
1774 | "colab_type": "text"
1775 | },
1776 | "source": [
1777 | "# Websocket threading"
1778 | ]
1779 | },
1780 | {
1781 | "cell_type": "code",
1782 | "metadata": {
1783 | "id": "vYRCVdgvL7Ov",
1784 | "colab_type": "code",
1785 | "colab": {}
1786 | },
1787 | "source": [
1788 | "import threading \n",
1789 | "class ThreadingData(object):\n",
1790 | " # Making Variables as global of websocket\n",
1791 | " global app_z_status\n",
1792 | " global app_z_order\n",
1793 | " global app_z_order_type\n",
1794 | " global app_z_transaction_type\n",
1795 | " # Variables of websocket\n",
1796 | " app_z_status=[]\n",
1797 | " app_z_order=[]\n",
1798 | " app_z_order_type=[]\n",
1799 | " app_z_transaction_type=[]\n",
1800 | " def __init__(self):\n",
1801 | " thread = threading.Thread(target=self.run, args=())\n",
1802 | " thread.daemon = True\n",
1803 | " thread.start()\n",
1804 | " def run(self):\n",
1805 | " \n",
1806 | " #----------------------------------------------------------------------------***** PUBLIC TOKEN\n",
1807 | " \n",
1808 | " access_token=\"tCplfRM4E5VZkfDc81byFjFGWy29R7uN\" \n",
1809 | " api_key=\"inx2yhl01h5udgec\"\n",
1810 | " logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')\n",
1811 | " clear_output(wait=True)\n",
1812 | " kws = KiteTicker(api_key, access_token, debug=True)\n",
1813 | " \n",
1814 | " # Making Variables as global of websocket\n",
1815 | " \n",
1816 | " global app_z_status\n",
1817 | " global app_z_order\n",
1818 | " global app_z_order_type\n",
1819 | " global app_z_transaction_type\n",
1820 | "\n",
1821 | " # INPUT TOKEN --------------------------------------------------------------***** INPUT TOKEN\n",
1822 | " \n",
1823 | " tokens = [131169028]\n",
1824 | " \n",
1825 | " print(\"Tokens length\", len(tokens)) \n",
1826 | " \n",
1827 | " # Websocket Working\n",
1828 | " def on_connect(ws, response):\n",
1829 | " logging.debug(\"on connect: {}\".format(response))\n",
1830 | " ws.subscribe(tokens)\n",
1831 | " ws.set_mode(ws.MODE_FULL, tokens)\n",
1832 | " def on_close(ws, code, reason):\n",
1833 | " logging.error(\"closed connection on close: {} {}\".format(code, reason))\n",
1834 | " def on_error(ws, code, reason):\n",
1835 | " logging.error(\"closed connection on error: {} {}\".format(code, reason))\n",
1836 | " def on_noreconnect(ws):\n",
1837 | " logging.error(\"Reconnecting the websocket failed\")\n",
1838 | " def on_reconnect(ws, attempt_count):\n",
1839 | " logging.debug(\"Reconnecting the websocket: {}\".format(attempt_count))\n",
1840 | " def on_order_update(ws, data):\n",
1841 | " \n",
1842 | " # Accessing Data from websocket \n",
1843 | " \n",
1844 | " z_status=data['status']\n",
1845 | " z_order=data[\"order_id\"]\n",
1846 | " z_order_type=data[\"order_type\"]\n",
1847 | " z_transaction_type=[\"transaction_type\"]\n",
1848 | " \n",
1849 | " # APPEND websocket data into variables\n",
1850 | " \n",
1851 | " app_z_status.append(z_status)\n",
1852 | " app_z_order.append(z_order)\n",
1853 | " app_z_order_type.append(z_order_type)\n",
1854 | " app_z_transaction_type.append(z_transaction_type)\n",
1855 | " \n",
1856 | " # printing All websocket data \n",
1857 | " \n",
1858 | " print(app_z_status)\n",
1859 | " print(app_z_order)\n",
1860 | " print(app_z_order_type)\n",
1861 | " print(app_z_transaction_type)\n",
1862 | " print(data)\n",
1863 | " \n",
1864 | " # calling all functions\n",
1865 | " \n",
1866 | " kws.on_connect = on_connect\n",
1867 | " kws.on_close = on_close\n",
1868 | " kws.on_error = on_error\n",
1869 | " kws.on_noreconnect = on_noreconnect\n",
1870 | " kws.on_reconnect = on_reconnect\n",
1871 | " kws.on_order_update = on_order_update\n",
1872 | " kws.connect()\n",
1873 | "\n",
1874 | "thre=ThreadingData()"
1875 | ],
1876 | "execution_count": 0,
1877 | "outputs": []
1878 | },
1879 | {
1880 | "cell_type": "markdown",
1881 | "metadata": {
1882 | "id": "-4X3r23vL7O0",
1883 | "colab_type": "text"
1884 | },
1885 | "source": [
1886 | "# STATUS of order from websocket :-"
1887 | ]
1888 | },
1889 | {
1890 | "cell_type": "markdown",
1891 | "metadata": {
1892 | "id": "s_RJNIPBL7O1",
1893 | "colab_type": "text"
1894 | },
1895 | "source": [
1896 | " {'average_price': 0,\n",
1897 | " 'cancelled_quantity': 0,\n",
1898 | " 'disclosed_quantity': 0,\n",
1899 | " 'exchange': 'NSE',\n",
1900 | " 'exchange_order_id': None,\n",
1901 | " 'exchange_timestamp': None,\n",
1902 | " 'filled_quantity': 0,\n",
1903 | " 'guid': '10778X8ZFO5b41UslC',\n",
1904 | " 'instrument_token': 857857,\n",
1905 | " 'market_protection': 0,\n",
1906 | " 'order_id': '190521001155150',\n",
1907 | " 'order_timestamp': datetime.datetime(2019, 5, 21, 10, 54, 46),\n",
1908 | " 'order_type': 'SL',\n",
1909 | " 'parent_order_id': None,\n",
1910 | " 'pending_quantity': 0,\n",
1911 | " 'placed_by': 'BQ8330',\n",
1912 | " 'price': 555,\n",
1913 | " 'product': 'CNC',\n",
1914 | " 'quantity': 1,\n",
1915 | " 'status': 'REJECTED',\n",
1916 | " 'status_message': 'Price exceeds circuit limits for the instrument. Place an order within the daily range.',\n",
1917 | " 'status_message_raw': 'RMS:R"
1918 | ]
1919 | }
1920 | ]
1921 | }
--------------------------------------------------------------------------------
/BACKTESTIG_PROGRAM_.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "kernelspec": {
6 | "display_name": "Python 3",
7 | "language": "python",
8 | "name": "python3"
9 | },
10 | "language_info": {
11 | "codemirror_mode": {
12 | "name": "ipython",
13 | "version": 3
14 | },
15 | "file_extension": ".py",
16 | "mimetype": "text/x-python",
17 | "name": "python",
18 | "nbconvert_exporter": "python",
19 | "pygments_lexer": "ipython3",
20 | "version": "3.7.3"
21 | },
22 | "colab": {
23 | "name": "BACKTESTIG_PROGRAM_.ipynb",
24 | "provenance": [],
25 | "collapsed_sections": [],
26 | "include_colab_link": true
27 | }
28 | },
29 | "cells": [
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {
33 | "id": "view-in-github",
34 | "colab_type": "text"
35 | },
36 | "source": [
37 | "
"
38 | ]
39 | },
40 | {
41 | "cell_type": "markdown",
42 | "metadata": {
43 | "id": "PnePljR6DpqC",
44 | "colab_type": "text"
45 | },
46 | "source": [
47 | "### **BACKTESTIG PROGRAM** "
48 | ]
49 | },
50 | {
51 | "cell_type": "markdown",
52 | "metadata": {
53 | "id": "o5RoyVNDDsIZ",
54 | "colab_type": "text"
55 | },
56 | "source": [
57 | "* This is a backtesting program for Alo trading."
58 | ]
59 | },
60 | {
61 | "cell_type": "markdown",
62 | "metadata": {
63 | "id": "HGQ7b2IJE0_P",
64 | "colab_type": "text"
65 | },
66 | "source": [
67 | "**1. What is Backtesting?**"
68 | ]
69 | },
70 | {
71 | "cell_type": "markdown",
72 | "metadata": {
73 | "id": "btX0tg9-E8Z7",
74 | "colab_type": "text"
75 | },
76 | "source": [
77 | "* Zerodha backtesting is a trading strategy that is based on historical data, where traders use past data to see how a strategy would have performed. The definition of a backtesting application is a set of technical rules applied to a set of historical price data, and the subsequent analysis of the returns that a Zerodha strategy would have generated over a specific period of time."
78 | ]
79 | },
80 | {
81 | "cell_type": "markdown",
82 | "metadata": {
83 | "id": "7EOsWPLwFFpV",
84 | "colab_type": "text"
85 | },
86 | "source": [
87 | "\n",
88 | "* Backtesting is a type of program that allows traders to test potential trading strategies using historical data.The program recreates the behaviour of trades and their reaction to a Zerodha trading strategy and the resulting data can then be used to measure and optimise the effectiveness of a given strategy before applying it to real market conditions. Backtesting strategies work on the assumption that trades that have performed successfully in the past will perform well in the future."
89 | ]
90 | },
91 | {
92 | "cell_type": "markdown",
93 | "metadata": {
94 | "id": "fQVEGF3vFFww",
95 | "colab_type": "text"
96 | },
97 | "source": [
98 | "**2. How Does a Backtester Work?**"
99 | ]
100 | },
101 | {
102 | "cell_type": "markdown",
103 | "metadata": {
104 | "id": "grne1IlnFfa8",
105 | "colab_type": "text"
106 | },
107 | "source": [
108 | "* Zerodha trading strategies are applied to a set of price data, and trades are reconstructed using that data. This data can be used by traders to ascertain any unforeseen flaws in their current strategies. Alternatively, new strategies can also be tested before using them in the live markets.Depending on the type of back testing software used in Zerodha trading, traders can get a wide range of indicators, such as:\n",
109 | "* Total Return on Equity (ROE): Returns, expressed in terms of percentage of the total equity invested.\n",
110 | "* Total Profit and Loss (P/L): Total profits and losses generated by a strategy, expressed as a percentage of the invested equity.\n",
111 | "* Total Gain/Loss Ratio: The ratio of how many trades resulted in gains, and how many in losses.\n",
112 | "* Annualised ROE: The total return likely to be generated by a Zerodha strategy over the entire calendar year.\n",
113 | "* Volatility: What kind of market conditions were your strategies working in, uptrends, and downtrends.\n",
114 | "* Risk-Adjusted Returns: Calculating your returns in relation to the risks involved within a strategy.\n",
115 | "* All these metrics provide you with insights about how your Zerodha trading strategies are performing."
116 | ]
117 | },
118 | {
119 | "cell_type": "markdown",
120 | "metadata": {
121 | "id": "P6ewBJ1lF3lS",
122 | "colab_type": "text"
123 | },
124 | "source": [
125 | "**Import All libraries required for BOT**"
126 | ]
127 | },
128 | {
129 | "cell_type": "code",
130 | "metadata": {
131 | "id": "AKrceMbTGK56",
132 | "colab_type": "code",
133 | "colab": {}
134 | },
135 | "source": [
136 | "import time\n",
137 | "import secrets \n",
138 | "import logging\n",
139 | "import threading\n",
140 | "import webbrowser\n",
141 | "import numpy as np\n",
142 | "import pandas as pd\n",
143 | "from pytz import timezone\n",
144 | "from math import modf\n",
145 | "from pprint import pprint\n",
146 | "from datetime import datetime\n",
147 | "from time import gmtime, strftime\n",
148 | "from kiteconnect import KiteTicker \n",
149 | "from kiteconnect import KiteConnect\n",
150 | "from IPython.display import clear_output"
151 | ],
152 | "execution_count": 0,
153 | "outputs": []
154 | },
155 | {
156 | "cell_type": "markdown",
157 | "metadata": {
158 | "id": "S24UDHl_GDam",
159 | "colab_type": "text"
160 | },
161 | "source": [
162 | "**LOGIN TO ZERODHA**"
163 | ]
164 | },
165 | {
166 | "cell_type": "markdown",
167 | "metadata": {
168 | "id": "ffEK2g1PGRWM",
169 | "colab_type": "text"
170 | },
171 | "source": [
172 | "*Enter* api key & api secret"
173 | ]
174 | },
175 | {
176 | "cell_type": "code",
177 | "metadata": {
178 | "id": "spPzooAJDTqB",
179 | "colab_type": "code",
180 | "colab": {}
181 | },
182 | "source": [
183 | "print(\" \\t \\t \\t \\t \\t \\n WELCOME to Backtesting program\")\n",
184 | "\n",
185 | "#\n",
186 | "# API your KEY and API secter \n",
187 | "\n",
188 | "api_key='******************* @@enter your api key here@@@**************' \n",
189 | "api_secret='**********************@@enter your api key here****************'\n",
190 | "\n",
191 | "webbrowser.open_new_tab(KiteConnect(api_key,api_secret).login_url())\n",
192 | "kite = KiteConnect(api_key=api_key)\n",
193 | "data = kite.generate_session(((str(input(\"ENTER full link generated in URL :- \")).split(\"request_token=\")[1]).split(\"&action\"))[0],api_secret)\n",
194 | "kite.set_access_token(data[\"access_token\"])\n",
195 | "print(\" \\n \")\n",
196 | "display(data)\n",
197 | "print(\" \\t \\t \\t \\t \\t \")\n",
198 | "print(\"\\n\" )"
199 | ],
200 | "execution_count": 0,
201 | "outputs": []
202 | },
203 | {
204 | "cell_type": "markdown",
205 | "metadata": {
206 | "id": "wpW0EiEvHCOt",
207 | "colab_type": "text"
208 | },
209 | "source": [
210 | "**Implementation of Coding**"
211 | ]
212 | },
213 | {
214 | "cell_type": "markdown",
215 | "metadata": {
216 | "id": "nuB6kLu0HHIt",
217 | "colab_type": "text"
218 | },
219 | "source": [
220 | "* Backtesting for BUY"
221 | ]
222 | },
223 | {
224 | "cell_type": "code",
225 | "metadata": {
226 | "id": "wxyDqF9JDTqY",
227 | "colab_type": "code",
228 | "outputId": "602b65c7-8d20-404e-dd1c-c705ca6fa565",
229 | "colab": {}
230 | },
231 | "source": [
232 | "\n",
233 | "#####################################\n",
234 | "\n",
235 | "# future\n",
236 | "#z=[945155, 944899, 54817287, 54322951, 54843655,54820615, 54817031, 14359298, 14359042, 14358786, 14358530, 14358274, 14358018, 14357762, 14357506, 14357250, 14356994, 14356738, 14356482, 14355970, 14355714, 14355458, 14355202, 14354946, 14354690, 14354434, 14354178, 14353922, 14353154, 14352898, 14352642, 14352386, 14352130, 14351874, 14351362, 14351106, 14350850, 14350594, 14350338, 14350082, 14349826, 14332930, 14332674, 14330882, 13992962, 13992706, 14277890, 14275330, 14265346, 14259458, 14243586, 14226946, 14226690, 14226178, 14225922, 14225666, 14213378, 14209282, 14209026, 14207234, 14206466, 14204418, 14203906, 14203394, 14200578, 14200322, 14200066, 14198274, 14198018, 14197762, 14197506, 14197250, 14182914, 14180354, 14174210, 14151682, 14142210, 14141954, 14141698, 14141442, 14141186, 14140930, 14140674, 14140418, 14140162, 14139906, 14139650, 14139394, 14139138, 14142722, 14138882, 14138626, 14138370, 14138114, 14137858, 14137602, 14137346, 14137090, 14136834, 14136578, 14136066, 14135810, 14135042, 14134530, 14134274, 14134018, 14133762, 14133506, 14133250, 14132482, 14132226, 14131970, 14092802, 14092546, 14080770, 14080514, 14019842, 14018562, 14018306, 14017026, 14016770, 14016514, 14016258, 14014978, 14014722, 14014466, 14014210, 14013954, 14008578, 14008322, 14008066, 14007810, 14007042, 14006786, 14006530, 13992450, 14006274, 14006018, 14005762, 14005506, 14005250, 14004994, 14004738, 14004482, 14004226, 14003970, 13995266, 13995010, 13994754, 13994498, 13994242, 13993986, 13993730, 13993474, 14330370, 14328578, 14321154, 14319874, 14315266, 14315010, 14313986, 14306818, 14298370, 14285058, 14284802, 14283522, 14281730, 14278146, 13993218, 14356226, 14351618]\n",
237 | "#nifty\n",
238 | "#z=[3861249, 60417, 1510401, 4267265, 81153, 4268801, 134657, 2714625, 7458561, 140033, 177665, 5215745, 225537, 232961, 1207553, 315393, 1850625, 341249, 345089, 348929, 356865, 340481, 1270529, 424961, 7712001, 415745, 1346049, 408065, 3001089, 492033, 2939649, 519937, 2815745, 2977281, 633601, 3834113, 738561, 779521, 857857, 2953217, 884737, 895745, 3465729, 897537, 2889473, 2952193, 784129, 969473, 3050241, 975873]\n",
239 | "## fno\n",
240 | "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",
241 | "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",
242 | "\n",
243 | "#################\n",
244 | "\n",
245 | "############\n",
246 | "\n",
247 | "sdate =\"2019-08-20 00:00:00\"\n",
248 | "todate =\"2019-09-16 09:16:00\"\n",
249 | "#sdate =\"2019-08-19\"\n",
250 | "#todate =\"2019-10-01\"\n",
251 | "time_frame =\"minute\"\n",
252 | "quu=50000\n",
253 | "eexchange=\"NSE\"\n",
254 | "productt=\"MIS\"\n",
255 | "qu=int(quu)\n",
256 | "sdate_backtest =\"2019-08-14\"\n",
257 | "todate_backtest =\"2019-10-02\"\n",
258 | "##############\n",
259 | "print(\"SCANNING START\")\n",
260 | "ttoken=5633\n",
261 | "ttradingsymbol=\"ACC\"\n",
262 | "#################\n",
263 | "strike_rate = 0\n",
264 | "strike_rate_list = []\n",
265 | "timeframe = []\n",
266 | "timeframe_list = []\n",
267 | "pnl = 0\n",
268 | "pnl_list = []\n",
269 | "\n",
270 | "###################\n",
271 | "def time_con(time,df):\n",
272 | " z = 0\n",
273 | " chg = []\n",
274 | " chg_index = 0\n",
275 | " timeframe = time\n",
276 | " date = np.array([])\n",
277 | " for i in range(len(df)):\n",
278 | " date = np.hstack((date,str(df.loc[i,\"date\"])))\n",
279 | " open_np = df.open.values\n",
280 | " high_np = df.high.values\n",
281 | " low_np = df.low.values\n",
282 | " close_np = df.close.values\n",
283 | "# volume_np = df.volume.values\n",
284 | "# print(date)\n",
285 | " for j in range(len(date)):\n",
286 | " if date[j][11:16] == \"09:15\":\n",
287 | " chg.append(j)\n",
288 | " np_date=np.array([])\n",
289 | " np_open = np.array([])\n",
290 | " np_high = np.array([])\n",
291 | " np_low = np.array([])\n",
292 | " np_close = np.array([])\n",
293 | "# np_volume = np.array([])\n",
294 | " for i in range(len(date)-timeframe+1):\n",
295 | " if i>=z:\n",
296 | " chg_index+=1\n",
297 | " if (chg_index < len(chg)):\n",
298 | " for k in range(i,chg[chg_index],timeframe):\n",
299 | "# print(date[k])\n",
300 | "# print(k,k+timeframe)\n",
301 | " np_date = np.hstack((np_date,date[k]))\n",
302 | " np_open = np.hstack((np_open,open_np[k]))\n",
303 | " np_high = np.hstack((np_high,np.amax(high_np[k:k+timeframe])))\n",
304 | "# print(high_np[k:k+timeframe])\n",
305 | " np_low = np.hstack((np_low,np.amin(low_np[k:k+timeframe])))\n",
306 | "# print(low_np[k:k+timeframe])\n",
307 | " np_close = np.hstack((np_close,close_np[k+timeframe-1]))\n",
308 | "# np_volume = np.hstack((np_volume,np.sum(volume_np[k:k+timeframe])))\n",
309 | " z = i+timeframe\n",
310 | " z=chg[chg_index]\n",
311 | " else:\n",
312 | " con_df = pd.DataFrame({\"date\":np_date,\"open\":np_open,\"high\":np_high,\"low\":np_low,\"close\":np_close})\n",
313 | " return con_df\n",
314 | "\n",
315 | "########################################################\n",
316 | "for t in range(1,2):\n",
317 | " dfw=kite.historical_data(ttoken,sdate_backtest,todate_backtest,time_frame,0)\n",
318 | " dfw=pd.DataFrame(dfw)\n",
319 | " df=pd.DataFrame(dfw[['date','open','high','low','close']])\n",
320 | " df = time_con(t,df)\n",
321 | " slow_ema = [3,5,7,9,11,13,15,17,19,21,23]\n",
322 | " fast_ema = [25,28,31,34,37,40,43,46,49,52,55,58,61,64,67,70,200]\n",
323 | " def EMA(df, base, target, period, alpha=False):\n",
324 | " con = pd.concat([df[:period][base].rolling(window=period).mean(), df[period:][base]])\n",
325 | " if (alpha == True):\n",
326 | " # (1 - alpha) * previous_val + alpha * current_val where alpha = 1 / period\n",
327 | " df[target] = con.ewm(alpha=1 / period, adjust=False).mean()\n",
328 | " else:\n",
329 | " # ((current_val - previous_val) * coeff) + previous_val where coeff = 2 / (period + 1)\n",
330 | " df[target] = con.ewm(span=period, adjust=False).mean()\n",
331 | " df.fillna(0,inplace = True)\n",
332 | " # return df\n",
333 | " for j in slow_ema:\n",
334 | " val = \"ema\"+\"_\"+str(j)\n",
335 | " EMA(df,\"close\",val,j)\n",
336 | " for k in fast_ema:\n",
337 | " val = \"ema\"+\"_\"+str(k)\n",
338 | " EMA(df,\"close\",val,k)\n",
339 | " def super_guppy(interval,df,anchor=0):\n",
340 | " anchor = 0\n",
341 | " ShowBreak = True\n",
342 | " ShowSwing = True\n",
343 | " ShowCon = False\n",
344 | " uOCCswing = False\n",
345 | " Lookback = 6\n",
346 | " emaFilter = False\n",
347 | " mult = 0\n",
348 | " buybreak = 0\n",
349 | " sellbreak = 0\n",
350 | " buy_barssince_var = 0\n",
351 | " sell_barssince_var = 0\n",
352 | " buybreak_barssince_var = 0\n",
353 | " sellbreak_barssince_var = 0\n",
354 | " barssince_lst = list()\n",
355 | " barssince_var = 0\n",
356 | " bar_count_var = 0\n",
357 | " buy1 = list()\n",
358 | " sell1 = list()\n",
359 | " buy2 = list()\n",
360 | " sell2 = list()\n",
361 | " buybreak1 = list()\n",
362 | " sellbreak1 = list()\n",
363 | " def barssince(b,barssince_var):\n",
364 | " barssince_lst = []\n",
365 | " barssince_var = 0 \n",
366 | " new_var = len(b)\n",
367 | " for i in b[::-1]:\n",
368 | " if i == 1:\n",
369 | " break\n",
370 | " barssince_lst.append(i)\n",
371 | " barssince_var = len(barssince_lst)\n",
372 | " return barssince_var\n",
373 | " barssince_lst.clear()\n",
374 | " if interval < 1441 :\n",
375 | " if (anchor==0 or interval <= 0 or interval >= anchor or anchor > 1441 ):\n",
376 | " mult = 1\n",
377 | " else:\n",
378 | " if round(anchor/interval) > 1:\n",
379 | " mult = round(anchor/interval)\n",
380 | " else:\n",
381 | " mult = 1\n",
382 | " else:\n",
383 | " mult = 1\n",
384 | " #isIntraday Not\n",
385 | " if interval > 1441:\n",
386 | " if (anchor==0 or interval <= 0 or interval >= anchor or anchor < 52 ):\n",
387 | " mult = mult\n",
388 | " else:\n",
389 | " if round(anchor/interval) > 1:\n",
390 | " mult = round(anchor/interval)\n",
391 | " else:\n",
392 | " mult = 1\n",
393 | " else:\n",
394 | " mult = mult\n",
395 | " mult = 1\n",
396 | " for i in range(len(df)):\n",
397 | " emaF1 = df.loc[i,'ema_3']\n",
398 | " emaF2 = df.loc[i,'ema_5']\n",
399 | " emaF3 = df.loc[i,'ema_7']\n",
400 | " emaF4 = df.loc[i,'ema_9']\n",
401 | " emaF5 = df.loc[i,'ema_11']\n",
402 | " emaF6 = df.loc[i,'ema_13']\n",
403 | " emaF7 = df.loc[i,'ema_15']\n",
404 | " emaF8 = df.loc[i,'ema_17']\n",
405 | " emaF9 = df.loc[i,'ema_19']\n",
406 | " emaF10 = df.loc[i,'ema_21']\n",
407 | " emaF11 = df.loc[i,'ema_23']\n",
408 | " emaS1 = df.loc[i,'ema_25']\n",
409 | " emaS2 = df.loc[i,'ema_28']\n",
410 | " emaS3 = df.loc[i,'ema_31']\n",
411 | " emaS4 = df.loc[i,'ema_34']\n",
412 | " emaS5 = df.loc[i,'ema_37']\n",
413 | " emaS6 = df.loc[i,'ema_40']\n",
414 | " emaS7 = df.loc[i,'ema_43']\n",
415 | " emaS8 = df.loc[i,'ema_46']\n",
416 | " emaS9 = df.loc[i,'ema_49']\n",
417 | " emaS10 = df.loc[i,'ema_52']\n",
418 | " emaS11 = df.loc[i,'ema_55']\n",
419 | " emaS12 = df.loc[i,'ema_58']\n",
420 | " emaS13 = df.loc[i,'ema_61']\n",
421 | " emaS14 = df.loc[i,'ema_64']\n",
422 | " emaS15 = df.loc[i,'ema_67']\n",
423 | " emaS16 = df.loc[i,'ema_70']\n",
424 | " ema200 = df.loc[i,'ema_200'] \n",
425 | " emafast = (emaF1 + emaF2 + emaF3 + emaF4 + emaF5 + emaF6 + emaF7 + emaF8 + emaF9 + emaF10 + emaF11)/11\n",
426 | " emaslow = (emaS1 + emaS2 + emaS3 + emaS4 + emaS5 + emaS6 + emaS7 + emaS8 + emaS9 + emaS10 + emaS11 + emaS12 + emaS13 + emaS14 + emaS15 + emaS16)/16\n",
427 | " #Fast EMA Color Rules\n",
428 | " 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",
429 | " 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",
432 | " colslowS = (emaS1 emaslow and not colslowS and colfastL and (not ShowCon or colslowL) and (not emaFilter or emafast>ema200):\n",
434 | " if int(buy1[-1]) > 0:\n",
435 | " buy = buy1[-1] + 1\n",
436 | " else:\n",
437 | " buy = 1\n",
438 | " else:\n",
439 | " buy = 0\n",
440 | " buy1.append(buy)\n",
441 | " if emafast < emaslow and not colslowL and colfastS and (not ShowCon or colslowS) and (not emaFilter or emafast 0:\n",
443 | " sell = sell1[-1] + 1\n",
444 | " else:\n",
445 | " sell = 1\n",
446 | " else:\n",
447 | " sell = 0\n",
448 | " sell1.append(sell)\n",
449 | " #buy\n",
450 | " if buy>1 and colfastL and (uOCCswing and ((df.loc[i-1,'close']df.loc[i,'open']))):\n",
451 | " buy3 = 1\n",
452 | " else:\n",
453 | " buy3 = buy\n",
454 | " buy2.append(buy3)\n",
455 | " #sell \n",
456 | " if sell>1 and colfastS and (uOCCswing and ((df.loc[i-1,'close']df.loc[i,'open']))):\n",
457 | " sell3 = 1\n",
458 | " else:\n",
459 | " sell3 = sell\n",
460 | " sell2.append(sell3)\n",
461 | " #buybreak\n",
462 | " if emafast > emaslow and not colslowS and (not emaFilter or emafast>ema200):\n",
463 | " if buybreak1[-1] > 0:\n",
464 | " buybreak = buybreak1[-1] + 1\n",
465 | " else:\n",
466 | " buybreak = 1\n",
467 | " else:\n",
468 | " buybreak = 0\n",
469 | " buybreak1.append(buybreak)\n",
470 | " if emafast < emaslow and not colslowL and (not emaFilter or emafast 0:\n",
472 | " sellbreak = sellbreak1[-1]+1\n",
473 | " else:\n",
474 | " sellbreak = 1\n",
475 | " else:\n",
476 | " sellbreak = 0\n",
477 | " sellbreak1.append(sellbreak)\n",
478 | " #arrow plotting\n",
479 | " #buy_arrow\n",
480 | " buy_barssince_var = barssince(buy2[:-1],barssince_var)\n",
481 | " if (ShowSwing and buy3==1)and buy_barssince_var > 6:\n",
482 | " buy_arrow = 1\n",
483 | " else:\n",
484 | " buy_arrow = 0\n",
485 | " #sell arrow\n",
486 | " sell_barssince_var = barssince(sell2[:-1],barssince_var)\n",
487 | " if ShowSwing and (sell3==1 and sell_barssince_var > 6):\n",
488 | " sell_arrow = 1\n",
489 | " else:\n",
490 | " sell_arrow = 0\n",
491 | " #buybreak_arrow\n",
492 | " buybreak_barssince_var = barssince(buybreak1[:-1],barssince_var)\n",
493 | " sellbreak_barssince_var = barssince(sellbreak1[:-1],barssince_var)\n",
494 | " if ShowBreak and buybreak==1 and (sellbreak_barssince_var>Lookback) and (buybreak_barssince_var>Lookback):\n",
495 | " buybreak_arrow = 1\n",
496 | " else:\n",
497 | " buybreak_arrow = 0\n",
498 | " #sellbreak_arrow\n",
499 | " if ShowBreak and sellbreak==1 and (buybreak_barssince_var>Lookback) and (sellbreak_barssince_var>Lookback):\n",
500 | " sellbreak_arrow = 1\n",
501 | " else:\n",
502 | " sellbreak_arrow = 0\n",
503 | " if buy_arrow==1 and sell_arrow==0 and buybreak_arrow==0 and sellbreak_arrow==0:\n",
504 | " arrow_color = 'green'\n",
505 | " elif buy_arrow==0 and sell_arrow==1 and buybreak_arrow==0 and sellbreak_arrow==0:\n",
506 | " arrow_color = 'red'\n",
507 | " elif sell_arrow==0 and (buy_arrow==0 or buy_arrow==1) and buybreak_arrow==1 and sellbreak_arrow==0:\n",
508 | " arrow_color = 'aqua'\n",
509 | " elif buy_arrow==0 and (sell_arrow==1 or sell_arrow==0) and buybreak_arrow==0 and sellbreak_arrow==1:\n",
510 | " arrow_color = 'blue'\n",
511 | " else:\n",
512 | " arrow_color = 'none'\n",
513 | " df.loc[i,'arrow_color'] = arrow_color\n",
514 | " df = df[['date','open','high','low','close','arrow_color']]\n",
515 | " return df\n",
516 | " df=super_guppy(15,df)\n",
517 | " gup=df\n",
518 | " def bidatrema(df,period):\n",
519 | " df['hl']=abs(df['high']-df['low'])\n",
520 | " df['hpc']=abs(df['high']-df['close'].shift())\n",
521 | " df['lpc']=abs(df['low']-df['close'].shift())\n",
522 | " df['tr']=df[['hl','hpc','lpc']].max(axis=1)\n",
523 | " df['ATR']=pd.DataFrame.ewm(df[\"tr\"], span=period,min_periods=period).mean()\n",
524 | " df.drop([\"hl\",\"hpc\",\"lpc\",\"tr\"],axis = 1 , inplace =True)\n",
525 | " bidatrema(gup,14)\n",
526 | " df['bid_value'] = 0\n",
527 | " df['executed_value'] = 0\n",
528 | " df['diff'] = 0\n",
529 | " df['pnl'] = 0\n",
530 | " qty = int ( qu/ (gup.iloc[-1,2]) )\n",
531 | " var = False\n",
532 | " var_2 = False\n",
533 | " var_1 = False\n",
534 | " for i in range(len(df)):\n",
535 | " zz=df.loc[i,\"date\"]\n",
536 | " za=str(zz)[11:13]\n",
537 | " if za!=\"15\":\n",
538 | " if var==True:\n",
539 | " df.loc[i,'diff'] = df.loc[i,'close'] - bid_value\n",
540 | " df.loc[i,'pnl'] = ( bid_value /bid_value)*df.loc[i,'diff']\n",
541 | " var=False\n",
542 | " var1=False \n",
543 | " var_2=True \n",
544 | " if za!=\"15\":\n",
545 | " var_2=False\n",
546 | " if var==False and var_2==False:\n",
547 | " if df.loc[i,'arrow_color']=='green':\n",
548 | " bid_value = (df.loc[i,'high']+df.loc[i,'ATR']*.25)+(df.loc[i,'ATR']*.1)\n",
549 | " df.loc[i,'bid_value'] =bid_value\n",
550 | " var=True\n",
551 | " var1=False\n",
552 | " if var==True and var1==False and var_2==False:\n",
553 | " if df.loc[i,'high'] > bid_value and df.loc[i,'low'] < bid_value:\n",
554 | " df.loc[i,'executed_value']=bid_value\n",
555 | " var1=True\n",
556 | " if var==True and var1==True and var_2==False:\n",
557 | " if df.loc[i,'arrow_color']=='red':\n",
558 | " df.loc[i,'diff'] = df.loc[i,'close'] - bid_value\n",
559 | " df.loc[i,'pnl'] = ( bid_value /bid_value)*df.loc[i,'diff']\n",
560 | " var=False\n",
561 | " var1=False\n",
562 | " list_1 = df['pnl']\n",
563 | " pos_count = len(list(filter(lambda x: (x>0),list_1)))\n",
564 | " neg_count = len(list(filter(lambda x: (x<0),list_1)))\n",
565 | " total_trade = pos_count + neg_count\n",
566 | " try :\n",
567 | " strike_rate = pos_count/total_trade\n",
568 | " except:\n",
569 | " strike_rate = 0\n",
570 | " pnl = df['pnl'].sum()\n",
571 | " pnl_list.append(pnl)\n",
572 | " timeframe_list.append(t)\n",
573 | " strike_rate_list.append(strike_rate)\n",
574 | "main_df = pd.DataFrame({'timeframe':timeframe_list,'pnl':pnl_list,'strike_rate':strike_rate_list})\n",
575 | "main_df.to_csv('pnlbuy.csv')\n",
576 | "print(\"complete\")"
577 | ],
578 | "execution_count": 0,
579 | "outputs": [
580 | {
581 | "output_type": "stream",
582 | "text": [
583 | "SCANNING START\n",
584 | "complete\n"
585 | ],
586 | "name": "stdout"
587 | }
588 | ]
589 | },
590 | {
591 | "cell_type": "code",
592 | "metadata": {
593 | "id": "eeltB9-HDTqg",
594 | "colab_type": "code",
595 | "outputId": "d1f45b59-cc42-486a-a50c-2523d2510e2c",
596 | "colab": {}
597 | },
598 | "source": [
599 | "display(main_df)"
600 | ],
601 | "execution_count": 0,
602 | "outputs": [
603 | {
604 | "output_type": "display_data",
605 | "data": {
606 | "text/html": [
607 | "\n",
608 | "\n",
621 | "
\n",
622 | " \n",
623 | " \n",
624 | " | \n",
625 | " timeframe | \n",
626 | " pnl | \n",
627 | " strike_rate | \n",
628 | "
\n",
629 | " \n",
630 | " \n",
631 | " \n",
632 | " 0 | \n",
633 | " 1 | \n",
634 | " -73.269911 | \n",
635 | " 0.214286 | \n",
636 | "
\n",
637 | " \n",
638 | "
\n",
639 | "
"
640 | ],
641 | "text/plain": [
642 | " timeframe pnl strike_rate\n",
643 | "0 1 -73.269911 0.214286"
644 | ]
645 | },
646 | "metadata": {
647 | "tags": []
648 | }
649 | }
650 | ]
651 | },
652 | {
653 | "cell_type": "markdown",
654 | "metadata": {
655 | "id": "-uz_e2YBGxOR",
656 | "colab_type": "text"
657 | },
658 | "source": [
659 | "* Backtesting for SELL "
660 | ]
661 | },
662 | {
663 | "cell_type": "code",
664 | "metadata": {
665 | "id": "zjgZ1qOODTqt",
666 | "colab_type": "code",
667 | "outputId": "fb1b4811-eebd-412e-fb22-2def42e1513e",
668 | "colab": {}
669 | },
670 | "source": [
671 | "\n",
672 | "# future\n",
673 | "#z=[945155, 944899, 54817287, 54322951, 54843655,54820615, 54817031, 14359298, 14359042, 14358786, 14358530, 14358274, 14358018, 14357762, 14357506, 14357250, 14356994, 14356738, 14356482, 14355970, 14355714, 14355458, 14355202, 14354946, 14354690, 14354434, 14354178, 14353922, 14353154, 14352898, 14352642, 14352386, 14352130, 14351874, 14351362, 14351106, 14350850, 14350594, 14350338, 14350082, 14349826, 14332930, 14332674, 14330882, 13992962, 13992706, 14277890, 14275330, 14265346, 14259458, 14243586, 14226946, 14226690, 14226178, 14225922, 14225666, 14213378, 14209282, 14209026, 14207234, 14206466, 14204418, 14203906, 14203394, 14200578, 14200322, 14200066, 14198274, 14198018, 14197762, 14197506, 14197250, 14182914, 14180354, 14174210, 14151682, 14142210, 14141954, 14141698, 14141442, 14141186, 14140930, 14140674, 14140418, 14140162, 14139906, 14139650, 14139394, 14139138, 14142722, 14138882, 14138626, 14138370, 14138114, 14137858, 14137602, 14137346, 14137090, 14136834, 14136578, 14136066, 14135810, 14135042, 14134530, 14134274, 14134018, 14133762, 14133506, 14133250, 14132482, 14132226, 14131970, 14092802, 14092546, 14080770, 14080514, 14019842, 14018562, 14018306, 14017026, 14016770, 14016514, 14016258, 14014978, 14014722, 14014466, 14014210, 14013954, 14008578, 14008322, 14008066, 14007810, 14007042, 14006786, 14006530, 13992450, 14006274, 14006018, 14005762, 14005506, 14005250, 14004994, 14004738, 14004482, 14004226, 14003970, 13995266, 13995010, 13994754, 13994498, 13994242, 13993986, 13993730, 13993474, 14330370, 14328578, 14321154, 14319874, 14315266, 14315010, 14313986, 14306818, 14298370, 14285058, 14284802, 14283522, 14281730, 14278146, 13993218, 14356226, 14351618]\n",
674 | "#nifty\n",
675 | "#z=[3861249, 60417, 1510401, 4267265, 81153, 4268801, 134657, 2714625, 7458561, 140033, 177665, 5215745, 225537, 232961, 1207553, 315393, 1850625, 341249, 345089, 348929, 356865, 340481, 1270529, 424961, 7712001, 415745, 1346049, 408065, 3001089, 492033, 2939649, 519937, 2815745, 2977281, 633601, 3834113, 738561, 779521, 857857, 2953217, 884737, 895745, 3465729, 897537, 2889473, 2952193, 784129, 969473, 3050241, 975873]\n",
676 | "## fno\n",
677 | "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",
678 | "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",
679 | "\n",
680 | "#################\n",
681 | "\n",
682 | "############\n",
683 | "\n",
684 | "sdate =\"2019-08-20 00:00:00\"\n",
685 | "todate =\"2019-09-16 09:16:00\"\n",
686 | "#sdate =\"2019-08-19\"\n",
687 | "#todate =\"2019-10-01\"\n",
688 | "time_frame =\"minute\"\n",
689 | "quu=50000\n",
690 | "eexchange=\"NSE\"\n",
691 | "productt=\"MIS\"\n",
692 | "qu=int(quu)\n",
693 | "sdate_backtest =\"2019-08-14\"\n",
694 | "todate_backtest =\"2019-10-02\"\n",
695 | "##############\n",
696 | "print(\"SCANNING START\")\n",
697 | "ttoken=5633\n",
698 | "ttradingsymbol=\"ACC\"\n",
699 | "\n",
700 | "###############\n",
701 | "\n",
702 | "def time_con(time,df):\n",
703 | " z = 0\n",
704 | " chg = []\n",
705 | " chg_index = 0\n",
706 | " timeframe = time\n",
707 | " date = np.array([])\n",
708 | " for i in range(len(df)):\n",
709 | " date = np.hstack((date,str(df.loc[i,\"date\"])))\n",
710 | " open_np = df.open.values\n",
711 | " high_np = df.high.values\n",
712 | " low_np = df.low.values\n",
713 | " close_np = df.close.values\n",
714 | "# volume_np = df.volume.values\n",
715 | "# print(date)\n",
716 | " for j in range(len(date)):\n",
717 | " if date[j][11:16] == \"09:15\":\n",
718 | " chg.append(j)\n",
719 | " np_date=np.array([])\n",
720 | " np_open = np.array([])\n",
721 | " np_high = np.array([])\n",
722 | " np_low = np.array([])\n",
723 | " np_close = np.array([])\n",
724 | "# np_volume = np.array([])\n",
725 | " for i in range(len(date)-timeframe+1):\n",
726 | " if i>=z:\n",
727 | " chg_index+=1\n",
728 | " if (chg_index < len(chg)):\n",
729 | " for k in range(i,chg[chg_index],timeframe):\n",
730 | "# print(date[k])\n",
731 | "# print(k,k+timeframe)\n",
732 | " np_date = np.hstack((np_date,date[k]))\n",
733 | " np_open = np.hstack((np_open,open_np[k]))\n",
734 | " np_high = np.hstack((np_high,np.amax(high_np[k:k+timeframe])))\n",
735 | "# print(high_np[k:k+timeframe])\n",
736 | " np_low = np.hstack((np_low,np.amin(low_np[k:k+timeframe])))\n",
737 | "# print(low_np[k:k+timeframe])\n",
738 | " np_close = np.hstack((np_close,close_np[k+timeframe-1]))\n",
739 | "# np_volume = np.hstack((np_volume,np.sum(volume_np[k:k+timeframe])))\n",
740 | " z = i+timeframe\n",
741 | " z=chg[chg_index]\n",
742 | " else:\n",
743 | " con_df = pd.DataFrame({\"date\":np_date,\"open\":np_open,\"high\":np_high,\"low\":np_low,\"close\":np_close})\n",
744 | " return con_df\n",
745 | "#################\n",
746 | "strike_rate = 0\n",
747 | "strike_rate_list = []\n",
748 | "timeframe = []\n",
749 | "timeframe_list = []\n",
750 | "pnl = 0\n",
751 | "pnl_list = []\n",
752 | "\n",
753 | "########################################################\n",
754 | "for t in range(1,2):\n",
755 | " dfw=kite.historical_data(ttoken,sdate_backtest,todate_backtest,time_frame,0)\n",
756 | " dfw=pd.DataFrame(dfw)\n",
757 | " df=pd.DataFrame(dfw[['date','open','high','low','close']])\n",
758 | " df= time_con(t,df)\n",
759 | " slow_ema = [3,5,7,9,11,13,15,17,19,21,23]\n",
760 | " fast_ema = [25,28,31,34,37,40,43,46,49,52,55,58,61,64,67,70,200]\n",
761 | " def EMA(df, base, target, period, alpha=False):\n",
762 | " con = pd.concat([df[:period][base].rolling(window=period).mean(), df[period:][base]])\n",
763 | " if (alpha == True):\n",
764 | " # (1 - alpha) * previous_val + alpha * current_val where alpha = 1 / period\n",
765 | " df[target] = con.ewm(alpha=1 / period, adjust=False).mean()\n",
766 | " else:\n",
767 | " # ((current_val - previous_val) * coeff) + previous_val where coeff = 2 / (period + 1)\n",
768 | " df[target] = con.ewm(span=period, adjust=False).mean()\n",
769 | " df.fillna(0,inplace = True)\n",
770 | " # return df\n",
771 | " for j in slow_ema:\n",
772 | " val = \"ema\"+\"_\"+str(j)\n",
773 | " EMA(df,\"close\",val,j)\n",
774 | " for k in fast_ema:\n",
775 | " val = \"ema\"+\"_\"+str(k)\n",
776 | " EMA(df,\"close\",val,k)\n",
777 | " def super_guppy(interval,df,anchor=0):\n",
778 | " anchor = 0\n",
779 | " ShowBreak = True\n",
780 | " ShowSwing = True\n",
781 | " ShowCon = False\n",
782 | " uOCCswing = False\n",
783 | " Lookback = 6\n",
784 | " emaFilter = False\n",
785 | " mult = 0\n",
786 | " buybreak = 0\n",
787 | " sellbreak = 0\n",
788 | " buy_barssince_var = 0\n",
789 | " sell_barssince_var = 0\n",
790 | " buybreak_barssince_var = 0\n",
791 | " sellbreak_barssince_var = 0\n",
792 | " barssince_lst = list()\n",
793 | " barssince_var = 0\n",
794 | " bar_count_var = 0\n",
795 | " buy1 = list()\n",
796 | " sell1 = list()\n",
797 | " buy2 = list()\n",
798 | " sell2 = list()\n",
799 | " buybreak1 = list()\n",
800 | " sellbreak1 = list()\n",
801 | " def barssince(b,barssince_var):\n",
802 | " barssince_lst = []\n",
803 | " barssince_var = 0 \n",
804 | " new_var = len(b)\n",
805 | " for i in b[::-1]:\n",
806 | " if i == 1:\n",
807 | " break\n",
808 | " barssince_lst.append(i)\n",
809 | " barssince_var = len(barssince_lst)\n",
810 | " return barssince_var\n",
811 | " barssince_lst.clear()\n",
812 | " if interval < 1441 :\n",
813 | " if (anchor==0 or interval <= 0 or interval >= anchor or anchor > 1441 ):\n",
814 | " mult = 1\n",
815 | " else:\n",
816 | " if round(anchor/interval) > 1:\n",
817 | " mult = round(anchor/interval)\n",
818 | " else:\n",
819 | " mult = 1\n",
820 | " else:\n",
821 | " mult = 1\n",
822 | " #isIntraday Not\n",
823 | " if interval > 1441:\n",
824 | " if (anchor==0 or interval <= 0 or interval >= anchor or anchor < 52 ):\n",
825 | " mult = mult\n",
826 | " else:\n",
827 | " if round(anchor/interval) > 1:\n",
828 | " mult = round(anchor/interval)\n",
829 | " else:\n",
830 | " mult = 1\n",
831 | " else:\n",
832 | " mult = mult\n",
833 | " mult = 1\n",
834 | " for i in range(len(df)):\n",
835 | " emaF1 = df.loc[i,'ema_3']\n",
836 | " emaF2 = df.loc[i,'ema_5']\n",
837 | " emaF3 = df.loc[i,'ema_7']\n",
838 | " emaF4 = df.loc[i,'ema_9']\n",
839 | " emaF5 = df.loc[i,'ema_11']\n",
840 | " emaF6 = df.loc[i,'ema_13']\n",
841 | " emaF7 = df.loc[i,'ema_15']\n",
842 | " emaF8 = df.loc[i,'ema_17']\n",
843 | " emaF9 = df.loc[i,'ema_19']\n",
844 | " emaF10 = df.loc[i,'ema_21']\n",
845 | " emaF11 = df.loc[i,'ema_23']\n",
846 | " emaS1 = df.loc[i,'ema_25']\n",
847 | " emaS2 = df.loc[i,'ema_28']\n",
848 | " emaS3 = df.loc[i,'ema_31']\n",
849 | " emaS4 = df.loc[i,'ema_34']\n",
850 | " emaS5 = df.loc[i,'ema_37']\n",
851 | " emaS6 = df.loc[i,'ema_40']\n",
852 | " emaS7 = df.loc[i,'ema_43']\n",
853 | " emaS8 = df.loc[i,'ema_46']\n",
854 | " emaS9 = df.loc[i,'ema_49']\n",
855 | " emaS10 = df.loc[i,'ema_52']\n",
856 | " emaS11 = df.loc[i,'ema_55']\n",
857 | " emaS12 = df.loc[i,'ema_58']\n",
858 | " emaS13 = df.loc[i,'ema_61']\n",
859 | " emaS14 = df.loc[i,'ema_64']\n",
860 | " emaS15 = df.loc[i,'ema_67']\n",
861 | " emaS16 = df.loc[i,'ema_70']\n",
862 | " ema200 = df.loc[i,'ema_200'] \n",
863 | " emafast = (emaF1 + emaF2 + emaF3 + emaF4 + emaF5 + emaF6 + emaF7 + emaF8 + emaF9 + emaF10 + emaF11)/11\n",
864 | " emaslow = (emaS1 + emaS2 + emaS3 + emaS4 + emaS5 + emaS6 + emaS7 + emaS8 + emaS9 + emaS10 + emaS11 + emaS12 + emaS13 + emaS14 + emaS15 + emaS16)/16\n",
865 | " #Fast EMA Color Rules\n",
866 | " 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",
867 | " 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",
870 | " colslowS = (emaS1 emaslow and not colslowS and colfastL and (not ShowCon or colslowL) and (not emaFilter or emafast>ema200):\n",
872 | " if int(buy1[-1]) > 0:\n",
873 | " buy = buy1[-1] + 1\n",
874 | " else:\n",
875 | " buy = 1\n",
876 | " else:\n",
877 | " buy = 0\n",
878 | " buy1.append(buy)\n",
879 | " if emafast < emaslow and not colslowL and colfastS and (not ShowCon or colslowS) and (not emaFilter or emafast 0:\n",
881 | " sell = sell1[-1] + 1\n",
882 | " else:\n",
883 | " sell = 1\n",
884 | " else:\n",
885 | " sell = 0\n",
886 | " sell1.append(sell)\n",
887 | " #buy\n",
888 | " if buy>1 and colfastL and (uOCCswing and ((df.loc[i-1,'close']df.loc[i,'open']))):\n",
889 | " buy3 = 1\n",
890 | " else:\n",
891 | " buy3 = buy\n",
892 | " buy2.append(buy3)\n",
893 | " #sell \n",
894 | " if sell>1 and colfastS and (uOCCswing and ((df.loc[i-1,'close']df.loc[i,'open']))):\n",
895 | " sell3 = 1\n",
896 | " else:\n",
897 | " sell3 = sell\n",
898 | " sell2.append(sell3)\n",
899 | " #buybreak\n",
900 | " if emafast > emaslow and not colslowS and (not emaFilter or emafast>ema200):\n",
901 | " if buybreak1[-1] > 0:\n",
902 | " buybreak = buybreak1[-1] + 1\n",
903 | " else:\n",
904 | " buybreak = 1\n",
905 | " else:\n",
906 | " buybreak = 0\n",
907 | " buybreak1.append(buybreak)\n",
908 | " if emafast < emaslow and not colslowL and (not emaFilter or emafast 0:\n",
910 | " sellbreak = sellbreak1[-1]+1\n",
911 | " else:\n",
912 | " sellbreak = 1\n",
913 | " else:\n",
914 | " sellbreak = 0\n",
915 | " sellbreak1.append(sellbreak)\n",
916 | " #arrow plotting\n",
917 | " #buy_arrow\n",
918 | " buy_barssince_var = barssince(buy2[:-1],barssince_var)\n",
919 | " if (ShowSwing and buy3==1)and buy_barssince_var > 6:\n",
920 | " buy_arrow = 1\n",
921 | " else:\n",
922 | " buy_arrow = 0\n",
923 | " #sell arrow\n",
924 | " sell_barssince_var = barssince(sell2[:-1],barssince_var)\n",
925 | " if ShowSwing and (sell3==1 and sell_barssince_var > 6):\n",
926 | " sell_arrow = 1\n",
927 | " else:\n",
928 | " sell_arrow = 0\n",
929 | " #buybreak_arrow\n",
930 | " buybreak_barssince_var = barssince(buybreak1[:-1],barssince_var)\n",
931 | " sellbreak_barssince_var = barssince(sellbreak1[:-1],barssince_var)\n",
932 | " if ShowBreak and buybreak==1 and (sellbreak_barssince_var>Lookback) and (buybreak_barssince_var>Lookback):\n",
933 | " buybreak_arrow = 1\n",
934 | " else:\n",
935 | " buybreak_arrow = 0\n",
936 | " #sellbreak_arrow\n",
937 | " if ShowBreak and sellbreak==1 and (buybreak_barssince_var>Lookback) and (sellbreak_barssince_var>Lookback):\n",
938 | " sellbreak_arrow = 1\n",
939 | " else:\n",
940 | " sellbreak_arrow = 0\n",
941 | " if buy_arrow==1 and sell_arrow==0 and buybreak_arrow==0 and sellbreak_arrow==0:\n",
942 | " arrow_color = 'green'\n",
943 | " elif buy_arrow==0 and sell_arrow==1 and buybreak_arrow==0 and sellbreak_arrow==0:\n",
944 | " arrow_color = 'red'\n",
945 | " elif sell_arrow==0 and (buy_arrow==0 or buy_arrow==1) and buybreak_arrow==1 and sellbreak_arrow==0:\n",
946 | " arrow_color = 'aqua'\n",
947 | " elif buy_arrow==0 and (sell_arrow==1 or sell_arrow==0) and buybreak_arrow==0 and sellbreak_arrow==1:\n",
948 | " arrow_color = 'blue'\n",
949 | " else:\n",
950 | " arrow_color = 'none'\n",
951 | " df.loc[i,'arrow_color'] = arrow_color\n",
952 | " df = df[['date','open','high','low','close','arrow_color']]\n",
953 | " return df\n",
954 | " df=super_guppy(15,df)\n",
955 | " gup=df\n",
956 | " def bidatrema(df,period):\n",
957 | " df['hl']=abs(df['high']-df['low'])\n",
958 | " df['hpc']=abs(df['high']-df['close'].shift())\n",
959 | " df['lpc']=abs(df['low']-df['close'].shift())\n",
960 | " df['tr']=df[['hl','hpc','lpc']].max(axis=1)\n",
961 | " df['ATR']=pd.DataFrame.ewm(df[\"tr\"], span=period,min_periods=period).mean()\n",
962 | " df.drop([\"hl\",\"hpc\",\"lpc\",\"tr\"],axis = 1 , inplace =True)\n",
963 | " bidatrema(gup,14)\n",
964 | " df['bid_value'] = 0\n",
965 | " df['executed_value'] = 0\n",
966 | " df['diff'] = 0\n",
967 | " df['pnl'] = 0\n",
968 | " qty = int ( qu/ (gup.iloc[-1,2]) )\n",
969 | " var = False\n",
970 | " var_2 = False\n",
971 | " var_1 = False\n",
972 | " for i in range(len(df)):\n",
973 | " zz=df.loc[i,\"date\"]\n",
974 | " za=str(zz)[11:13]\n",
975 | " if za==\"15\":\n",
976 | " if var==True:\n",
977 | " df.loc[i,'diff'] = bid_value - df.loc[i,'close']\n",
978 | " df.loc[i,'pnl'] = ( bid_value /bid_value)*df.loc[i,'diff']\n",
979 | " ##\n",
980 | " var=False\n",
981 | " var1=False \n",
982 | " var_2=True\n",
983 | " if za!=\"15\":\n",
984 | " var_2=False\n",
985 | " if var==False and var_2==False:\n",
986 | " if df.loc[i,'arrow_color']=='red':\n",
987 | " bid_value = (df.loc[i,'low']-df.loc[i,'ATR']*.25)-(df.loc[i,'ATR']*.1)\n",
988 | " df.loc[i,'bid_value'] =bid_value\n",
989 | " var=True\n",
990 | " var1=False\n",
991 | " if var==True and var1==False and var_2==False:\n",
992 | " if df.loc[i,'high'] > bid_value and df.loc[i,'low'] < bid_value:\n",
993 | " df.loc[i,'executed_value']=bid_value\n",
994 | " var1=True\n",
995 | " if var==True and var1==True and var_2==False:\n",
996 | " if df.loc[i,'arrow_color']=='green':\n",
997 | " df.loc[i,'diff'] = bid_value - df.loc[i,'close'] \n",
998 | " df.loc[i,'pnl'] = ( bid_value /bid_value)*df.loc[i,'diff']\n",
999 | " var=False\n",
1000 | " var1=False\n",
1001 | " list_1 = df['pnl']\n",
1002 | " pos_count = len(list(filter(lambda x: (x>0),list_1)))\n",
1003 | " neg_count = len(list(filter(lambda x: (x<0),list_1)))\n",
1004 | " total_trade = pos_count + neg_count\n",
1005 | " try :\n",
1006 | " strike_rate = pos_count/total_trade\n",
1007 | " except:\n",
1008 | " strike_rate = 0\n",
1009 | " #print(strike_rate)\n",
1010 | " pnl = df['pnl'].sum()\n",
1011 | " pnl_list.append(pnl)\n",
1012 | " timeframe_list.append(t)\n",
1013 | " strike_rate_list.append(strike_rate)\n",
1014 | "main_df = pd.DataFrame({'timeframe':timeframe_list,'pnl':pnl_list,'strike_rate':strike_rate_list})\n",
1015 | "main_df.to_csv('pnlsell.csv')\n",
1016 | "print(\"complete\")"
1017 | ],
1018 | "execution_count": 0,
1019 | "outputs": [
1020 | {
1021 | "output_type": "stream",
1022 | "text": [
1023 | "SCANNING START\n",
1024 | "complete\n"
1025 | ],
1026 | "name": "stdout"
1027 | }
1028 | ]
1029 | },
1030 | {
1031 | "cell_type": "code",
1032 | "metadata": {
1033 | "id": "QVYSprzLDTq3",
1034 | "colab_type": "code",
1035 | "outputId": "cda83d77-8f0d-4547-f6a1-b9bbb22a289b",
1036 | "colab": {}
1037 | },
1038 | "source": [
1039 | "display(main_df)"
1040 | ],
1041 | "execution_count": 0,
1042 | "outputs": [
1043 | {
1044 | "output_type": "display_data",
1045 | "data": {
1046 | "text/html": [
1047 | "\n",
1048 | "\n",
1061 | "
\n",
1062 | " \n",
1063 | " \n",
1064 | " | \n",
1065 | " timeframe | \n",
1066 | " pnl | \n",
1067 | " strike_rate | \n",
1068 | "
\n",
1069 | " \n",
1070 | " \n",
1071 | " \n",
1072 | " 0 | \n",
1073 | " 1 | \n",
1074 | " -11.92732 | \n",
1075 | " 0.333333 | \n",
1076 | "
\n",
1077 | " \n",
1078 | "
\n",
1079 | "
"
1080 | ],
1081 | "text/plain": [
1082 | " timeframe pnl strike_rate\n",
1083 | "0 1 -11.92732 0.333333"
1084 | ]
1085 | },
1086 | "metadata": {
1087 | "tags": []
1088 | }
1089 | }
1090 | ]
1091 | }
1092 | ]
1093 | }
--------------------------------------------------------------------------------
/Buy on RSI when the current high of candle is more then previous high of candle .ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Buy on RSI (sunpharma stock ) "
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "# when the Current high of candle is more then previous high of candle "
15 | ]
16 | },
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {},
20 | "source": [
21 | "From this code we can place a limit order of sunpharma, when the RSI is greater then 30 and the current high of candle is higher then the previous high of candle "
22 | ]
23 | },
24 | {
25 | "cell_type": "code",
26 | "execution_count": null,
27 | "metadata": {},
28 | "outputs": [],
29 | "source": [
30 | "from kiteconnect import KiteTicker \n",
31 | "from kiteconnect import KiteConnect \n",
32 | "api_key='************************' \n",
33 | "api_secret=' *************************' \n",
34 | "kite=KiteConnect(api_key,api_secret)\n",
35 | "kite.login_url()"
36 | ]
37 | },
38 | {
39 | "cell_type": "code",
40 | "execution_count": null,
41 | "metadata": {},
42 | "outputs": [],
43 | "source": [
44 | "import logging\n",
45 | "logging.basicConfig(level=logging.DEBUG)\n",
46 | "kite = KiteConnect(api_key=api_key)\n",
47 | "token=\"HTJQmxal5dsFRQ5WbibO1B4MYvclB8OF\"\n",
48 | "data = kite.generate_session(token,api_secret)\n",
49 | "kite.set_access_token(data[\"access_token\"])\n",
50 | "print(data)"
51 | ]
52 | },
53 | {
54 | "cell_type": "code",
55 | "execution_count": 26,
56 | "metadata": {},
57 | "outputs": [
58 | {
59 | "name": "stderr",
60 | "output_type": "stream",
61 | "text": [
62 | "DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.kite.trade:443\n",
63 | "DEBUG:urllib3.connectionpool:https://api.kite.trade:443 \"GET /instruments/historical/857857/minute?instrument_token=857857&from=2019-01-05&to=2019-06-02&interval=minute&continuous=0 HTTP/1.1\" 200 None\n",
64 | "DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.kite.trade:443\n"
65 | ]
66 | },
67 | {
68 | "name": "stdout",
69 | "output_type": "stream",
70 | "text": [
71 | "66.9597502000033\n"
72 | ]
73 | },
74 | {
75 | "name": "stderr",
76 | "output_type": "stream",
77 | "text": [
78 | "DEBUG:urllib3.connectionpool:https://api.kite.trade:443 \"POST /orders/regular HTTP/1.1\" 200 None\n"
79 | ]
80 | },
81 | {
82 | "name": "stdout",
83 | "output_type": "stream",
84 | "text": [
85 | "one order placed\n"
86 | ]
87 | }
88 | ],
89 | "source": [
90 | "from pprint import pprint\n",
91 | "import pandas as pd \n",
92 | "import numpy as np\n",
93 | "from datetime import datetime \n",
94 | "from IPython.display import clear_output\n",
95 | "import time\n",
96 | "from kiteconnect import KiteTicker \n",
97 | "from kiteconnect import KiteConnect\n",
98 | "\n",
99 | "def livedata():\n",
100 | " while (True):\n",
101 | " km=datetime.now().minute\n",
102 | " ks=datetime.now().second\n",
103 | " if km%1==0 and ks==1:\n",
104 | " clear_output(wait=True)\n",
105 | " z=kite.historical_data(857857, \"2019-01-05\", \"2019-06-02\",\"minute\",0)\n",
106 | " za=pd.DataFrame(z)\n",
107 | " rsi_period=14\n",
108 | " chg=za[\"close\"].diff(1)\n",
109 | " gain=chg.mask(chg<0,0) \n",
110 | " loss=chg.mask(chg>0,0)\n",
111 | " avg_gain=gain.ewm(com=rsi_period-1,min_periods=rsi_period).mean()\n",
112 | " avg_loss=loss.ewm(com=rsi_period-1,min_periods=rsi_period).mean()\n",
113 | " rs =abs(avg_gain / avg_loss)\n",
114 | " rsi =100 -(100/(1+rs))\n",
115 | " za['rsi']=rsi\n",
116 | " print(za.iloc[-1,6])\n",
117 | " \n",
118 | " # rsi value #prev high #current high \n",
119 | " \n",
120 | " if za.iloc[-1,6] > 30 and za.iloc[-2,2] > za.iloc[-1,2]:\n",
121 | " \n",
122 | " kite.place_order( variety=\"regular\",tradingsymbol='SUNPHARMA',quantity=1,exchange='NSE',order_type='LIMIT',price= 453,transaction_type='BUY',product='CNC')\n",
123 | " print(\"one order placed\")\n",
124 | " break\n",
125 | " else:\n",
126 | " pass\n",
127 | " time.sleep(60)\n",
128 | "livedata()"
129 | ]
130 | }
131 | ],
132 | "metadata": {
133 | "kernelspec": {
134 | "display_name": "Python 3",
135 | "language": "python",
136 | "name": "python3"
137 | },
138 | "language_info": {
139 | "codemirror_mode": {
140 | "name": "ipython",
141 | "version": 3
142 | },
143 | "file_extension": ".py",
144 | "mimetype": "text/x-python",
145 | "name": "python",
146 | "nbconvert_exporter": "python",
147 | "pygments_lexer": "ipython3",
148 | "version": "3.7.3"
149 | }
150 | },
151 | "nbformat": 4,
152 | "nbformat_minor": 2
153 | }
154 |
--------------------------------------------------------------------------------
/Hisorical_Data_Download_of_stocks.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "kernelspec": {
6 | "display_name": "Python 3",
7 | "language": "python",
8 | "name": "python3"
9 | },
10 | "language_info": {
11 | "codemirror_mode": {
12 | "name": "ipython",
13 | "version": 3
14 | },
15 | "file_extension": ".py",
16 | "mimetype": "text/x-python",
17 | "name": "python",
18 | "nbconvert_exporter": "python",
19 | "pygments_lexer": "ipython3",
20 | "version": "3.7.3"
21 | },
22 | "colab": {
23 | "name": "Hisorical_Data_Download of stocks.ipynb",
24 | "provenance": [],
25 | "include_colab_link": true
26 | }
27 | },
28 | "cells": [
29 | {
30 | "cell_type": "markdown",
31 | "metadata": {
32 | "id": "view-in-github",
33 | "colab_type": "text"
34 | },
35 | "source": [
36 | "
"
37 | ]
38 | },
39 | {
40 | "cell_type": "markdown",
41 | "metadata": {
42 | "id": "RK2RqbqmNOAO",
43 | "colab_type": "text"
44 | },
45 | "source": [
46 | "# Historical Data Download Code for any stock of Stock Market"
47 | ]
48 | },
49 | {
50 | "cell_type": "markdown",
51 | "metadata": {
52 | "id": "33L1lQO3NOAU",
53 | "colab_type": "text"
54 | },
55 | "source": [
56 | "This code will download historical data of any stocks or multiple stocks in csv "
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "metadata": {
62 | "id": "EQhn_q1ZNOAW",
63 | "colab_type": "code",
64 | "colab": {},
65 | "outputId": "96a8f0bf-cd2a-4443-b977-e46cbf25638a"
66 | },
67 | "source": [
68 | "print(\" \\n \")\n",
69 | "print(\" \\t \\t \\t WELCOME ! DATA_DOWNLOAD PROGRAM - ZERODHA ! \") \n",
70 | "print(\"\\n\")\n",
71 | "print(\" \\t \\t \\t Please wait while we are installing library\")\n",
72 | "\n",
73 | "import time\n",
74 | "import webbrowser\n",
75 | "import numpy as np\n",
76 | "import pandas as pd\n",
77 | "from kiteconnect import KiteTicker \n",
78 | "from kiteconnect import KiteConnect\n",
79 | "\n",
80 | "# login to zerodha \n",
81 | "\n",
82 | "api_key='******************* @@enter your api key here@@@**************' \n",
83 | "api_secret='**********************@@enter your api key here****************'\n",
84 | "print(KiteConnect(api_key,api_secret).login_url())\n",
85 | "\n",
86 | "print(\" \\n \")\n",
87 | "\n",
88 | "kite = KiteConnect(api_key=api_key)\n",
89 | "data = kite.generate_session(((str(input(\"ENTER full link generated in URL :- \")).split(\"request_token=\")[1]).split(\"&action\"))[0],api_secret)\n",
90 | "kite.set_access_token(data[\"access_token\"])\n",
91 | "print(\" \\n \")\n",
92 | "print(\" \\n \")\n",
93 | "\n",
94 | "# inputs \n",
95 | "\n",
96 | "while(True):\n",
97 | " print(\" \\n \")\n",
98 | " while(True):\n",
99 | " zde=str(input(\"Enter Symbols manually or full_list ? 'YES' for Manually and 'NO' to Pass Full_list :=\")).upper()\n",
100 | " if zde:\n",
101 | " if \"YES\"==zde:\n",
102 | " break\n",
103 | " if \"NO\" in zde:\n",
104 | " break\n",
105 | " print(\"\\n\")\n",
106 | " if \"YES\" in zde:\n",
107 | " while(True):\n",
108 | " k=str(input(\"ENTER SYMBOL''S ex:-'sunpharma,sbin':=\")).upper()\n",
109 | " if k:\n",
110 | " z=k.split(',')\n",
111 | " break\n",
112 | " if \"NO\" in zde:\n",
113 | " while(True):\n",
114 | " a=input( \"ENTER FULL LIST OF SYMBOL's ex:-'sunpharma,sbin,adani,ongc':=\").upper()\n",
115 | " if a:\n",
116 | " z=a.split(',')\n",
117 | " break\n",
118 | " while(True):\n",
119 | " eexchange=str(input(\"ENTER EXCHANGE ex:-'NSE':=\")).upper()\n",
120 | " if eexchange:\n",
121 | " break\n",
122 | " while(True):\n",
123 | " time_frame=str(input(\"ENTER TIME_FRAME of Data ex:-'5minute \\15minute \\day etc' :=\")).lower()\n",
124 | " if time_frame:\n",
125 | " break\n",
126 | " while(True):\n",
127 | " print(\" \\n \")\n",
128 | " zd=str(input(\"DO you want to change Date? 'NO'for Default and'YES'to change date:=\")).upper()\n",
129 | " if zd:\n",
130 | " if \"YES\"==zd:\n",
131 | " break\n",
132 | " if \"NO\"==zd:\n",
133 | " break\n",
134 | " if \"YES\" in zd:\n",
135 | " while(True):\n",
136 | " sdate =str(input(\"Starting Date in formatof '2019-07-23' :=\")).lower()\n",
137 | " if sdate:\n",
138 | " break\n",
139 | " while(True):\n",
140 | " todate =str(input(\"Ending Date in format of '2019-10-23' :=\")).lower()\n",
141 | " if todate:\n",
142 | " break\n",
143 | " if \"NO\" in zd:\n",
144 | " sdate =\"2019-07-23\"\n",
145 | " todate=\"2019-10-23\"\n",
146 | " print(\" \\n \")\n",
147 | " while(True):\n",
148 | " st=str(input(\"Is All above Entered inputs are TRUE ? 'YES' to continute or 'NO' to enter again ? \")).upper()\n",
149 | " if st:\n",
150 | " if \"YES\"==st:\n",
151 | " break\n",
152 | " else:\n",
153 | " pass\n",
154 | " if \"YES\"==st:\n",
155 | " break\n",
156 | "tokenall=[]\n",
157 | "symbl=[]\n",
158 | "aa=0\n",
159 | "print(\" \\n \")\n",
160 | "print(\"Processing.....\")\n",
161 | "\n",
162 | "while(True):\n",
163 | " ttoken=int(pd.DataFrame(kite.ltp(eexchange+\":\"+z[aa])).iloc[-2,0])\n",
164 | " tokenall.append(ttoken) # fetching tokens\n",
165 | " symbl.append(z[aa])\n",
166 | " aa=aa+1\n",
167 | " if aa==len(z):\n",
168 | " break\n",
169 | " \n",
170 | "print(\"Processing.....\") \n",
171 | "ee=0\n",
172 | "\n",
173 | "# downloading data\n",
174 | "\n",
175 | "while(True):\n",
176 | " dff=kite.historical_data(tokenall[ee],sdate,todate,time_frame,0)\n",
177 | " time.sleep(1)\n",
178 | " dfw=pd.DataFrame(dff)\n",
179 | " s=f\"{ symbl[ee]}.csv\" # writing to csv\n",
180 | " dfw.to_csv(s)\n",
181 | " ee=ee+1\n",
182 | " if ee==len(z):\n",
183 | " print(\"\\n\")\n",
184 | " print(\"Download Complete\")\n",
185 | " break"
186 | ],
187 | "execution_count": 0,
188 | "outputs": [
189 | {
190 | "output_type": "stream",
191 | "text": [
192 | " \n",
193 | " \n",
194 | " \t \t \t WELCOME ! DATA_DOWNLOAD PROGRAM - ZERODHA ! \n",
195 | "\n",
196 | "\n",
197 | " \t \t \t Please wait while we are installing library\n",
198 | " \n",
199 | " \n",
200 | "ENTER full link generated in URL :- https://kite.trade/?request_token=dGHSMlipgeR3kOKa3bPm9T4i8uG9pHIA&action=login&status=success\n",
201 | " \n",
202 | " \n",
203 | " \n",
204 | " \n",
205 | " \n",
206 | " \n",
207 | "Enter Symbols manually or full_list ? 'YES' for Manually and 'NO' to Pass Full_list :=yes\n",
208 | "\n",
209 | "\n",
210 | "ENTER SYMBOL''S ex:-'sunpharma,sbin':=SBIN\n",
211 | "ENTER EXCHANGE ex:-'NSE':=NSE\n",
212 | "ENTER TIME_FRAME of Data ex:-'5minute \n",
213 | "minute \\day etc' :=5minute\n",
214 | " \n",
215 | " \n",
216 | "DO you want to change Date? 'NO'for Default and'YES'to change date:=no\n",
217 | " \n",
218 | " \n",
219 | "Is All above Entered inputs are TRUE ? 'YES' to continute or 'NO' to enter again ? yes\n",
220 | " \n",
221 | " \n",
222 | "Processing.....\n",
223 | "Processing.....\n",
224 | "\n",
225 | "\n",
226 | "Download Complete\n"
227 | ],
228 | "name": "stdout"
229 | }
230 | ]
231 | },
232 | {
233 | "cell_type": "code",
234 | "metadata": {
235 | "id": "VZmnYBb4NOAf",
236 | "colab_type": "code",
237 | "colab": {},
238 | "outputId": "d493d42a-1c8f-4b77-d90b-ed32e3fbb3e2"
239 | },
240 | "source": [
241 | "# Downloaded Data\n",
242 | "\n",
243 | "new_file=pd.DataFrame(pd.read_csv(\"SBIN.csv\"))\n",
244 | "new_file[[\"date\",\"open\",\"high\",\"low\",\"close\"]]"
245 | ],
246 | "execution_count": 0,
247 | "outputs": [
248 | {
249 | "output_type": "execute_result",
250 | "data": {
251 | "text/html": [
252 | "\n",
253 | "\n",
266 | "
\n",
267 | " \n",
268 | " \n",
269 | " | \n",
270 | " date | \n",
271 | " open | \n",
272 | " high | \n",
273 | " low | \n",
274 | " close | \n",
275 | "
\n",
276 | " \n",
277 | " \n",
278 | " \n",
279 | " 0 | \n",
280 | " 2019-07-23 09:15:00+05:30 | \n",
281 | " 351.20 | \n",
282 | " 352.85 | \n",
283 | " 350.55 | \n",
284 | " 350.70 | \n",
285 | "
\n",
286 | " \n",
287 | " 1 | \n",
288 | " 2019-07-23 09:20:00+05:30 | \n",
289 | " 350.80 | \n",
290 | " 350.80 | \n",
291 | " 348.95 | \n",
292 | " 349.05 | \n",
293 | "
\n",
294 | " \n",
295 | " 2 | \n",
296 | " 2019-07-23 09:25:00+05:30 | \n",
297 | " 349.00 | \n",
298 | " 350.00 | \n",
299 | " 348.80 | \n",
300 | " 349.20 | \n",
301 | "
\n",
302 | " \n",
303 | " 3 | \n",
304 | " 2019-07-23 09:30:00+05:30 | \n",
305 | " 349.10 | \n",
306 | " 349.20 | \n",
307 | " 347.80 | \n",
308 | " 348.20 | \n",
309 | "
\n",
310 | " \n",
311 | " 4 | \n",
312 | " 2019-07-23 09:35:00+05:30 | \n",
313 | " 348.15 | \n",
314 | " 348.25 | \n",
315 | " 347.50 | \n",
316 | " 347.85 | \n",
317 | "
\n",
318 | " \n",
319 | " 5 | \n",
320 | " 2019-07-23 09:40:00+05:30 | \n",
321 | " 347.80 | \n",
322 | " 348.30 | \n",
323 | " 347.50 | \n",
324 | " 347.50 | \n",
325 | "
\n",
326 | " \n",
327 | " 6 | \n",
328 | " 2019-07-23 09:45:00+05:30 | \n",
329 | " 347.50 | \n",
330 | " 349.00 | \n",
331 | " 347.50 | \n",
332 | " 348.90 | \n",
333 | "
\n",
334 | " \n",
335 | " 7 | \n",
336 | " 2019-07-23 09:50:00+05:30 | \n",
337 | " 348.95 | \n",
338 | " 350.00 | \n",
339 | " 348.85 | \n",
340 | " 349.30 | \n",
341 | "
\n",
342 | " \n",
343 | " 8 | \n",
344 | " 2019-07-23 09:55:00+05:30 | \n",
345 | " 349.30 | \n",
346 | " 350.30 | \n",
347 | " 349.05 | \n",
348 | " 349.85 | \n",
349 | "
\n",
350 | " \n",
351 | " 9 | \n",
352 | " 2019-07-23 10:00:00+05:30 | \n",
353 | " 349.85 | \n",
354 | " 350.15 | \n",
355 | " 349.10 | \n",
356 | " 349.10 | \n",
357 | "
\n",
358 | " \n",
359 | " 10 | \n",
360 | " 2019-07-23 10:05:00+05:30 | \n",
361 | " 349.10 | \n",
362 | " 349.10 | \n",
363 | " 348.30 | \n",
364 | " 348.75 | \n",
365 | "
\n",
366 | " \n",
367 | " 11 | \n",
368 | " 2019-07-23 10:10:00+05:30 | \n",
369 | " 348.75 | \n",
370 | " 349.80 | \n",
371 | " 348.70 | \n",
372 | " 349.80 | \n",
373 | "
\n",
374 | " \n",
375 | " 12 | \n",
376 | " 2019-07-23 10:15:00+05:30 | \n",
377 | " 349.70 | \n",
378 | " 350.00 | \n",
379 | " 349.10 | \n",
380 | " 349.20 | \n",
381 | "
\n",
382 | " \n",
383 | " 13 | \n",
384 | " 2019-07-23 10:20:00+05:30 | \n",
385 | " 349.20 | \n",
386 | " 349.40 | \n",
387 | " 348.70 | \n",
388 | " 349.05 | \n",
389 | "
\n",
390 | " \n",
391 | " 14 | \n",
392 | " 2019-07-23 10:25:00+05:30 | \n",
393 | " 349.00 | \n",
394 | " 349.35 | \n",
395 | " 348.85 | \n",
396 | " 349.00 | \n",
397 | "
\n",
398 | " \n",
399 | " 15 | \n",
400 | " 2019-07-23 10:30:00+05:30 | \n",
401 | " 349.15 | \n",
402 | " 349.20 | \n",
403 | " 348.85 | \n",
404 | " 349.05 | \n",
405 | "
\n",
406 | " \n",
407 | " 16 | \n",
408 | " 2019-07-23 10:35:00+05:30 | \n",
409 | " 349.05 | \n",
410 | " 349.50 | \n",
411 | " 348.90 | \n",
412 | " 349.40 | \n",
413 | "
\n",
414 | " \n",
415 | " 17 | \n",
416 | " 2019-07-23 10:40:00+05:30 | \n",
417 | " 349.40 | \n",
418 | " 349.95 | \n",
419 | " 349.25 | \n",
420 | " 349.85 | \n",
421 | "
\n",
422 | " \n",
423 | " 18 | \n",
424 | " 2019-07-23 10:45:00+05:30 | \n",
425 | " 349.90 | \n",
426 | " 350.80 | \n",
427 | " 349.65 | \n",
428 | " 350.45 | \n",
429 | "
\n",
430 | " \n",
431 | " 19 | \n",
432 | " 2019-07-23 10:50:00+05:30 | \n",
433 | " 350.45 | \n",
434 | " 350.45 | \n",
435 | " 349.75 | \n",
436 | " 349.85 | \n",
437 | "
\n",
438 | " \n",
439 | " 20 | \n",
440 | " 2019-07-23 10:55:00+05:30 | \n",
441 | " 349.80 | \n",
442 | " 349.85 | \n",
443 | " 349.40 | \n",
444 | " 349.70 | \n",
445 | "
\n",
446 | " \n",
447 | " 21 | \n",
448 | " 2019-07-23 11:00:00+05:30 | \n",
449 | " 349.70 | \n",
450 | " 349.80 | \n",
451 | " 349.10 | \n",
452 | " 349.30 | \n",
453 | "
\n",
454 | " \n",
455 | " 22 | \n",
456 | " 2019-07-23 11:05:00+05:30 | \n",
457 | " 349.20 | \n",
458 | " 349.30 | \n",
459 | " 348.60 | \n",
460 | " 348.60 | \n",
461 | "
\n",
462 | " \n",
463 | " 23 | \n",
464 | " 2019-07-23 11:10:00+05:30 | \n",
465 | " 348.60 | \n",
466 | " 349.00 | \n",
467 | " 348.55 | \n",
468 | " 348.80 | \n",
469 | "
\n",
470 | " \n",
471 | " 24 | \n",
472 | " 2019-07-23 11:15:00+05:30 | \n",
473 | " 348.80 | \n",
474 | " 349.30 | \n",
475 | " 348.60 | \n",
476 | " 349.15 | \n",
477 | "
\n",
478 | " \n",
479 | " 25 | \n",
480 | " 2019-07-23 11:20:00+05:30 | \n",
481 | " 349.15 | \n",
482 | " 349.85 | \n",
483 | " 348.80 | \n",
484 | " 349.75 | \n",
485 | "
\n",
486 | " \n",
487 | " 26 | \n",
488 | " 2019-07-23 11:25:00+05:30 | \n",
489 | " 349.75 | \n",
490 | " 349.80 | \n",
491 | " 349.25 | \n",
492 | " 349.30 | \n",
493 | "
\n",
494 | " \n",
495 | " 27 | \n",
496 | " 2019-07-23 11:30:00+05:30 | \n",
497 | " 349.25 | \n",
498 | " 349.30 | \n",
499 | " 348.60 | \n",
500 | " 348.60 | \n",
501 | "
\n",
502 | " \n",
503 | " 28 | \n",
504 | " 2019-07-23 11:35:00+05:30 | \n",
505 | " 348.65 | \n",
506 | " 349.10 | \n",
507 | " 348.05 | \n",
508 | " 348.10 | \n",
509 | "
\n",
510 | " \n",
511 | " 29 | \n",
512 | " 2019-07-23 11:40:00+05:30 | \n",
513 | " 348.10 | \n",
514 | " 348.35 | \n",
515 | " 347.80 | \n",
516 | " 348.10 | \n",
517 | "
\n",
518 | " \n",
519 | " ... | \n",
520 | " ... | \n",
521 | " ... | \n",
522 | " ... | \n",
523 | " ... | \n",
524 | " ... | \n",
525 | "
\n",
526 | " \n",
527 | " 1995 | \n",
528 | " 2019-08-30 13:00:00+05:30 | \n",
529 | " 267.50 | \n",
530 | " 268.30 | \n",
531 | " 267.50 | \n",
532 | " 268.10 | \n",
533 | "
\n",
534 | " \n",
535 | " 1996 | \n",
536 | " 2019-08-30 13:05:00+05:30 | \n",
537 | " 268.15 | \n",
538 | " 269.10 | \n",
539 | " 267.70 | \n",
540 | " 268.70 | \n",
541 | "
\n",
542 | " \n",
543 | " 1997 | \n",
544 | " 2019-08-30 13:10:00+05:30 | \n",
545 | " 268.70 | \n",
546 | " 269.40 | \n",
547 | " 268.30 | \n",
548 | " 268.50 | \n",
549 | "
\n",
550 | " \n",
551 | " 1998 | \n",
552 | " 2019-08-30 13:15:00+05:30 | \n",
553 | " 268.50 | \n",
554 | " 269.30 | \n",
555 | " 268.40 | \n",
556 | " 268.50 | \n",
557 | "
\n",
558 | " \n",
559 | " 1999 | \n",
560 | " 2019-08-30 13:20:00+05:30 | \n",
561 | " 268.50 | \n",
562 | " 268.70 | \n",
563 | " 267.60 | \n",
564 | " 268.05 | \n",
565 | "
\n",
566 | " \n",
567 | " 2000 | \n",
568 | " 2019-08-30 13:25:00+05:30 | \n",
569 | " 268.10 | \n",
570 | " 268.40 | \n",
571 | " 267.70 | \n",
572 | " 268.30 | \n",
573 | "
\n",
574 | " \n",
575 | " 2001 | \n",
576 | " 2019-08-30 13:30:00+05:30 | \n",
577 | " 268.20 | \n",
578 | " 268.40 | \n",
579 | " 267.80 | \n",
580 | " 268.15 | \n",
581 | "
\n",
582 | " \n",
583 | " 2002 | \n",
584 | " 2019-08-30 13:35:00+05:30 | \n",
585 | " 268.10 | \n",
586 | " 268.85 | \n",
587 | " 268.10 | \n",
588 | " 268.35 | \n",
589 | "
\n",
590 | " \n",
591 | " 2003 | \n",
592 | " 2019-08-30 13:40:00+05:30 | \n",
593 | " 268.50 | \n",
594 | " 269.35 | \n",
595 | " 268.50 | \n",
596 | " 269.15 | \n",
597 | "
\n",
598 | " \n",
599 | " 2004 | \n",
600 | " 2019-08-30 13:45:00+05:30 | \n",
601 | " 269.25 | \n",
602 | " 270.00 | \n",
603 | " 269.00 | \n",
604 | " 269.90 | \n",
605 | "
\n",
606 | " \n",
607 | " 2005 | \n",
608 | " 2019-08-30 13:50:00+05:30 | \n",
609 | " 269.80 | \n",
610 | " 270.80 | \n",
611 | " 269.80 | \n",
612 | " 270.35 | \n",
613 | "
\n",
614 | " \n",
615 | " 2006 | \n",
616 | " 2019-08-30 13:55:00+05:30 | \n",
617 | " 270.30 | \n",
618 | " 270.35 | \n",
619 | " 268.80 | \n",
620 | " 268.90 | \n",
621 | "
\n",
622 | " \n",
623 | " 2007 | \n",
624 | " 2019-08-30 14:00:00+05:30 | \n",
625 | " 268.90 | \n",
626 | " 270.00 | \n",
627 | " 268.65 | \n",
628 | " 269.90 | \n",
629 | "
\n",
630 | " \n",
631 | " 2008 | \n",
632 | " 2019-08-30 14:05:00+05:30 | \n",
633 | " 269.95 | \n",
634 | " 270.20 | \n",
635 | " 269.60 | \n",
636 | " 269.75 | \n",
637 | "
\n",
638 | " \n",
639 | " 2009 | \n",
640 | " 2019-08-30 14:10:00+05:30 | \n",
641 | " 269.80 | \n",
642 | " 270.05 | \n",
643 | " 269.50 | \n",
644 | " 269.50 | \n",
645 | "
\n",
646 | " \n",
647 | " 2010 | \n",
648 | " 2019-08-30 14:15:00+05:30 | \n",
649 | " 269.50 | \n",
650 | " 269.55 | \n",
651 | " 268.85 | \n",
652 | " 269.45 | \n",
653 | "
\n",
654 | " \n",
655 | " 2011 | \n",
656 | " 2019-08-30 14:20:00+05:30 | \n",
657 | " 269.40 | \n",
658 | " 269.65 | \n",
659 | " 268.85 | \n",
660 | " 269.25 | \n",
661 | "
\n",
662 | " \n",
663 | " 2012 | \n",
664 | " 2019-08-30 14:25:00+05:30 | \n",
665 | " 269.30 | \n",
666 | " 270.25 | \n",
667 | " 269.15 | \n",
668 | " 270.10 | \n",
669 | "
\n",
670 | " \n",
671 | " 2013 | \n",
672 | " 2019-08-30 14:30:00+05:30 | \n",
673 | " 270.15 | \n",
674 | " 270.55 | \n",
675 | " 270.05 | \n",
676 | " 270.10 | \n",
677 | "
\n",
678 | " \n",
679 | " 2014 | \n",
680 | " 2019-08-30 14:35:00+05:30 | \n",
681 | " 270.20 | \n",
682 | " 270.40 | \n",
683 | " 269.05 | \n",
684 | " 269.40 | \n",
685 | "
\n",
686 | " \n",
687 | " 2015 | \n",
688 | " 2019-08-30 14:40:00+05:30 | \n",
689 | " 269.40 | \n",
690 | " 270.30 | \n",
691 | " 269.40 | \n",
692 | " 270.10 | \n",
693 | "
\n",
694 | " \n",
695 | " 2016 | \n",
696 | " 2019-08-30 14:45:00+05:30 | \n",
697 | " 270.10 | \n",
698 | " 270.95 | \n",
699 | " 270.10 | \n",
700 | " 270.90 | \n",
701 | "
\n",
702 | " \n",
703 | " 2017 | \n",
704 | " 2019-08-30 14:50:00+05:30 | \n",
705 | " 270.90 | \n",
706 | " 271.75 | \n",
707 | " 270.85 | \n",
708 | " 271.55 | \n",
709 | "
\n",
710 | " \n",
711 | " 2018 | \n",
712 | " 2019-08-30 14:55:00+05:30 | \n",
713 | " 271.60 | \n",
714 | " 272.45 | \n",
715 | " 271.40 | \n",
716 | " 272.35 | \n",
717 | "
\n",
718 | " \n",
719 | " 2019 | \n",
720 | " 2019-08-30 15:00:00+05:30 | \n",
721 | " 272.45 | \n",
722 | " 273.80 | \n",
723 | " 272.35 | \n",
724 | " 272.85 | \n",
725 | "
\n",
726 | " \n",
727 | " 2020 | \n",
728 | " 2019-08-30 15:05:00+05:30 | \n",
729 | " 272.95 | \n",
730 | " 273.95 | \n",
731 | " 272.45 | \n",
732 | " 273.90 | \n",
733 | "
\n",
734 | " \n",
735 | " 2021 | \n",
736 | " 2019-08-30 15:10:00+05:30 | \n",
737 | " 273.85 | \n",
738 | " 274.50 | \n",
739 | " 273.65 | \n",
740 | " 273.90 | \n",
741 | "
\n",
742 | " \n",
743 | " 2022 | \n",
744 | " 2019-08-30 15:15:00+05:30 | \n",
745 | " 273.95 | \n",
746 | " 274.40 | \n",
747 | " 273.75 | \n",
748 | " 274.35 | \n",
749 | "
\n",
750 | " \n",
751 | " 2023 | \n",
752 | " 2019-08-30 15:20:00+05:30 | \n",
753 | " 274.35 | \n",
754 | " 274.60 | \n",
755 | " 273.85 | \n",
756 | " 274.15 | \n",
757 | "
\n",
758 | " \n",
759 | " 2024 | \n",
760 | " 2019-08-30 15:25:00+05:30 | \n",
761 | " 274.20 | \n",
762 | " 274.25 | \n",
763 | " 273.00 | \n",
764 | " 273.50 | \n",
765 | "
\n",
766 | " \n",
767 | "
\n",
768 | "
2025 rows × 5 columns
\n",
769 | "
"
770 | ],
771 | "text/plain": [
772 | " date open high low close\n",
773 | "0 2019-07-23 09:15:00+05:30 351.20 352.85 350.55 350.70\n",
774 | "1 2019-07-23 09:20:00+05:30 350.80 350.80 348.95 349.05\n",
775 | "2 2019-07-23 09:25:00+05:30 349.00 350.00 348.80 349.20\n",
776 | "3 2019-07-23 09:30:00+05:30 349.10 349.20 347.80 348.20\n",
777 | "4 2019-07-23 09:35:00+05:30 348.15 348.25 347.50 347.85\n",
778 | "5 2019-07-23 09:40:00+05:30 347.80 348.30 347.50 347.50\n",
779 | "6 2019-07-23 09:45:00+05:30 347.50 349.00 347.50 348.90\n",
780 | "7 2019-07-23 09:50:00+05:30 348.95 350.00 348.85 349.30\n",
781 | "8 2019-07-23 09:55:00+05:30 349.30 350.30 349.05 349.85\n",
782 | "9 2019-07-23 10:00:00+05:30 349.85 350.15 349.10 349.10\n",
783 | "10 2019-07-23 10:05:00+05:30 349.10 349.10 348.30 348.75\n",
784 | "11 2019-07-23 10:10:00+05:30 348.75 349.80 348.70 349.80\n",
785 | "12 2019-07-23 10:15:00+05:30 349.70 350.00 349.10 349.20\n",
786 | "13 2019-07-23 10:20:00+05:30 349.20 349.40 348.70 349.05\n",
787 | "14 2019-07-23 10:25:00+05:30 349.00 349.35 348.85 349.00\n",
788 | "15 2019-07-23 10:30:00+05:30 349.15 349.20 348.85 349.05\n",
789 | "16 2019-07-23 10:35:00+05:30 349.05 349.50 348.90 349.40\n",
790 | "17 2019-07-23 10:40:00+05:30 349.40 349.95 349.25 349.85\n",
791 | "18 2019-07-23 10:45:00+05:30 349.90 350.80 349.65 350.45\n",
792 | "19 2019-07-23 10:50:00+05:30 350.45 350.45 349.75 349.85\n",
793 | "20 2019-07-23 10:55:00+05:30 349.80 349.85 349.40 349.70\n",
794 | "21 2019-07-23 11:00:00+05:30 349.70 349.80 349.10 349.30\n",
795 | "22 2019-07-23 11:05:00+05:30 349.20 349.30 348.60 348.60\n",
796 | "23 2019-07-23 11:10:00+05:30 348.60 349.00 348.55 348.80\n",
797 | "24 2019-07-23 11:15:00+05:30 348.80 349.30 348.60 349.15\n",
798 | "25 2019-07-23 11:20:00+05:30 349.15 349.85 348.80 349.75\n",
799 | "26 2019-07-23 11:25:00+05:30 349.75 349.80 349.25 349.30\n",
800 | "27 2019-07-23 11:30:00+05:30 349.25 349.30 348.60 348.60\n",
801 | "28 2019-07-23 11:35:00+05:30 348.65 349.10 348.05 348.10\n",
802 | "29 2019-07-23 11:40:00+05:30 348.10 348.35 347.80 348.10\n",
803 | "... ... ... ... ... ...\n",
804 | "1995 2019-08-30 13:00:00+05:30 267.50 268.30 267.50 268.10\n",
805 | "1996 2019-08-30 13:05:00+05:30 268.15 269.10 267.70 268.70\n",
806 | "1997 2019-08-30 13:10:00+05:30 268.70 269.40 268.30 268.50\n",
807 | "1998 2019-08-30 13:15:00+05:30 268.50 269.30 268.40 268.50\n",
808 | "1999 2019-08-30 13:20:00+05:30 268.50 268.70 267.60 268.05\n",
809 | "2000 2019-08-30 13:25:00+05:30 268.10 268.40 267.70 268.30\n",
810 | "2001 2019-08-30 13:30:00+05:30 268.20 268.40 267.80 268.15\n",
811 | "2002 2019-08-30 13:35:00+05:30 268.10 268.85 268.10 268.35\n",
812 | "2003 2019-08-30 13:40:00+05:30 268.50 269.35 268.50 269.15\n",
813 | "2004 2019-08-30 13:45:00+05:30 269.25 270.00 269.00 269.90\n",
814 | "2005 2019-08-30 13:50:00+05:30 269.80 270.80 269.80 270.35\n",
815 | "2006 2019-08-30 13:55:00+05:30 270.30 270.35 268.80 268.90\n",
816 | "2007 2019-08-30 14:00:00+05:30 268.90 270.00 268.65 269.90\n",
817 | "2008 2019-08-30 14:05:00+05:30 269.95 270.20 269.60 269.75\n",
818 | "2009 2019-08-30 14:10:00+05:30 269.80 270.05 269.50 269.50\n",
819 | "2010 2019-08-30 14:15:00+05:30 269.50 269.55 268.85 269.45\n",
820 | "2011 2019-08-30 14:20:00+05:30 269.40 269.65 268.85 269.25\n",
821 | "2012 2019-08-30 14:25:00+05:30 269.30 270.25 269.15 270.10\n",
822 | "2013 2019-08-30 14:30:00+05:30 270.15 270.55 270.05 270.10\n",
823 | "2014 2019-08-30 14:35:00+05:30 270.20 270.40 269.05 269.40\n",
824 | "2015 2019-08-30 14:40:00+05:30 269.40 270.30 269.40 270.10\n",
825 | "2016 2019-08-30 14:45:00+05:30 270.10 270.95 270.10 270.90\n",
826 | "2017 2019-08-30 14:50:00+05:30 270.90 271.75 270.85 271.55\n",
827 | "2018 2019-08-30 14:55:00+05:30 271.60 272.45 271.40 272.35\n",
828 | "2019 2019-08-30 15:00:00+05:30 272.45 273.80 272.35 272.85\n",
829 | "2020 2019-08-30 15:05:00+05:30 272.95 273.95 272.45 273.90\n",
830 | "2021 2019-08-30 15:10:00+05:30 273.85 274.50 273.65 273.90\n",
831 | "2022 2019-08-30 15:15:00+05:30 273.95 274.40 273.75 274.35\n",
832 | "2023 2019-08-30 15:20:00+05:30 274.35 274.60 273.85 274.15\n",
833 | "2024 2019-08-30 15:25:00+05:30 274.20 274.25 273.00 273.50\n",
834 | "\n",
835 | "[2025 rows x 5 columns]"
836 | ]
837 | },
838 | "metadata": {
839 | "tags": []
840 | },
841 | "execution_count": 10
842 | }
843 | ]
844 | },
845 | {
846 | "cell_type": "code",
847 | "metadata": {
848 | "id": "iTg7nVRONOAm",
849 | "colab_type": "code",
850 | "colab": {},
851 | "outputId": "d5556b4d-242b-4ae3-dfda-526d38bb4094"
852 | },
853 | "source": [
854 | "display(dfw) # data from exchange"
855 | ],
856 | "execution_count": 0,
857 | "outputs": [
858 | {
859 | "output_type": "display_data",
860 | "data": {
861 | "text/html": [
862 | "\n",
863 | "\n",
876 | "
\n",
877 | " \n",
878 | " \n",
879 | " | \n",
880 | " close | \n",
881 | " date | \n",
882 | " high | \n",
883 | " low | \n",
884 | " open | \n",
885 | " volume | \n",
886 | "
\n",
887 | " \n",
888 | " \n",
889 | " \n",
890 | " 0 | \n",
891 | " 350.70 | \n",
892 | " 2019-07-23 09:15:00+05:30 | \n",
893 | " 352.85 | \n",
894 | " 350.55 | \n",
895 | " 351.20 | \n",
896 | " 752547 | \n",
897 | "
\n",
898 | " \n",
899 | " 1 | \n",
900 | " 349.05 | \n",
901 | " 2019-07-23 09:20:00+05:30 | \n",
902 | " 350.80 | \n",
903 | " 348.95 | \n",
904 | " 350.80 | \n",
905 | " 743751 | \n",
906 | "
\n",
907 | " \n",
908 | " 2 | \n",
909 | " 349.20 | \n",
910 | " 2019-07-23 09:25:00+05:30 | \n",
911 | " 350.00 | \n",
912 | " 348.80 | \n",
913 | " 349.00 | \n",
914 | " 594206 | \n",
915 | "
\n",
916 | " \n",
917 | " 3 | \n",
918 | " 348.20 | \n",
919 | " 2019-07-23 09:30:00+05:30 | \n",
920 | " 349.20 | \n",
921 | " 347.80 | \n",
922 | " 349.10 | \n",
923 | " 592144 | \n",
924 | "
\n",
925 | " \n",
926 | " 4 | \n",
927 | " 347.85 | \n",
928 | " 2019-07-23 09:35:00+05:30 | \n",
929 | " 348.25 | \n",
930 | " 347.50 | \n",
931 | " 348.15 | \n",
932 | " 503455 | \n",
933 | "
\n",
934 | " \n",
935 | " 5 | \n",
936 | " 347.50 | \n",
937 | " 2019-07-23 09:40:00+05:30 | \n",
938 | " 348.30 | \n",
939 | " 347.50 | \n",
940 | " 347.80 | \n",
941 | " 411564 | \n",
942 | "
\n",
943 | " \n",
944 | " 6 | \n",
945 | " 348.90 | \n",
946 | " 2019-07-23 09:45:00+05:30 | \n",
947 | " 349.00 | \n",
948 | " 347.50 | \n",
949 | " 347.50 | \n",
950 | " 384230 | \n",
951 | "
\n",
952 | " \n",
953 | " 7 | \n",
954 | " 349.30 | \n",
955 | " 2019-07-23 09:50:00+05:30 | \n",
956 | " 350.00 | \n",
957 | " 348.85 | \n",
958 | " 348.95 | \n",
959 | " 387064 | \n",
960 | "
\n",
961 | " \n",
962 | " 8 | \n",
963 | " 349.85 | \n",
964 | " 2019-07-23 09:55:00+05:30 | \n",
965 | " 350.30 | \n",
966 | " 349.05 | \n",
967 | " 349.30 | \n",
968 | " 337632 | \n",
969 | "
\n",
970 | " \n",
971 | " 9 | \n",
972 | " 349.10 | \n",
973 | " 2019-07-23 10:00:00+05:30 | \n",
974 | " 350.15 | \n",
975 | " 349.10 | \n",
976 | " 349.85 | \n",
977 | " 257093 | \n",
978 | "
\n",
979 | " \n",
980 | " 10 | \n",
981 | " 348.75 | \n",
982 | " 2019-07-23 10:05:00+05:30 | \n",
983 | " 349.10 | \n",
984 | " 348.30 | \n",
985 | " 349.10 | \n",
986 | " 396477 | \n",
987 | "
\n",
988 | " \n",
989 | " 11 | \n",
990 | " 349.80 | \n",
991 | " 2019-07-23 10:10:00+05:30 | \n",
992 | " 349.80 | \n",
993 | " 348.70 | \n",
994 | " 348.75 | \n",
995 | " 226859 | \n",
996 | "
\n",
997 | " \n",
998 | " 12 | \n",
999 | " 349.20 | \n",
1000 | " 2019-07-23 10:15:00+05:30 | \n",
1001 | " 350.00 | \n",
1002 | " 349.10 | \n",
1003 | " 349.70 | \n",
1004 | " 215059 | \n",
1005 | "
\n",
1006 | " \n",
1007 | " 13 | \n",
1008 | " 349.05 | \n",
1009 | " 2019-07-23 10:20:00+05:30 | \n",
1010 | " 349.40 | \n",
1011 | " 348.70 | \n",
1012 | " 349.20 | \n",
1013 | " 207552 | \n",
1014 | "
\n",
1015 | " \n",
1016 | " 14 | \n",
1017 | " 349.00 | \n",
1018 | " 2019-07-23 10:25:00+05:30 | \n",
1019 | " 349.35 | \n",
1020 | " 348.85 | \n",
1021 | " 349.00 | \n",
1022 | " 147200 | \n",
1023 | "
\n",
1024 | " \n",
1025 | " 15 | \n",
1026 | " 349.05 | \n",
1027 | " 2019-07-23 10:30:00+05:30 | \n",
1028 | " 349.20 | \n",
1029 | " 348.85 | \n",
1030 | " 349.15 | \n",
1031 | " 148228 | \n",
1032 | "
\n",
1033 | " \n",
1034 | " 16 | \n",
1035 | " 349.40 | \n",
1036 | " 2019-07-23 10:35:00+05:30 | \n",
1037 | " 349.50 | \n",
1038 | " 348.90 | \n",
1039 | " 349.05 | \n",
1040 | " 100049 | \n",
1041 | "
\n",
1042 | " \n",
1043 | " 17 | \n",
1044 | " 349.85 | \n",
1045 | " 2019-07-23 10:40:00+05:30 | \n",
1046 | " 349.95 | \n",
1047 | " 349.25 | \n",
1048 | " 349.40 | \n",
1049 | " 234573 | \n",
1050 | "
\n",
1051 | " \n",
1052 | " 18 | \n",
1053 | " 350.45 | \n",
1054 | " 2019-07-23 10:45:00+05:30 | \n",
1055 | " 350.80 | \n",
1056 | " 349.65 | \n",
1057 | " 349.90 | \n",
1058 | " 294020 | \n",
1059 | "
\n",
1060 | " \n",
1061 | " 19 | \n",
1062 | " 349.85 | \n",
1063 | " 2019-07-23 10:50:00+05:30 | \n",
1064 | " 350.45 | \n",
1065 | " 349.75 | \n",
1066 | " 350.45 | \n",
1067 | " 130359 | \n",
1068 | "
\n",
1069 | " \n",
1070 | " 20 | \n",
1071 | " 349.70 | \n",
1072 | " 2019-07-23 10:55:00+05:30 | \n",
1073 | " 349.85 | \n",
1074 | " 349.40 | \n",
1075 | " 349.80 | \n",
1076 | " 208711 | \n",
1077 | "
\n",
1078 | " \n",
1079 | " 21 | \n",
1080 | " 349.30 | \n",
1081 | " 2019-07-23 11:00:00+05:30 | \n",
1082 | " 349.80 | \n",
1083 | " 349.10 | \n",
1084 | " 349.70 | \n",
1085 | " 106642 | \n",
1086 | "
\n",
1087 | " \n",
1088 | " 22 | \n",
1089 | " 348.60 | \n",
1090 | " 2019-07-23 11:05:00+05:30 | \n",
1091 | " 349.30 | \n",
1092 | " 348.60 | \n",
1093 | " 349.20 | \n",
1094 | " 273213 | \n",
1095 | "
\n",
1096 | " \n",
1097 | " 23 | \n",
1098 | " 348.80 | \n",
1099 | " 2019-07-23 11:10:00+05:30 | \n",
1100 | " 349.00 | \n",
1101 | " 348.55 | \n",
1102 | " 348.60 | \n",
1103 | " 239866 | \n",
1104 | "
\n",
1105 | " \n",
1106 | " 24 | \n",
1107 | " 349.15 | \n",
1108 | " 2019-07-23 11:15:00+05:30 | \n",
1109 | " 349.30 | \n",
1110 | " 348.60 | \n",
1111 | " 348.80 | \n",
1112 | " 182088 | \n",
1113 | "
\n",
1114 | " \n",
1115 | " 25 | \n",
1116 | " 349.75 | \n",
1117 | " 2019-07-23 11:20:00+05:30 | \n",
1118 | " 349.85 | \n",
1119 | " 348.80 | \n",
1120 | " 349.15 | \n",
1121 | " 206997 | \n",
1122 | "
\n",
1123 | " \n",
1124 | " 26 | \n",
1125 | " 349.30 | \n",
1126 | " 2019-07-23 11:25:00+05:30 | \n",
1127 | " 349.80 | \n",
1128 | " 349.25 | \n",
1129 | " 349.75 | \n",
1130 | " 242315 | \n",
1131 | "
\n",
1132 | " \n",
1133 | " 27 | \n",
1134 | " 348.60 | \n",
1135 | " 2019-07-23 11:30:00+05:30 | \n",
1136 | " 349.30 | \n",
1137 | " 348.60 | \n",
1138 | " 349.25 | \n",
1139 | " 148363 | \n",
1140 | "
\n",
1141 | " \n",
1142 | " 28 | \n",
1143 | " 348.10 | \n",
1144 | " 2019-07-23 11:35:00+05:30 | \n",
1145 | " 349.10 | \n",
1146 | " 348.05 | \n",
1147 | " 348.65 | \n",
1148 | " 215921 | \n",
1149 | "
\n",
1150 | " \n",
1151 | " 29 | \n",
1152 | " 348.10 | \n",
1153 | " 2019-07-23 11:40:00+05:30 | \n",
1154 | " 348.35 | \n",
1155 | " 347.80 | \n",
1156 | " 348.10 | \n",
1157 | " 340481 | \n",
1158 | "
\n",
1159 | " \n",
1160 | " ... | \n",
1161 | " ... | \n",
1162 | " ... | \n",
1163 | " ... | \n",
1164 | " ... | \n",
1165 | " ... | \n",
1166 | " ... | \n",
1167 | "
\n",
1168 | " \n",
1169 | " 1995 | \n",
1170 | " 268.10 | \n",
1171 | " 2019-08-30 13:00:00+05:30 | \n",
1172 | " 268.30 | \n",
1173 | " 267.50 | \n",
1174 | " 267.50 | \n",
1175 | " 409983 | \n",
1176 | "
\n",
1177 | " \n",
1178 | " 1996 | \n",
1179 | " 268.70 | \n",
1180 | " 2019-08-30 13:05:00+05:30 | \n",
1181 | " 269.10 | \n",
1182 | " 267.70 | \n",
1183 | " 268.15 | \n",
1184 | " 548218 | \n",
1185 | "
\n",
1186 | " \n",
1187 | " 1997 | \n",
1188 | " 268.50 | \n",
1189 | " 2019-08-30 13:10:00+05:30 | \n",
1190 | " 269.40 | \n",
1191 | " 268.30 | \n",
1192 | " 268.70 | \n",
1193 | " 564742 | \n",
1194 | "
\n",
1195 | " \n",
1196 | " 1998 | \n",
1197 | " 268.50 | \n",
1198 | " 2019-08-30 13:15:00+05:30 | \n",
1199 | " 269.30 | \n",
1200 | " 268.40 | \n",
1201 | " 268.50 | \n",
1202 | " 439667 | \n",
1203 | "
\n",
1204 | " \n",
1205 | " 1999 | \n",
1206 | " 268.05 | \n",
1207 | " 2019-08-30 13:20:00+05:30 | \n",
1208 | " 268.70 | \n",
1209 | " 267.60 | \n",
1210 | " 268.50 | \n",
1211 | " 348325 | \n",
1212 | "
\n",
1213 | " \n",
1214 | " 2000 | \n",
1215 | " 268.30 | \n",
1216 | " 2019-08-30 13:25:00+05:30 | \n",
1217 | " 268.40 | \n",
1218 | " 267.70 | \n",
1219 | " 268.10 | \n",
1220 | " 225214 | \n",
1221 | "
\n",
1222 | " \n",
1223 | " 2001 | \n",
1224 | " 268.15 | \n",
1225 | " 2019-08-30 13:30:00+05:30 | \n",
1226 | " 268.40 | \n",
1227 | " 267.80 | \n",
1228 | " 268.20 | \n",
1229 | " 231371 | \n",
1230 | "
\n",
1231 | " \n",
1232 | " 2002 | \n",
1233 | " 268.35 | \n",
1234 | " 2019-08-30 13:35:00+05:30 | \n",
1235 | " 268.85 | \n",
1236 | " 268.10 | \n",
1237 | " 268.10 | \n",
1238 | " 398516 | \n",
1239 | "
\n",
1240 | " \n",
1241 | " 2003 | \n",
1242 | " 269.15 | \n",
1243 | " 2019-08-30 13:40:00+05:30 | \n",
1244 | " 269.35 | \n",
1245 | " 268.50 | \n",
1246 | " 268.50 | \n",
1247 | " 528277 | \n",
1248 | "
\n",
1249 | " \n",
1250 | " 2004 | \n",
1251 | " 269.90 | \n",
1252 | " 2019-08-30 13:45:00+05:30 | \n",
1253 | " 270.00 | \n",
1254 | " 269.00 | \n",
1255 | " 269.25 | \n",
1256 | " 1166120 | \n",
1257 | "
\n",
1258 | " \n",
1259 | " 2005 | \n",
1260 | " 270.35 | \n",
1261 | " 2019-08-30 13:50:00+05:30 | \n",
1262 | " 270.80 | \n",
1263 | " 269.80 | \n",
1264 | " 269.80 | \n",
1265 | " 1218472 | \n",
1266 | "
\n",
1267 | " \n",
1268 | " 2006 | \n",
1269 | " 268.90 | \n",
1270 | " 2019-08-30 13:55:00+05:30 | \n",
1271 | " 270.35 | \n",
1272 | " 268.80 | \n",
1273 | " 270.30 | \n",
1274 | " 605796 | \n",
1275 | "
\n",
1276 | " \n",
1277 | " 2007 | \n",
1278 | " 269.90 | \n",
1279 | " 2019-08-30 14:00:00+05:30 | \n",
1280 | " 270.00 | \n",
1281 | " 268.65 | \n",
1282 | " 268.90 | \n",
1283 | " 608792 | \n",
1284 | "
\n",
1285 | " \n",
1286 | " 2008 | \n",
1287 | " 269.75 | \n",
1288 | " 2019-08-30 14:05:00+05:30 | \n",
1289 | " 270.20 | \n",
1290 | " 269.60 | \n",
1291 | " 269.95 | \n",
1292 | " 401145 | \n",
1293 | "
\n",
1294 | " \n",
1295 | " 2009 | \n",
1296 | " 269.50 | \n",
1297 | " 2019-08-30 14:10:00+05:30 | \n",
1298 | " 270.05 | \n",
1299 | " 269.50 | \n",
1300 | " 269.80 | \n",
1301 | " 492073 | \n",
1302 | "
\n",
1303 | " \n",
1304 | " 2010 | \n",
1305 | " 269.45 | \n",
1306 | " 2019-08-30 14:15:00+05:30 | \n",
1307 | " 269.55 | \n",
1308 | " 268.85 | \n",
1309 | " 269.50 | \n",
1310 | " 498125 | \n",
1311 | "
\n",
1312 | " \n",
1313 | " 2011 | \n",
1314 | " 269.25 | \n",
1315 | " 2019-08-30 14:20:00+05:30 | \n",
1316 | " 269.65 | \n",
1317 | " 268.85 | \n",
1318 | " 269.40 | \n",
1319 | " 302189 | \n",
1320 | "
\n",
1321 | " \n",
1322 | " 2012 | \n",
1323 | " 270.10 | \n",
1324 | " 2019-08-30 14:25:00+05:30 | \n",
1325 | " 270.25 | \n",
1326 | " 269.15 | \n",
1327 | " 269.30 | \n",
1328 | " 624969 | \n",
1329 | "
\n",
1330 | " \n",
1331 | " 2013 | \n",
1332 | " 270.10 | \n",
1333 | " 2019-08-30 14:30:00+05:30 | \n",
1334 | " 270.55 | \n",
1335 | " 270.05 | \n",
1336 | " 270.15 | \n",
1337 | " 605022 | \n",
1338 | "
\n",
1339 | " \n",
1340 | " 2014 | \n",
1341 | " 269.40 | \n",
1342 | " 2019-08-30 14:35:00+05:30 | \n",
1343 | " 270.40 | \n",
1344 | " 269.05 | \n",
1345 | " 270.20 | \n",
1346 | " 672557 | \n",
1347 | "
\n",
1348 | " \n",
1349 | " 2015 | \n",
1350 | " 270.10 | \n",
1351 | " 2019-08-30 14:40:00+05:30 | \n",
1352 | " 270.30 | \n",
1353 | " 269.40 | \n",
1354 | " 269.40 | \n",
1355 | " 441850 | \n",
1356 | "
\n",
1357 | " \n",
1358 | " 2016 | \n",
1359 | " 270.90 | \n",
1360 | " 2019-08-30 14:45:00+05:30 | \n",
1361 | " 270.95 | \n",
1362 | " 270.10 | \n",
1363 | " 270.10 | \n",
1364 | " 646640 | \n",
1365 | "
\n",
1366 | " \n",
1367 | " 2017 | \n",
1368 | " 271.55 | \n",
1369 | " 2019-08-30 14:50:00+05:30 | \n",
1370 | " 271.75 | \n",
1371 | " 270.85 | \n",
1372 | " 270.90 | \n",
1373 | " 960641 | \n",
1374 | "
\n",
1375 | " \n",
1376 | " 2018 | \n",
1377 | " 272.35 | \n",
1378 | " 2019-08-30 14:55:00+05:30 | \n",
1379 | " 272.45 | \n",
1380 | " 271.40 | \n",
1381 | " 271.60 | \n",
1382 | " 805946 | \n",
1383 | "
\n",
1384 | " \n",
1385 | " 2019 | \n",
1386 | " 272.85 | \n",
1387 | " 2019-08-30 15:00:00+05:30 | \n",
1388 | " 273.80 | \n",
1389 | " 272.35 | \n",
1390 | " 272.45 | \n",
1391 | " 1830246 | \n",
1392 | "
\n",
1393 | " \n",
1394 | " 2020 | \n",
1395 | " 273.90 | \n",
1396 | " 2019-08-30 15:05:00+05:30 | \n",
1397 | " 273.95 | \n",
1398 | " 272.45 | \n",
1399 | " 272.95 | \n",
1400 | " 1481904 | \n",
1401 | "
\n",
1402 | " \n",
1403 | " 2021 | \n",
1404 | " 273.90 | \n",
1405 | " 2019-08-30 15:10:00+05:30 | \n",
1406 | " 274.50 | \n",
1407 | " 273.65 | \n",
1408 | " 273.85 | \n",
1409 | " 1642681 | \n",
1410 | "
\n",
1411 | " \n",
1412 | " 2022 | \n",
1413 | " 274.35 | \n",
1414 | " 2019-08-30 15:15:00+05:30 | \n",
1415 | " 274.40 | \n",
1416 | " 273.75 | \n",
1417 | " 273.95 | \n",
1418 | " 1680898 | \n",
1419 | "
\n",
1420 | " \n",
1421 | " 2023 | \n",
1422 | " 274.15 | \n",
1423 | " 2019-08-30 15:20:00+05:30 | \n",
1424 | " 274.60 | \n",
1425 | " 273.85 | \n",
1426 | " 274.35 | \n",
1427 | " 1607910 | \n",
1428 | "
\n",
1429 | " \n",
1430 | " 2024 | \n",
1431 | " 273.50 | \n",
1432 | " 2019-08-30 15:25:00+05:30 | \n",
1433 | " 274.25 | \n",
1434 | " 273.00 | \n",
1435 | " 274.20 | \n",
1436 | " 591154 | \n",
1437 | "
\n",
1438 | " \n",
1439 | "
\n",
1440 | "
2025 rows × 6 columns
\n",
1441 | "
"
1442 | ],
1443 | "text/plain": [
1444 | " close date high low open volume\n",
1445 | "0 350.70 2019-07-23 09:15:00+05:30 352.85 350.55 351.20 752547\n",
1446 | "1 349.05 2019-07-23 09:20:00+05:30 350.80 348.95 350.80 743751\n",
1447 | "2 349.20 2019-07-23 09:25:00+05:30 350.00 348.80 349.00 594206\n",
1448 | "3 348.20 2019-07-23 09:30:00+05:30 349.20 347.80 349.10 592144\n",
1449 | "4 347.85 2019-07-23 09:35:00+05:30 348.25 347.50 348.15 503455\n",
1450 | "5 347.50 2019-07-23 09:40:00+05:30 348.30 347.50 347.80 411564\n",
1451 | "6 348.90 2019-07-23 09:45:00+05:30 349.00 347.50 347.50 384230\n",
1452 | "7 349.30 2019-07-23 09:50:00+05:30 350.00 348.85 348.95 387064\n",
1453 | "8 349.85 2019-07-23 09:55:00+05:30 350.30 349.05 349.30 337632\n",
1454 | "9 349.10 2019-07-23 10:00:00+05:30 350.15 349.10 349.85 257093\n",
1455 | "10 348.75 2019-07-23 10:05:00+05:30 349.10 348.30 349.10 396477\n",
1456 | "11 349.80 2019-07-23 10:10:00+05:30 349.80 348.70 348.75 226859\n",
1457 | "12 349.20 2019-07-23 10:15:00+05:30 350.00 349.10 349.70 215059\n",
1458 | "13 349.05 2019-07-23 10:20:00+05:30 349.40 348.70 349.20 207552\n",
1459 | "14 349.00 2019-07-23 10:25:00+05:30 349.35 348.85 349.00 147200\n",
1460 | "15 349.05 2019-07-23 10:30:00+05:30 349.20 348.85 349.15 148228\n",
1461 | "16 349.40 2019-07-23 10:35:00+05:30 349.50 348.90 349.05 100049\n",
1462 | "17 349.85 2019-07-23 10:40:00+05:30 349.95 349.25 349.40 234573\n",
1463 | "18 350.45 2019-07-23 10:45:00+05:30 350.80 349.65 349.90 294020\n",
1464 | "19 349.85 2019-07-23 10:50:00+05:30 350.45 349.75 350.45 130359\n",
1465 | "20 349.70 2019-07-23 10:55:00+05:30 349.85 349.40 349.80 208711\n",
1466 | "21 349.30 2019-07-23 11:00:00+05:30 349.80 349.10 349.70 106642\n",
1467 | "22 348.60 2019-07-23 11:05:00+05:30 349.30 348.60 349.20 273213\n",
1468 | "23 348.80 2019-07-23 11:10:00+05:30 349.00 348.55 348.60 239866\n",
1469 | "24 349.15 2019-07-23 11:15:00+05:30 349.30 348.60 348.80 182088\n",
1470 | "25 349.75 2019-07-23 11:20:00+05:30 349.85 348.80 349.15 206997\n",
1471 | "26 349.30 2019-07-23 11:25:00+05:30 349.80 349.25 349.75 242315\n",
1472 | "27 348.60 2019-07-23 11:30:00+05:30 349.30 348.60 349.25 148363\n",
1473 | "28 348.10 2019-07-23 11:35:00+05:30 349.10 348.05 348.65 215921\n",
1474 | "29 348.10 2019-07-23 11:40:00+05:30 348.35 347.80 348.10 340481\n",
1475 | "... ... ... ... ... ... ...\n",
1476 | "1995 268.10 2019-08-30 13:00:00+05:30 268.30 267.50 267.50 409983\n",
1477 | "1996 268.70 2019-08-30 13:05:00+05:30 269.10 267.70 268.15 548218\n",
1478 | "1997 268.50 2019-08-30 13:10:00+05:30 269.40 268.30 268.70 564742\n",
1479 | "1998 268.50 2019-08-30 13:15:00+05:30 269.30 268.40 268.50 439667\n",
1480 | "1999 268.05 2019-08-30 13:20:00+05:30 268.70 267.60 268.50 348325\n",
1481 | "2000 268.30 2019-08-30 13:25:00+05:30 268.40 267.70 268.10 225214\n",
1482 | "2001 268.15 2019-08-30 13:30:00+05:30 268.40 267.80 268.20 231371\n",
1483 | "2002 268.35 2019-08-30 13:35:00+05:30 268.85 268.10 268.10 398516\n",
1484 | "2003 269.15 2019-08-30 13:40:00+05:30 269.35 268.50 268.50 528277\n",
1485 | "2004 269.90 2019-08-30 13:45:00+05:30 270.00 269.00 269.25 1166120\n",
1486 | "2005 270.35 2019-08-30 13:50:00+05:30 270.80 269.80 269.80 1218472\n",
1487 | "2006 268.90 2019-08-30 13:55:00+05:30 270.35 268.80 270.30 605796\n",
1488 | "2007 269.90 2019-08-30 14:00:00+05:30 270.00 268.65 268.90 608792\n",
1489 | "2008 269.75 2019-08-30 14:05:00+05:30 270.20 269.60 269.95 401145\n",
1490 | "2009 269.50 2019-08-30 14:10:00+05:30 270.05 269.50 269.80 492073\n",
1491 | "2010 269.45 2019-08-30 14:15:00+05:30 269.55 268.85 269.50 498125\n",
1492 | "2011 269.25 2019-08-30 14:20:00+05:30 269.65 268.85 269.40 302189\n",
1493 | "2012 270.10 2019-08-30 14:25:00+05:30 270.25 269.15 269.30 624969\n",
1494 | "2013 270.10 2019-08-30 14:30:00+05:30 270.55 270.05 270.15 605022\n",
1495 | "2014 269.40 2019-08-30 14:35:00+05:30 270.40 269.05 270.20 672557\n",
1496 | "2015 270.10 2019-08-30 14:40:00+05:30 270.30 269.40 269.40 441850\n",
1497 | "2016 270.90 2019-08-30 14:45:00+05:30 270.95 270.10 270.10 646640\n",
1498 | "2017 271.55 2019-08-30 14:50:00+05:30 271.75 270.85 270.90 960641\n",
1499 | "2018 272.35 2019-08-30 14:55:00+05:30 272.45 271.40 271.60 805946\n",
1500 | "2019 272.85 2019-08-30 15:00:00+05:30 273.80 272.35 272.45 1830246\n",
1501 | "2020 273.90 2019-08-30 15:05:00+05:30 273.95 272.45 272.95 1481904\n",
1502 | "2021 273.90 2019-08-30 15:10:00+05:30 274.50 273.65 273.85 1642681\n",
1503 | "2022 274.35 2019-08-30 15:15:00+05:30 274.40 273.75 273.95 1680898\n",
1504 | "2023 274.15 2019-08-30 15:20:00+05:30 274.60 273.85 274.35 1607910\n",
1505 | "2024 273.50 2019-08-30 15:25:00+05:30 274.25 273.00 274.20 591154\n",
1506 | "\n",
1507 | "[2025 rows x 6 columns]"
1508 | ]
1509 | },
1510 | "metadata": {
1511 | "tags": []
1512 | }
1513 | }
1514 | ]
1515 | }
1516 | ]
1517 | }
--------------------------------------------------------------------------------
/change time frame.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import pandas as pd\n",
10 | "import numpy as np\n",
11 | "import time\n",
12 | "import math"
13 | ]
14 | },
15 | {
16 | "cell_type": "code",
17 | "execution_count": 3,
18 | "metadata": {},
19 | "outputs": [],
20 | "source": [
21 | "\n",
22 | "def minute_convertion(timeframe,data):\n",
23 | " \n",
24 | " i=0\n",
25 | " value1,value1_index,value2,value2_index =0,0,0,0\n",
26 | " count=0\n",
27 | " new_list =[]\n",
28 | " df = data\n",
29 | " day_change_index = []\n",
30 | " \n",
31 | " for i,x in enumerate(data.date):\n",
32 | " if x[11:] == \"00:01:00.000Z\":\n",
33 | " day_change_index.append(i)\n",
34 | "\n",
35 | " for j in day_change_index:\n",
36 | " d=math.ceil((j-i)/timeframe)\n",
37 | " count =0\n",
38 | "# print(j)\n",
39 | " \n",
40 | " for k in range(i,j+1,timeframe):\n",
41 | " count=count+1\n",
42 | "# print(k)\n",
43 | " \n",
44 | " if count!=d: \n",
45 | " m=df.loc[k,\"date\"]\n",
46 | " n=df.loc[k+(timeframe)-1,\"date\"]\n",
47 | "# print(m,n)\n",
48 | " \n",
49 | " for p,q in enumerate(df.date):\n",
50 | " if q == m:\n",
51 | " value1 = df.loc[p]\n",
52 | " value1_index=p\n",
53 | " break\n",
54 | " \n",
55 | " for p,q in enumerate(df.date):\n",
56 | " if q == n:\n",
57 | " value2 = df.loc[p]\n",
58 | " value2_index = p\n",
59 | " break\n",
60 | " \n",
61 | " new_list.append([n,df.loc[value1_index,\"open\"],max(df.loc[value1_index:value2_index,\"high\"])\n",
62 | " ,min(df.loc[value1_index:value2_index,\"low\"]),df.loc[value2_index,\"close\"],\n",
63 | " sum(df.loc[value1_index:value2_index,\"volume\"])])\n",
64 | "\n",
65 | " else:\n",
66 | " m=df.loc[k,\"date\"]\n",
67 | " n=df.loc[j-1,\"date\"]\n",
68 | " \n",
69 | " for p,q in enumerate(df.date):\n",
70 | " if q == m:\n",
71 | " value1 = df.loc[p]\n",
72 | " value1_index=p\n",
73 | "# print(value1)\n",
74 | " break\n",
75 | " \n",
76 | " for p,q in enumerate(df.date):\n",
77 | " if q == n:\n",
78 | " value2 = df.loc[p]\n",
79 | " value2_index = p\n",
80 | "# print(value2)\n",
81 | " break\n",
82 | " \n",
83 | " new_list.append([n,df.loc[value1_index,\"open\"],max(df.loc[value1_index:value2_index,\"high\"])\n",
84 | " ,min(df.loc[value1_index:value2_index,\"low\"]),df.loc[value2_index,\"close\"],\n",
85 | " sum(df.loc[value1_index:value2_index,\"volume\"])])\n",
86 | " i=j\n",
87 | " \n",
88 | " new_data = pd.DataFrame(new_list,columns = [\"date\",\"open\",\"high\",\"low\",\"close\",\"volume\"],index= None)\n",
89 | " return new_data"
90 | ]
91 | },
92 | {
93 | "cell_type": "code",
94 | "execution_count": 5,
95 | "metadata": {
96 | "scrolled": true
97 | },
98 | "outputs": [],
99 | "source": [
100 | "a = minute_convertion(72,new_df)\n",
101 | "a = a.drop_duplicates()\n",
102 | "a = a.reset_index(drop=True)\n",
103 | "b = HA(new_df)\n",
104 | "b"
105 | ]
106 | }
107 | ],
108 | "metadata": {
109 | "kernelspec": {
110 | "display_name": "Python 3",
111 | "language": "python",
112 | "name": "python3"
113 | },
114 | "language_info": {
115 | "codemirror_mode": {
116 | "name": "ipython",
117 | "version": 3
118 | },
119 | "file_extension": ".py",
120 | "mimetype": "text/x-python",
121 | "name": "python",
122 | "nbconvert_exporter": "python",
123 | "pygments_lexer": "ipython3",
124 | "version": "3.7.3"
125 | }
126 | },
127 | "nbformat": 4,
128 | "nbformat_minor": 2
129 | }
130 |
--------------------------------------------------------------------------------
/conversion code of Candles to hikenashi.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 18,
6 | "metadata": {},
7 | "outputs": [
8 | {
9 | "data": {
10 | "text/html": [
11 | "\n",
12 | "\n",
25 | "
\n",
26 | " \n",
27 | " \n",
28 | " | \n",
29 | " date | \n",
30 | " open | \n",
31 | " high | \n",
32 | " low | \n",
33 | " close | \n",
34 | " volume | \n",
35 | "
\n",
36 | " \n",
37 | " \n",
38 | " \n",
39 | " 0 | \n",
40 | " 2018-04-12 09:15:00+05:30 | \n",
41 | " 295.00 | \n",
42 | " 295.75 | \n",
43 | " 293.25 | \n",
44 | " 293.80 | \n",
45 | " 55378 | \n",
46 | "
\n",
47 | " \n",
48 | " 1 | \n",
49 | " 2018-04-12 09:20:00+05:30 | \n",
50 | " 293.75 | \n",
51 | " 293.75 | \n",
52 | " 292.55 | \n",
53 | " 292.95 | \n",
54 | " 32219 | \n",
55 | "
\n",
56 | " \n",
57 | " 2 | \n",
58 | " 2018-04-12 09:25:00+05:30 | \n",
59 | " 292.95 | \n",
60 | " 293.40 | \n",
61 | " 292.65 | \n",
62 | " 292.80 | \n",
63 | " 23643 | \n",
64 | "
\n",
65 | " \n",
66 | " 3 | \n",
67 | " 2018-04-12 09:30:00+05:30 | \n",
68 | " 292.80 | \n",
69 | " 293.00 | \n",
70 | " 292.75 | \n",
71 | " 292.80 | \n",
72 | " 12313 | \n",
73 | "
\n",
74 | " \n",
75 | " 4 | \n",
76 | " 2018-04-12 09:35:00+05:30 | \n",
77 | " 292.75 | \n",
78 | " 292.85 | \n",
79 | " 291.50 | \n",
80 | " 291.55 | \n",
81 | " 32198 | \n",
82 | "
\n",
83 | " \n",
84 | "
\n",
85 | "
"
86 | ],
87 | "text/plain": [
88 | " date open high low close volume\n",
89 | "0 2018-04-12 09:15:00+05:30 295.00 295.75 293.25 293.80 55378\n",
90 | "1 2018-04-12 09:20:00+05:30 293.75 293.75 292.55 292.95 32219\n",
91 | "2 2018-04-12 09:25:00+05:30 292.95 293.40 292.65 292.80 23643\n",
92 | "3 2018-04-12 09:30:00+05:30 292.80 293.00 292.75 292.80 12313\n",
93 | "4 2018-04-12 09:35:00+05:30 292.75 292.85 291.50 291.55 32198"
94 | ]
95 | },
96 | "execution_count": 18,
97 | "metadata": {},
98 | "output_type": "execute_result"
99 | }
100 | ],
101 | "source": [
102 | "import pandas as pd\n",
103 | "import numpy as np\n",
104 | "import time\n",
105 | "import math\n",
106 | "new_df = pd.read_csv(\"data.csv\")\n",
107 | "new_df = new_df[['date','open','high','low','close','volume']]\n",
108 | "new_df.head()"
109 | ]
110 | },
111 | {
112 | "cell_type": "code",
113 | "execution_count": 19,
114 | "metadata": {},
115 | "outputs": [
116 | {
117 | "data": {
118 | "text/html": [
119 | "\n",
120 | "\n",
133 | "
\n",
134 | " \n",
135 | " \n",
136 | " | \n",
137 | " date | \n",
138 | " open | \n",
139 | " high | \n",
140 | " low | \n",
141 | " close | \n",
142 | " volume | \n",
143 | "
\n",
144 | " \n",
145 | " \n",
146 | " \n",
147 | " 0 | \n",
148 | " 2018-04-12 09:15:00+05:30 | \n",
149 | " 294.4 | \n",
150 | " 295.8 | \n",
151 | " 293.2 | \n",
152 | " 294.4 | \n",
153 | " 55378 | \n",
154 | "
\n",
155 | " \n",
156 | " 1 | \n",
157 | " 2018-04-12 09:20:00+05:30 | \n",
158 | " 294.4 | \n",
159 | " 294.4 | \n",
160 | " 292.6 | \n",
161 | " 293.2 | \n",
162 | " 32219 | \n",
163 | "
\n",
164 | " \n",
165 | " 2 | \n",
166 | " 2018-04-12 09:25:00+05:30 | \n",
167 | " 293.8 | \n",
168 | " 293.8 | \n",
169 | " 292.6 | \n",
170 | " 293.0 | \n",
171 | " 23643 | \n",
172 | "
\n",
173 | " \n",
174 | " 3 | \n",
175 | " 2018-04-12 09:30:00+05:30 | \n",
176 | " 293.4 | \n",
177 | " 293.4 | \n",
178 | " 292.8 | \n",
179 | " 292.8 | \n",
180 | " 12313 | \n",
181 | "
\n",
182 | " \n",
183 | " 4 | \n",
184 | " 2018-04-12 09:35:00+05:30 | \n",
185 | " 293.1 | \n",
186 | " 293.1 | \n",
187 | " 291.5 | \n",
188 | " 292.2 | \n",
189 | " 32198 | \n",
190 | "
\n",
191 | " \n",
192 | "
\n",
193 | "
"
194 | ],
195 | "text/plain": [
196 | " date open high low close volume\n",
197 | "0 2018-04-12 09:15:00+05:30 294.4 295.8 293.2 294.4 55378\n",
198 | "1 2018-04-12 09:20:00+05:30 294.4 294.4 292.6 293.2 32219\n",
199 | "2 2018-04-12 09:25:00+05:30 293.8 293.8 292.6 293.0 23643\n",
200 | "3 2018-04-12 09:30:00+05:30 293.4 293.4 292.8 292.8 12313\n",
201 | "4 2018-04-12 09:35:00+05:30 293.1 293.1 291.5 292.2 32198"
202 | ]
203 | },
204 | "execution_count": 19,
205 | "metadata": {},
206 | "output_type": "execute_result"
207 | }
208 | ],
209 | "source": [
210 | "def HA(df, ohlc=['open', 'high', 'low', 'close']):\n",
211 | " ha_open = 'HA_' + ohlc[0]\n",
212 | " ha_high = 'HA_' + ohlc[1]\n",
213 | " ha_low = 'HA_' + ohlc[2]\n",
214 | " ha_close = 'HA_' + ohlc[3]\n",
215 | "\n",
216 | " df[ha_open] = 0.0000\n",
217 | " df[ha_high] = 0.0000\n",
218 | " df[ha_low] = 0.0000\n",
219 | " df[ha_close] = 0.0000\n",
220 | " \n",
221 | " df[ha_close] = (df[ohlc[0]] + df[ohlc[1]] + df[ohlc[2]] + df[ohlc[3]]) / 4\n",
222 | "\n",
223 | " df[ha_open] = 0.00\n",
224 | " for i in range(0, len(df)):\n",
225 | " if i == 0:\n",
226 | " df[ha_open].iat[i] = (df[ohlc[0]].iat[i] + df[ohlc[3]].iat[i]) / 2\n",
227 | " else:\n",
228 | " df[ha_open].iat[i] = (df[ha_open].iat[i - 1] + df[ha_close].iat[i - 1]) / 2\n",
229 | " \n",
230 | " df[ha_high]=df[[ha_open, ha_close, ohlc[1]]].max(axis=1)\n",
231 | " df[ha_low]=df[[ha_open, ha_close, ohlc[2]]].min(axis=1)\n",
232 | " \n",
233 | " del df['open']\n",
234 | " del df['high']\n",
235 | " del df['low']\n",
236 | " del df['close']\n",
237 | " \n",
238 | " df['open'] = df['HA_open']\n",
239 | " df['high'] = df['HA_high']\n",
240 | " df['low'] = df['HA_low']\n",
241 | " df['close'] = df['HA_close']\n",
242 | " \n",
243 | " df = df[['date','open','high','low','close','volume']]\n",
244 | " df = df.round(1)\n",
245 | " return df\n",
246 | "z=HA(new_df)\n",
247 | "z.head() "
248 | ]
249 | }
250 | ],
251 | "metadata": {
252 | "kernelspec": {
253 | "display_name": "Python 3",
254 | "language": "python",
255 | "name": "python3"
256 | },
257 | "language_info": {
258 | "codemirror_mode": {
259 | "name": "ipython",
260 | "version": 3
261 | },
262 | "file_extension": ".py",
263 | "mimetype": "text/x-python",
264 | "name": "python",
265 | "nbconvert_exporter": "python",
266 | "pygments_lexer": "ipython3",
267 | "version": "3.7.3"
268 | }
269 | },
270 | "nbformat": 4,
271 | "nbformat_minor": 2
272 | }
273 |
--------------------------------------------------------------------------------
/order_information.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ashishkumar30/Stock_Market_Live_Trading_using_AI/d59f8ce1cbefc000e5eb02b90dfdbf4159aad406/order_information.jpg
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # Zerodha Trading Strategies and Backtesting
2 |
3 | ## About the Project
4 | This repository is a collection of Python-based trading strategies and backtesting programs, developed as part of my Computer Science Engineering (CSE) tenure at UIET Kurukshetra University in 2018. The project focuses on automated and manual live trading on the NSE-BSE markets using Zerodha's API. It incorporates Artificial Intelligence techniques to execute trades based on various technical indicators.
5 |
6 | The repository consists of:
7 | - **Live trading bots**
8 | - **Backtesting programs**
9 | - **Stock screeners**
10 | - **Technical indicator implementations**
11 | - **Historical data downloaders**
12 |
13 | ## Features
14 | - **Live Trading Bots:** Automated and manual trading bots utilizing technical strategies.
15 | - **Backtesting Programs:** Test trading strategies on historical data.
16 | - **Stock Screener:** Scans stocks based on Guppy and other technical indicators.
17 | - **Technical Indicators:** Implementation of popular stock market indicators.
18 | - **Data Conversion:** Converts candlestick data to Heikin-Ashi format.
19 | - **Time Frame Adjustments:** Custom time frame modifications for analysis.
20 |
21 | ## Files and Notebooks
22 |
23 | | File Name | Description |
24 | |-----------|-------------|
25 | | **1)_Getting_Started_with_Zerodha_.ipynb** | Introduction and setup guide for Zerodha API |
26 | | **BACKTESTIG_PROGRAM_.ipynb** | Backtesting any stock's buy/sell strategy on historical data |
27 | | **Buy on RSI when the current high of candle is more than previous high of candle.ipynb** | Buy order based on RSI when RSI > 50 |
28 | | **Hisorical_Data_Download_of_stocks.ipynb** | Code to download historical data for any stock |
29 | | **Live_BOT_(1)_on_RSI_.ipynb** | Live trading bot based on RSI strategy |
30 | | **Live_BOT_(2)_on_GUPPY_with_screener.ipynb** | Manual input Guppy strategy bot |
31 | | **Live_BOT_(3)_Guppy_Automated_.ipynb** | Fully automated Guppy strategy bot |
32 | | **Live_BOT_(4)_advance_bot_multiple_bot_working_in_single_bot_.ipynb** | Mini Guppy bot with backtesting, screener, and indicators |
33 | | **Live_BOT_(5).ipynb** | Mini Guppy bot with stock tracking features |
34 | | **Stock_Screener_(GUPPY)_.ipynb** | Stock screener scanning multiple stocks based on Guppy strategy |
35 | | **Technical_Indicator's_of_Indian_Stock_market.ipynb** | Implementation of key technical indicators for Indian stock market |
36 | | **change time frame.ipynb** | Adjusts time frames for Zerodha trading |
37 | | **conversion code of Candles to hikenashi.ipynb** | Converts candlestick data into Heikin-Ashi format |
38 | | **order_information.jpg** | Image related to order placement |
39 |
40 | ## Technologies Used
41 | - **Python**
42 | - **Zerodha API (Kite Connect)**
43 | - **Pandas & NumPy**
44 | - **Matplotlib & Seaborn**
45 | - **TA-Lib (Technical Analysis Library)**
46 | - **REST API & Websockets**
47 |
48 | ## Setup Instructions
49 | 1. Install required dependencies:
50 | ```sh
51 | pip install kiteconnect pandas numpy matplotlib seaborn TA-Lib
52 | ```
53 | 2. Get API credentials from Zerodha and configure them.
54 | 3. Run the desired Jupyter Notebook.
55 |
56 | ## License
57 | This project is for educational and research purposes only. Live trading involves financial risks, and users should trade responsibly.
58 |
59 | ## Author
60 | Developed as part of my academic and professional exploration into AI-driven stock trading strategies.
61 |
62 | Feel free to explore and contribute! 🚀
63 |
64 |
--------------------------------------------------------------------------------