Untitled
unknown
plain_text
a year ago
5.6 kB
8
Indexable
Never
//+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ #include <WinUser32.mqh> #import "user32.dll" bool BlockInput(bool flag); #import // Define global variables int lastOrderTicket = 0; int timerHandle; // Timer handle for sending data at intervals // Define input parameters input string google_sheets_api_credentials = "path_to_your_credentials.json"; // Replace with the path to your API credentials JSON file input string google_sheets_url = "your_default_google_sheets_url"; // Default Google Sheets URL input int updateIntervalMinutes = 15; // Update interval in minutes //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { // Initialize API credentials if (!CredentialsInit(google_sheets_api_credentials)) { Print("Failed to initialize Google Sheets API credentials."); return(INIT_FAILED); } // Set up a timer to send data at the specified interval timerHandle = EventSetTimer(updateIntervalMinutes * 60 * 1000); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { // Deinitialize any resources here EventKillTimer(timerHandle); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTimer() { // Fetch order data string orderData = GetOrderData(); // Send order data to Google Sheets if it's not empty and if the order is new if (StringLen(orderData) > 0) { if (!SendToGoogleSheets(orderData)) { Print("Failed to send data to Google Sheets"); } } } // Implement the CredentialsInit function for initializing Google Sheets API credentials bool CredentialsInit(string credentialsFile) { string jsonContents; // Read the contents of the credentials file if (FileReadString(credentialsFile, jsonContents) == -1) { Print("Failed to read credentials file."); return (false); } // Parse the JSON contents to extract the client ID, client secret, etc. // Initialize the API client using your extracted credentials // Return true if the initialization is successful, false otherwise return (true); // Replace with your implementation } // Implement the GetOrderData function to fetch order data from MetaTrader 5 string GetOrderData() { int totalOrders = OrdersTotal(); if (totalOrders > 0) { // Select the most recent order if (OrderSelect(totalOrders - 1, SELECT_BY_POS, MODE_TRADES)) { int orderTicket = OrderTicket(); datetime orderDate = TimeCurrent(); datetime openTime = OrderOpenTime(); datetime closeTime = OrderCloseTime(); string symbol = OrderSymbol(); double volume = OrderLots(); string action = (OrderType() == OP_BUY) ? "Buy" : "Sell"; double stopLoss = OrderStopLoss(); double takeProfit = OrderTakeProfit(); double profitPips = OrderProfit(); double commissions = OrderCommission(); double profitValue = OrderProfitCheck(symbol, OP_BUY) + OrderProfitCheck(symbol, OP_SELL); string comment = OrderComment(); // Include the manual columns in the data sent to Google Sheets string orderData = IntegerToString(orderTicket) + "," + TimeToString(orderDate, TIME_DATE|TIME_MINUTES) + "," + TimeToString(openTime, TIME_DATE|TIME_MINUTES) + "," + TimeToString(closeTime, TIME_DATE|TIME_MINUTES) + "," + symbol + "," + DoubleToString(volume, 2) + "," + action + "," + DoubleToString(stopLoss, MarketInfo(symbol, MODE_DIGITS)) + "," + DoubleToString(takeProfit, MarketInfo(symbol, MODE_DIGITS)) + "," + DoubleToString(profitPips, 2) + "," + DoubleToString(commissions, 2) + "," + DoubleToString(profitValue, 2) + "," + comment; return (orderData); } } return (""); // Return an empty string if no new order data } // Implement the SendToGoogleSheets function to send data to Google Sheets bool SendToGoogleSheets(string data) { // Get the current month and year for sheet naming string currentMonthYear = TimeToString(TimeCurrent(), TIME_YEAR | TIME_MON); // Construct the sheet name (e.g., "Orders_January_2023") string sheetName = "Orders_" + currentMonthYear; string headers; string result; // Construct the HTTP request headers headers = "Content-Type: application/x-www-form-urlencoded\r\n"; // Construct the HTTP POST data string post_data = "sheetName=" + sheetName + "&data=" + data; // Send the HTTP POST request to Google Sheets int res = WebRequest("POST", google_sheets_url, headers, post_data, 5000, result, headers); if (res == 200) { Print("Data sent successfully to Google Sheets."); return (true); } else { Print("Failed to send data to Google Sheets. HTTP response code: ", res); return (false); } }