File size: 31,348 Bytes
976166f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sqlalchemy import create_engine\n",
    "import pandas as pd\n",
    "from datetime import timedelta\n",
    "import jqdatasdk as jq\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load existing portfolio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# check if database need update\n",
    "def fetch_data_for_table():\n",
    "    '''\n",
    "    return None if no update needed\n",
    "    else return (starttime, endtime, frequency)\n",
    "    '''\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Need to fetch stock price periodically from jqdatasdk, also need to check if portfolio updated, but this case can be handled seperately \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# fetch stock price from api\n",
    "def fetch_stock_price():\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "auth success \n"
     ]
    }
   ],
   "source": [
    "# TODO auth, remove later\n",
    "user_name = \"13126862272\"\n",
    "password = \"862272\"\n",
    "jq.auth(user_name, password)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>money</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-01-05 09:31:00</th>\n",
       "      <td>32.54</td>\n",
       "      <td>31.73</td>\n",
       "      <td>32.54</td>\n",
       "      <td>31.72</td>\n",
       "      <td>1205715.0</td>\n",
       "      <td>38788451.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-05 09:32:00</th>\n",
       "      <td>31.68</td>\n",
       "      <td>32.00</td>\n",
       "      <td>32.00</td>\n",
       "      <td>31.39</td>\n",
       "      <td>1084879.0</td>\n",
       "      <td>34358448.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-05 09:33:00</th>\n",
       "      <td>31.91</td>\n",
       "      <td>31.62</td>\n",
       "      <td>31.91</td>\n",
       "      <td>31.61</td>\n",
       "      <td>811350.0</td>\n",
       "      <td>25785154.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-05 09:34:00</th>\n",
       "      <td>31.62</td>\n",
       "      <td>32.07</td>\n",
       "      <td>32.07</td>\n",
       "      <td>31.62</td>\n",
       "      <td>760611.0</td>\n",
       "      <td>24135183.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-05 09:35:00</th>\n",
       "      <td>32.11</td>\n",
       "      <td>31.98</td>\n",
       "      <td>32.16</td>\n",
       "      <td>31.98</td>\n",
       "      <td>655989.0</td>\n",
       "      <td>21046238.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-05 11:26:00</th>\n",
       "      <td>32.25</td>\n",
       "      <td>32.29</td>\n",
       "      <td>32.29</td>\n",
       "      <td>32.20</td>\n",
       "      <td>118071.0</td>\n",
       "      <td>3807211.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-05 11:27:00</th>\n",
       "      <td>32.29</td>\n",
       "      <td>32.30</td>\n",
       "      <td>32.30</td>\n",
       "      <td>32.21</td>\n",
       "      <td>114251.0</td>\n",
       "      <td>3686602.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-05 11:28:00</th>\n",
       "      <td>32.26</td>\n",
       "      <td>32.24</td>\n",
       "      <td>32.30</td>\n",
       "      <td>32.21</td>\n",
       "      <td>60077.0</td>\n",
       "      <td>1938244.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-05 11:29:00</th>\n",
       "      <td>32.25</td>\n",
       "      <td>32.28</td>\n",
       "      <td>32.30</td>\n",
       "      <td>32.22</td>\n",
       "      <td>65634.0</td>\n",
       "      <td>2117204.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-01-05 11:30:00</th>\n",
       "      <td>32.23</td>\n",
       "      <td>32.30</td>\n",
       "      <td>32.30</td>\n",
       "      <td>32.23</td>\n",
       "      <td>49052.0</td>\n",
       "      <td>1582947.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>120 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                      open  close   high    low     volume       money\n",
       "2021-01-05 09:31:00  32.54  31.73  32.54  31.72  1205715.0  38788451.0\n",
       "2021-01-05 09:32:00  31.68  32.00  32.00  31.39  1084879.0  34358448.0\n",
       "2021-01-05 09:33:00  31.91  31.62  31.91  31.61   811350.0  25785154.0\n",
       "2021-01-05 09:34:00  31.62  32.07  32.07  31.62   760611.0  24135183.0\n",
       "2021-01-05 09:35:00  32.11  31.98  32.16  31.98   655989.0  21046238.0\n",
       "...                    ...    ...    ...    ...        ...         ...\n",
       "2021-01-05 11:26:00  32.25  32.29  32.29  32.20   118071.0   3807211.0\n",
       "2021-01-05 11:27:00  32.29  32.30  32.30  32.21   114251.0   3686602.0\n",
       "2021-01-05 11:28:00  32.26  32.24  32.30  32.21    60077.0   1938244.0\n",
       "2021-01-05 11:29:00  32.25  32.28  32.30  32.22    65634.0   2117204.0\n",
       "2021-01-05 11:30:00  32.23  32.30  32.30  32.23    49052.0   1582947.0\n",
       "\n",
       "[120 rows x 6 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## exam return type of each api \n",
    "\n",
    "# range of stock price in different resolution\n",
    "single_stock_df = jq.get_price('002709.XSHE',start_date='2021-01-05 9:00:00', end_date='2021-01-05 12:00:00', frequency='1m')\n",
    "single_stock_df\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>code</th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>money</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2021-01-05 09:31:00</td>\n",
       "      <td>600409.XSHG</td>\n",
       "      <td>9.23</td>\n",
       "      <td>9.16</td>\n",
       "      <td>9.23</td>\n",
       "      <td>9.08</td>\n",
       "      <td>1417593.0</td>\n",
       "      <td>13000397.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2021-01-05 09:32:00</td>\n",
       "      <td>600409.XSHG</td>\n",
       "      <td>9.17</td>\n",
       "      <td>9.17</td>\n",
       "      <td>9.17</td>\n",
       "      <td>9.15</td>\n",
       "      <td>394595.0</td>\n",
       "      <td>3613283.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2021-01-05 09:33:00</td>\n",
       "      <td>600409.XSHG</td>\n",
       "      <td>9.16</td>\n",
       "      <td>9.15</td>\n",
       "      <td>9.16</td>\n",
       "      <td>9.13</td>\n",
       "      <td>492224.0</td>\n",
       "      <td>4501667.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2021-01-05 09:34:00</td>\n",
       "      <td>600409.XSHG</td>\n",
       "      <td>9.14</td>\n",
       "      <td>9.21</td>\n",
       "      <td>9.22</td>\n",
       "      <td>9.14</td>\n",
       "      <td>451578.0</td>\n",
       "      <td>4152355.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2021-01-05 09:35:00</td>\n",
       "      <td>600409.XSHG</td>\n",
       "      <td>9.20</td>\n",
       "      <td>9.20</td>\n",
       "      <td>9.22</td>\n",
       "      <td>9.20</td>\n",
       "      <td>503040.0</td>\n",
       "      <td>4631781.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>715</th>\n",
       "      <td>2021-01-05 11:26:00</td>\n",
       "      <td>600415.XSHG</td>\n",
       "      <td>5.87</td>\n",
       "      <td>5.87</td>\n",
       "      <td>5.87</td>\n",
       "      <td>5.87</td>\n",
       "      <td>279118.0</td>\n",
       "      <td>1639550.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>716</th>\n",
       "      <td>2021-01-05 11:27:00</td>\n",
       "      <td>600415.XSHG</td>\n",
       "      <td>5.87</td>\n",
       "      <td>5.87</td>\n",
       "      <td>5.87</td>\n",
       "      <td>5.87</td>\n",
       "      <td>253667.0</td>\n",
       "      <td>1490048.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>717</th>\n",
       "      <td>2021-01-05 11:28:00</td>\n",
       "      <td>600415.XSHG</td>\n",
       "      <td>5.87</td>\n",
       "      <td>5.87</td>\n",
       "      <td>5.87</td>\n",
       "      <td>5.87</td>\n",
       "      <td>137293.0</td>\n",
       "      <td>806465.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>718</th>\n",
       "      <td>2021-01-05 11:29:00</td>\n",
       "      <td>600415.XSHG</td>\n",
       "      <td>5.87</td>\n",
       "      <td>5.87</td>\n",
       "      <td>5.87</td>\n",
       "      <td>5.87</td>\n",
       "      <td>218351.0</td>\n",
       "      <td>1282600.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>719</th>\n",
       "      <td>2021-01-05 11:30:00</td>\n",
       "      <td>600415.XSHG</td>\n",
       "      <td>5.87</td>\n",
       "      <td>5.87</td>\n",
       "      <td>5.87</td>\n",
       "      <td>5.87</td>\n",
       "      <td>248941.0</td>\n",
       "      <td>1462285.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>720 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                   time         code  open  close  high   low     volume  \\\n",
       "0   2021-01-05 09:31:00  600409.XSHG  9.23   9.16  9.23  9.08  1417593.0   \n",
       "1   2021-01-05 09:32:00  600409.XSHG  9.17   9.17  9.17  9.15   394595.0   \n",
       "2   2021-01-05 09:33:00  600409.XSHG  9.16   9.15  9.16  9.13   492224.0   \n",
       "3   2021-01-05 09:34:00  600409.XSHG  9.14   9.21  9.22  9.14   451578.0   \n",
       "4   2021-01-05 09:35:00  600409.XSHG  9.20   9.20  9.22  9.20   503040.0   \n",
       "..                  ...          ...   ...    ...   ...   ...        ...   \n",
       "715 2021-01-05 11:26:00  600415.XSHG  5.87   5.87  5.87  5.87   279118.0   \n",
       "716 2021-01-05 11:27:00  600415.XSHG  5.87   5.87  5.87  5.87   253667.0   \n",
       "717 2021-01-05 11:28:00  600415.XSHG  5.87   5.87  5.87  5.87   137293.0   \n",
       "718 2021-01-05 11:29:00  600415.XSHG  5.87   5.87  5.87  5.87   218351.0   \n",
       "719 2021-01-05 11:30:00  600415.XSHG  5.87   5.87  5.87  5.87   248941.0   \n",
       "\n",
       "          money  \n",
       "0    13000397.0  \n",
       "1     3613283.0  \n",
       "2     4501667.0  \n",
       "3     4152355.0  \n",
       "4     4631781.0  \n",
       "..          ...  \n",
       "715   1639550.0  \n",
       "716   1490048.0  \n",
       "717    806465.0  \n",
       "718   1282600.0  \n",
       "719   1462285.0  \n",
       "\n",
       "[720 rows x 8 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# range of multiple stocks price in different resolution\n",
    "mul_stocks_df = jq.get_..0n63\n",
    "\n",
    "\n",
    "price(['002709.XSHE',\n",
    " '002920.XSHE',\n",
    " '300274.XSHE',\n",
    " '600409.XSHG',\n",
    " '600415.XSHG',\n",
    " '603882.XSHG'],start_date='2021-01-05 9:00:00', end_date='2021-01-05 12:00:00', frequency='1m')\n",
    "mul_stocks_df\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>weight</th>\n",
       "      <th>display_name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>000008.XSHE</th>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>0.088</td>\n",
       "      <td>神州高铁</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000009.XSHE</th>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>0.344</td>\n",
       "      <td>中国宝安</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000012.XSHE</th>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>0.180</td>\n",
       "      <td>南玻A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000021.XSHE</th>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>0.297</td>\n",
       "      <td>深科技</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>001872.XSHE</th>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>0.030</td>\n",
       "      <td>招商港口</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>688002.XSHG</th>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>0.438</td>\n",
       "      <td>睿创微纳</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>688099.XSHG</th>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>0.287</td>\n",
       "      <td>晶晨股份</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>688088.XSHG</th>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>0.252</td>\n",
       "      <td>虹软科技</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>688321.XSHG</th>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>0.134</td>\n",
       "      <td>微芯生物</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>688029.XSHG</th>\n",
       "      <td>2020-12-31</td>\n",
       "      <td>0.130</td>\n",
       "      <td>南微医学</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>500 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                   date  weight display_name\n",
       "000008.XSHE  2020-12-31   0.088         神州高铁\n",
       "000009.XSHE  2020-12-31   0.344         中国宝安\n",
       "000012.XSHE  2020-12-31   0.180          南玻A\n",
       "000021.XSHE  2020-12-31   0.297          深科技\n",
       "001872.XSHE  2020-12-31   0.030         招商港口\n",
       "...                 ...     ...          ...\n",
       "688002.XSHG  2020-12-31   0.438         睿创微纳\n",
       "688099.XSHG  2020-12-31   0.287         晶晨股份\n",
       "688088.XSHG  2020-12-31   0.252         虹软科技\n",
       "688321.XSHG  2020-12-31   0.134         微芯生物\n",
       "688029.XSHG  2020-12-31   0.130         南微医学\n",
       "\n",
       "[500 rows x 3 columns]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# composition of benchmark\n",
    "jq.get_index_weights('000905.XSHG',date='2021-01-20 9:00:00')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'600409.XSHG': {'sw_l1': {'industry_code': '801030', 'industry_name': '化工I'},\n",
       "  'sw_l2': {'industry_code': '801033', 'industry_name': '化学原料II'},\n",
       "  'sw_l3': {'industry_code': '850321', 'industry_name': '纯碱III'},\n",
       "  'zjw': {'industry_code': 'C26', 'industry_name': '化学原料和化学制品制造业'},\n",
       "  'jq_l2': {'industry_code': 'HY02107', 'industry_name': '粘胶'},\n",
       "  'jq_l1': {'industry_code': 'HY002', 'industry_name': '原材料'}},\n",
       " '603882.XSHG': {'sw_l1': {'industry_code': '801150',\n",
       "   'industry_name': '医药生物I'},\n",
       "  'sw_l2': {'industry_code': '801156', 'industry_name': '医疗服务II'},\n",
       "  'sw_l3': {'industry_code': '851562', 'industry_name': '诊断服务III'},\n",
       "  'zjw': {'industry_code': 'Q83', 'industry_name': '卫生'},\n",
       "  'jq_l2': {'industry_code': 'HY06103', 'industry_name': '体外诊断'},\n",
       "  'jq_l1': {'industry_code': 'HY006', 'industry_name': '医药卫生'}},\n",
       " '300274.XSHE': {'sw_l1': {'industry_code': '801730',\n",
       "   'industry_name': '电气设备I'},\n",
       "  'sw_l2': {'industry_code': '801735', 'industry_name': '光伏设备II'},\n",
       "  'sw_l3': {'industry_code': '857353', 'industry_name': '逆变器III'},\n",
       "  'zjw': {'industry_code': 'C38', 'industry_name': '电气机械和器材制造业'},\n",
       "  'jq_l2': {'industry_code': 'HY03111', 'industry_name': '光伏设备'},\n",
       "  'jq_l1': {'industry_code': 'HY003', 'industry_name': '工业'}},\n",
       " '002709.XSHE': {'sw_l1': {'industry_code': '801730',\n",
       "   'industry_name': '电气设备I'},\n",
       "  'sw_l2': {'industry_code': '801737', 'industry_name': '电池II'},\n",
       "  'sw_l3': {'industry_code': '857372', 'industry_name': '电池化学品III'},\n",
       "  'zjw': {'industry_code': 'C26', 'industry_name': '化学原料和化学制品制造业'},\n",
       "  'jq_l2': {'industry_code': 'HY03119', 'industry_name': '电池部件及材料'},\n",
       "  'jq_l1': {'industry_code': 'HY003', 'industry_name': '工业'}},\n",
       " '002920.XSHE': {'sw_l1': {'industry_code': '801750', 'industry_name': '计算机I'},\n",
       "  'sw_l2': {'industry_code': '801104', 'industry_name': '软件开发II'},\n",
       "  'sw_l3': {'industry_code': '851041', 'industry_name': '垂直应用软件III'},\n",
       "  'zjw': {'industry_code': 'C36', 'industry_name': '汽车制造业'},\n",
       "  'jq_l2': {'industry_code': 'HY04103', 'industry_name': '汽车电子'},\n",
       "  'jq_l1': {'industry_code': 'HY004', 'industry_name': '可选消费'}},\n",
       " '600415.XSHG': {'sw_l1': {'industry_code': '801200',\n",
       "   'industry_name': '商业贸易I'},\n",
       "  'sw_l2': {'industry_code': '801203', 'industry_name': '一般零售II'},\n",
       "  'sw_l3': {'industry_code': '852034', 'industry_name': '商业物业经营III'},\n",
       "  'zjw': {'industry_code': 'L72', 'industry_name': '商务服务业'},\n",
       "  'jq_l2': {'industry_code': 'HY03147', 'industry_name': '市场服务'},\n",
       "  'jq_l1': {'industry_code': 'HY003', 'industry_name': '工业'}}}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# sector information of each stock\n",
    "jq.get_industry(['002709.XSHE',\n",
    " '002920.XSHE',\n",
    " '300274.XSHE',\n",
    " '600409.XSHG',\n",
    " '600415.XSHG',\n",
    " '603882.XSHG'])\n",
    "# display name of each stock"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([datetime.date(2005, 1, 4), datetime.date(2005, 1, 5),\n",
       "       datetime.date(2005, 1, 6), ..., datetime.date(2025, 8, 11),\n",
       "       datetime.date(2025, 8, 12), datetime.date(2025, 8, 13)],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# get all trading days\n",
    "jq.get_all_trade_days()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>display_name</th>\n",
       "      <th>name</th>\n",
       "      <th>start_date</th>\n",
       "      <th>end_date</th>\n",
       "      <th>type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>000001.XSHE</th>\n",
       "      <td>平安银行</td>\n",
       "      <td>PAYH</td>\n",
       "      <td>1991-04-03</td>\n",
       "      <td>2200-01-01</td>\n",
       "      <td>stock</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000002.XSHE</th>\n",
       "      <td>万科A</td>\n",
       "      <td>WKA</td>\n",
       "      <td>1991-01-29</td>\n",
       "      <td>2200-01-01</td>\n",
       "      <td>stock</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000004.XSHE</th>\n",
       "      <td>国华网安</td>\n",
       "      <td>GHWA</td>\n",
       "      <td>1990-12-01</td>\n",
       "      <td>2200-01-01</td>\n",
       "      <td>stock</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000005.XSHE</th>\n",
       "      <td>ST星源</td>\n",
       "      <td>STXY</td>\n",
       "      <td>1990-12-10</td>\n",
       "      <td>2200-01-01</td>\n",
       "      <td>stock</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000006.XSHE</th>\n",
       "      <td>深振业A</td>\n",
       "      <td>SZYA</td>\n",
       "      <td>1992-04-27</td>\n",
       "      <td>2200-01-01</td>\n",
       "      <td>stock</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>688799.XSHG</th>\n",
       "      <td>华纳药厂</td>\n",
       "      <td>HNYC</td>\n",
       "      <td>2021-07-13</td>\n",
       "      <td>2200-01-01</td>\n",
       "      <td>stock</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>688800.XSHG</th>\n",
       "      <td>瑞可达</td>\n",
       "      <td>RKD</td>\n",
       "      <td>2021-07-22</td>\n",
       "      <td>2200-01-01</td>\n",
       "      <td>stock</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>688819.XSHG</th>\n",
       "      <td>天能股份</td>\n",
       "      <td>TNGF</td>\n",
       "      <td>2021-01-18</td>\n",
       "      <td>2200-01-01</td>\n",
       "      <td>stock</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>688981.XSHG</th>\n",
       "      <td>中芯国际</td>\n",
       "      <td>ZXGJ</td>\n",
       "      <td>2020-07-16</td>\n",
       "      <td>2200-01-01</td>\n",
       "      <td>stock</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>689009.XSHG</th>\n",
       "      <td>九号公司</td>\n",
       "      <td>JHGS</td>\n",
       "      <td>2020-10-29</td>\n",
       "      <td>2200-01-01</td>\n",
       "      <td>stock</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5248 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            display_name  name start_date   end_date   type\n",
       "000001.XSHE         平安银行  PAYH 1991-04-03 2200-01-01  stock\n",
       "000002.XSHE          万科A   WKA 1991-01-29 2200-01-01  stock\n",
       "000004.XSHE         国华网安  GHWA 1990-12-01 2200-01-01  stock\n",
       "000005.XSHE         ST星源  STXY 1990-12-10 2200-01-01  stock\n",
       "000006.XSHE         深振业A  SZYA 1992-04-27 2200-01-01  stock\n",
       "...                  ...   ...        ...        ...    ...\n",
       "688799.XSHG         华纳药厂  HNYC 2021-07-13 2200-01-01  stock\n",
       "688800.XSHG          瑞可达   RKD 2021-07-22 2200-01-01  stock\n",
       "688819.XSHG         天能股份  TNGF 2021-01-18 2200-01-01  stock\n",
       "688981.XSHG         中芯国际  ZXGJ 2020-07-16 2200-01-01  stock\n",
       "689009.XSHG         九号公司  JHGS 2020-10-29 2200-01-01  stock\n",
       "\n",
       "[5248 rows x 5 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "jq.get_all_securities()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'engine' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[4], line 9\u001b[0m\n\u001b[1;32m      7\u001b[0m     df[\u001b[39m'\u001b[39m\u001b[39mdate\u001b[39m\u001b[39m'\u001b[39m] \u001b[39m=\u001b[39m pd\u001b[39m.\u001b[39mto_datetime(df[\u001b[39m'\u001b[39m\u001b[39mdate\u001b[39m\u001b[39m'\u001b[39m])\n\u001b[1;32m      8\u001b[0m     \u001b[39mreturn\u001b[39;00m df\n\u001b[0;32m----> 9\u001b[0m get_most_recent_profile(\u001b[39m'\u001b[39;49m\u001b[39mportfolio\u001b[39;49m\u001b[39m'\u001b[39;49m)\n",
      "Cell \u001b[0;32mIn[4], line 5\u001b[0m, in \u001b[0;36mget_most_recent_profile\u001b[0;34m(type)\u001b[0m\n\u001b[1;32m      3\u001b[0m table_name \u001b[39m=\u001b[39m \u001b[39m'\u001b[39m\u001b[39mbenchmark_profile\u001b[39m\u001b[39m'\u001b[39m \u001b[39mif\u001b[39;00m \u001b[39mtype\u001b[39m \u001b[39m==\u001b[39m \u001b[39m'\u001b[39m\u001b[39mbenchmark\u001b[39m\u001b[39m'\u001b[39m \u001b[39melse\u001b[39;00m \u001b[39m'\u001b[39m\u001b[39mportfolio_profile\u001b[39m\u001b[39m'\u001b[39m\n\u001b[1;32m      4\u001b[0m query \u001b[39m=\u001b[39m \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mSELECT * FROM \u001b[39m\u001b[39m{\u001b[39;00mtable_name\u001b[39m}\u001b[39;00m\u001b[39m WHERE date = (SELECT MAX(date) FROM \u001b[39m\u001b[39m{\u001b[39;00mtable_name\u001b[39m}\u001b[39;00m\u001b[39m)\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m----> 5\u001b[0m df \u001b[39m=\u001b[39m pd\u001b[39m.\u001b[39mread_sql(query, con\u001b[39m=\u001b[39mengine)\n\u001b[1;32m      6\u001b[0m \u001b[39m# convert date to datetime object\u001b[39;00m\n\u001b[1;32m      7\u001b[0m df[\u001b[39m'\u001b[39m\u001b[39mdate\u001b[39m\u001b[39m'\u001b[39m] \u001b[39m=\u001b[39m pd\u001b[39m.\u001b[39mto_datetime(df[\u001b[39m'\u001b[39m\u001b[39mdate\u001b[39m\u001b[39m'\u001b[39m])\n",
      "\u001b[0;31mNameError\u001b[0m: name 'engine' is not defined"
     ]
    }
   ],
   "source": [
    "# when update stock price just need the latest portfolio frame\n",
    "def get_most_recent_profile(type):\n",
    "    table_name = 'benchmark_profile' if type == 'benchmark' else 'portfolio_profile'\n",
    "    query = f\"SELECT * FROM {table_name} WHERE date = (SELECT MAX(date) FROM {table_name})\"\n",
    "    with create_engine(db_url).connect() as conn:\n",
    "        df = pd.read_sql(query, con=engine)\n",
    "        # convert date to datetime object\n",
    "        df['date'] = pd.to_datetime(df['date'])\n",
    "        return df\n",
    "get_most_recent_profile('portfolio')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "portfolio_risk_assesment",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}