소스 검색

Fix MT5 EA: precision, instrument type, and cleanup improvements

- Increase candle price precision from %.5f to %.8f to match API requirements
- Change instrument type from 'metal' to 'commodity' for XAU/XAG symbols (API validation)
- Fix cleanup function to handle all timeframes instead of just default '1h'
- Update timer comments to accurately reflect 10-minute sync interval
- Improve cleanup to iterate through all timeframes (15m, 30m, 1h, 1D, 1W, 1M)

These fixes ensure:
- Candle data is sent with correct precision (8 decimals)
- Symbol creation won't fail for metal instruments
- Old candles are properly cleaned up across all timeframes
Hussain Afzal 8 달 전
부모
커밋
2b037e3119
1개의 변경된 파일19개의 추가작업 그리고 13개의 파일을 삭제
  1. 19 13
      MT5/Experts/MarketDataSender.mq5

+ 19 - 13
MT5/Experts/MarketDataSender.mq5

@@ -35,8 +35,8 @@ int OnInit()
 
    Print("✅ Symbols initialized: ", ArraySize(symbols));
    SendAllHistoricalCandles();
-   EventSetTimer(60);  // ⏱️ Trigger OnTimer() every 30 minutes
-   Print("✅ Timer set: SendAllHistoricalCandles() will run every 30 minutes.");
+   EventSetTimer(60);  // ⏱️ Trigger OnTimer() every 60 seconds
+   Print("✅ Timer set: SendAllHistoricalCandles() will run every 10 minutes (checked every 60 seconds).");
    return(INIT_SUCCEEDED);
 }
 
@@ -253,7 +253,7 @@ int CreateSymbolInDatabase(string symbolName)
    if(StringFind(symbolName, "BTC") == 0 || StringFind(symbolName, "ETH") == 0)
       instrumentType = "crypto";
    else if(StringFind(symbolName, "XAU") == 0 || StringFind(symbolName, "XAG") == 0)
-      instrumentType = "metal";
+      instrumentType = "commodity";  // ✅ Fixed: "metal" not in API validation, use "commodity"
    else if(StringFind(symbolName, "US30") == 0 || StringFind(symbolName, "NAS") == 0)
       instrumentType = "index";
    else
@@ -611,7 +611,7 @@ string BuildCandleJSONFromRates(int symbolId, MqlRates &rates[], int startIndex,
       double quoteVolume  = (r.real_volume > 0 ? r.real_volume : volume);
 
       string one = StringFormat(
-         "{\"symbolId\":%d,\"timeframe\":\"%s\",\"openTime\":\"%s\",\"closeTime\":\"%s\",\"open\":%.5f,\"high\":%.5f,\"low\":%.5f,\"close\":%.5f,\"volume\":%.5f,\"tradesCount\":%d,\"quoteVolume\":%.5f}",
+         "{\"symbolId\":%d,\"timeframe\":\"%s\",\"openTime\":\"%s\",\"closeTime\":\"%s\",\"open\":%.8f,\"high\":%.8f,\"low\":%.8f,\"close\":%.8f,\"volume\":%.8f,\"tradesCount\":%d,\"quoteVolume\":%.8f}",
          symbolId, timeframe, openTime, closeTime,
          r.open, r.high, r.low, r.close,
          volume, (int)volume, quoteVolume
@@ -668,11 +668,11 @@ bool SendJSON(string url, string json, string &response)
 
 //+------------------------------------------------------------------+
 //+------------------------------------------------------------------+
-//| Cleanup old candles (keep only last 1000)                        |
+//| Cleanup old candles (keep only last 1000) for a specific timeframe |
 //+------------------------------------------------------------------+
-void CleanupOldCandles(int symbolId)
+void CleanupOldCandles(int symbolId, string timeframe)
 {
-   string url = ApiBaseUrl + "/api/candles/cleanup/" + IntegerToString(symbolId) + "?keep=1000";
+   string url = ApiBaseUrl + "/api/candles/cleanup/" + IntegerToString(symbolId) + "?timeframe=" + timeframe + "&keep=1000";
    string headers = "Content-Type: application/json\r\n";
    string resultHeaders = "";
    char result[];
@@ -683,9 +683,9 @@ void CleanupOldCandles(int symbolId)
 
    string response = CharArrayToString(result);
    if(res == 200 || res == 204)
-      Print("🧹 Cleanup successful for symbolId=", symbolId, " → kept last 1000 candles.");
+      Print("🧹 Cleanup successful for symbolId=", symbolId, " timeframe=", timeframe, " → kept last 1000 candles.");
    else
-      Print("⚠️ Cleanup failed for symbolId=", symbolId, " HTTP=", res, " Response=", response);
+      Print("⚠️ Cleanup failed for symbolId=", symbolId, " timeframe=", timeframe, " HTTP=", res, " Response=", response);
 }
 
 //+------------------------------------------------------------------+
@@ -695,19 +695,25 @@ void OnTimer()
 {
    datetime now = TimeCurrent();
 
-   // ✅ Run full candle sync only once every minute
+   // ✅ Run full candle sync only once every 10 minutes (600 seconds)
    if(now - lastCandleSync >= 600)
    {
       Print("⏰ Running scheduled candle sync and cleanup...");
       SendAllHistoricalCandles();
 
-      // ✅ After uploading candles, clean up old ones
+      // ✅ After uploading candles, clean up old ones for all timeframes
       for(int i = 0; i < ArraySize(symbols); i++)
       {
          int symId = symbolIds[i];
          if(symId <= 0) continue;
-         CleanupOldCandles(symId);
-         Sleep(500); // small delay to avoid API overload
+         
+         // Clean up for each timeframe
+         for(int tfIndex = 0; tfIndex < ArraySize(Timeframes); tfIndex++)
+         {
+            string tfStr = TimeframeStrings[tfIndex];
+            CleanupOldCandles(symId, tfStr);
+            Sleep(300); // small delay to avoid API overload
+         }
       }
 
       lastCandleSync = now;