Vizion-Trading-EA/VZ_FeaturesExport.mq5
132 lines
7.3 KiB
MQL5
//+------------------------------------------------------------------+
//| VZ_FeaturesExport.mq5 |
//| VizionAI — MT5 SERVICE. Every Interval_Seconds, for each symbol |
//| x timeframe, computes the VZ_Core feature set (same math as the |
//| EA and the VZ_* indicators) and writes |
//| MQL5\Files\vizion\snapshot_<SYMBOL>_<TF>.json |
//| which vizion-mt5-rest serves as /snapshot/{account}/{sym}/{tf} |
//| to n8n (Market Data Snapshot, Signal Generator) and vizion-nn. |
//| No terminal IPC needed: files are the interface. |
//| |
//| Install: compile, then MT5 Navigator -> Services -> Shared |
//| Projects\...\VZ_FeaturesExport -> Add service -> Start. |
//+------------------------------------------------------------------+
#property copyright "VIZION"
#property version "1.00"
#property service
#include "VZ_Core.mqh"
input string Symbols = "XAUUSD,XAGUSD,US Tech 100,Wall Street 30,US Oil"; // broker symbol names, comma-separated
input string Timeframes = "M15,H1,H4";
input int Interval_Seconds = 60;
input int Fib_Lookback_Bars = 350; // same as the EA
input bool EA_Compat_Slopes = true; // negate f4/f5 to match the EA's current convention
input string Out_Dir = "vizion"; // under MQL5\Files
struct Target { string sym; ENUM_TIMEFRAMES tf; string tf_name; VZ_Handles h; bool ok; };
Target g_t[];
ENUM_TIMEFRAMES TfFromName(const string s)
{
string u = s; StringToUpper(u); StringTrimLeft(u); StringTrimRight(u);
if(u == "M1") return PERIOD_M1; if(u == "M5") return PERIOD_M5; if(u == "M15") return PERIOD_M15;
if(u == "M30") return PERIOD_M30; if(u == "H1") return PERIOD_H1; if(u == "H4") return PERIOD_H4;
if(u == "D1") return PERIOD_D1; if(u == "W1") return PERIOD_W1;
return PERIOD_CURRENT;
}
string Sanitize(const string s)
{
string r = s;
StringReplace(r, " ", "_"); StringReplace(r, "/", "_"); StringReplace(r, ".", "_");
return r;
}
string Iso(const datetime t) { return TimeToString(t, TIME_DATE | TIME_SECONDS); }
string D(const double v, const int digits = 5) { return DoubleToString(v, digits); }
string BuildJson(Target &t, const VZ_Bar &live, const VZ_Bar &closed)
{
int digits = (int)SymbolInfoInteger(t.sym, SYMBOL_DIGITS);
double point = SymbolInfoDouble(t.sym, SYMBOL_POINT);
double fl[], fc[];
VZ_Features(live, point, EA_Compat_Slopes, fl);
VZ_Features(closed, point, EA_Compat_Slopes, fc);
string j = "{";
j += "\"symbol\":\"" + t.sym + "\",\"timeframe\":\"" + t.tf_name + "\",";
j += "\"collected_at\":\"" + Iso(TimeCurrent()) + "\",\"ts\":\"" + Iso(iTime(t.sym, t.tf, 1)) + "\",";
j += "\"ohlcv\":{\"open\":" + D(iOpen(t.sym, t.tf, 1), digits) + ",\"high\":" + D(iHigh(t.sym, t.tf, 1), digits)
+ ",\"low\":" + D(iLow(t.sym, t.tf, 1), digits) + ",\"close\":" + D(iClose(t.sym, t.tf, 1), digits)
+ ",\"volume\":" + IntegerToString(iTickVolume(t.sym, t.tf, 1)) + "},";
j += "\"live_close\":" + D(live.close, digits) + ",\"bid\":" + D(SymbolInfoDouble(t.sym, SYMBOL_BID), digits) + ",";
j += "\"mas\":{";
for(int k = 0; k < VZ_MA_COUNT; ++k) j += "\"ma" + IntegerToString(VZ_MA_PERIODS[k]) + "\":" + D(closed.ma[k], digits) + (k < VZ_MA_COUNT - 1 ? "," : "");
j += "},";
j += "\"stoch\":{\"k\":" + D(closed.stoch_k, 2) + ",\"d\":" + D(closed.stoch_d, 2) + ",\"k_prev\":" + D(live.stoch_k, 2) + "},";
j += "\"atr\":" + D(closed.atr, digits) + ",\"adx\":" + D(closed.adx, 2) + ",";
j += "\"fib\":{\"ath\":" + D(closed.fib_high, digits) + ",\"atl\":" + D(closed.fib_low, digits) + ",\"atr\":" + D(closed.atr, digits)
+ ",\"hinge\":" + D(closed.fib_low + (closed.fib_high - closed.fib_low) * 0.32, digits) + ",\"levels\":{"
+ "\"0.0\":" + D(closed.level[0], digits) + ",\"0.236\":" + D(closed.level[1], digits) + ",\"0.382\":" + D(closed.level[2], digits)
+ ",\"0.5\":" + D(closed.level[3], digits) + ",\"0.618\":" + D(closed.level[4], digits) + ",\"0.786\":" + D(closed.level[5], digits)
+ ",\"1.0\":" + D(closed.level[6], digits) + "}},";
j += "\"mfib\":{\"0.236\":" + D(closed.mfib_236, digits) + ",\"0.382\":" + D(closed.mfib_382, digits) + ",\"0.5\":" + D(closed.mfib_500, digits)
+ ",\"0.618\":" + D(closed.mfib_618, digits) + ",\"0.786\":" + D(closed.mfib_786, digits) + "},";
j += "\"features\":["; // live bar, EA convention — what /predict expects
for(int k = 0; k < 12; ++k) j += D(fl[k], 8) + (k < 11 ? "," : "");
j += "],\"features_closed\":["; // last closed bar — stable for snapshots/training
for(int k = 0; k < 12; ++k) j += D(fc[k], 8) + (k < 11 ? "," : "");
j += "],";
j += "\"mode\":" + IntegerToString(closed.mode) + ",\"mode_name\":\"" + (closed.mode == 0 ? "trending" : (closed.mode == 1 ? "ranging" : "chop")) + "\",";
j += "\"bias\":" + IntegerToString(closed.bias) + ",\"ma_stack_bull\":" + (closed.stack_bull ? "1" : "0") + ",\"ma_stack_bear\":" + (closed.stack_bear ? "1" : "0") + ",";
j += "\"ea_compat_slopes\":" + (EA_Compat_Slopes ? "true" : "false");
j += "}";
return j;
}
bool WriteFile(const string name, const string content)
{
int h = FileOpen(Out_Dir + "\\" + name, FILE_WRITE | FILE_TXT | FILE_ANSI);
if(h == INVALID_HANDLE) { Print("VZ_FeaturesExport: cannot open ", name, " err=", GetLastError()); return false; }
FileWriteString(h, content);
FileClose(h);
return true;
}
void OnStart()
{
string syms[], tfs[];
int ns = StringSplit(Symbols, ',', syms), nt = StringSplit(Timeframes, ',', tfs);
ArrayResize(g_t, 0);
for(int s = 0; s < ns; ++s)
{
string sym = syms[s]; StringTrimLeft(sym); StringTrimRight(sym);
if(StringLen(sym) == 0) continue;
if(!SymbolSelect(sym, true)) { Print("VZ_FeaturesExport: symbol not found, skipped: ", sym); continue; }
for(int t = 0; t < nt; ++t)
{
ENUM_TIMEFRAMES tf = TfFromName(tfs[t]);
if(tf == PERIOD_CURRENT) continue;
int n = ArraySize(g_t); ArrayResize(g_t, n + 1);
g_t[n].sym = sym; g_t[n].tf = tf; g_t[n].tf_name = tfs[t]; StringTrimLeft(g_t[n].tf_name); StringTrimRight(g_t[n].tf_name);
g_t[n].ok = VZ_Init(g_t[n].h, sym, tf);
if(!g_t[n].ok) Print("VZ_FeaturesExport: indicator init failed for ", sym, " ", g_t[n].tf_name);
}
}
Print("VZ_FeaturesExport: ", ArraySize(g_t), " symbol/timeframe targets, every ", Interval_Seconds, "s -> Files\\", Out_Dir);
while(!IsStopped())
{
int written = 0;
for(int i = 0; i < ArraySize(g_t); ++i)
{
if(!g_t[i].ok) continue;
VZ_Bar live, closed;
if(!VZ_Fill(g_t[i].h, g_t[i].sym, g_t[i].tf, 0, Fib_Lookback_Bars, live)) continue;
if(!VZ_Fill(g_t[i].h, g_t[i].sym, g_t[i].tf, 1, Fib_Lookback_Bars, closed)) continue;
if(WriteFile("snapshot_" + Sanitize(g_t[i].sym) + "_" + g_t[i].tf_name + ".json", BuildJson(g_t[i], live, closed))) written++;
}
WriteFile("export_status.json", "{\"updated_at\":\"" + Iso(TimeCurrent()) + "\",\"targets\":" + IntegerToString(ArraySize(g_t)) + ",\"written\":" + IntegerToString(written) + ",\"interval_s\":" + IntegerToString(Interval_Seconds) + "}");
for(int w = 0; w < Interval_Seconds * 10 && !IsStopped(); ++w) Sleep(100);
}
for(int i = 0; i < ArraySize(g_t); ++i) VZ_Release(g_t[i].h);
}