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)#endifextern"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<<"Welcome to ..."<<Endl; cout<<"Start calculation ..."<<Endl; int M=N; Double Wma[arraylen]; if(Arraylen >= n && N >=0) {switch (n) {case 0: {cout<<"length is 0 ..."<<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<<"Length Error ..."<<Endl; } cout<<"Deposit File ..."<<Endl; Ofstream fwma ("C:\\wma.csv"); if(!fwma) {cout<<"Open Error ..."<<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<<"Finish Writing ..."<<Endl; Fwma.close (); return1; }}
wma.py
ImportTushare fromcTYPESImport*#Data preprocessingDataFrame = Tushare.get_hist_data ('SH') Open= dataframe['Open'].valuesopenlen=Len (Open)#python's list format to C's array formatArray = (c_double * openlen) (*Open) Arraylen=Openlen#calling the DLL function WMA calculates the WMA value into the filehDLL = Cdll ('c:\\users\\perelman\\. Clion2016.1\\system\\cmake\\generated\\wma-4d5bfe42\\4d5bfe42\\debug\\libwma.dll')Print(hdll.wma (Array, Arraylen, 5))
Python downloads the Tushare data and then calls the C + + DLL calculation WMA into the local CSV file