Category Oscilators  Published on 08/01/2024

Price Weighted Sums Time Gap

Description

This custom indicator's key difference lies in the weights assigned to each element in the price array. In the narrow formula, the last weights of price members are multiplied by 1, 2, 2, and 1, respectively; in the wide formula, the last weights of price members are multiplied by 4, 3, 2, and 1, respectively.

Mathematically, these formulas represent weighted sums of the last four elements of prices as a result of a smoothed price array. The choice of weights affects the emphasis given to each element in the sum. Larger weights assign more importance to the corresponding element, making it contribute more significantly to the overall sum.

The difference in results from the two different weighted sums will evaluate both formulas to see how the choice of weights sum impacts the calculated sum. In other words, the difference between the two weighted sums of price arrays will identify the price smooth gap, called the gap.

Using the original indicator (https://ctrader.com/algos/indicators/show/3870) for trading zones is easy; for positive output values (output result > 0), trade only long, and for negative output values (output result < 0), trade only short.

In this version of Price Weighted Sums Gap, the result is shown in a time series as the accumulation sum of positive and negative values for a period of time. This mathematical formula is needed to identify the price zone sentiment for a time period ago, to compare the major and minor price sentiment. In other words, all the time, we need an indicator to identify the sentiment in three timeframes (e.g., D1, H4, H1) to trade in m15 bars (see fig. 1).

The TimeGap component of the indicator (red color) with a positive value from a larger timeframe indicates bullish sentiment for x bars; similarly, a negative result from a larger timeframe indicates bearish sentiment for x bars. Use it as a trade zone.

The indicator output value, with black color show the Price Weighted Sums Gap for current timeframe.

This epic custom indicator was developed by Iba and is dedicated to his memory.


using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    [Levels(0)]
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class mPriceWeightedSumsTimeGap : Indicator
    {
        [Parameter("Bigger TimeFrame (D1)", DefaultValue = "Daily")]
        public TimeFrame inpbtfTF1 { get; set; }
        [Parameter("Data Source (close)", DefaultValue = enumPriceTypes.Close)]
        public enumPriceTypes inpPriceType { get; set; }

        [Output("Price Weighted Sums Gap", LineColor = "Black", PlotType = PlotType.Line, Thickness = 1)]
        public IndicatorDataSeries outGAP { get; set; }
        [Output("TimeSeries Gap", LineColor = "Red", PlotType = PlotType.Line, Thickness = 2)]
        public IndicatorDataSeries outTimeGAP { get; set; }
        
        private Bars _btfBars;
        private IndicatorDataSeries _price, _pwsnarrow, _pwswide, _gap, _timegap;


        protected override void Initialize()
        {
            _btfBars = MarketData.GetBars(inpbtfTF1);
            _price = CreateDataSeries();
            _pwsnarrow = CreateDataSeries();
            _pwswide = CreateDataSeries();
            _gap = CreateDataSeries();
            _timegap = CreateDataSeries();
        }

        public override void Calculate(int i)
        {
            switch (inpPriceType)
            {
                case enumPriceTypes.Open:
                    _price[i] = Bars.OpenPrices[i];
                    break;
                case enumPriceTypes.Close:
                    _price[i] = Bars.ClosePrices[i];
                    break;
                case enumPriceTypes.High:
                    _price[i] = Bars.HighPrices[i];
                    break;
                case enumPriceTypes.Low:
                    _price[i] = Bars.LowPrices[i];
                    break;
                case enumPriceTypes.Median:
                    _price[i] = Bars.MedianPrices[i];
                    break;
                case enumPriceTypes.Typical:
                    _price[i] = Bars.TypicalPrices[i];
                    break;
                case enumPriceTypes.Weighted:
                    _price[i] = Bars.WeightedPrices[i];
                    break;
                default:
                    _price[i] = Bars.ClosePrices[i];
                    break;
            }
            _pwsnarrow[i] = i>3 ? (_price[i] + 2.0 * _price[i-1] + 2.0 * _price[i-2] + _price[i-3]) / 6.0 : _price[i];
            _pwswide[i] = (4 * _price[i] + 3 * _price[i-1] + 2 * _price[i-2] + _price[i-3]) / 10;
            _gap[i] = (_pwswide[i] - _pwsnarrow[i]) / Symbol.PipSize;
            _timegap[i] =
                          _btfBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[i]) > _btfBars.OpenTimes.GetIndexByTime(Bars.OpenTimes[i-1]) ? 0
                        : (i>1 ? _timegap[i-1] : 0) + _gap[i];
                        
            outGAP[i] = _gap[i];
            outTimeGAP[i] = _timegap[i];
        }
    }

    public enum enumPriceTypes
    {
        Open,
        Close,
        High,
        Low,
        Median,
        Typical,
        Weighted
    }
}


mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mPriceWeightedSumsTimeGap.algo
  • Rating: 5
  • Installs: 249
Comments
Log in to add a comment.
No comments found.