Python 下载 tushare 数据,然后调用 C++ DLL 计算 wMA 存入本地 csv 文件

Posted 佩雷尔曼的信徒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 下载 tushare 数据,然后调用 C++ DLL 计算 wMA 存入本地 csv 文件相关的知识,希望对你有一定的参考价值。

CMakeLists.txt

project(wMA)
add_library(wMA SHARED wMA.cpp)

wMA.h

#pragma once

#ifndef WMA_WMA_H
#define WMA_WMA_H
#endif

#ifdef BUILD_WMA_DLL
#define IO_WMA_DLL __declspec(export)
#else
#define IO_WMA_DLL __declspec(import)
#endif

extern "C"
{
IO_WMA_DLL int wMA(double array[], int arrayLen, int n);
}

wMA.cpp

#define BUILD_WMA_DLL

#include "wMA.h"
#include <iostream>
#include <fstream>

using namespace std;

IO_WMA_DLL int wMA(double array[], int arrayLen, int n)
{
    cout << "欢迎光临..." << endl;
    cout << "开始计算..." << endl;
    int m = n;
    double wMA[arrayLen];
    if(arrayLen >= n && n >= 0)
    {
        switch(n)
        {
            case 0:
            {
                cout << "长度为零..." << endl;
                for(int i = 0; i < m; i++)
                {
                    wMA[i] = 0;
                }
                break;
            }
            default:
            {
                for(int i = 0; i < m - 1; i++)
                {
                    wMA[i] = 0;
                }
                while(arrayLen >= m)
                {
                    double temp = 0;
                    for(int i = m - n, j = 0; i < m; i++, j++)
                    {
                        temp += array[i] * j;
                    }
                    wMA[m - 1] = temp / n;
                    m++;
                }
                break;
            }
        }
    }
    else
    {
        cout << "长度错误..." << endl;
    }
    cout << "存入文件..." << endl;
    ofstream fWMA("c:\\\\wMA.csv");
    if(!fWMA)
    {
        cout << "打开错误..." << endl;
        return -1;
    }
    else
    {
        fWMA << "Index" << "," << "index of sh" << "," << "wMA" << "," << endl;
        for(int i = 0; i < arrayLen; ++i)
        {
            fWMA << i << "," << array[i] << "," << wMA[i] << "," << endl;
        }
        cout << "完成写入..." << endl;
        fWMA.close();
        return 1;
    }
}

wMA.py

import tushare
from ctypes import *
# 数据预处理
dataFrame = tushare.get_hist_data(\'sh\')
open = dataFrame[\'open\'].values
openLen = len(open)
# python 的 list 格式转为 c 的 array 格式
array = (c_double * openLen)(*open)
arrayLen = openLen
# 调用 DLL 函数 wMA 算出 wMA 值存入文件
hDLL = CDLL(\'C:\\\\Users\\\\Perelman\\\\.CLion2016.1\\\\system\\\\cmake\\\\generated\\\\wMA-4d5bfe42\\\\4d5bfe42\\\\Debug\\\\libwMA.dll\')
print(hDLL.wMA(array, arrayLen, 5))

 

1 2

读取 csv,调整后存数据库,python socket 和 mysql 部分见其他随笔,

import pandas
dataFrame = pandas.DataFrame(pandas.read_csv("c:\\\\wMA.csv"))
print(dataFrame)
dataFrame = dataFrame.drop([\'Unnamed: 3\'], axis=1)
print(dataFrame)

1

以上是关于Python 下载 tushare 数据,然后调用 C++ DLL 计算 wMA 存入本地 csv 文件的主要内容,如果未能解决你的问题,请参考以下文章

python调用tushare获取沪深A股票资金流向数据

python调用tushare获取A股周线行情

python调用tushare的pro_bar通用行情接口

通过Python SDK 获取tushare数据

使用python和tushare股票交易日历数据,判断节假日周末休市

python tushare安装