zoukankan      html  css  js  c++  java
  • 通达信日线数据提取

    #!/usr/bin/env python

    from struct import *

    import os
    import sys

    def exactStock(fileName, code):
    ofile = open(fileName, 'rb')
    buf = ofile.read()
    ofile.close()
    num = len(buf)
    no = num / 32
    b = 0
    e = 32
    items = list()
    for i in range(int(no)):
    a = unpack('IIIIIfII', buf[b:e])
    year = int(a[0] / 10000)
    m = int((a[0] % 10000) / 100)
    month = str(m)
    if m < 10:
    month = "0" + month
    d = (a[0] % 10000) % 100
    day = str(d)
    if d < 10:
    day = "0" + str(d)
    dd = str(year) + "-" + month + "-" + day
    openPrice = a[1] / 100.0
    high = a[2] / 100.0
    low = a[3] / 100.0
    close = a[4] / 100.0
    amount = a[5]
    vol = a[6]
    unused = a[7]
    if i == 0:
    preClose = close
    ratio = round((close - preClose) / preClose * 100, 2)
    preClose = close
    item = [code, dd, str(openPrice), str(high), str(low), str(close), str(ratio), str(amount), str(vol)]
    items.append(item)
    b = b + 32
    e = e + 32

    return items


    #exactStock('I:\e\tdx\NEW PTTQ V12(5.90S)\vipdocsh\lday\sh000001.day', "000001")




    def day2csv_data(dirname, fname, targetDir):
    ofile = open(dirname + os.sep + fname, 'rb')
    buf = ofile.read()
    ofile.close()

    ifile = open(targetDir + os.sep + fname + '.csv', 'w')
    num = len(buf)
    no = num / 32
    b = 0
    e = 32
    line = ''
    linename = str('date') + ',' + str('open') + ', ' + str('high') + ' ,' + str('low') + ', ' + str(
    'close') + ' ,' + str('amout') + ', ' + str('vol') + ' ,' + str('str07') + '' + ' '
    # print line
    ifile.write(linename)
    # for i in xrange(no):
    for i in range(int(no)):
    a = unpack('IIIIIfII', buf[b:e])
    line = str(a[0]) + ',' + str(a[1] / 100.0) + ', ' + str(a[2] / 100.0) + ' ,' + str(a[3] / 100.0) + ', ' + str(
    a[4] / 100.0) + ' ,' + str(a[5]) + ', ' + str(a[6]) + ' ,' + str(a[7]) + '' + ' '
    # print line
    ifile.write(line)
    b = b + 32
    e = e + 32
    ifile.close()


    # pathdir='/vipdoc/sh/lday'
    pathdir = 'F:\python\untitled1\core\data'
    # targetDir='/_python_gp_tdx/data_gupiao/sh/lday'
    targetDir = 'F:\python\untitled1\core\data'

    listfile = os.listdir(pathdir)

    for f in listfile:

    day2csv_data(pathdir, f, targetDir)
    else:
    print('The for ' + pathdir + ' to ' + targetDir + ' loop is over')



    参考来源:https://blog.csdn.net/liuyukuan/article/details/53560278?utm_source=blogxgwz2
  • 相关阅读:
    C++11 二叉堆
    OpenCV --- 实现两幅图像并排合并(ROI)
    OpenCV --- 修改图像的对比度、亮度 、RGB转Gray图像、修改图像的尺寸
    Opencv --- 图像像素遍历的各种方法
    Ubuntu系统的安装(虚拟机) 并配置C/C++编译器
    在Ubuntu下编译安装nginx
    【OpenCV3】threshold()函数详解
    MFC 剪切板的使用、线程介绍
    C++基础知识 基类指针、虚函数、多态性、纯虚函数、虚析构
    【OpenCV3】cvRound()、cvFloor()、cvCeil()函数详解
  • 原文地址:https://www.cnblogs.com/rongye/p/12318239.html
Copyright © 2011-2022 走看看