-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPythonNumpyMean.cpp
112 lines (105 loc) · 3.45 KB
/
PythonNumpyMean.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//+------------------------------------------------------------------+
//| Sample DLL for MQL4 |
//| Copyright © 2004-2006, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
//#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include "Python.h"
#include "numpy/arrayobject.h"
//----
#define MT4_EXPFUNC __declspec(dllexport)
PyObject *NumPy;
PyObject *PyNumpyMeanFunc;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#pragma pack(push,1)
struct RateInfo
{
unsigned int ctm;
double open;
double low;
double high;
double close;
double vol;
};
#pragma pack(pop)
//----
struct MqlStr
{
int len;
char *string;
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
BOOL APIENTRY DllMain(HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved)
{
//----
switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
Py_Initialize();
import_array1(-1);
PyObject *pName;
pName = PyString_FromString("numpy");
NumPy = PyImport_Import(pName);
Py_DECREF(pName);
if(NumPy != NULL){
PyNumpyMeanFunc = PyObject_GetAttrString(NumPy, "mean");
if(PyNumpyMeanFunc == NULL){
OutputDebugString("Failed to get mean func.");
}
}else{
OutputDebugString("Failed to load numpy.");
}
OutputDebugString("Attached.");
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
Py_DECREF(NumPy);
Py_DECREF(PyNumpyMeanFunc);
Py_Finalize();
break;
}
//----
return(TRUE);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
MT4_EXPFUNC double __stdcall GetNumpyMean(double *value, int arraysize, int period, int index)
{
//----
if(value==NULL)
{
//OutputDebugString("GetArrayItemValue: NULL array\n");
return(0.0);
}
if(arraysize < index + period)
{
//OutputDebugString("GetArrayItemValue: wrong arraysize \n");
return(0.0);
}
npy_intp npy_arraysize;
npy_arraysize = period;
double* shifted_value = (double*)((int)value + sizeof(double)*index);
PyObject *np_value = PyArray_SimpleNewFromData(1, &npy_arraysize, NPY_DOUBLE, shifted_value);
Py_INCREF(np_value);;
PyObject *pArgs = PyTuple_New(1);
PyTuple_SetItem(pArgs, 0, np_value);
PyObject *pResult = PyObject_CallObject(PyNumpyMeanFunc, pArgs);
Py_DECREF(pArgs);
Py_DECREF(np_value);
double mean = PyFloat_AsDouble(pResult);
Py_DECREF(pResult);
//----
return(mean);
}