L1Trend/Scripts/TestL1TrendFilterSP500.mq5

89 líneas
3,2 KiB
MQL5

//+------------------------------------------------------------------+
//| TestL1TrendFilterSP500.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>
//+------------------------------------------------------------------+
//| LoadData |
//+------------------------------------------------------------------+
void LoadData(string filename,vector<double> &data,int &data_count)
{
data_count=0;
ResetLastError();
int file_handle=FileOpen(filename,FILE_READ|FILE_TXT|FILE_ANSI);
if(file_handle!=INVALID_HANDLE)
{
while(!FileIsEnding(file_handle))
{
string str=FileReadString(file_handle);
if(data.Size()<=(ulong)data_count)
data.Resize(data_count+1);
data[data_count]=StringToDouble(str);
data_count++;
}
FileClose(file_handle);
}
else
{
PrintFormat("Failed to open %s file, Error code = %d",filename,GetLastError());
}
data.Resize(data_count);
}
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
long chart=0;
string name="log SP500";
int data_count=0;
vector<double> data_sp500;
LoadData("snp500.txt",data_sp500,data_count);
vector<double> data_l1_sp500;
data_l1_sp500.Resize(data_count);
//--- L1TrendFilterLambdaMax
double lambda_max=data_sp500.L1TrendFilterLambdaMax();
PrintFormat("Lambda_max=%f",lambda_max);
double lambda=50;
//--- L1TrendFilter
bool res=data_sp500.L1TrendFilter(data_l1_sp500,lambda,false);
if(res)
{
//--- prepare arrays for chart
double x[],y[],y2[];
ArrayResize(x,data_count);
ArrayResize(y,data_count);
ArrayResize(y2,data_count);
for(int i=0; i<data_count; i++)
{
x[i]=i;
y[i]=data_sp500[i];
y2[i]=data_l1_sp500[i];
}
//---
CGraphic graphic;
if(ObjectFind(chart,name)<0)
graphic.Create(chart,name,0,0,0,1000,600);
else
graphic.Attach(chart,name);
graphic.BackgroundMain("log SP500 L1 trend filtering");
graphic.BackgroundMainSize(16);
graphic.HistoryNameWidth(60);
graphic.HistoryColor(ColorToARGB(clrGray,255));
graphic.XAxis().AutoScale(false);
graphic.XAxis().Min(0);
graphic.XAxis().Max(data_count);
graphic.XAxis().DefaultStep(100);
graphic.CurveAdd(x,y,CURVE_LINES,"SP500").LinesWidth(1);
graphic.CurveAdd(x,y2,CURVE_LINES,"L1 trend").LinesWidth(3);
graphic.CurvePlotAll();
graphic.Update();
DebugBreak();
}
}
//+------------------------------------------------------------------+