96 lines
6.8 KiB
MQL5
96 lines
6.8 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| L1TrendFilter.mq5 |
|
|
//| Copyright 2000-2026, MetaQuotes Ltd. |
|
|
//| https://www.mql5.com |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "Copyright 2000-2026, MetaQuotes Ltd."
|
|
#property link "https://www.mql5.com"
|
|
#property version "1.00"
|
|
#property indicator_chart_window
|
|
#property indicator_buffers 1
|
|
#property indicator_plots 1
|
|
//---
|
|
#property indicator_label1 "L1TrendFilter"
|
|
#property indicator_type1 DRAW_LINE
|
|
#property indicator_color1 clrDodgerBlue
|
|
#property indicator_width1 2
|
|
//---
|
|
input int BarsToShow = 1000; // Number of bars to calculate L1
|
|
input double CoefLambda = 0.015; // Lambda in lambda_max units
|
|
//---
|
|
double Trend[];
|
|
//+------------------------------------------------------------------+
|
|
//| Indicator initialization function |
|
|
//+------------------------------------------------------------------+
|
|
int OnInit()
|
|
{
|
|
SetIndexBuffer(0,Trend,INDICATOR_DATA);
|
|
ArrayInitialize(Trend,EMPTY_VALUE);
|
|
//---
|
|
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
|
|
IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
|
|
//---
|
|
return INIT_SUCCEEDED;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Indicator iteration function |
|
|
//+------------------------------------------------------------------+
|
|
int OnCalculate(const int rates_total,
|
|
const int prev_calculated,
|
|
const datetime &time[],
|
|
const double &open[],
|
|
const double &high[],
|
|
const double &low[],
|
|
const double &close[],
|
|
const long &tick_volume[],
|
|
const long &volume[],
|
|
const int &spread[])
|
|
{
|
|
//--- check bars
|
|
static bool warned=false;
|
|
if(rates_total < BarsToShow)
|
|
{
|
|
if(!warned)
|
|
{
|
|
Print("Waiting for enough bars: ",BarsToShow);
|
|
warned=true;
|
|
}
|
|
ArrayInitialize(Trend,EMPTY_VALUE);
|
|
return 0;
|
|
}
|
|
//--- check new bar
|
|
static datetime last_bar_time=0;
|
|
bool new_bar=(time[0]!=last_bar_time);
|
|
bool need_recalc=(prev_calculated==0) || new_bar || (rates_total!=prev_calculated);
|
|
if(!need_recalc)
|
|
return prev_calculated;
|
|
last_bar_time=time[0];
|
|
//--- range
|
|
int start=rates_total-BarsToShow;
|
|
//--- hide old bars
|
|
for(int i=0;i<start;i++)
|
|
Trend[i]=EMPTY_VALUE;
|
|
//---
|
|
int data_count=BarsToShow;
|
|
//--- copy Close
|
|
vector<double> DataClose;
|
|
DataClose.Resize(data_count);
|
|
for(int i=0;i<data_count;i++)
|
|
DataClose[i]=close[start+i];
|
|
//--- lambda max
|
|
double lambda_max=DataClose.L1TrendFilterLambdaMax();
|
|
PrintFormat("lambda_max=%f (%s,%s) Coef=%f lambda=%f",
|
|
lambda_max,Symbol(),EnumToString(Period()),CoefLambda,lambda_max*CoefLambda);
|
|
//--- L1 trend filtering
|
|
vector<double> filtered_data;
|
|
filtered_data.Resize(data_count);
|
|
bool res=DataClose.L1TrendFilter(filtered_data,CoefLambda,true);
|
|
if(res)
|
|
{
|
|
for(int i=0;i<data_count;i++)
|
|
Trend[start+i]=filtered_data[i];
|
|
}
|
|
//---
|
|
return rates_total;
|
|
}
|
|
//+------------------------------------------------------------------+
|