2026-02-27 21:49:28 -06:00 | | | #ifndef NN_DATA_LOGGER_MQH
|
| | | #define NN_DATA_LOGGER_MQH
|
2026-02-15 05:13:55 +00:00 | | | //+------------------------------------------------------------------+
|
2026-02-27 21:49:28 -06:00 | | | //| NNDataLogger.mqh |
|
| | | //| Real-trade labeling via MT5 deal history. |
|
| | | //| Phase 1 — NN_CaptureEntry(): snapshot features at trade open. |
|
| | | //| Phase 2 — NN_LabelFromDeal(): write labeled row when trade closes.|
|
| | | //| |
|
| | | //| Replaces the old speculative 180-bar lookahead approach. |
|
2026-02-15 05:13:55 +00:00 | | | //+------------------------------------------------------------------+
|
| | | #property strict
|
| | |
|
| | | #include <Files\File.mqh>
|
| | | #include "NNFeatures.mqh"
|
| | | #include "InputParams.mqh"
|
| | |
|
2026-02-27 21:49:28 -06:00 | | | // ─── Pending-entry store ─────────────────────────────────────────────────────
|
2026-02-15 05:13:55 +00:00 | | |
|
2026-02-27 21:49:28 -06:00 | | | struct NNPendingEntry
|
2026-02-15 05:13:55 +00:00 | | | {
|
2026-02-27 21:49:28 -06:00 | | | ulong position_id;
|
| | | bool is_buy;
|
| | | double entry_price;
|
| | | datetime entry_time;
|
| | | string setup_name;
|
| | | double cluster_strength;
|
| | | double conflict_score;
|
| | | int category; // 0 = continuation, 1 = counter
|
| | | double features[12];
|
| | | int n_features;
|
| | | };
|
| | |
|
| | | #define NN_MAX_PENDING 60 // safely above Max_Open_Trades (20) with headroom
|
| | |
|
| | | NNPendingEntry g_nn_pending[NN_MAX_PENDING];
|
| | | int g_nn_pending_count = 0;
|
| | |
|
| | | // ─── Phase 1: capture at trade open ─────────────────────────────────────────
|
| | | //
|
| | | // Call this immediately after a successful Trade.Buy() / Trade.Sell().
|
| | | // position_id — from HistoryDealGetInteger(entry_deal, DEAL_POSITION_ID)
|
| | | // is_buy — true for long, false for short
|
| | | // setup — reason string passed to Trade.Buy/Sell
|
| | | // cluster_strength, conflict_score — from Coordinator globals
|
| | | // category — 0 = continuation, 1 = counter
|
| | |
|
2026-11-10 06:24:52 -06:00 | | | // ─── Durable entry log (2026-11-10) ─────────────────────────────────────────
|
| | | // One line per opened position, written at open: MQL5/Files/<NN_ModelDir>/nn_entries.tsv.
|
| | | // The weekly trainer labels it from trade_executions (broker-reconciled P/L) by position id,
|
| | | // so nothing depends on this EA instance surviving until the close. The pending store below
|
| | | // lost most trades: it lives in memory, and recompiles/restarts/symbol rotation wiped it
|
| | | // (32 of ~465 November trades reached nn_dataset_trades.csv).
|
| | | void NN_LogEntryDurable(ulong position_id, bool is_buy, const string setup,
|
| | | double cluster_strength, double conflict_score, int category,
|
| | | const double &feat[], int n)
|
| | | {
|
| | | string path = NN_ModelDir + "\\nn_entries.tsv";
|
| | | int h = FileOpen(path, FILE_READ|FILE_WRITE|FILE_TXT|FILE_ANSI|FILE_SHARE_READ|FILE_SHARE_WRITE);
|
| | | if(h == INVALID_HANDLE) { Print("NN entry log open failed ", GetLastError()); return; }
|
| | | FileSeek(h, 0, SEEK_END);
|
| | | if(FileTell(h) == 0)
|
| | | FileWriteString(h, "time_utc\tposition_id\taccount\tsymbol\ttf\tis_buy\tsetup\tcluster_strength\tconflict_score\tcategory\tspread_points\tfeatures\n");
|
| | | string s = setup;
|
| | | StringReplace(s, "\t", " ");
|
| | | StringReplace(s, "\n", " ");
|
| | | string f = "";
|
| | | for(int i = 0; i < n; i++) f += (i ? "," : "") + DoubleToString(feat[i], 8);
|
| | | FileWriteString(h, TimeToString(TimeGMT(), TIME_DATE|TIME_SECONDS) + "\t" +
|
| | | IntegerToString((long)position_id) + "\t" +
|
| | | IntegerToString(AccountInfoInteger(ACCOUNT_LOGIN)) + "\t" +
|
| | | _Symbol + "\t" + IntegerToString(PeriodSeconds() / 60) + "\t" +
|
| | | (is_buy ? "1" : "0") + "\t" + s + "\t" +
|
| | | DoubleToString(cluster_strength, 2) + "\t" +
|
| | | DoubleToString(conflict_score, 2) + "\t" +
|
| | | IntegerToString(category) + "\t" +
|
| | | IntegerToString(SymbolInfoInteger(_Symbol, SYMBOL_SPREAD)) + "\t" + f + "\n");
|
| | | FileClose(h);
|
| | | }
|
| | |
|
2026-02-27 21:49:28 -06:00 | | | void NN_CaptureEntry(ulong position_id,
|
| | | bool is_buy,
|
| | | const string setup,
|
| | | double cluster_strength,
|
| | | double conflict_score,
|
| | | int category)
|
| | | {
|
| | | if(!NN_LogData) return;
|
2026-11-10 06:24:52 -06:00 | | |
|
| | | {
|
| | | double ef[];
|
| | | int en = BuildNNFeatures(ef);
|
| | | if(en > 0)
|
| | | NN_LogEntryDurable(position_id, is_buy, setup, cluster_strength, conflict_score, category, ef, en);
|
| | | }
|
2026-02-27 21:49:28 -06:00 | | | if(conflict_score > NN_MaxConflictToLog) return;
|
| | | if(g_nn_pending_count >= NN_MAX_PENDING) return;
|
2026-02-15 05:13:55 +00:00 | | |
|
2026-02-27 21:49:28 -06:00 | | | double feat[];
|
| | | int n = BuildNNFeatures(feat);
|
| | | if(n <= 0) return;
|
2026-02-15 05:13:55 +00:00 | | |
|
2026-02-27 21:49:28 -06:00 | | | int idx = g_nn_pending_count;
|
2026-02-15 05:13:55 +00:00 | | |
|
2026-02-27 21:49:28 -06:00 | | | g_nn_pending[idx].position_id = position_id;
|
| | | g_nn_pending[idx].is_buy = is_buy;
|
| | | g_nn_pending[idx].entry_price = is_buy
|
| | | ? SymbolInfoDouble(_Symbol, SYMBOL_ASK)
|
| | | : SymbolInfoDouble(_Symbol, SYMBOL_BID);
|
| | | g_nn_pending[idx].entry_time = TimeCurrent();
|
| | | g_nn_pending[idx].setup_name = setup;
|
| | | g_nn_pending[idx].cluster_strength = cluster_strength;
|
| | | g_nn_pending[idx].conflict_score = conflict_score;
|
| | | g_nn_pending[idx].category = category;
|
| | | g_nn_pending[idx].n_features = MathMin(n, 12);
|
2026-02-15 05:13:55 +00:00 | | |
|
2026-02-27 21:49:28 -06:00 | | | for(int i = 0; i < g_nn_pending[idx].n_features; i++)
|
| | | g_nn_pending[idx].features[i] = feat[i];
|
| | |
|
| | | g_nn_pending_count++;
|
2026-02-15 05:13:55 +00:00 | | | }
|
| | |
|
2026-02-27 21:49:28 -06:00 | | | // ─── Phase 2: label and write at trade close ─────────────────────────────────
|
| | | //
|
| | | // Call from OnTradeTransaction when entry == DEAL_ENTRY_OUT.
|
EA: sync live build (Jul-Nov 2026) — trade report direction/setup fix, VZ indicators, risk modules
- AIGateway/profitgtx: report_trade takes direction and setup from the position's
opening deal ("dir_src":"entry"); the closing deal inverted every direction and
restarts lost the setup (99% "auto")
- add MATheory, RiskGuards, RiskSizing, TradePolicy (setup blocklist, strict AI gate)
- add VZ_Core + VZ_Features/MFIB/Regime/Setups/Sessions indicators, VZ_FeaturesExport
service, VZ_DumpMA, VZ_TogglePause
- margin-aware sizing, news, labels and session updates since 2026-07-14
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 13:48:22 -06:00 | | | // Setup name recorded at entry for a position (empty if the trade was not captured).
|
| | | // Call BEFORE NN_LabelFromDeal(), which consumes the pending record.
|
| | | string NN_SetupForPosition(const ulong pos_id)
|
| | | {
|
| | | for(int i = 0; i < g_nn_pending_count; i++)
|
| | | if(g_nn_pending[i].position_id == pos_id)
|
| | | return g_nn_pending[i].setup_name;
|
| | | return "";
|
| | | }
|
| | |
|
2026-02-27 21:49:28 -06:00 | | | // deal_ticket must already be selected with HistoryDealSelect().
|
| | |
|
| | | void NN_LabelFromDeal(ulong deal_ticket)
|
2026-02-15 05:13:55 +00:00 | | | {
|
| | | if(!NN_LogData) return;
|
| | |
|
2026-02-27 21:49:28 -06:00 | | | ulong pos_id = (ulong)HistoryDealGetInteger(deal_ticket, DEAL_POSITION_ID);
|
2026-02-15 05:13:55 +00:00 | | |
|
2026-02-27 21:49:28 -06:00 | | | // Find the matching pending entry
|
| | | int idx = -1;
|
| | | for(int i = 0; i < g_nn_pending_count; i++)
|
| | | {
|
| | | if(g_nn_pending[i].position_id == pos_id)
|
| | | {
|
| | | idx = i;
|
| | | break;
|
| | | }
|
| | | }
|
| | | if(idx < 0) return; // trade wasn't captured (manual trade or already labeled)
|
2026-02-15 05:13:55 +00:00 | | |
|
2026-02-27 21:49:28 -06:00 | | | double profit = HistoryDealGetDouble(deal_ticket, DEAL_PROFIT);
|
| | | double exit_price = HistoryDealGetDouble(deal_ticket, DEAL_PRICE);
|
| | | datetime exit_time = (datetime)HistoryDealGetInteger(deal_ticket, DEAL_TIME);
|
| | | int dur_min = (int)((exit_time - g_nn_pending[idx].entry_time) / 60);
|
| | | int label = (profit > 0.0) ? 1 : 0; // 1 = win, 0 = loss/breakeven
|
2026-02-15 05:13:55 +00:00 | | |
|
2026-02-27 21:49:28 -06:00 | | | // Write CSV row
|
2026-02-15 05:13:55 +00:00 | | | int h = FileOpen(NN_LogFileName, FILE_READ|FILE_WRITE|FILE_CSV|FILE_ANSI);
|
2026-02-27 21:49:28 -06:00 | | | if(h != INVALID_HANDLE)
|
| | | {
|
| | | FileSeek(h, 0, SEEK_END);
|
| | |
|
| | | if(FileSize(h) == 0)
|
| | | {
|
| | | FileWrite(h,
|
| | | "entry_time", "exit_time", "symbol", "tf",
|
| | | "is_buy", "category", "setup",
|
| | | "cluster_strength", "conflict_score",
|
| | | "entry_price", "exit_price",
|
| | | "profit", "label", "duration_min",
|
| | | "n_features", "features_json"
|
| | | );
|
| | | }
|
2026-02-15 05:13:55 +00:00 | | |
|
2026-02-27 21:49:28 -06:00 | | | string fjson = "[";
|
| | | for(int k = 0; k < g_nn_pending[idx].n_features; k++)
|
| | | {
|
| | | fjson += DoubleToString(g_nn_pending[idx].features[k], 8);
|
| | | if(k < g_nn_pending[idx].n_features - 1) fjson += ",";
|
| | | }
|
| | | fjson += "]";
|
2026-02-15 05:13:55 +00:00 | | |
|
| | | FileWrite(h,
|
2026-02-27 21:49:28 -06:00 | | | (string)g_nn_pending[idx].entry_time,
|
| | | (string)exit_time,
|
| | | _Symbol,
|
| | | (string)Period(),
|
| | | (g_nn_pending[idx].is_buy ? 1 : 0),
|
| | | g_nn_pending[idx].category,
|
| | | g_nn_pending[idx].setup_name,
|
| | | DoubleToString(g_nn_pending[idx].cluster_strength, 2),
|
| | | DoubleToString(g_nn_pending[idx].conflict_score, 2),
|
| | | DoubleToString(g_nn_pending[idx].entry_price, _Digits),
|
| | | DoubleToString(exit_price, _Digits),
|
| | | DoubleToString(profit, 2),
|
| | | label,
|
| | | dur_min,
|
| | | g_nn_pending[idx].n_features,
|
| | | fjson
|
2026-02-15 05:13:55 +00:00 | | | );
|
| | |
|
2026-02-27 21:49:28 -06:00 | | | FileClose(h);
|
2026-02-15 05:13:55 +00:00 | | | }
|
2026-02-27 21:49:28 -06:00 | | |
|
| | | // Remove from pending array (shift remaining entries down)
|
| | | for(int i = idx; i < g_nn_pending_count - 1; i++)
|
| | | g_nn_pending[i] = g_nn_pending[i + 1];
|
| | | g_nn_pending_count--;
|
2026-02-15 05:13:55 +00:00 | | | }
|
| | |
|
2026-02-27 21:49:28 -06:00 | | | #endif
|