{"id":22895798,"url":"https://github.com/fractal-solutions/cortex","last_synced_at":"2025-05-07T20:24:36.507Z","repository":{"id":249771510,"uuid":"832438175","full_name":"fractal-solutions/cortex","owner":"fractal-solutions","description":"Deep Q Network implementation for use with MQL5 (DLL)","archived":false,"fork":false,"pushed_at":"2024-08-01T10:02:04.000Z","size":471,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-08-02T11:30:11.531Z","etag":null,"topics":["ai","deep-q-learning","deep-q-network","metatrader-5","mql5","mql5-api","neural-network"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fractal-solutions.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-23T03:09:42.000Z","updated_at":"2024-08-01T10:03:52.000Z","dependencies_parsed_at":"2024-08-01T11:23:09.401Z","dependency_job_id":null,"html_url":"https://github.com/fractal-solutions/cortex","commit_stats":null,"previous_names":["fractal-solutions/cortex"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fractal-solutions%2Fcortex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fractal-solutions%2Fcortex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fractal-solutions%2Fcortex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fractal-solutions%2Fcortex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fractal-solutions","download_url":"https://codeload.github.com/fractal-solutions/cortex/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229629256,"owners_count":18101263,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","deep-q-learning","deep-q-network","metatrader-5","mql5","mql5-api","neural-network"],"created_at":"2024-12-13T23:32:08.453Z","updated_at":"2024-12-13T23:32:09.125Z","avatar_url":"https://github.com/fractal-solutions.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DQN LIBRARY FOR MQL5\nCortex is a cutting-edge Deep Q-Network (DQN) library that's meticulously crafted for seamless integration with MetaTrader 5 (MQL5) 🚀. \n\nIt empowers traders to harness the advanced capabilities of reinforcement learning, taking their trading strategies to the next level 🚀📈. \n\nBy tapping into the power of deep Q-networks, Cortex enables the creation of sophisticated AI-driven decision-making processes that boost trading performance and maximize profits 💹💰.\n\n\n## How To .....\nThe program files are compiled to a DLL which can be used in MQL5 by placing in `MQL5/Libraries` folder. \n\nIncase you want to edit the library for optimization purposes you are free to tinker just remember to build so that you get your DLL file.\nThe NN has several optimization methods but the `'Adam'` is used by default. Since it is hard coded, you would have to change that in the `DQN.cpp` file then recompile the DLL for use.\n\n### Build Command for DLL\n`g++ -shared -o dqn.dll export.cpp DQN.cpp NeuralNetwork.cpp`\n\n## Using the DLL in MQL5\nIn your MQL5 code, you'll need to import the functions from your DLL and call them. Here's how you can set it up:\n\n#### Architecture Overview\n1. Initialization (OnInit):\n    \u003e Create the DQN instance.\n    \u003e Optionally load a pre-trained model.\n\n2. Tick Handling (OnTick):\n    \u003e Use the DQN to make decisions based on incoming market signals.\n    \u003e Optionally train the DQN based on actions taken and received rewards.\n\n3. Deinitialization (OnDeinit):\n    \u003e Save the current model state.\n    \u003e Clean up resources.\n\n\n### Example MQL5 Code:\nPlease note that some methods in the code are simply placeholders and you would need to implement your own logic depending on the library you use (CTrade, etc.)\n\n```\n\n// DLL IMPORT\n#import \"dqn.dll\"\nint CreateDQN(int stateSize, int actionSize, int \u0026hiddenLayers[], int hiddenLayersSize);\nint SelectAction(int dqn, double \u0026state[], int stateSize, double epsilon);\nvoid Train(int dqn, double \u0026state[], int stateSize, int action, double reward, double \u0026nextState[], int nextStateSize, double gamma, double epsilonDecay);\nvoid UpdateTargetNetwork(int dqn);\nvoid DestroyDQN(int dqn);\ndouble GetGamma(int dqn);\ndouble GetEpsilon(int dqn);\ndouble GetEpsilonDecay(int dqn);\ndouble GetQNetLoss(int dqn);\ndouble GetTargetNetLoss(int dqn);\nint SaveModel(int dqn, string filepath);\nint LoadModel(int dqn, string filepath);\n#import\n\n// Global variables\nint dqn;\ndouble gamma;\ndouble epsilon;\ndouble epsilonDecay;\nstring modelPath = \"DQN_Model.dat\";\n\n// Helper function to print error messages\nvoid PrintError(int errorCode) {\n    switch (errorCode) {\n        case 0:\n            Print(\"Operation successful.\");\n            break;\n        case 1:\n            Print(\"Error: Invalid parameters.\");\n            break;\n        case 2:\n            Print(\"Error: Failed to open file.\");\n            break;\n        default:\n            Print(\"Unknown error code: \", errorCode);\n            break;\n    }\n}\n\n// Initialization function\nint OnInit() {\n    int stateSize = 4;  // Example state size\n    int actionSize = 2; // Example action size\n    int hiddenLayers[] = {24, 24}; // Example hidden layers\n    int hiddenLayersSize = ArraySize(hiddenLayers);\n\n    // Create DQN instance\n    dqn = CreateDQN(stateSize, actionSize, hiddenLayers, hiddenLayersSize);\n\n    // Load model if available\n    int loadResult = LoadModel(dqn, modelPath);\n    PrintError(loadResult);\n\n    // Set DQN parameters\n    gamma = GetGamma(dqn);\n    epsilon = GetEpsilon(dqn);\n    epsilonDecay = GetEpsilonDecay(dqn);\n\n    return INIT_SUCCEEDED;\n}\n\n// Deinitialization function\nvoid OnDeinit(const int reason) {\n    // Save the model before exiting\n    int saveResult = SaveModel(dqn, modelPath);\n    PrintError(saveResult);\n\n    // Destroy the DQN instance\n    DestroyDQN(dqn);\n}\n\n// Function to handle new ticks\nvoid OnTick() {\n    // Example state (replace with actual market data)\n    double state[] = {iClose(Symbol(), PERIOD_M1, 1), iHigh(Symbol(), PERIOD_M1, 1), iLow(Symbol(), PERIOD_M1, 1), iVolume(Symbol(), PERIOD_M1, 1)};\n    \n    // Update DQN parameters\n    epsilon = GetEpsilon(dqn);\n\n    // Select an action\n    int action = SelectAction(dqn, state, ArraySize(state), epsilon);\n\n    // Execute the action (replace with actual trading logic)\n    if (action == 0) {\n        // Buy signal\n        if (OrderSelect(0, SELECT_BY_POS)) {\n            if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), Ask, 2);\n        }\n        OrderSend(Symbol(), OP_BUY, 0.1, Ask, 2, 0, 0);\n    } else {\n        // Sell signal\n        if (OrderSelect(0, SELECT_BY_POS)) {\n            if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), Bid, 2);\n        }\n        OrderSend(Symbol(), OP_SELL, 0.1, Bid, 2, 0, 0);\n    }\n\n    // Example reward (replace with actual reward calculation)\n    double reward = OrderProfit();\n\n    // Example next state (replace with actual market data)\n    double nextState[] = {iClose(Symbol(), PERIOD_M1, 0), iHigh(Symbol(), PERIOD_M1, 0), iLow(Symbol(), PERIOD_M1, 0), iVolume(Symbol(), PERIOD_M1, 0)};\n\n    // Train the model\n    Train(dqn, state, ArraySize(state), action, reward, nextState, ArraySize(nextState), gamma, epsilonDecay);\n\n    // Update the target network periodically\n    if (TimeCurrent() % 60 == 0) {\n        UpdateTargetNetwork(dqn);\n    }\n}\n\n\n```\n\n### ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffractal-solutions%2Fcortex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffractal-solutions%2Fcortex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffractal-solutions%2Fcortex/lists"}