Quantify stock market through Python (Wind)

1. This article takes the data of the last 5 trading days of a certain trading month
. 2.
It is more convenient to take the data of all A shares for 5 times a day or all the A shares for 5 days: a. The leader requires each stock to be saved as a file, which is more convenient;
b. Measured by the time library on this machine, the time to fetch data and write files is about 1:3, and it takes more time to write files;
c. The machine program runs for about 25 minutes, if you fetch all A shares a day 5 times Data, which will cause a large memory occupation for a long time, and there is also the time overhead of merging, grouping, and sorting the acquired data

import datetime
import math
import os

import pandas as pd
import xlsxwriter as xlsx
from click import progressbar  # 进度条
from WindPy import *

folder_target = "C:\\stocks\\"


def check_result(data):
    if data.ErrorCode != 0:
        print("Error[" + str(data.ErrorCode) + "]\n")
        sys.exit()


if os.path.exists(folder_target) == False:
    os.mkdir(folder_target)

w.start()

today = ""
print("\n请输入要导出的数据的下一交易月第一天,如需导出2020年11月最后5天,则输入 2020-12-01")
today = input("\n请输入:")

# 简单验证
while today[-2:] != "01":
    today = input("\n输入格式错误,请从新输入:")
today = today + "T00:00:00"

endTime = w.tdaysoffset(-1, today, "Period=D;Days=Trading").Times[0]
beginTime = w.tdaysoffset(-5, today, "Period=D;Days=Trading").Times[0]

res = w.wset(
    "sectorconstituent", "date=%s;sectorid=a001010100000000" % (str(beginTime))
)
check_result(res)

fields = ["pre_close", "open", "high", "low", "close", "vwap"]
with progressbar(length=len(res.Data[1])) as pbar:
    print("\n导出进度:")
    for i, code in enumerate(res.Data[1]):
        stock = w.wsd(
            codes=code,
            fields=fields,
            beginTime=beginTime,
            endTime=endTime,
            options="Fill=Previous",
        )
        excel = xlsx.Workbook("%s.xlsx" % (folder_target + code))
        sheet = excel.add_worksheet(code)
        sheet.write(0, 0, "代码")
        sheet.write(0, 1, "简称")
        sheet.write(0, 2, "日期")
        sheet.write(0, 3, "前收盘价(元)")
        sheet.write(0, 4, "开盘价(元)")
        sheet.write(0, 5, "最高价(元)")
        sheet.write(0, 6, "最低价(元)")
        sheet.write(0, 7, "收盘价(元)")
        sheet.write(0, 8, "均价(元)")
        for dateN in range(1, 6):
            sheet.write(dateN, 0, code)
            sheet.write(dateN, 1, res.Data[2][i])
            sheet.write(dateN, 2, str(stock.Times[dateN - 1]))
            for fieldsN in range(0, len(fields)):
                sheet.write(
                    dateN,
                    3 + fieldsN,
                    "--"
                    if math.isnan(stock.Data[fieldsN][dateN - 1])
                    else stock.Data[fieldsN][dateN - 1],
                )
        excel.close()
        pbar.update(1)
input("\n%s-%s五个交易日行情导出完成!" % (str(beginTime), str(endTime)))


Guess you like

Origin blog.csdn.net/tianxiefenxiang/article/details/110941105
Recommended