L1Trend/Scripts/TestL1Trend.mq5

89 líneas
3,3 KiB
MQL5

//+------------------------------------------------------------------+
//| TestL1Trend.mq5 |
//| Copyright 2000-2026, MetaQuotes Ltd. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2000-2026, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#property script_show_inputs
#include <Graphics\Graphic.mqh>
//+------------------------------------------------------------------+
//| Generate Brown movement data |
//+------------------------------------------------------------------+
void BMData(vector<double> &data,int &data_count)
{
data.Resize(data_count);
data[0] = 0.0;
for(int i = 1; i < data_count; i++)
data[i] = data[i - 1] + (MathRand()/32767.0 - 0.5);
}
//+------------------------------------------------------------------+
//| CopyValues |
//+------------------------------------------------------------------+
bool CopyValues(vector<double> &data_v,double &data[])
{
int data_count=(int)data.Size();
if(data_count==0)
return(false);
ArrayResize(data,data.Size());
for(int i = 1; i < data_count; i++)
data[i]=data_v[i];
return(true);
}
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
MathSrand(1);
int data_count=1000;
vector<double> data_test;
BMData(data_test,data_count);
//--- prepare arrays for chart
double x[],y[];
ArrayResize(x,data_count);
ArrayResize(y,data_count);
for(int i=0; i<data_count; i++)
x[i]=i;
//---
CGraphic graphic;
long chart=0;
string name="test";
if(ObjectFind(chart,name)<0)
graphic.Create(chart,name,0,0,0,1000,600);
else
graphic.Attach(chart,name);
graphic.BackgroundMain("L1 Trend filtering (random walk) with different lambda");
graphic.BackgroundMainSize(16);
graphic.HistoryNameWidth(60);
graphic.HistoryColor(ColorToARGB(clrGray,255));
graphic.XAxis().AutoScale(false);
graphic.XAxis().Min(0);
graphic.XAxis().Max(data_count);
//---
CopyValues(data_test,y);
graphic.CurveAdd(x,y,CURVE_LINES,"Data").LinesWidth(1);
//--- L1TrendFilterLambdaMax
double lambda_max=data_test.L1TrendFilterLambdaMax();
PrintFormat("lambda_max=%f",lambda_max);
//---
vector<double> data_l1;
const double lambda_factors[]= {1.0,0.9,0.8,0.5,0.25,0.1,0.01,0.05,0.001,0.0005};
for(int i=0; i<ArraySize(lambda_factors); i++)
{
double lambda=lambda_max*lambda_factors[i];
PrintFormat("%d. lambda=%f",i+1,lambda);
bool res=data_test.L1TrendFilter(data_l1,lambda_factors[i],true);
if(res)
{
CopyValues(data_l1,y);
graphic.CurveAdd(x,y,CURVE_LINES,"lambda="+DoubleToString(lambda,0)).LinesWidth(3);
}
}
//---
graphic.CurvePlotAll();
graphic.Update();
DebugBreak();
}
//+------------------------------------------------------------------+