I can't find the range with loop - page 2

 
Nikolai Semko #:


IIf so, then it is better to use not the range, but these two functions to check whether the last bar is an extremum for the last N bars:

example script

#property script_show_inputs
#include <Canvas\iCanvas_CB.mqh> // https://www.mql5.com/ru/code/22164

input int Bars = 200;
//+------------------------------------------------------------------+
void OnStart() {
   Canvas.Erase(0x00FFFFFF);
   for (int i = W.Left_bar; i>=W.Right_bar; i--) {
      if (IsNewMaxForLastN_Bars(Bars,i)) Canvas.FillCircle((int)Canvas.X(double(i)),(int)Canvas.Y(iHigh(NULL,0,i)),5,0x8000FF00);
      if (IsNewMinForLastN_Bars(Bars,i)) Canvas.FillCircle((int)Canvas.X(double(i)),(int)Canvas.Y(iLow(NULL,0,i)),5,0x80FF0000);
   }
   Canvas.Update();
   Sleep(20000);
}
//+------------------------------------------------------------------+
bool IsNewMaxForLastN_Bars (int N_Bars, int start = 0) {
   return iHighest(NULL,0,MODE_HIGH,N_Bars,start)==start;
}
//+------------------------------------------------------------------+
bool IsNewMinForLastN_Bars (int N_Bars, int start = 0) {
   return iLowest(NULL,0,MODE_LOW,N_Bars,start)==start;
}
//+------------------------------------------------------------------+


But I would recommend that you look towards at least linear regression, and not horizontal linear channels.

Files:
 

Expert example

#include <Canvas\iCanvas_CB.mqh> // https://www.mql5.com/ru/code/22164

int Bars = 200;
//+------------------------------------------------------------------+
int OnInit() {
   Canvas.CurentFont("Tahoma",30);
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {}
//+------------------------------------------------------------------+
void OnTick() {
   Draw();
}
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam) {
   if (id==CHARTEVENT_MOUSE_MOVE) {
      Bars = (int)W.MouseBar;
      if (Bars<1) Bars = 1;
      Draw();
   }

}
//+------------------------------------------------------------------+
void Draw() {
   static uint last_time = 0;
   uint cur_time = GetTickCount();
   if(cur_time-last_time<30) return;
   last_time = cur_time;
   if(Canvas.tester) ChartChanged();
   Canvas.Erase(0x00FFFFFF);
   Canvas.FillRectangle((int)W.MouseX,(int)_Y(iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,Bars,0))),(int)_X(0.0),(int)_Y(iLow(NULL,0,iLowest(NULL,0,MODE_LOW,Bars,0))),0x448000FF);
   for (int i = W.Left_bar; i>=W.Right_bar; i--) {
      if (IsNewMaxForLastN_Bars(Bars,i)) {
         Canvas.FillCircle((int)Canvas.X(double(i)),(int)Canvas.Y(iHigh(NULL,0,i)),5,0x8000FF00);
         Canvas.LineHorizontal((int)Canvas.X(double(i+Bars)),(int)Canvas.X(double(i)), (int)Canvas.Y(iHigh(NULL,0,i)),0x8000FF00);
      }
      if (IsNewMinForLastN_Bars(Bars,i)) {
         Canvas.FillCircle((int)Canvas.X(double(i)),(int)Canvas.Y(iLow(NULL,0,i)),5,0x80FF0000);
         Canvas.LineHorizontal((int)Canvas.X(double(i+Bars)),(int)Canvas.X(double(i)), (int)Canvas.Y(iHigh(NULL,0,i)),0x80FF0000);
      }
   }
   _CommXY(W.Width/2-150,50, "Total Bars = "+string(Bars));
   Canvas.Update();
}
//+------------------------------------------------------------------+
bool IsNewMaxForLastN_Bars (int N_Bars, int start = 0) {
   return iHighest(NULL,0,MODE_HIGH,N_Bars,start)==start;
}
//+------------------------------------------------------------------+
bool IsNewMinForLastN_Bars (int N_Bars, int start = 0) {
   return iLowest(NULL,0,MODE_LOW,N_Bars,start)==start;
}
//+------------------------------------------------------------------+


 
Nikolai Semko #:

IIf so, then it is better to use not the range, but these two functions to check whether the last bar is an extremum for the last N bars:


Thank you Nikolai. This is exactly what I would like to use. Your solution on how to check if the last bars are in range is new to me. I hadn't thought about approaching it this way. Thank you for the examples you posted. I don't understand everything, especially Canvas is new to me, but I will analyze everything and learn something new.

Thank you for your help!

 
jakub idzikowski #:

Thank you Nikolai. This is exactly what I would like to use. Your solution on how to check if the last bars are in range is new to me. I hadn't thought about approaching it this way. Thank you for the examples you posted. I don't understand everything, especially Canvas is new to me, but I will analyze everything and learn something new.

Thank you for your help!

no problem