我在 Multicharts 中使用 GlobalVariables,没有任何问题。例如,如果我创建这个指标:
inputs: DayValue(18);
AccuracySetNamedInt("Day", DayValue);
RecalcLastBarAfter(1);
我可以毫无问题地从其他指标获取变量“Day”的值:
value1 = AccuracyGetNamedInt("Day", -1);
我需要从Python获取“Day”变量的值。我已经尝试过这段代码:
import ctypes
# Load the GlobalVariable.dll
gv = ctypes.CDLL("C:\\Program Files\\MultiCharts\\GlobalVariable.dll")
# Define the function prototypes
gv.AccuracyGetNamedInt.argtypes = [ctypes.c_char_p, ctypes.c_int]
gv.AccuracyGetNamedInt.restype = ctypes.c_int
# Access the global variable
variable_name = "Day".encode('utf-8')
default_value = -1
value = gv.AccuracyGetNamedInt(variable_name, default_value)
print(f"The value of the global variable 'Day' is: {value}")
但是,我收到错误消息:
属性错误:找不到函数“AccuracyGetNamedInt”
如果我获得 DLL 的函数列表,我会得到这个 (dir(gv):
['加载库', 'class','class_getitem','delattr','dict','dir','doc','eq','format',' ge'、'getattr'、'getattribute'、'getitem'、'getstate'、'gt'、'hash'、'init'、'init_subclass ', 'le', 'lt', 'module', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr'、'sizeof'、'str'、'subclasshook'、'weakref'、'_dlltype']
函数“AccuracyGetNamedInt”在 DLL 中不可用。我应该尝试使用 DLL 的其他函数来获取变量的值吗?
任何帮助将不胜感激。
MC指示灯代码:
DefineDLLFunc: "C:\Program Files\TS Support\MultiCharts64\globalvariable.dll", double, "GV_SetNamedDouble", lpstr, double;
Variables: PythonTestValue(0);
inputs: DayValue(18);
if LastBarOnChart then
Begin
PythonTestValue = GV_SetNamedDouble("PythonTestVariable", DayValue);
Print("PythonTestVariable value is: ", PythonTestValue);
End;
RecalcLastBarAfter(1);
Python脚本代码:
import ctypes
dll_path = "c:\\Program Files\\TS Support\\MultiCharts64\\GlobalVariable.dll"
gv = ctypes.WinDLL(dll_path)
gv.GV_GetNamedDouble.argtypes = [ctypes.c_char_p, ctypes.c_double]
gv.GV_GetNamedDouble.restype = ctypes.c_double
variable_name = "PythonTestVariable".encode('utf-8')
default_value = ctypes.c_double(-1.0)
value = gv.GV_GetNamedDouble(variable_name, default_value)
print(f"The value of the global variable 'PythonTestVariable' is: {value}")