Category Trend  Published on 29/03/2020

Key Levels (Bank Levels)

Description

Found some nice script on Tradingview, modified it and converted to calgo.

Basically its like pivot points but with different calculation.

These Levels are meant to be often used by the Banks.

You can choose Source between HL2 (recommended) ,HLC3 and Weighted.

Customizable Timeframe (Daily and Weekly works best imo), and colors. Optional Fill between Lines for better visibility.

Enjoy

 

GBPJPY, m15:

 

EURCHF, m15:

 

 

   If you like my work, feel free to spend me a Corona Beer :-)

Donate


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


namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class KEYLEVELS : Indicator
    {

        //////////////////////////////////////////////////////////////////////// GENERAL PARAMETERS
        [Parameter("Lookback (Days)", Group = "GENERAL", DefaultValue = 7, MinValue = 0, MaxValue = 100)]
        public int _lookback { get; set; }

        [Parameter("Timeframe", Group = "GENERAL", DefaultValue = "Daily")]
        public TimeFrame _tf { get; set; }

        [Parameter("Source", Group = "GENERAL", DefaultValue = SourceType.HL2)]
        public SourceType source { get; set; }

        public enum SourceType
        {
            HLC3,
            HL2,
            weighted
        }

        [Parameter("min. tf (minutes)", Group = "GENERAL", DefaultValue = 3, MinValue = 3)]
        public int _mintf { get; set; }
        [Parameter("max. tf (minutes)", Group = "GENERAL", DefaultValue = 120)]
        public int _maxtf { get; set; }

        //////////////////////////////////////////////////////////////////////// VISUALS PARAMETERS
        [Parameter("Levels to show", Group = "VISUALS", DefaultValue = 8, MinValue = 1, MaxValue = 15)]
        public int _levelsToShow { get; set; }
        [Parameter("Fill each second band", Group = "VISUALS", DefaultValue = true)]
        public bool _fillbands { get; set; }
        [Parameter("Line thickness", Group = "VISUALS", DefaultValue = 1, MinValue = 1, MaxValue = 5)]
        public int _thickness { get; set; }

        //////////////////////////////////////////////////////////////////////// COLOR PARAMETERS
        [Parameter("Pivot Color", Group = "COLORS", DefaultValue = "Yellow")]
        public string col_param_pivot { get; set; }
        [Parameter("Near Pivot Color", Group = "COLORS", DefaultValue = "Purple")]
        public string col_param_nearpivot { get; set; }
        [Parameter("Support Color", Group = "COLORS", DefaultValue = "Lime")]
        public string col_param_sup { get; set; }
        [Parameter("Resistance Color", Group = "COLORS", DefaultValue = "Red")]
        public string col_param_res { get; set; }

        private Color col_pivot, col_nearpivot, col_sup, col_res;

        private Bars dailybars;
        private Bars minutebars;

        //////////////////////////////////////////////////////////////////////// INITIALIZE
        protected override void Initialize()
        {
            // Convert Color strings to color value
            col_pivot = Color.FromArgb(255, Color.FromName(col_param_pivot).R, Color.FromName(col_param_pivot).G, Color.FromName(col_param_pivot).B);
            col_nearpivot = Color.FromArgb(255, Color.FromName(col_param_nearpivot).R, Color.FromName(col_param_nearpivot).G, Color.FromName(col_param_nearpivot).B);
            col_sup = Color.FromArgb(255, Color.FromName(col_param_sup).R, Color.FromName(col_param_sup).G, Color.FromName(col_param_sup).B);
            col_res = Color.FromArgb(255, Color.FromName(col_param_res).R, Color.FromName(col_param_res).G, Color.FromName(col_param_res).B);

            // get daily bars for lookback length
            dailybars = MarketData.GetBars(TimeFrame.Daily);

            // get minute bars for Label position
            minutebars = MarketData.GetBars(TimeFrame.Minute);
        }

        //////////////////////////////////////////////////////////////////////// MAIN CALCULATION
        public override void Calculate(int index)
        {
            // Abort and show warning if any Timeframe is set to < 1H
            if (_tf < TimeFrame.Hour)
            {
                Chart.DrawText("Warning", "KEY LEVELS: \r\nDont set TF to under 1H", Bars.LastBar.OpenTime, Bars.LastBar.High, Color.Red);
                return;
            }

            // Get Time
            int index2 = dailybars.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);

            // plot only x Days
            if (dailybars.ClosePrices.Count - index2 > _lookback && _lookback != -1)
                return;

            // minute difference between bars
            var timeFrameMinutes = (int)(Bars.OpenTimes[index] - Bars.OpenTimes[index - 1]).TotalMinutes;

            // prevent showing up on < 3m chart
            if (timeFrameMinutes < 3)
                return;

            //Call functions depending on timeframe
            if (timeFrameMinutes >= _mintf && timeFrameMinutes <= _maxtf)
                PlotKeyLevels(index, _tf, _levelsToShow, _thickness);
        }

        //////////////////////////////////////////////////////////////////////// PLOT LEVELS
        public void PlotKeyLevels(int index, TimeFrame tf, int levelsToShow, int thickness)
        {

            // get bars of selected timeframe
            Bars tf_bars = MarketData.GetBars(tf);

            // abort if chart timeframe = choosen indicator timeframe
            if (tf_bars.Count == Bars.Count)
                return;

            int idx1 = tf_bars.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]);
            int idx2 = Bars.OpenTimes.GetIndexByTime(tf_bars.OpenTimes[idx1]);

            // get prices
            double open = tf_bars.OpenPrices[idx1];
            double high = tf_bars.HighPrices[idx1 - 1];
            double low = tf_bars.LowPrices[idx1 - 1];
            double close = tf_bars.ClosePrices[idx1 - 1];
            double hl2 = tf_bars.MedianPrices[idx1 - 1];
            double hlc3 = tf_bars.TypicalPrices[idx1 - 1];
            double weighted = tf_bars.WeightedPrices[idx1 - 1];

            double pivot = source == SourceType.HL2 ? hl2 : source == SourceType.HLC3 ? hlc3 : source == SourceType.weighted ? weighted : 0;
            double q = 0.5 * (pivot - high);

            // draw pivot
            Chart.DrawTrendLine("pivot" + idx1, idx2, pivot, index, pivot, col_pivot, thickness);

            // draw support
            for (int i = 1; i <= _levelsToShow; i++)
                Chart.DrawTrendLine("support" + idx1 + i, idx2, pivot + q * i, index, pivot + q * i, col_sup, thickness);

            // draw resistance
            for (int i = 1; i <= _levelsToShow; i++)
                Chart.DrawTrendLine("resistance" + idx1 + i, idx2, pivot - q * i, index, pivot - q * i, col_res, thickness);

            // fill each second bar for better visibility
            if (_fillbands)
            {
                // fill support
                for (int i = 3; i <= _levelsToShow; i += 2)
                    Chart.DrawRectangle("SupFill " + idx1 + i, idx2, pivot + q * i, index, pivot + (q * (i - 1)), Color.FromArgb(50, 0, 150, 0)).IsFilled = true;

                // fill resistance
                for (int i = 3; i <= _levelsToShow; i += 2)
                    Chart.DrawRectangle("ResFill " + idx1 + i, idx2, pivot - q * i, index, pivot - (q * (i - 1)), Color.FromArgb(50, 150, 0, 0)).IsFilled = true;

            }
        }
    }
}


DO
DontMatter

Joined on 15.11.2019

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: KEY LEVELS 1.0.algo
  • Rating: 5
  • Installs: 4428
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
PA
patg87 · 4 years ago

Great indicator, thx for sharing. But it seems like the pivot lines aren't extended throughout the trading day, when you're on a tick chart? anyway around this issue? thx

DO
DontMatter · 4 years ago

working on notifications for all my indicators in my spare time. I'll release new version soon.

MA
massi.agresti · 4 years ago

Thanks DontMatter !!
 I am not a programmer, would you be able to add an acoustic signal when reaching the pivots?
I would be grateful.

DO
DontMatter · 4 years ago

HL2 Lines get respected more often than HLC3 imo, but dyor

MA
massi.agresti · 4 years ago

Very interesting!! Why do you recommend setting up HL2?
Thanks.

Max