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))
时间: 2024-10-11 05:50:45