Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

MetaTrader 5 Libraries | PriceChannel

MetaTrader Experts, Indicators, Scripts and Libraries

PriceChannel the highest and the lowest price over the period selected.

This cross-platform library allows computing the PriceChannel without attributing it to standard bars, i.e., by ticks.

Accordingly, PriceChannel can be computed on any timeframe, including the one-second one. It can also work as a channel on ticks.

Example.

// PriceChannel library operation sample    input int inPeriod = 1;            // Channel period  sinput bool inPriceChannel = true; // Enable/disable calculating the PriceChannel via the library    #define PRICECHANNEL_LOW_PRICE bid  // bid/ask/last for the bar Low  #include <fxsaber\PriceChannel\PriceChannel.mqh> // https://www.mql5.com/en/code/23418    // The highest price over the last iPeriod bars  double GetHigh( const int iPeriod )  {    static double Highs[];      CopyHigh(_Symbol, PERIOD_CURRENT, 0, iPeriod, Highs);      return(Highs[ArrayMaximum(Highs)]);  }    // The lowest price over the last iPeriod bars  double GetLow( const int iPeriod )  {    static double Lows[];      CopyLow(_Symbol, PERIOD_CURRENT, 0, iPeriod, Lows);      return(Lows[ArrayMinimum(Lows)]);  }    double Sum = 0;    void OnTick()  {    static const int BarInterval = PeriodSeconds(PERIOD_CURRENT);    static PRICECHANNEL PriceChannel(inPeriod); // Created a PriceChannel object with a predefined period      static int CountBars = 0;    static datetime PrevTime = 0;      if (inPriceChannel)      PriceChannel.NewTick(); // Considered the new tick      if (TimeCurrent() / BarInterval != PrevTime / BarInterval) // New bar      CountBars++;      if (CountBars > MAX_BARS) // Start calculating when the relevant history has accumulated      Sum += inPriceChannel ? PriceChannel.GetHigh() - PriceChannel.GetLow() // Calculate the PriceChannel via the library                            :   GetHigh(inPeriod) - GetLow(inPeriod);        // Calculate the PriceChannel by standard method      PrevTime = TimeCurrent();  }    double OnTester()  {    return(Sum);  }  

This EA will show in Tester the identical results: The standard one and that via the library.

Performance.

Standard calculations

optimization finished, total passes 200  optimization done in 6 minutes 55 seconds  shortest pass 0:00:01.061, longest pass 0:00:03.432, average pass 0:00:02.066  

Via the library

optimization finished, total passes 200  optimization done in 2 minutes 57 seconds  shortest pass 0:00:00.764, longest pass 0:00:01.982, average pass 0:00:00.862  

Features.

  • Calculations on every tick.
  • You can set any duration for the bar.
  • No binding to standard bars.
  • The largest default value (can be specified) is calculated by Bid, the smallest one by Ask.
  • The performance does not practically depend on the period.
23418