328 lignes
13 Kio
MQL5
//+------------------------------------------------------------------+
| |||
//| OMV Features.mqh |
| |||
//| Copyright 2026, Allan Munene Mutiiria. |
| |||
//| https://t.me/Forex_Algo_Trader |
| |||
//+------------------------------------------------------------------+
| |||
#property copyright "Copyright 2026, Allan Munene Mutiiria."
| |||
#property link "https://t.me/Forex_Algo_Trader"
| |||
| |||
#ifndef OMV_FEATURES_MQH
| |||
#define OMV_FEATURES_MQH
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Feature limits |
| |||
//+------------------------------------------------------------------+
| |||
#define OF_INPUTS 16 // Numbers the model reads on every bar
| |||
#define OF_WARMUP 50 // Bars of history each number needs behind it
| |||
#define OF_FLOOR 1e-9 // Smallest divisor allowed, so a flat run is safe
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Name what each of the sixteen numbers measures |
| |||
//+------------------------------------------------------------------+
| |||
string OmvFeatureName(const int i)
| |||
{
| |||
//--- Return the name for each feature index
| |||
switch(i)
| |||
{
| |||
case 0:
| |||
return "return 1 bar";
| |||
case 1:
| |||
return "return 3 bars";
| |||
case 2:
| |||
return "return 6 bars";
| |||
case 3:
| |||
return "return 12 bars";
| |||
case 4:
| |||
return "return 24 bars";
| |||
case 5:
| |||
return "place in 20 range";
| |||
case 6:
| |||
return "place in 50 range";
| |||
case 7:
| |||
return "volatility regime";
| |||
case 8:
| |||
return "body share";
| |||
case 9:
| |||
return "upper wick";
| |||
case 10:
| |||
return "lower wick";
| |||
case 11:
| |||
return "from fast mean";
| |||
case 12:
| |||
return "from slow mean";
| |||
case 13:
| |||
return "mean separation";
| |||
case 14:
| |||
return "hour as sine";
| |||
case 15:
| |||
return "hour as cosine";
| |||
}
| |||
//--- Name anything outside the sixteen by its place
| |||
return "input " + IntegerToString(i);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| One bar's worth of model input |
| |||
//+------------------------------------------------------------------+
| |||
struct OmvFeatures
| |||
{
| |||
// Hold the sixteen numbers in the order the model was trained on
| |||
double value[OF_INPUTS];
| |||
// Stamp the bar these came from
| |||
datetime when;
| |||
// Report whether every number came out usable
| |||
bool ready;
| |||
};
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Builder that mirrors the training script exactly |
| |||
//+------------------------------------------------------------------+
| |||
class COmvFeatures
| |||
{
| |||
private:
| |||
// Keep the bars the numbers are built from, oldest first
| |||
double m_open[];
| |||
double m_high[];
| |||
double m_low[];
| |||
double m_close[];
| |||
datetime m_time[];
| |||
// Count the bars held
| |||
int m_bars;
| |||
// Explain why the last build failed
| |||
string m_error;
| |||
| |||
double TrueRange(const int i) const;
| |||
double RangeMean(const int i, const int n) const;
| |||
double RollingHigh(const int i, const int n) const;
| |||
double RollingLow(const int i, const int n) const;
| |||
void Smooth(const double &src[], const int span, double &out[]) const;
| |||
double Back(const double &src[], const int i, const int n) const;
| |||
| |||
public:
| |||
COmvFeatures(void);
| |||
bool Load(const string symbol, const ENUM_TIMEFRAMES period, const int bars);
| |||
bool Build(const int i, OmvFeatures &out) const;
| |||
| |||
int Bars(void) const { return m_bars; }
| |||
string Error(void) const { return m_error; }
| |||
datetime TimeAt(const int i) const { return (i >= 0 && i < m_bars) ? m_time[i] : 0; }
| |||
int IndexOf(const datetime stamp) const;
| |||
};
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Construct an empty builder |
| |||
//+------------------------------------------------------------------+
| |||
COmvFeatures::COmvFeatures(void)
| |||
{
| |||
//--- Start holding nothing
| |||
m_bars = 0;
| |||
m_error = "";
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Take the bars the numbers will be built from |
| |||
//+------------------------------------------------------------------+
| |||
bool COmvFeatures::Load(const string symbol, const ENUM_TIMEFRAMES period, const int bars)
| |||
{
| |||
//--- Refuse a request too short to fill the warm up
| |||
if(bars <= OF_WARMUP)
| |||
{
| |||
m_error = "ask for more than " + IntegerToString(OF_WARMUP) + " bars";
| |||
return false;
| |||
}
| |||
//--- Take the rates in one call
| |||
MqlRates rates[];
| |||
const int got = CopyRates(symbol, period, 0, bars, rates);
| |||
if(got <= OF_WARMUP)
| |||
{
| |||
m_error = "only " + IntegerToString(got) + " bars came back for " + symbol;
| |||
return false;
| |||
}
| |||
//--- Size the stores to what arrived
| |||
ArrayResize(m_open, got);
| |||
ArrayResize(m_high, got);
| |||
ArrayResize(m_low, got);
| |||
ArrayResize(m_close, got);
| |||
ArrayResize(m_time, got);
| |||
//--- Copy oldest first, which is the order the training script worked in
| |||
for(int i = 0; i < got; i++)
| |||
{
| |||
//--- CopyRates already hands these back oldest first
| |||
m_open[i] = rates[i].open;
| |||
m_high[i] = rates[i].high;
| |||
m_low[i] = rates[i].low;
| |||
m_close[i] = rates[i].close;
| |||
m_time[i] = rates[i].time;
| |||
}
| |||
m_bars = got;
| |||
m_error = "";
| |||
//--- Report success
| |||
return true;
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Find which held bar carries a given stamp |
| |||
//+------------------------------------------------------------------+
| |||
int COmvFeatures::IndexOf(const datetime stamp) const
| |||
{
| |||
//--- Walk the stamps for an exact match
| |||
for(int i = 0; i < m_bars; i++)
| |||
if(m_time[i] == stamp)
| |||
return i;
| |||
//--- Report that no bar carries it
| |||
return -1;
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Measure a bar, counting any gap from the bar before it |
| |||
//+------------------------------------------------------------------+
| |||
double COmvFeatures::TrueRange(const int i) const
| |||
{
| |||
//--- The first bar has nothing before it, so it compares against itself
| |||
const double prev = (i > 0) ? m_close[i - 1] : m_close[0];
| |||
//--- Take the widest of the three spans the bar covers
| |||
const double a = m_high[i] - m_low[i];
| |||
const double b = MathAbs(m_high[i] - prev);
| |||
const double c = MathAbs(m_low[i] - prev);
| |||
return MathMax(a, MathMax(b, c));
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Average the true range over the last n bars, ending at one |
| |||
//+------------------------------------------------------------------+
| |||
double COmvFeatures::RangeMean(const int i, const int n) const
| |||
{
| |||
//--- Before the window fills, the training script averaged the opening run
| |||
const int from = (i < n) ? 0 : i - n + 1;
| |||
const int upto = (i < n) ? n - 1 : i;
| |||
//--- Add every value the window covers
| |||
double total = 0.0;
| |||
int seen = 0;
| |||
for(int k = from; k <= upto && k < m_bars; k++)
| |||
{
| |||
//--- Take the true range of this bar
| |||
total += TrueRange(k);
| |||
seen++;
| |||
}
| |||
//--- Report the average of what was covered
| |||
return (seen > 0) ? total / seen : 0.0;
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Take the highest high over the last n bars |
| |||
//+------------------------------------------------------------------+
| |||
double COmvFeatures::RollingHigh(const int i, const int n) const
| |||
{
| |||
//--- Walk back at most n bars, stopping at the start
| |||
const int from = (i - n + 1 > 0) ? i - n + 1 : 0;
| |||
double top = m_high[from];
| |||
for(int k = from; k <= i; k++)
| |||
if(m_high[k] > top)
| |||
top = m_high[k];
| |||
//--- Report the highest found
| |||
return top;
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Take the lowest low over the last n bars |
| |||
//+------------------------------------------------------------------+
| |||
double COmvFeatures::RollingLow(const int i, const int n) const
| |||
{
| |||
//--- Walk back at most n bars, stopping at the start
| |||
const int from = (i - n + 1 > 0) ? i - n + 1 : 0;
| |||
double floor = m_low[from];
| |||
for(int k = from; k <= i; k++)
| |||
if(m_low[k] < floor)
| |||
floor = m_low[k];
| |||
//--- Report the lowest found
| |||
return floor;
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Smooth a series, seeding from its first value |
| |||
//+------------------------------------------------------------------+
| |||
void COmvFeatures::Smooth(const double &src[], const int span, double &out[]) const
| |||
{
| |||
//--- Size the result to the series
| |||
ArrayResize(out, m_bars);
| |||
if(m_bars <= 0)
| |||
return;
| |||
//--- Seed from the first value, which is where the terminal's own average differs
| |||
out[0] = src[0];
| |||
const double k = 2.0 / (span + 1.0);
| |||
for(int i = 1; i < m_bars; i++)
| |||
out[i] = src[i] * k + out[i - 1] * (1.0 - k);
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Reach back n bars, clamping at the start of the series |
| |||
//+------------------------------------------------------------------+
| |||
double COmvFeatures::Back(const double &src[], const int i, const int n) const
| |||
{
| |||
//--- Before the series is long enough, the training script held the first value
| |||
return (i - n >= 0) ? src[i - n] : src[0];
| |||
}
| |||
| |||
//+------------------------------------------------------------------+
| |||
//| Build the sixteen numbers for one bar |
| |||
//+------------------------------------------------------------------+
| |||
bool COmvFeatures::Build(const int i, OmvFeatures &out) const
| |||
{
| |||
//--- Start with nothing filled
| |||
out.ready = false;
| |||
out.when = 0;
| |||
for(int k = 0; k < OF_INPUTS; k++)
| |||
out.value[k] = 0.0;
| |||
//--- Refuse a bar with too little history behind it
| |||
if(i < OF_WARMUP || i >= m_bars)
| |||
return false;
| |||
//--- Measure how far the market has been moving lately
| |||
const double atr14 = RangeMean(i, 14);
| |||
const double atr50 = RangeMean(i, 50);
| |||
const double safe = MathMax(atr14, OF_FLOOR);
| |||
//--- Measure the ranges the price sits inside
| |||
const double hi20 = RollingHigh(i, 20), lo20 = RollingLow(i, 20);
| |||
const double hi50 = RollingHigh(i, 50), lo50 = RollingLow(i, 50);
| |||
const double span20 = MathMax(hi20 - lo20, OF_FLOOR);
| |||
const double span50 = MathMax(hi50 - lo50, OF_FLOOR);
| |||
//--- Smooth the closes twice, at the two spans the model was trained on
| |||
double ema12[], ema50[];
| |||
Smooth(m_close, 12, ema12);
| |||
Smooth(m_close, 50, ema50);
| |||
//--- Measure the bar itself
| |||
const double rng = MathMax(m_high[i] - m_low[i], OF_FLOOR);
| |||
const double topBody = MathMax(m_open[i], m_close[i]);
| |||
const double lowBody = MathMin(m_open[i], m_close[i]);
| |||
//--- Take the hour the bar opened, which the model reads as an angle
| |||
MqlDateTime parts;
| |||
TimeToStruct(m_time[i], parts);
| |||
const double angle = 2.0 * M_PI * parts.hour / 24.0;
| |||
//--- Fill the sixteen in the order the model was trained on
| |||
out.value[0] = (m_close[i] - Back(m_close, i, 1)) / safe;
| |||
out.value[1] = (m_close[i] - Back(m_close, i, 3)) / safe;
| |||
out.value[2] = (m_close[i] - Back(m_close, i, 6)) / safe;
| |||
out.value[3] = (m_close[i] - Back(m_close, i, 12)) / safe;
| |||
out.value[4] = (m_close[i] - Back(m_close, i, 24)) / safe;
| |||
out.value[5] = (m_close[i] - lo20) / span20;
| |||
out.value[6] = (m_close[i] - lo50) / span50;
| |||
out.value[7] = atr14 / MathMax(atr50, OF_FLOOR);
| |||
out.value[8] = (m_close[i] - m_open[i]) / rng;
| |||
out.value[9] = (m_high[i] - topBody) / rng;
| |||
out.value[10] = (lowBody - m_low[i]) / rng;
| |||
out.value[11] = (m_close[i] - ema12[i]) / safe;
| |||
out.value[12] = (m_close[i] - ema50[i]) / safe;
| |||
out.value[13] = (ema12[i] - ema50[i]) / safe;
| |||
out.value[14] = MathSin(angle);
| |||
out.value[15] = MathCos(angle);
| |||
//--- Reject the bar when any number came out unusable
| |||
for(int k = 0; k < OF_INPUTS; k++)
| |||
if(!MathIsValidNumber(out.value[k]))
| |||
return false;
| |||
//--- Stamp the bar and report success
| |||
out.when = m_time[i];
| |||
out.ready = true;
| |||
return true;
| |||
}
| |||
| |||
#endif // OMV_FEATURES_MQH
| |||
//+------------------------------------------------------------------+
|