Advertisement
cjmt2

Untitled

Jun 18th, 2025 (edited)
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.70 KB | None | 0 0
  1. // AnaBridge.cpp - Ultima versiune cu MessageBoxA pentru debugging
  2.  
  3. #include "pch.h"
  4. #include <iostream>
  5. #include <vector>
  6. #include <string>
  7. #include <filesystem>
  8. #include <Windows.h> // Asigură-te că include <Windows.h> pentru MessageBoxA
  9. #include <locale>
  10. #include <codecvt>
  11.  
  12. // Include the Python header directly
  13. #include <Python.h>
  14.  
  15. // Declare pModule and pFunc as global or static in your AnaBridge.cpp
  16. static PyObject* pModule = nullptr;
  17. static PyObject* pFunc_getAnaDecision = nullptr;
  18. static PyObject* pFunc_reportClosedTrade = nullptr;
  19.  
  20. // Helper function to print Python errors
  21. void PrintPythonError() {
  22. if (PyErr_Occurred()) {
  23. PyObject* ptype, * pvalue, * ptraceback;
  24. PyErr_Fetch(&ptype, &pvalue, &ptraceback);
  25.  
  26. PyObject* pstr_value = nullptr;
  27. if (pvalue != nullptr) {
  28. pstr_value = PyObject_Str(pvalue);
  29. }
  30.  
  31. std::cerr << "AnaBridge (Python Error): ";
  32. if (ptype != nullptr) {
  33. std::cerr << "Type: " << PyUnicode_AsUTF8(PyObject_Str(ptype)) << "; ";
  34. }
  35. if (pstr_value != nullptr) {
  36. std::cerr << "Value: " << PyUnicode_AsUTF8(pstr_value) << "; ";
  37. }
  38. // Note: For traceback, you might want to print the full traceback using traceback module
  39. // For simplicity, we just print type and value here.
  40. std::cerr << std::endl;
  41.  
  42. Py_XDECREF(ptype);
  43. Py_XDECREF(pvalue);
  44. Py_XDECREF(ptraceback);
  45. Py_XDECREF(pstr_value);
  46. }
  47. }
  48.  
  49.  
  50. extern "C" _declspec(dllexport) bool InitializePython() {
  51. // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
  52. MessageBoxA(NULL, "AnaBridge: Entering InitializePython", "Debug", MB_OK | MB_ICONINFORMATION);
  53. // --- SFÂRȘIT ADĂUGĂRI ---
  54.  
  55. std::cout << "AnaBridge (C++): Inițializarea punții Python...\n";
  56. if (Py_IsInitialized()) {
  57. std::cerr << "AnaBridge (C++): Avertisment: Python este deja inițializat.\n";
  58. return true;
  59. }
  60.  
  61. PyConfig config;
  62. PyConfig_InitPythonConfig(&config);
  63. config.isolated_coop = 0;
  64.  
  65. // Get current DLL path (MetaTrader terminal path)
  66. wchar_t dllPath[MAX_PATH];
  67. GetModuleFileNameW(GetModuleHandleW(L"AnaBridge.dll"), dllPath, MAX_PATH);
  68. std::filesystem::path currentDllDir = std::filesystem::path(dllPath).parent_path();
  69.  
  70. // Use the hardcoded path for Python Home that you confirmed earlier
  71. // Te rog, ASIGURĂ-TE că aceasta este calea corectă către instalarea Python 3.12 (unde se află python312.dll)
  72. std::filesystem::path pythonHomePath = L"C:\\Program Files\\Python312";
  73.  
  74. if (!std::filesystem::exists(pythonHomePath)) {
  75. std::cerr << "AnaBridge (C++): EROARE: Directorul Python HOME specificat nu există: " << pythonHomePath << std::endl;
  76. // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
  77. MessageBoxA(NULL, ("AnaBridge (C++): EROARE: Directorul Python HOME specificat nu există: " + pythonHomePath.string()).c_str(), "Debug", MB_OK | MB_ICONERROR);
  78. // --- SFÂRȘIT ADĂUGĂRI ---
  79. PyConfig_Clear(&config);
  80. return false;
  81. }
  82.  
  83. config.home = Py_DecodeLocale(pythonHomePath.string().c_str(), nullptr);
  84. if (config.home == nullptr) {
  85. PrintPythonError();
  86. std::cerr << "AnaBridge (C++): EROARE: Nu s-a putut decoda calea Python Home." << std::endl;
  87. // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
  88. MessageBoxA(NULL, "AnaBridge (C++): EROARE: Nu s-a putut decoda calea Python Home.", "Debug", MB_OK | MB_ICONERROR);
  89. // --- SFÂRȘIT ADĂUGĂRI ---
  90. PyConfig_Clear(&config);
  91. return false;
  92. }
  93. std::cout << "AnaBridge (C++): Setare Python Home: " << pythonHomePath << std::endl;
  94.  
  95. // Get the path to AnaBridge.py (assuming it's in the same directory as AnaBridge.dll for now)
  96. std::filesystem::path anaBridgePyPath = currentDllDir / "AnaBridge.py";
  97. if (!std::filesystem::exists(anaBridgePyPath)) {
  98. std::cerr << "AnaBridge (C++): EROARE: Scriptul AnaBridge.py nu a fost găsit în directorul DLL-ului: " << anaBridgePyPath << std::endl;
  99. // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
  100. MessageBoxA(NULL, ("AnaBridge (C++): EROARE: Scriptul AnaBridge.py nu a fost găsit: " + anaBridgePyPath.string()).c_str(), "Debug", MB_OK | MB_ICONERROR);
  101. // --- SFÂRȘIT ADĂUGĂRI ---
  102. PyConfig_Clear(&config);
  103. return false;
  104. }
  105. std::cout << "AnaBridge (C++): Calea catre scriptul AnaBridge.py: " << anaBridgePyPath << std::endl;
  106.  
  107. // Set sys.path for Python to find our script
  108. // For Python 3.8+, use config.module_search_paths with PyConfig_SetBytesStringList
  109. std::string scriptPathNarrow(anaBridgePyPath.string().begin(), anaBridgePyPath.string().end());
  110. char* scriptPathPtr = (char*)scriptPathNarrow.c_str(); // Potentially unsafe, better use Py_DecodeLocale
  111.  
  112. PyConfig_SetBytesStringList(&config, &config.module_search_paths, &scriptPathPtr, 1);
  113.  
  114. // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
  115. MessageBoxA(NULL, "AnaBridge: About to call Py_InitializeFromConfig", "Debug", MB_OK | MB_ICONINFORMATION);
  116. // --- SFÂRȘIT ADĂUGĂRI ---
  117.  
  118. if (Py_InitializeFromConfig(&config) < 0) {
  119. PrintPythonError();
  120. std::cerr << "AnaBridge (C++): EROARE: Py_InitializeFromConfig a eșuat.\n";
  121. // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
  122. MessageBoxA(NULL, "AnaBridge (C++): EROARE: Py_InitializeFromConfig a eșuat.", "Debug", MB_OK | MB_ICONERROR);
  123. // --- SFÂRȘIT ADĂUGĂRI ---
  124. PyConfig_Clear(&config);
  125. return false;
  126. }
  127. PyConfig_Clear(&config); // Clear the config after initialization
  128.  
  129. std::cout << "AnaBridge (C++): Interpretorul Python a fost initializat cu succes.\n";
  130. // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
  131. MessageBoxA(NULL, "AnaBridge: Python initialized successfully!", "Debug", MB_OK | MB_ICONINFORMATION);
  132. // --- SFÂRȘIT ADĂUGĂRI ---
  133.  
  134.  
  135. // Import the Python module
  136. PyObject* pName = PyUnicode_DecodeFSDefault("AnaBridge"); // Name of your Python script (AnaBridge.py)
  137. if (pName == nullptr) {
  138. PrintPythonError();
  139. std::cerr << "AnaBridge (C++): EROARE: Nu s-a putut crea numele modulului Python." << std::endl;
  140. return false;
  141. }
  142.  
  143. pModule = PyImport_Import(pName);
  144. Py_XDECREF(pName);
  145.  
  146. if (pModule == nullptr) {
  147. PrintPythonError();
  148. std::cerr << "AnaBridge (C++): EROARE: Nu s-a putut importa modulul Python 'AnaBridge'." << std::endl;
  149. // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
  150. MessageBoxA(NULL, "AnaBridge (C++): EROARE: Nu s-a putut importa modulul Python 'AnaBridge'.", "Debug", MB_OK | MB_ICONERROR);
  151. // --- SFÂRȘIT ADĂUGĂRI ---
  152. return false;
  153. }
  154. std::cout << "AnaBridge (C++): Modulul Python 'AnaBridge' importat cu succes.\n";
  155.  
  156.  
  157. // Get the Python functions
  158. pFunc_getAnaDecision = PyObject_GetAttrString(pModule, "get_ana_decision_py");
  159. if (pFunc_getAnaDecision == nullptr || !PyCallable_Check(pFunc_getAnaDecision)) {
  160. PrintPythonError();
  161. std::cerr << "AnaBridge (C++): EROARE: Nu s-a putut găsi sau apela funcția 'get_ana_decision_py'." << std::endl;
  162. Py_XDECREF(pModule);
  163. pModule = nullptr;
  164. // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
  165. MessageBoxA(NULL, "AnaBridge (C++): EROARE: Nu s-a putut găsi 'get_ana_decision_py'.", "Debug", MB_OK | MB_ICONERROR);
  166. // --- SFÂRȘIT ADĂUGĂRI ---
  167. return false;
  168. }
  169. std::cout << "AnaBridge (C++): Funcția 'get_ana_decision_py' găsită cu succes.\n";
  170.  
  171. pFunc_reportClosedTrade = PyObject_GetAttrString(pModule, "report_closed_trade_py");
  172. if (pFunc_reportClosedTrade == nullptr || !PyCallable_Check(pFunc_reportClosedTrade)) {
  173. PrintPythonError();
  174. std::cerr << "AnaBridge (C++): EROARE: Nu s-a putut găsi sau apela funcția 'report_closed_trade_py'." << std::endl;
  175. Py_XDECREF(pFunc_getAnaDecision);
  176. pFunc_getAnaDecision = nullptr;
  177. Py_XDECREF(pModule);
  178. pModule = nullptr;
  179. // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
  180. MessageBoxA(NULL, "AnaBridge (C++): EROARE: Nu s-a putut găsi 'report_closed_trade_py'.", "Debug", MB_OK | MB_ICONERROR);
  181. // --- SFÂRȘIT ADĂUGĂRI ---
  182. return false;
  183. }
  184. std::cout << "AnaBridge (C++): Funcția 'report_closed_trade_py' găsită cu succes.\n";
  185.  
  186. // Call the Python initialization function within the script
  187. PyObject* pResult = PyObject_CallMethod(pModule, "initialize_ana_ai", nullptr);
  188. if (pResult == nullptr || !PyBool_Check(pResult) || PyObject_IsTrue(pResult) == 0) {
  189. PrintPythonError();
  190. std::cerr << "AnaBridge (C++): EROARE: Apelul la 'initialize_ana_ai' a eșuat sau a returnat false." << std::endl;
  191. Py_XDECREF(pFunc_getAnaDecision);
  192. pFunc_getAnaDecision = nullptr;
  193. Py_XDECREF(pFunc_reportClosedTrade);
  194. pFunc_reportClosedTrade = nullptr;
  195. Py_XDECREF(pModule);
  196. pModule = nullptr;
  197. Py_XDECREF(pResult);
  198. // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
  199. MessageBoxA(NULL, "AnaBridge (C++): EROARE: Apelul la 'initialize_ana_ai' a eșuat.", "Debug", MB_OK | MB_ICONERROR);
  200. // --- SFÂRȘIT ADĂUGĂRI ---
  201. return false;
  202. }
  203. Py_XDECREF(pResult);
  204. std::cout << "AnaBridge (C++): Funcția 'initialize_ana_ai' apelată cu succes.\n";
  205. // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
  206. MessageBoxA(NULL, "AnaBridge: All Python functions and internal initialization successful.", "Debug", MB_OK | MB_ICONINFORMATION);
  207. // --- SFÂRȘIT ADĂUGĂRI ---
  208. return true;
  209. }
  210.  
  211.  
  212. extern "C" _declspec(dllexport) int GetAnaDecision(const char* symbol_name, double current_price) {
  213. if (pFunc_getAnaDecision == nullptr) {
  214. std::cerr << "AnaBridge (C++): EROARE: Funcția Python 'get_ana_decision_py' nu este inițializată." << std::endl;
  215. return 0; // Return HOLD on error
  216. }
  217.  
  218. PyObject* pArgs = Py_BuildValue("(sd)", symbol_name, current_price);
  219. if (pArgs == nullptr) {
  220. PrintPythonError();
  221. std::cerr << "AnaBridge (C++): Failed to build Python arguments for get_ana_decision." << std::endl;
  222. return 0;
  223. }
  224.  
  225. PyObject* pValue = PyObject_CallObject(pFunc_getAnaDecision, pArgs);
  226. Py_XDECREF(pArgs); // Release arguments tuple
  227.  
  228. if (pValue == nullptr) {
  229. PrintPythonError();
  230. std::cerr << "AnaBridge (C++): Call to Python 'get_ana_decision_py' failed." << std::endl;
  231. return 0; // Return HOLD on error
  232. }
  233.  
  234. int decision = 0;
  235. if (PyLong_Check(pValue)) {
  236. decision = (int)PyLong_AsLong(pValue);
  237. } else {
  238. std::cerr << "AnaBridge (C++): Unexpected return type from get_ana_decision_py." << std::endl;
  239. }
  240.  
  241. Py_XDECREF(pValue); // Release return value
  242. return decision;
  243. }
  244.  
  245. extern "C" _declspec(dllexport) void ReportClosedTrade(const char* symbol_name, int trade_type, double lots, double open_price, double close_price, double profit, double duration_seconds) {
  246. if (pFunc_reportClosedTrade == nullptr) {
  247. std::cerr << "AnaBridge (C++): EROARE: Funcția Python 'report_closed_trade_py' nu este inițializată." << std::endl;
  248. return;
  249. }
  250.  
  251. // Note: The format string should match the number and types of arguments.
  252. // If trade_type is int, and lots, prices, profit, duration are doubles, then "siddddd"
  253. PyObject* pArgs = Py_BuildValue("(siddddd)", symbol_name, trade_type, lots, open_price, close_price, profit, duration_seconds);
  254. if (pArgs == nullptr) {
  255. PrintPythonError();
  256. std::cerr << "AnaBridge (C++): Failed to build Python arguments for report_closed_trade." << std::endl;
  257. return;
  258. }
  259.  
  260. PyObject* pValue = PyObject_CallObject(pFunc_reportClosedTrade, pArgs);
  261. Py_XDECREF(pArgs); // Release arguments tuple
  262.  
  263. if (pValue == nullptr) {
  264. PrintPythonError();
  265. std::cerr << "AnaBridge (C++): Call to Python 'report_closed_trade' failed." << std::endl;
  266. } else {
  267. // If report_closed_trade returns something, you might want to check it.
  268. // For now, just decrement its reference count.
  269. Py_XDECREF(pValue);
  270. }
  271. }
  272.  
  273.  
  274. extern "C" _declspec(dllexport) void UninitializePython() {
  275. if (Py_IsInitialized()) {
  276. Py_XDECREF(pFunc_getAnaDecision);
  277. pFunc_getAnaDecision = nullptr;
  278. Py_XDECREF(pFunc_reportClosedTrade);
  279. pFunc_reportClosedTrade = nullptr;
  280. Py_XDECREF(pModule);
  281. pModule = nullptr;
  282. Py_Finalize();
  283. std::cout << "AnaBridge (C++): Interprerorul Python a fost dezinițializat.\n";
  284. }
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement