Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // AnaBridge.cpp - Ultima versiune cu MessageBoxA pentru debugging
- #include "pch.h"
- #include <iostream>
- #include <vector>
- #include <string>
- #include <filesystem>
- #include <Windows.h> // Asigură-te că include <Windows.h> pentru MessageBoxA
- #include <locale>
- #include <codecvt>
- // Include the Python header directly
- #include <Python.h>
- // Declare pModule and pFunc as global or static in your AnaBridge.cpp
- static PyObject* pModule = nullptr;
- static PyObject* pFunc_getAnaDecision = nullptr;
- static PyObject* pFunc_reportClosedTrade = nullptr;
- // Helper function to print Python errors
- void PrintPythonError() {
- if (PyErr_Occurred()) {
- PyObject* ptype, * pvalue, * ptraceback;
- PyErr_Fetch(&ptype, &pvalue, &ptraceback);
- PyObject* pstr_value = nullptr;
- if (pvalue != nullptr) {
- pstr_value = PyObject_Str(pvalue);
- }
- std::cerr << "AnaBridge (Python Error): ";
- if (ptype != nullptr) {
- std::cerr << "Type: " << PyUnicode_AsUTF8(PyObject_Str(ptype)) << "; ";
- }
- if (pstr_value != nullptr) {
- std::cerr << "Value: " << PyUnicode_AsUTF8(pstr_value) << "; ";
- }
- // Note: For traceback, you might want to print the full traceback using traceback module
- // For simplicity, we just print type and value here.
- std::cerr << std::endl;
- Py_XDECREF(ptype);
- Py_XDECREF(pvalue);
- Py_XDECREF(ptraceback);
- Py_XDECREF(pstr_value);
- }
- }
- extern "C" _declspec(dllexport) bool InitializePython() {
- // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
- MessageBoxA(NULL, "AnaBridge: Entering InitializePython", "Debug", MB_OK | MB_ICONINFORMATION);
- // --- SFÂRȘIT ADĂUGĂRI ---
- std::cout << "AnaBridge (C++): Inițializarea punții Python...\n";
- if (Py_IsInitialized()) {
- std::cerr << "AnaBridge (C++): Avertisment: Python este deja inițializat.\n";
- return true;
- }
- PyConfig config;
- PyConfig_InitPythonConfig(&config);
- config.isolated_coop = 0;
- // Get current DLL path (MetaTrader terminal path)
- wchar_t dllPath[MAX_PATH];
- GetModuleFileNameW(GetModuleHandleW(L"AnaBridge.dll"), dllPath, MAX_PATH);
- std::filesystem::path currentDllDir = std::filesystem::path(dllPath).parent_path();
- // Use the hardcoded path for Python Home that you confirmed earlier
- // Te rog, ASIGURĂ-TE că aceasta este calea corectă către instalarea Python 3.12 (unde se află python312.dll)
- std::filesystem::path pythonHomePath = L"C:\\Program Files\\Python312";
- if (!std::filesystem::exists(pythonHomePath)) {
- std::cerr << "AnaBridge (C++): EROARE: Directorul Python HOME specificat nu există: " << pythonHomePath << std::endl;
- // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
- MessageBoxA(NULL, ("AnaBridge (C++): EROARE: Directorul Python HOME specificat nu există: " + pythonHomePath.string()).c_str(), "Debug", MB_OK | MB_ICONERROR);
- // --- SFÂRȘIT ADĂUGĂRI ---
- PyConfig_Clear(&config);
- return false;
- }
- config.home = Py_DecodeLocale(pythonHomePath.string().c_str(), nullptr);
- if (config.home == nullptr) {
- PrintPythonError();
- std::cerr << "AnaBridge (C++): EROARE: Nu s-a putut decoda calea Python Home." << std::endl;
- // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
- MessageBoxA(NULL, "AnaBridge (C++): EROARE: Nu s-a putut decoda calea Python Home.", "Debug", MB_OK | MB_ICONERROR);
- // --- SFÂRȘIT ADĂUGĂRI ---
- PyConfig_Clear(&config);
- return false;
- }
- std::cout << "AnaBridge (C++): Setare Python Home: " << pythonHomePath << std::endl;
- // Get the path to AnaBridge.py (assuming it's in the same directory as AnaBridge.dll for now)
- std::filesystem::path anaBridgePyPath = currentDllDir / "AnaBridge.py";
- if (!std::filesystem::exists(anaBridgePyPath)) {
- std::cerr << "AnaBridge (C++): EROARE: Scriptul AnaBridge.py nu a fost găsit în directorul DLL-ului: " << anaBridgePyPath << std::endl;
- // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
- MessageBoxA(NULL, ("AnaBridge (C++): EROARE: Scriptul AnaBridge.py nu a fost găsit: " + anaBridgePyPath.string()).c_str(), "Debug", MB_OK | MB_ICONERROR);
- // --- SFÂRȘIT ADĂUGĂRI ---
- PyConfig_Clear(&config);
- return false;
- }
- std::cout << "AnaBridge (C++): Calea catre scriptul AnaBridge.py: " << anaBridgePyPath << std::endl;
- // Set sys.path for Python to find our script
- // For Python 3.8+, use config.module_search_paths with PyConfig_SetBytesStringList
- std::string scriptPathNarrow(anaBridgePyPath.string().begin(), anaBridgePyPath.string().end());
- char* scriptPathPtr = (char*)scriptPathNarrow.c_str(); // Potentially unsafe, better use Py_DecodeLocale
- PyConfig_SetBytesStringList(&config, &config.module_search_paths, &scriptPathPtr, 1);
- // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
- MessageBoxA(NULL, "AnaBridge: About to call Py_InitializeFromConfig", "Debug", MB_OK | MB_ICONINFORMATION);
- // --- SFÂRȘIT ADĂUGĂRI ---
- if (Py_InitializeFromConfig(&config) < 0) {
- PrintPythonError();
- std::cerr << "AnaBridge (C++): EROARE: Py_InitializeFromConfig a eșuat.\n";
- // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
- MessageBoxA(NULL, "AnaBridge (C++): EROARE: Py_InitializeFromConfig a eșuat.", "Debug", MB_OK | MB_ICONERROR);
- // --- SFÂRȘIT ADĂUGĂRI ---
- PyConfig_Clear(&config);
- return false;
- }
- PyConfig_Clear(&config); // Clear the config after initialization
- std::cout << "AnaBridge (C++): Interpretorul Python a fost initializat cu succes.\n";
- // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
- MessageBoxA(NULL, "AnaBridge: Python initialized successfully!", "Debug", MB_OK | MB_ICONINFORMATION);
- // --- SFÂRȘIT ADĂUGĂRI ---
- // Import the Python module
- PyObject* pName = PyUnicode_DecodeFSDefault("AnaBridge"); // Name of your Python script (AnaBridge.py)
- if (pName == nullptr) {
- PrintPythonError();
- std::cerr << "AnaBridge (C++): EROARE: Nu s-a putut crea numele modulului Python." << std::endl;
- return false;
- }
- pModule = PyImport_Import(pName);
- Py_XDECREF(pName);
- if (pModule == nullptr) {
- PrintPythonError();
- std::cerr << "AnaBridge (C++): EROARE: Nu s-a putut importa modulul Python 'AnaBridge'." << std::endl;
- // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
- MessageBoxA(NULL, "AnaBridge (C++): EROARE: Nu s-a putut importa modulul Python 'AnaBridge'.", "Debug", MB_OK | MB_ICONERROR);
- // --- SFÂRȘIT ADĂUGĂRI ---
- return false;
- }
- std::cout << "AnaBridge (C++): Modulul Python 'AnaBridge' importat cu succes.\n";
- // Get the Python functions
- pFunc_getAnaDecision = PyObject_GetAttrString(pModule, "get_ana_decision_py");
- if (pFunc_getAnaDecision == nullptr || !PyCallable_Check(pFunc_getAnaDecision)) {
- PrintPythonError();
- std::cerr << "AnaBridge (C++): EROARE: Nu s-a putut găsi sau apela funcția 'get_ana_decision_py'." << std::endl;
- Py_XDECREF(pModule);
- pModule = nullptr;
- // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
- MessageBoxA(NULL, "AnaBridge (C++): EROARE: Nu s-a putut găsi 'get_ana_decision_py'.", "Debug", MB_OK | MB_ICONERROR);
- // --- SFÂRȘIT ADĂUGĂRI ---
- return false;
- }
- std::cout << "AnaBridge (C++): Funcția 'get_ana_decision_py' găsită cu succes.\n";
- pFunc_reportClosedTrade = PyObject_GetAttrString(pModule, "report_closed_trade_py");
- if (pFunc_reportClosedTrade == nullptr || !PyCallable_Check(pFunc_reportClosedTrade)) {
- PrintPythonError();
- std::cerr << "AnaBridge (C++): EROARE: Nu s-a putut găsi sau apela funcția 'report_closed_trade_py'." << std::endl;
- Py_XDECREF(pFunc_getAnaDecision);
- pFunc_getAnaDecision = nullptr;
- Py_XDECREF(pModule);
- pModule = nullptr;
- // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
- MessageBoxA(NULL, "AnaBridge (C++): EROARE: Nu s-a putut găsi 'report_closed_trade_py'.", "Debug", MB_OK | MB_ICONERROR);
- // --- SFÂRȘIT ADĂUGĂRI ---
- return false;
- }
- std::cout << "AnaBridge (C++): Funcția 'report_closed_trade_py' găsită cu succes.\n";
- // Call the Python initialization function within the script
- PyObject* pResult = PyObject_CallMethod(pModule, "initialize_ana_ai", nullptr);
- if (pResult == nullptr || !PyBool_Check(pResult) || PyObject_IsTrue(pResult) == 0) {
- PrintPythonError();
- std::cerr << "AnaBridge (C++): EROARE: Apelul la 'initialize_ana_ai' a eșuat sau a returnat false." << std::endl;
- Py_XDECREF(pFunc_getAnaDecision);
- pFunc_getAnaDecision = nullptr;
- Py_XDECREF(pFunc_reportClosedTrade);
- pFunc_reportClosedTrade = nullptr;
- Py_XDECREF(pModule);
- pModule = nullptr;
- Py_XDECREF(pResult);
- // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
- MessageBoxA(NULL, "AnaBridge (C++): EROARE: Apelul la 'initialize_ana_ai' a eșuat.", "Debug", MB_OK | MB_ICONERROR);
- // --- SFÂRȘIT ADĂUGĂRI ---
- return false;
- }
- Py_XDECREF(pResult);
- std::cout << "AnaBridge (C++): Funcția 'initialize_ana_ai' apelată cu succes.\n";
- // --- ADĂUGĂRI PENTRU DEBUGGING (MessageBoxA) ---
- MessageBoxA(NULL, "AnaBridge: All Python functions and internal initialization successful.", "Debug", MB_OK | MB_ICONINFORMATION);
- // --- SFÂRȘIT ADĂUGĂRI ---
- return true;
- }
- extern "C" _declspec(dllexport) int GetAnaDecision(const char* symbol_name, double current_price) {
- if (pFunc_getAnaDecision == nullptr) {
- std::cerr << "AnaBridge (C++): EROARE: Funcția Python 'get_ana_decision_py' nu este inițializată." << std::endl;
- return 0; // Return HOLD on error
- }
- PyObject* pArgs = Py_BuildValue("(sd)", symbol_name, current_price);
- if (pArgs == nullptr) {
- PrintPythonError();
- std::cerr << "AnaBridge (C++): Failed to build Python arguments for get_ana_decision." << std::endl;
- return 0;
- }
- PyObject* pValue = PyObject_CallObject(pFunc_getAnaDecision, pArgs);
- Py_XDECREF(pArgs); // Release arguments tuple
- if (pValue == nullptr) {
- PrintPythonError();
- std::cerr << "AnaBridge (C++): Call to Python 'get_ana_decision_py' failed." << std::endl;
- return 0; // Return HOLD on error
- }
- int decision = 0;
- if (PyLong_Check(pValue)) {
- decision = (int)PyLong_AsLong(pValue);
- } else {
- std::cerr << "AnaBridge (C++): Unexpected return type from get_ana_decision_py." << std::endl;
- }
- Py_XDECREF(pValue); // Release return value
- return decision;
- }
- 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) {
- if (pFunc_reportClosedTrade == nullptr) {
- std::cerr << "AnaBridge (C++): EROARE: Funcția Python 'report_closed_trade_py' nu este inițializată." << std::endl;
- return;
- }
- // Note: The format string should match the number and types of arguments.
- // If trade_type is int, and lots, prices, profit, duration are doubles, then "siddddd"
- PyObject* pArgs = Py_BuildValue("(siddddd)", symbol_name, trade_type, lots, open_price, close_price, profit, duration_seconds);
- if (pArgs == nullptr) {
- PrintPythonError();
- std::cerr << "AnaBridge (C++): Failed to build Python arguments for report_closed_trade." << std::endl;
- return;
- }
- PyObject* pValue = PyObject_CallObject(pFunc_reportClosedTrade, pArgs);
- Py_XDECREF(pArgs); // Release arguments tuple
- if (pValue == nullptr) {
- PrintPythonError();
- std::cerr << "AnaBridge (C++): Call to Python 'report_closed_trade' failed." << std::endl;
- } else {
- // If report_closed_trade returns something, you might want to check it.
- // For now, just decrement its reference count.
- Py_XDECREF(pValue);
- }
- }
- extern "C" _declspec(dllexport) void UninitializePython() {
- if (Py_IsInitialized()) {
- Py_XDECREF(pFunc_getAnaDecision);
- pFunc_getAnaDecision = nullptr;
- Py_XDECREF(pFunc_reportClosedTrade);
- pFunc_reportClosedTrade = nullptr;
- Py_XDECREF(pModule);
- pModule = nullptr;
- Py_Finalize();
- std::cout << "AnaBridge (C++): Interprerorul Python a fost dezinițializat.\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement