//+------------------------------------------------------------------+ //| L1TrendRandomWalk.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" //--- #include //--- uint32_t ExtSeed = 1; //--- buffer vector ExtBuffer; double ExtLastValue = 0.0; int ExtHead = 0; bool ExtReady = false; //+------------------------------------------------------------------+ //| Init | //+------------------------------------------------------------------+ void BMInit(uint32_t size) { MathSrand(ExtSeed); ExtBuffer.Resize(size); ExtLastValue = 0.0; for(uint32_t i = 0; i < size; i++) { ExtLastValue += (MathRand() / 32767.0 - 0.5); ExtBuffer[i] = ExtLastValue; } ExtHead = 0; ExtReady = true; } //+------------------------------------------------------------------+ //| Add N new points | //+------------------------------------------------------------------+ void BMNext(int n_points) { if(!ExtReady) return; int size = (int)ExtBuffer.Size(); for(int k=0; k= size) ExtHead = 0; } } //+------------------------------------------------------------------+ //| Get ordered data | //+------------------------------------------------------------------+ bool BMGet(double &out[]) { if(!ExtReady) return(false); int size = (int)ExtBuffer.Size(); if(ArrayResize(out, size) != (int)size) return(false); for(int i = 0; i < size; i++) { int idx = (ExtHead + i) % size; out[i] = ExtBuffer[idx]; } return(true); } //+------------------------------------------------------------------+ //| L1Trend from existing data | //+------------------------------------------------------------------+ bool L1Trend(double &result[], const double &src[], int size, double lambda) { vector v; v.Resize(size); for(int i=0; i vres; if(!v.L1TrendFilter(lambda, true, vres)) return(false); if(ArrayResize(result,(int)vres.Size()) != (int)vres.Size()) return(false); for(int i=0; i<(int)vres.Size(); i++) result[i]=vres[i]; return(true); } //+------------------------------------------------------------------+ //| TestRun | //+------------------------------------------------------------------+ bool TestRun(int data_count) { //--- init or continue if(!ExtReady || ExtBuffer.Size() != data_count) BMInit(data_count); else BMNext(10); // add new values //--- graphic CGraphic graphic; long chart = 0; string name = "Random walk"; if(ObjectFind(chart,name)<0) graphic.Create(chart,name,0,0,0,1000,600); else graphic.Attach(chart,name); //--- graphic.BackgroundMain("L1Trend 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); //--- X double x[]; ArrayResize(x, data_count); for(int i=0; i