Category Other  Published on 23/01/2018

Cheat Chart

Description

Cheat Chart draws some future candles.


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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class CheatChart : Indicator
    {
        [Parameter("Candle Width", DefaultValue = 3, MinValue = 1, MaxValue = 5)]
        public int CandleWidth { get; set; }

        [Parameter("Checked Years", DefaultValue = 1, MinValue = 1, MaxValue = 3)]
        public int CheckYears { get; set; }

        private int actualCandleWidth;
        private TimeSlipCandles ts1, ts2, ts3;

        protected override void Initialize()
        {
            ts1 = Indicators.GetIndicator<TimeSlipCandles>(1, 1);
            ts2 = Indicators.GetIndicator<TimeSlipCandles>(2, 1);
            ts3 = Indicators.GetIndicator<TimeSlipCandles>(3, 1);

            switch (CandleWidth)
            {
                case 1:
                    actualCandleWidth = 1;
                    break;
                case 2:
                    actualCandleWidth = 3;
                    break;
                case 3:
                    actualCandleWidth = 5;
                    break;
                case 4:
                    actualCandleWidth = 11;
                    break;
                case 5:
                    actualCandleWidth = 25;
                    break;
                default:
                    actualCandleWidth = 0;
                    break;
            }
        }

        public override void Calculate(int index)
        {
            if (!IsLastBar)
                return;
            if (CheckYears <= 0)
                return;

            var lastRealBarClose = MarketSeries.Close[index];
            var shiftDown1 = ts1.Open[index + 1] - lastRealBarClose;
            var shiftDown2 = ts2.Open[index + 1] - lastRealBarClose;
            var shiftDown3 = ts3.Open[index + 1] - lastRealBarClose;

            if (MarketSeries.OpenTime[0].AddYears(CheckYears) > MarketSeries.OpenTime.LastValue)
            {
                ChartObjects.DrawText("Need Loading Message", "Scroll back to load past chart more!!", StaticPosition.Center, Colors.Yellow);
                return;
            }
            ChartObjects.RemoveAllObjects();

            for (var i = 0; i < 10; i++)
            {
                var j = index + i + 1;

                var year1valid = false;
                var year2valid = false;
                var year3valid = false;

                var open = 0.0;
                var close = 0.0;
                var high = 0.0;
                var low = 0.0;
                if (CheckYears >= 1 && (year1valid = IsValidPastData(ts1, j)))
                {
                    open += ts1.Open[j] - shiftDown1;
                    close += ts1.Close[j] - shiftDown1;
                    high += ts1.High[j] - shiftDown1;
                    low += ts1.Low[j] - shiftDown1;
                }
                if (CheckYears >= 2 && (year2valid = IsValidPastData(ts2, j)))
                {
                    open += ts2.Open[j] - shiftDown2;
                    close += ts2.Close[j] - shiftDown2;
                    high += ts2.High[j] - shiftDown2;
                    low += ts2.Low[j] - shiftDown2;
                }
                if (CheckYears >= 3 && (year3valid = IsValidPastData(ts3, j)))
                {
                    open += ts3.Open[j] - shiftDown3;
                    close += ts3.Close[j] - shiftDown3;
                    high += ts3.High[j] - shiftDown3;
                    low += ts3.Low[j] - shiftDown3;
                }

                var years = 0;
                if (year1valid)
                    years++;
                if (year2valid)
                    years++;
                if (year3valid)
                    years++;
                if (years == 0)
                    return;

                open /= years;
                close /= years;
                high /= years;
                low /= years;

                if (open > close)
                {
                    ChartObjects.DrawLine(string.Format("High {0}", i), j, high, j, low, Colors.Gray, 1);
                    ChartObjects.DrawLine(string.Format("Open {0}", i), j, open, j, close, Colors.DimGray, actualCandleWidth);
                }
                else
                {
                    ChartObjects.DrawLine(string.Format("Low {0}", i), j, high, j, low, Colors.Gray, 1);
                    ChartObjects.DrawLine(string.Format("Close {0}", i), j, open, j, close, Colors.LightGray, actualCandleWidth);
                }
            }
        }

        private bool IsValidPastData(TimeSlipCandles ts, int index)
        {
            var TOLERANCE = 1E-07;
            if (Math.Abs(ts.Open[index] - ts.Open[index + 1]) < TOLERANCE && Math.Abs(ts.Close[index] - ts.Close[index + 1]) < TOLERANCE && Math.Abs(ts.High[index] - ts.High[index + 1]) < TOLERANCE && Math.Abs(ts.Low[index] - ts.Low[index + 1]) < TOLERANCE)
                return false;
            if (Math.Abs(ts.Open[index] - ts.Open[index - 1]) < TOLERANCE && Math.Abs(ts.Close[index] - ts.Close[index - 1]) < TOLERANCE && Math.Abs(ts.High[index] - ts.High[index - 1]) < TOLERANCE && Math.Abs(ts.Low[index] - ts.Low[index - 1]) < TOLERANCE)
                return false;
            return true;
        }
    }
}


Allah's avatar
Allah

Joined on 13.01.2016

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Cheat Chart.algo
  • Rating: 0
  • Installs: 3162
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
UZ
uzairmaster97 · 3 years ago

Hi, please share TimeSlipCandles Indicator....

trader.calgo's avatar
trader.calgo · 5 years ago

Hi, share Indicator TimeSlipCandles, please