How to code and compile mql4 and mql5 language in VS Code?

How to code and compile mql4 and mql5 language in VS Code?

Manage alerts

Loading saved threads...

Hozaifa Bin Masud · External communityPost link
External question — Stack Overflow Stack Exchange Author: Hozaifa Bin Masud Original post: https://stackoverflow.com/questions/71060499 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I moved to mql4 and mql5 programming language. But, MetaEditor is very noob to code with, so I want to code mql4 language and compile with VS Code. If you have any solution please share with us..... Thanks
Quote
Report
user3666197 · External communityPost link
External answer — Stack Overflow Stack Exchange Author: user3666197 Original post: https://stackoverflow.com/a/71102563 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. In the very early years of MQL4 ( MetaTrader Terminal, versions in about 2008-2010 ), there was a separate compiler programme, thus was configurable into other IDE ( me, using Geany-IDE ecosystem for doing this since ever ) After MQL4 language-revision, somewhere about 2009-2010 iirc, this option ceased to exist. Inside MQL5 ecosystem, this option was available never. The maximum achievable comfort thus remains in code-syntax highlights, code-completion configurations, if doable & done manually for the sake of your individual preferences ( me, using Geany-IDE ecosystem, being happy with MQL4-language configurations ( revised whenever MQL4 took further language specification revisions ) & copy/paste "technology" or #include -trick for moving between the two distinct ecosystems )
Quote
Report
JacyLouis · External communityPost link
External answer — Stack Overflow Stack Exchange Author: JacyLouis Original post: https://stackoverflow.com/a/74557318 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. There is a Extention(Plugin) in the VS Code called "MQL Tools", that can make you very easy to code MQL4 or MQL5 language , and compile with VS Code. You should try it.
Quote
Report
Michel Jongbloed · External communityPost link
External answer — Stack Overflow Stack Exchange Author: Michel Jongbloed Original post: https://stackoverflow.com/a/74759171 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. If you open the Extensions tab and filter on MQL4, you'll find a MQL4 Syntax Highlight extension that also works for MQL5, and you'll find a MQL Tools extension that will add compilation options to VSCode. Works perfect!
Quote
Report
Santhosh · External communityPost link
External answer — Stack Overflow Stack Exchange Author: Santhosh Original post: https://stackoverflow.com/a/79443560 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ #include <Trade\Trade.mqh> // Include the CTrade class CTrade trade; // Declare an instance of the CTrade class // Input variables for adjustable settings input double LotSize1 = 0.01; // Lot size for the 1st trade input double LotSize2 = 0.01; // Lot size for the 2nd trade input double LotSize3 = 0.01; // Lot size for the 3rd trade input double LotSize4 = 0.01; // Lot size for the 4th trade input double LotSize5 = 0.03; // Lot size for the 5th trade input double LotSize6 = 0.05; // Lot size for the 6th trade input double LotSize7 = 0.08; // Lot size for the 7th trade input double LotSize8 = 0.14; // Lot size for the 8th trade input double LotSize9 = 0.23; // Lot size for the 9th trade input double LotSize10 = 0.38; // Lot size for the 10th trade input double TakeProfitPips = 20; // Take profit in pips input double StopLossPips = 10; // Stop loss in pips int OnInit() { // Initialization code Print("EA initialized successfully."); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { // Deinitialization code Print("EA deinitialized."); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { static int tradeCount = 0; // Counter for the number of trades in the cycle static double lastOrderPrice = 0; // Price of the last order placed // Array to store lot sizes (populated from input variables) double lotSizes[10] = {LotSize1, LotSize2, LotSize3, LotSize4, LotSize5, LotSize6, LotSize7, LotSize8, LotSize9, LotSize10}; // Get the current bid price double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID); // Calculate the nearest round number double roundNumber = MathRound(currentPrice / 1.0) * 1.0; // Round to the nearest whole number (e.g., 2000.000) // Debugging: Print current price and round number Print("Current Price: ", currentPrice, " | Round Number: ", roundNumber); // Check if no orders are open if (PositionsTotal() == 0) { // Place the first buy order at the nearest round number lastOrderPrice = roundNumber; tradeCount = 0; Print("Attempting to place a buy order at round number: ", roundNumber); PlaceBuyOrder(lotSizes[tradeCount], roundNumber); } else { // Check the profit/loss of the latest order double profitPips = (currentPrice - lastOrderPrice) / _Point; // Debugging: Print profit/loss in pips Print("Current Profit/Loss: ", profitPips, " pips"); // If profit reaches the take profit level, close all orders and reset the cycle if (profitPips >= TakeProfitPips) { Print("Take profit reached. Closing all orders."); CloseAllOrders(); tradeCount = 0; return; } // If loss reaches the stop loss level, place another buy order with the next lot size if (profitPips <= -StopLossPips && tradeCount < ArraySize(lotSizes) - 1) { tradeCount++; double nextLotSize = lotSizes[tradeCount]; lastOrderPrice = currentPrice; Print("Stop loss hit. Placing next buy order with lot size: ", nextLotSize); PlaceBuyOrder(nextLotSize, currentPrice); } } } //+------------------------------------------------------------------+ //| Function to place a buy order | //+------------------------------------------------------------------+ void PlaceBuyOrder(double lotSize, double price) { // Create a trade request MqlTradeRequest request; MqlTradeResult result; ZeroMemory(request); ZeroMemory(result); request.action = TRADE_ACTION_DEAL; request.symbol = _Symbol; request.volume = lotSize; request.type = ORDER_TYPE_BUY; request.price = price; request.deviation = 10; // Allowable slippage in points request.type_filling = ORDER_FILLING_IOC; // Immediate or Cancel // Send the trade request if (!OrderSend(request, result)) { Print("Error placing buy order. Error code: ", GetLastError()); } else { Print("Buy order placed successfully. Ticket: ", result.order); } } //+------------------------------------------------------------------+ //| Function to close all open orders | //+------------------------------------------------------------------+ void CloseAllOrders() { for (int i = PositionsTotal() - 1; i >= 0; i--) { ulong ticket = PositionGetTicket(i); if (!trade.PositionClose(ticket)) { Print("Error closing order. Error code: ", GetLastError()); } else { Print("Order closed successfully. Ticket: ", ticket); } } } //+------------------------------------------------------------------+
Quote
Report

Post Reply

Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: JacyLouis Source score (net votes, not local likes): 4 Original post: https://stackoverflow.com/a/74557318 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. There is a Extention(Plugin) in the VS Code called "MQL Tools", that can make you very easy to code MQL4 or MQL5 language , and compile with VS Code. You should try it.

Cancel quote

Checking account access…