Belkhayate Live!

by cysecsbin.01 in category Volatility at 02/06/2019
Description

Follow my cTrader Telegram group at https://t.me/cTraderCommunity; it's a new community but it will grow fast, plus everyone can talk about cTrader indicators and algorithm without restrictions, though it is not allowed to spam commercial indicators to sell them.

This is a rework of the PRC by A T.

I just added some features to make backtesting easier with this indicator, now you can use ctrl to move the channel, and shift to set its lenght.

try it, you won't be disappointed

 

Changelog 1 - Added the extension feature! Now you can extend the channel beyond its ending point to create pivots that can be a useful substitution for fibonacci and the classic pivots; this method can be applied to swings to see their volatility projection.

Changelog 2 - Now if you scroll past the right end of the channel while holding shift you can adjust the projection lenght

Changelog 3 - Added trend projection! Now you can switch between static and trending volatility projections by pressing ctrl + alt + left click. Also, the degree of the projection can be changeg by using ctrl + click and alt + click!

 

Download
153 downloads
How to install
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader
using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AutoRescale = false, AccessRights = AccessRights.None)]
    public class BelkhayateLive : Indicator
    {
        [Parameter(DefaultValue = 3.0, MinValue = 1, MaxValue = 4)]
        public int degree { get; set; }

        [Parameter(DefaultValue = 200)]
        public int period { get; set; }

        [Parameter(DefaultValue = 1.5)]
        public double strdDev { get; set; }

        [Parameter(DefaultValue = 2)]
        public double strdDev2 { get; set; }

        [Parameter(DefaultValue = 3.5)]
        public double strdDev3 { get; set; }

        [Parameter(DefaultValue = 200)]
        public int extension { get; set; }

        [Parameter(DefaultValue = true)]
        public bool proType { get; set; }

        [Parameter("Source")]
        public DataSeries source { get; set; }

        [Output("PRC", Color = Colors.Gray, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries prc { get; set; }

        [Output("SQH", Color = Colors.Red, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries sqh { get; set; }

        [Output("SQL", Color = Colors.Blue, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries sql { get; set; }

        [Output("SQL2", Color = Colors.Blue, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries sql2 { get; set; }

        [Output("SQH2", Color = Colors.Red, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries sqh2 { get; set; }

        [Output("SQL3", Color = Colors.Blue, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries sql3 { get; set; }

        [Output("SQH3", Color = Colors.Red, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries sqh3 { get; set; }

        public int ix { get; set; }

        private double[,] ai = new double[10, 10];
        private double[] b = new double[10];
        private double[] x = new double[10];
        private double[] sx = new double[10];
        private double sum;
        private int ip;
        private int p;
        private int n;
        //private int f;
        private double qq;
        private double mm;
        private double tt;
        private int ii;
        private int jj;
        private int kk;
        private int ll;
        private int nn;
        private double sq;
        private double sq2;
        private double sq3;
        private int i0 = 0;
        private int mi;
        public int start;
        public int end;

        protected override void Initialize()
        {
            Chart.MouseMove += OnChartMouseMove;
            Chart.MouseWheel += OnChartMouseWheel;
            Chart.MouseDown += OnChartMouseDown;
        }

        void OnChartMouseDown(ChartMouseEventArgs obj)
        {
            if (obj.CtrlKey && obj.AltKey && !obj.ShiftKey)
            {
                proType = proType ? false : true;
                Calculate_(end);
            }
            if (obj.AltKey && !obj.ShiftKey)
            {
                degree++;

                if (degree < 1)
                    degree = 1;
                else if (degree > 4)
                    degree = 4;
                Calculate_(end);
            }
            if (obj.CtrlKey && !obj.ShiftKey)
            {
                degree--;

                if (degree < 1)
                    degree = 1;
                else if (degree > 4)
                    degree = 4;
                Calculate_(end);
            }

        }

        void OnChartMouseWheel(ChartMouseWheelEventArgs obj)
        {
            if (obj.CtrlKey && obj.AltKey && !obj.ShiftKey)
            {
                if (obj.Delta > 0)
                    degree++;
                else
                    degree--;
                if (degree < 1)
                    degree = 1;
                else if (degree > 4)
                    degree = 4;
            }
            Calculate_(end);
        }

        void OnChartMouseMove(ChartMouseEventArgs obj)
        {
            if (obj.CtrlKey && !obj.ShiftKey)
            {
                end = (int)obj.BarIndex;
                start = end - period;
                if (end > start)
                    Calculate_(end);
            }
            if (obj.ShiftKey && !obj.CtrlKey && !obj.AltKey)
            {

                if (end > obj.BarIndex)
                {
                    start = (int)obj.BarIndex;
                    Calculate_(end);
                }
                else
                {
                    extension = (int)obj.BarIndex - end;
                    Calculate_(end);
                }
            }
        }

        public void Calculate_(int index)
        {
            //if (!IsLastBar || index < period)
            //return;
            for (int a = 0; a <= Chart.BarsTotal || !double.IsNaN(prc[a]); a++)
            {
                prc[a] = double.NaN;
                sqh[a] = double.NaN;
                sqh2[a] = double.NaN;
                sqh3[a] = double.NaN;
                sql[a] = double.NaN;
                sql2[a] = double.NaN;
                sql3[a] = double.NaN;
            }
            period = end - start;
            int i = index;
            ix = i;
            ip = period;
            p = ip;
            sx[1] = p + 1;
            nn = degree + 1;
            //----------------------sx-------------------------------------------------------------------
            // 

            for (mi = 1; mi <= nn * 2 - 2; mi++)
            {
                sum = 0;
                for (n = i0; n <= i0 + p; n++)
                {
                    sum += Math.Pow(n, mi);
                }
                sx[mi + 1] = sum;
            }
            //----------------------syx-----------

            for (mi = 1; mi <= nn; mi++)
            {
                sum = 0.0;
                for (n = i0; n <= i0 + p; n++)
                {
                    if (mi == 1)
                        sum += source[index - n];
                    else
                        sum += source[index - n] * Math.Pow(n, mi - 1);
                }
                b[mi] = sum;
            }
            //===============Matrix=======================================================================================================

            for (jj = 1; jj <= nn; jj++)
            {
                for (ii = 1; ii <= nn; ii++)
                {
                    kk = ii + jj - 1;
                    ai[ii, jj] = sx[kk];
                }
            }

            //===============Gauss========================================================================================================
            for (kk = 1; kk <= nn - 1; kk++)
            {
                ll = 0;
                mm = 0;
                for (ii = kk; ii <= nn; ii++)
                {
                    if (Math.Abs(ai[ii, kk]) > mm)
                    {
                        mm = Math.Abs(ai[ii, kk]);
                        ll = ii;
                    }
                }
                if (ll == 0)
                    return;
                if (ll != kk)
                {
                    for (jj = 1; jj <= nn; jj++)
                    {
                        tt = ai[kk, jj];
                        ai[kk, jj] = ai[ll, jj];
                        ai[ll, jj] = tt;
                    }
                    tt = b[kk];
                    b[kk] = b[ll];
                    b[ll] = tt;
                }
                for (ii = kk + 1; ii <= nn; ii++)
                {
                    qq = ai[ii, kk] / ai[kk, kk];
                    for (jj = 1; jj <= nn; jj++)
                    {
                        if (jj == kk)
                            ai[ii, jj] = 0;
                        else
                            ai[ii, jj] = ai[ii, jj] - qq * ai[kk, jj];
                    }
                    b[ii] = b[ii] - qq * b[kk];
                }
            }

            x[nn] = b[nn] / ai[nn, nn];
            for (ii = nn - 1; ii >= 1; ii--)
            {
                tt = 0;
                for (jj = 1; jj <= nn - ii; jj++)
                {
                    tt = tt + ai[ii, ii + jj] * x[ii + jj];
                    x[ii] = (1 / ai[ii, ii]) * (b[ii] - tt);
                }
            }
            sq = 0.0;
            sq2 = 0.0;
            sq3 = 0.0;
            for (n = proType ? i0 : i0 - extension; n <= i0 + p; n++)
            {
                sum = 0;
                for (kk = 1; kk <= degree; kk++)
                {
                    sum += x[kk + 1] * Math.Pow(n, kk);
                }

                prc[index - n] = (x[1] + sum);
                sq += index - n <= index - i0 ? Math.Pow(source[index - n] - prc[index - n], 2) : sq;
                sq2 = sq;
                sq3 = sq;
            }

            sq = Math.Sqrt(sq / (p + 1)) * strdDev;
            sq2 = Math.Sqrt(sq2 / (p + 1)) * strdDev2;
            sq3 = Math.Sqrt(sq3 / (p + 1)) * strdDev3;
            for (n = proType ? 0 : -extension; n <= period; n++)
            {

                sqh[index - n] = prc[index - n] + sq;
                sqh2[index - n] = prc[index - n] + sq2;
                sqh3[index - n] = prc[index - n] + sq3;
                sql[index - n] = prc[index - n] - sq;
                sql2[index - n] = prc[index - n] - sq2;
                sql3[index - n] = prc[index - n] - sq3;
            }

            prc[index - period] = double.NaN;
            sqh[index - period] = double.NaN;
            sqh2[index - period] = double.NaN;
            sqh3[index - period] = double.NaN;
            sql[index - period] = double.NaN;
            sql2[index - period] = double.NaN;
            sql3[index - period] = double.NaN;

            if (proType)
                for (int ext = 0; ext < extension; ext++)
                {
                    prc[index + ext] = prc[index];
                    sqh[index + ext] = sqh[index];
                    sql[index + ext] = sql[index];
                    sqh2[index + ext] = sqh2[index];
                    sql2[index + ext] = sql2[index];
                    sqh3[index + ext] = sqh3[index];
                    sql3[index + ext] = sql3[index];
                }

            Chart.DrawTrendLine("end", end, sql3[end], end, sqh3[end], Color.Yellow, 1, LineStyle.LinesDots);
            //Hprc[index] = prc[index];
            //Hsqh[index] = sqh[index];
            //Hsqh2[index] = sqh2[index];
            //Hsqh3[index] = sqh3[index];
            //Hsql[index] = sql[index];
            //Hsql2[index] = sql2[index];
            //Hsql3[index] = sql3[index];
        }

        public override void Calculate(int index)
        {
        }

    }
}
Comments
0