数据的批量完整下载方式代码

https://github.com/rainx/pytdx/issues/21 via yutiansut

看到有issue提出不知道如果下载完整的数据,关于数据位置指针的使用,给一个示例代码:

from pytdx.hq import  TdxHq_API
api=TdxHq_API()

with api.connect():
     data=[]

      for i in range(10):
              data+=api.get_security_bars(9,0,'000001',(9-i)*800,800)
print(api.to_df(data))
open close high low vol amount year month day hour minute datetime
27.70 27.80 27.90 27.60 1270.0 3.530600e+06 1991 12 23 15 0 1991-12-23 15:00
27.90 29.05 29.30 27.00 1050.0 3.050250e+06 1991 12 24 15 0 1991-12-24 15:00
29.15 29.30 30.00 29.10 2269.0 6.648170e+06 1991 12 25 15 0 1991-12-25 15:00
29.30 28.00 29.30 28.00 1918.0 5.370400e+06 1991 12 26 15 0 1991-12-26 15:00
28.00 28.45 28.50 28.00 2105.0 5.988725e+06 1991 12 27 15 0 1991-12-27 15:00
28.40 29.25 29.30 28.40 1116.0 3.264300e+06 1991 12 28 15 0 1991-12-28 15:00
29.30 28.80 29.40 28.80 1059.0 3.049920e+06 1991 12 30 15 0 1991-12-30 15:00
29.15 29.15 29.40 29.00 1886.0 5.497690e+06 1992 1 2 15 0 1992-01-02 15:00
29.20 29.10 29.30 29.00 2212.0 6.436920e+06 1992 1 3 15 0 1992-01-03 15:00
29.10 29.70 29.80 29.05 2634.0 7.822980e+06 1992 1 6 15 0 1992-01-06 15:00
29.70 29.90 30.20 29.55 2931.0 8.763690e+06 1992 1 7 15 0 1992-01-07 15:00
29.80 29.65 30.20 29.55 1928.0 5.716520e+06 1992 1 8 15 0 1992-01-08 15:00
29.70 29.70 29.80 29.30 1535.0 4.558950e+06 1992 1 9 15 0 1992-01-09 15:00
29.65 29.80 29.80 29.50 1525.0 4.544500e+06 1992 1 10 15 0 1992-01-10 15:00
29.80 29.65 29.80 29.60 1353.0 4.011645e+06 1992 1 13 15 0 1992-01-13 15:00
29.65 29.20 29.65 29.00 1711.0 4.996120e+06 1992 1 14 15 0 1992-01-14 15:00
29.20 29.10 29.40 29.00 2205.0 6.416550e+06 1992 1 15 15 0 1992-01-15 15:00
29.00 28.90 29.00 28.55 1268.0 3.664520e+06 1992 1 16 15 0 1992-01-16 15:00
28.90 28.80 28.90 28.35 1143.0 3.291840e+06 1992 1 17 15 0 1992-01-17 15:00
28.80 29.45 29.50 28.80 1085.0 3.195325e+06 1992 1 20 15 0 1992-01-20 15:00
29.50 29.30 29.80 29.20 1576.0 4.617680e+06 1992 1 21 15 0 1992-01-21 15:00
29.25 28.95 29.30 28.80 1592.0 4.608840e+06 1992 1 22 15 0 1992-01-22 15:00
28.95 29.20 29.30 28.95 1477.0 4.312840e+06 1992 1 23 15 0 1992-01-23 15:00
29.25 29.05 29.70 29.05 2327.0 6.759935e+06 1992 1 24 15 0 1992-01-24 15:00
29.55 29.40 29.60 29.30 2571.0 7.558740e+06 1992 1 27 15 0 1992-01-27 15:00
29.40 29.50 29.50 29.35 2336.0 6.891200e+06 1992 1 28 15 0 1992-01-28 15:00
29.50 30.00 30.05 29.50 2127.0 6.381000e+06 1992 1 29 15 0 1992-01-29 15:00
29.95 30.20 30.35 29.95 1735.0 5.239700e+06 1992 1 30 15 0 1992-01-30 15:00
31.15 32.00 32.00 31.15 2228.0 7.129600e+06 1992 2 1 15 0 1992-02-01 15:00
32.00 32.32 32.70 31.90 1735.0 5.607520e+06 1992 2 2 15 0 1992-02-02 15:00
... ... ... ... ... ... ... ... ... ... ... ...
9.30 9.36 9.39 9.27 546016.0 5.091620e+08 2017 6 27 15 0 2017-06-27 15:00
9.35 9.43 9.49 9.33 1168796.0 1.102438e+09 2017 6 28 15 0 2017-06-28 15:00
9.43 9.43 9.45 9.37 488804.0 4.598104e+08 2017 6 29 15 0 2017-06-29 15:00
9.40 9.39 9.43 9.31 499633.0 4.680035e+08 2017 6 30 15 0 2017-06-30 15:00
9.40 9.40 9.43 9.34 388349.0 3.644659e+08 2017 7 3 15 0 2017-07-03 15:00
9.40 9.34 9.41 9.30 488362.0 4.565770e+08 2017 7 4 15 0 2017-07-04 15:00
9.29 9.37 9.38 9.27 567720.0 5.292941e+08 2017 7 5 15 0 2017-07-05 15:00
9.36 9.40 9.41 9.31 738911.0 6.913872e+08 2017 7 6 15 0 2017-07-06 15:00
9.37 9.47 9.48 9.34 760369.0 7.170844e+08 2017 7 7 15 0 2017-07-07 15:00
9.45 9.59 9.66 9.44 1360815.0 1.303090e+09 2017 7 10 15 0 2017-07-10 15:00
9.61 10.25 10.46 9.61 3812086.0 3.842010e+09 2017 7 11 15 0 2017-07-11 15:00
10.27 10.34 10.58 10.20 2998844.0 3.113681e+09 2017 7 12 15 0 2017-07-12 15:00
10.30 10.90 10.90 10.24 2994534.0 3.180145e+09 2017 7 13 15 0 2017-07-13 15:00
10.81 10.90 10.94 10.66 1722570.0 1.864449e+09 2017 7 14 15 0 2017-07-14 15:00
10.95 10.81 11.33 10.72 3273123.0 3.608692e+09 2017 7 17 15 0 2017-07-17 15:00
10.75 11.05 11.14 10.62 2349431.0 2.558434e+09 2017 7 18 15 0 2017-07-18 15:00
10.99 11.09 11.19 10.88 1933075.0 2.131336e+09 2017 7 19 15 0 2017-07-19 15:00
11.08 10.97 11.22 10.91 1537338.0 1.695061e+09 2017 7 20 15 0 2017-07-20 15:00
10.83 10.89 10.95 10.69 1501020.0 1.625416e+09 2017 7 21 15 0 2017-07-21 15:00
10.82 10.95 11.06 10.73 1692664.0 1.846887e+09 2017 7 24 15 0 2017-07-24 15:00
10.98 11.00 11.27 10.95 1954768.0 2.172115e+09 2017 7 25 15 0 2017-07-25 15:00
10.92 10.74 11.18 10.66 1697412.0 1.846282e+09 2017 7 26 15 0 2017-07-26 15:00
10.72 10.59 10.77 10.53 1194490.0 1.273889e+09 2017 7 27 15 0 2017-07-27 15:00
10.61 10.74 10.81 10.58 819195.0 8.777693e+08 2017 7 28 15 0 2017-07-28 15:00
10.80 10.67 10.82 10.45 1575864.0 1.671814e+09 2017 7 31 15 0 2017-07-31 15:00
10.64 11.04 11.08 10.60 2035709.0 2.222888e+09 2017 8 1 15 0 2017-08-01 15:00
11.05 11.15 11.34 10.96 2062069.0 2.307727e+09 2017 8 2 15 0 2017-08-02 15:00
11.14 11.01 11.22 10.97 984219.0 1.090954e+09 2017 8 3 15 0 2017-08-03 15:00
11.00 11.17 11.29 10.93 1353951.0 1.511390e+09 2017 8 4 15 0 2017-08-04 15:00
11.06 11.00 11.17 10.90 860644.0 9.469757e+08 2017 8 7 15 0 2017-08-07 15:00

这么做的原因很简单,改变指针的位置

分别是

0-799

800-1599

1600-2399

....

依次

然后需要注意的是 0代表的是今天的指针 ,所以 指针获取方式要翻过来写 首先是 7200-7999的数据

最后才是0-799的数据

基本上 日线级别 8000条足够覆盖了

分钟线,小时线要长一点

封装成函数


from pytdx.hq import  TdxHq_API
api=TdxHq_API()


def get_all_day_data():
   with api.connect():
        data=[]

        for i in range(10):
              data+=api.get_security_bars(9,0,'000001',(9-i)*800,800)
    print(api.to_df(data))

results matching ""

    No results matching ""