Category Trend  Published on 11/12/2022

renko

Description

using cAlgo.API;<font></font>

using cAlgo.API.Indicators;<font></font>

using cAlgo.API.Internals;<font></font>

using cAlgo.Indicators;<font></font>

using System;<font></font>

using System.Collections.Generic;<font></font>

using System.Linq;<font></font>

using System.Windows.Forms;<font></font>

using System.Runtime.InteropServices;<font></font>

using System.Diagnostics;<font></font>

using cT_MedianRenkoEngine;<font></font>

<font></font>

namespace cAlgo<font></font>

{<font></font>

    [Indicator("MedianRenko", IsOverlay = true, AutoRescale = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]<font></font>

    public class MedianRenko : Indicator<font></font>

    {<font></font>

        [Parameter("Bar Size (Pips)", DefaultValue = 10, MinValue = 0.1, Step = 1)]<font></font>

        public double BarSizePips { get; set; }<font></font>

<font></font>

        [Parameter("Retracement factor (0.01 - 1.00)", DefaultValue = 0.5, MinValue = 0.01, MaxValue = 1.0, Step = 0.01)]<font></font>

        public double Retracement { get; set; }<font></font>

<font></font>

        [Parameter("Maximum Bars", DefaultValue = 300, MinValue = 1)]<font></font>

        public int BricksToShow { get; set; }<font></font>

<font></font>

        [Parameter("Symmetrical Reversals", DefaultValue = true)]<font></font>

        public bool SymmetricalReversals { get; set; }<font></font>

<font></font>

        [Parameter("Reset Open on new trading day", DefaultValue = false)]<font></font>

        public bool ResetOpenOnNewTradingDay { get; set; }<font></font>

<font></font>

        [Parameter("Apply offset to first renko bar", DefaultValue = false)]<font></font>

        public bool ApplyOffsetToFirstBar { get; set; }<font></font>

<font></font>

        [Parameter("Tick offset value", DefaultValue = 0, MinValue = 0)]<font></font>

        public int OffsetValue { get; set; }<font></font>

<font></font>

        [Parameter("Bullish Bar Color", DefaultValue = "SeaGreen")]<font></font>

        public string ColorBull { get; set; }<font></font>

<font></font>

        [Parameter("Bearish Bar Color", DefaultValue = "Tomato")]<font></font>

        public string ColorBear { get; set; }<font></font>

<font></font>

        [Output("Open", Color = Colors.DimGray, Thickness = 0, PlotType = PlotType.Points)]<font></font>

        public IndicatorDataSeries Open { get; set; }<font></font>

<font></font>

        [Output("High", Color = Colors.DimGray, Thickness = 1, PlotType = PlotType.Points)]<font></font>

        public IndicatorDataSeries High { get; set; }<font></font>

<font></font>

        [Output("Low", Color = Colors.DimGray, Thickness = 0, PlotType = PlotType.Points)]<font></font>

        public IndicatorDataSeries Low { get; set; }<font></font>

<font></font>

        [Output("Close", Color = Colors.DimGray, Thickness = 0, PlotType = PlotType.Points)]<font></font>

        public IndicatorDataSeries Close { get; set; }<font></font>

<font></font>

        private MedianRenkoCore core = null;<font></font>

        private bool coreOK = true;<font></font>

<font></font>

        private DateTime lastTime;<font></font>

<font></font>

        private bool appliedOffset = false;<font></font>

<font></font>

        private T_OLHCV pOLHCV;<font></font>

        private T_OLHCV OLHCV;<font></font>

        private T_OLHCV mOLHCV;<font></font>

        private T_OLHCV cOLHCV;<font></font>

        private T_OLHCV memRates;<font></font>

        private T_OLHCV lastBar;<font></font>

<font></font>

        private List<T_OLHCV> bars = new List<T_OLHCV>();<font></font>

        private double barSize;<font></font>

        private Colors colorBull, colorBear;<font></font>

<font></font>

        protected override void Initialize()<font></font>

        {<font></font>

            core = new MedianRenkoCore();<font></font>

            coreOK = core.StatusCheck();<font></font>

<font></font>

            barSize = BarSizePips * Symbol.PipSize;<font></font>

<font></font>

            lastTime = new DateTime();<font></font>

            lastTime = MarketSeries.OpenTime[MarketSeries.Close.Count - 1];<font></font>

<font></font>

            lastBar = new T_OLHCV();<font></font>

            pOLHCV = new T_OLHCV();<font></font>

            OLHCV = new T_OLHCV();<font></font>

            mOLHCV = new T_OLHCV();<font></font>

            cOLHCV = new T_OLHCV();<font></font>

            memRates = new T_OLHCV();<font></font>

<font></font>

            if (!Enum.TryParse<Colors>(ColorBull, out colorBull))<font></font>

            {<font></font>

                colorBull = Colors.SeaGreen;<font></font>

            }<font></font>

            if (!Enum.TryParse<Colors>(ColorBear, out colorBear))<font></font>

            {<font></font>

                colorBear = Colors.Tomato;<font></font>

            }<font></font>

<font></font>

            appliedOffset = false;<font></font>

        }<font></font>

<font></font>

        private bool NewBar(int index)<font></font>

        {<font></font>

            if (lastTime != MarketSeries.OpenTime[index])<font></font>

            {<font></font>

                lastTime = MarketSeries.OpenTime[index];<font></font>

                return true;<font></font>

            }<font></font>

<font></font>

            return false;<font></font>

        }<font></font>

<font></font>

        private static int cc = 0;<font></font>

        private void pushBars(T_OLHCV bar, bool display)<font></font>

        {<font></font>

            int count = bars.Count();<font></font>

<font></font>

            var newBar = new T_OLHCV();<font></font>

            newBar.Copy(bar);<font></font>

            newBar.Index = cc++;<font></font>

<font></font>

            bars.Insert(0, newBar);<font></font>

            count++;<font></font>

<font></font>

            if (display)<font></font>

            {<font></font>

                shiftBars(0);<font></font>

            }<font></font>

<font></font>

            if (count > BricksToShow)<font></font>

            {<font></font>

                if (display)<font></font>

                {<font></font>

                    for (int i = BricksToShow - 1; i < count; i++)<font></font>

                    {<font></font>

                        ChartObjects.RemoveObject("mr_Wick_" + bars[i].Index);<font></font>

                        ChartObjects.RemoveObject("mr_Bar_" + bars[i].Index);<font></font>

                    }<font></font>

                }<font></font>

<font></font>

                bars.RemoveRange(BricksToShow, count - BricksToShow);<font></font>

<font></font>

                Open[count - BricksToShow] = double.NaN;<font></font>

                High[count - BricksToShow] = double.NaN;<font></font>

                Low[count - BricksToShow] = double.NaN;<font></font>

                Close[count - BricksToShow] = double.NaN;<font></font>

            }<font></font>

<font></font>

        }<font></font>

<font></font>

        private void shiftBars(int shift)<font></font>

        {<font></font>

            int count = bars.Count;<font></font>

            int ix = MarketSeries.Close.Count - 2;<font></font>

<font></font>

            for (int i = 0; i < count && i < BricksToShow; i++)<font></font>

            {<font></font>

                var candleColor = (bars[i].Close > bars[i].Open) ? colorBull : colorBear;<font></font>

<font></font>

                ChartObjects.DrawLine("mr_Wick_" + bars[i].Index, ix, bars[i].High, ix, bars[i].Low, candleColor, 1, LineStyle.Solid);<font></font>

                ChartObjects.DrawLine("mr_Bar_" + bars[i].Index, ix, bars[i].Close, ix, bars[i].Open, candleColor, 5, LineStyle.Solid);<font></font>

<font></font>

                Open[ix] = bars[i].Open;<font></font>

                High[ix] = bars[i].High;<font></font>

                Low[ix] = bars[i].Low;<font></font>

                Close[ix] = bars[i].Close;<font></font>

<font></font>

                ix--;<font></font>

            }<font></font>

        }<font></font>

<font></font>

        private void UpdateLastBar(int ix)<font></font>

        {<font></font>

            var candleColor = (memRates.Close > memRates.Open) ? colorBull : colorBear;<font></font>

<font></font>

            ChartObjects.DrawLine("mr_Wick_Live", ix, memRates.High, ix, memRates.Low, candleColor, 1, LineStyle.Solid);<font></font>

            ChartObjects.DrawLine("mr_Bar_Live", ix, memRates.Close, ix, memRates.Open, candleColor, 5, LineStyle.Solid);<font></font>

<font></font>

            Open[ix] = memRates.Open;<font></font>

            High[ix] = memRates.High;<font></font>

            Low[ix] = memRates.Low;<font></font>

            Close[ix] = memRates.Close;<font></font>

<font></font>

        }<font></font>

<font></font>

        public override void Calculate(int index)<font></font>

        {<font></font>

            if (ResetOpenOnNewTradingDay)<font></font>

            {<font></font>

                if (IsNewSession(MarketSeries.OpenTime[(index > 0) ? (index - 1) : index], MarketSeries.OpenTime[index]))<font></font>

                {<font></font>

                    cOLHCV.Clear();<font></font>

                    mOLHCV.Clear();<font></font>

                    pOLHCV.Clear();<font></font>

                    memRates.Copy(mOLHCV);<font></font>

                }<font></font>

            }<font></font>

<font></font>

            if (IsRealTime)<font></font>

            {<font></font>

                if (!coreOK)<font></font>

                {<font></font>

                    ChartObjects.DrawText("TrialOverMsg", "MedianRenko Indicator - Trial period ended. Please purchase a license file at www.az-invest.eu", StaticPosition.Center, Colors.White);<font></font>

                    return;<font></font>

                }<font></font>

<font></font>

                if (NewBar(index))<font></font>

                {<font></font>

                    shiftBars(0);<font></font>

<font></font>

                    Open[index - BricksToShow - 1] = double.NaN;<font></font>

                    High[index - BricksToShow - 1] = double.NaN;<font></font>

                    Low[index - BricksToShow - 1] = double.NaN;<font></font>

                    Close[index - BricksToShow - 1] = double.NaN;<font></font>

                }<font></font>

<font></font>

                OLHCV.Open = MarketSeries.Close[index];<font></font>

                OLHCV.Low = MarketSeries.Close[index];<font></font>

                OLHCV.High = MarketSeries.Close[index];<font></font>

                OLHCV.Close = MarketSeries.Close[index];<font></font>

                OLHCV.OpenTime = MarketSeries.OpenTime[index];<font></font>

                OLHCV.TickVolume = 1;<font></font>

<font></font>

<font></font>

                ProcessMarketData(OLHCV);<font></font>

                UpdateLastBar(index);<font></font>

            }<font></font>

            else<font></font>

            {<font></font>

                OLHCV.Open = MarketSeries.Open[index];<font></font>

                OLHCV.Low = MarketSeries.Low[index];<font></font>

                OLHCV.High = MarketSeries.High[index];<font></font>

                OLHCV.Close = MarketSeries.Close[index];<font></font>

                OLHCV.OpenTime = MarketSeries.OpenTime[index];<font></font>

                OLHCV.TickVolume = MarketSeries.TickVolume[index];<font></font>

<font></font>

                if (ApplyOffsetToFirstBar && !appliedOffset)<font></font>

                {<font></font>

                    OLHCV.Open = OffsetValue * Symbol.TickSize + Math.Floor(OLHCV.Open / barSize) * barSize;<font></font>

                    appliedOffset = true;<font></font>

                }<font></font>

<font></font>

                ProcessMarketData(OLHCV);<font></font>

<font></font>

                if (IsLastBar)<font></font>

                    UpdateLastBar(index);<font></font>

            }<font></font>

        }<font></font>

<font></font>

        private void ProcessMarketData(T_OLHCV ___OLHCV)<font></font>

        {<font></font>

            int rb_return = core.ProcessMedianRenko(barSize, Retracement, SymmetricalReversals, ___OLHCV, ref pOLHCV, ref mOLHCV, ref cOLHCV, 1);<font></font>

            while (true)<font></font>

            {<font></font>

                if ((rb_return >= 1) && (rb_return <= 4))<font></font>

                {<font></font>

                    pushBars(cOLHCV, IsRealTime);<font></font>

                    memRates.Copy(mOLHCV);<font></font>

                    OLHCV.Copy(memRates);<font></font>

<font></font>

                    cOLHCV.Clear();<font></font>

                    mOLHCV.Clear();<font></font>

<font></font>

                    rb_return = core.ProcessMedianRenko(barSize, Retracement, SymmetricalReversals, ___OLHCV, ref pOLHCV, ref mOLHCV, ref cOLHCV, 0);<font></font>

                    continue;<font></font>

<font></font>

                }<font></font>

                else if (rb_return == 10)<font></font>

                {<font></font>

                    memRates.Copy(mOLHCV);<font></font>

                    break;<font></font>

                }<font></font>

                else if (rb_return == -1)<font></font>

                {<font></font>

                    return;<font></font>

                }<font></font>

                else<font></font>

                {<font></font>

                    throw new Exception("Unhandled return value = " + rb_return);<font></font>

                }<font></font>

            }<font></font>

<font></font>

            if (IsLastBar && !IsRealTime)<font></font>

            {<font></font>

                shiftBars(0);<font></font>

            }<font></font>

        }<font></font>

<font></font>

        private bool IsNewSession(DateTime prevTime, DateTime currTime)<font></font>

        {<font></font>

            if (prevTime.DayOfWeek != currTime.DayOfWeek)<font></font>

                return true;<font></font>

            else<font></font>

                return false;<font></font>

        }<font></font>

<font></font>

<font></font>

    }<font></font>

}<font></font>

<font></font>


using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System;
using System.Collections.Generic;
using System.Linq;

namespace cAlgo
{
    [Indicator("Renko", IsOverlay = true, AutoRescale = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Renko : Indicator
    {
        [Parameter("Renko (Pips)", DefaultValue = 10, MinValue = 0.1, Step = 1)]
        public double RenkoPips { get; set; }

        [Parameter("Bricks To Show", DefaultValue = 100, MinValue = 1)]
        public int BricksToShow { get; set; }

        [Parameter("Zoom Level", DefaultValue = 3, MinValue = 0, MaxValue = 5, Step = 1)]
        public double ZoomLevel { get; set; }

        [Parameter("Bullish Color", DefaultValue = "SeaGreen")]
        public string ColorBull { get; set; }

        [Parameter("Bearish Color", DefaultValue = "Tomato")]
        public string ColorBear { get; set; }

        [Output("Open", Color = Colors.DimGray, Thickness = 1, PlotType = PlotType.Points)]
        public IndicatorDataSeries Open { get; set; }

        [Output("High", Color = Colors.DimGray, Thickness = 1, PlotType = PlotType.Points)]
        public IndicatorDataSeries High { get; set; }

        [Output("Low", Color = Colors.DimGray, Thickness = 1, PlotType = PlotType.Points)]
        public IndicatorDataSeries Low { get; set; }

        [Output("Close", Color = Colors.DimGray, Thickness = 1, PlotType = PlotType.Points)]
        public IndicatorDataSeries Close { get; set; }

        public class Brick
        {
            public double Open { get; set; }
            public double Close { get; set; }
        }

        private List<Brick> renkos = new List<Brick>();
        private double closeLastValue, thickness, renkoPips, renkoLastValue;
        private Colors colorBull, colorBear;
        private bool colorError;
        private int lastCount;

        protected override void Initialize()
        {
            if (!Enum.TryParse<Colors>(ColorBull, out colorBull) || !Enum.TryParse<Colors>(ColorBear, out colorBear))
                colorError = true;

            renkoPips = RenkoPips * Symbol.PipSize;
            thickness = Math.Pow(2, ZoomLevel) - (ZoomLevel > 0 ? 1 : 0);
            renkoLastValue = 0;
        }

        public override void Calculate(int index)
        {
            if (colorError)
            {
                ChartObjects.DrawText("Error0", "{o,o}\n/)_)\n \" \"\nOops! Incorrect colors.", StaticPosition.TopCenter, Colors.Gray);
                return;
            }

            if (renkoLastValue == 0)
            {
                var open = MarketSeries.Open.LastValue;

                renkoLastValue = open - (open % renkoPips) + renkoPips / 2;
            }

            closeLastValue = MarketSeries.Close.LastValue;

            while (closeLastValue >= renkoLastValue + renkoPips * 1.5)
            {
                renkoLastValue += renkoPips;
                renkos.Insert(0, new Brick 
                {
                    Open = renkoLastValue - renkoPips / 2,
                    Close = renkoLastValue + renkoPips / 2
                });
                if (renkos.Count() > BricksToShow)
                    renkos.RemoveRange(BricksToShow, renkos.Count() - BricksToShow);
                if (IsLastBar)
                    UpdateHistory(index);
            }
            while (closeLastValue <= renkoLastValue - renkoPips * 1.5)
            {
                renkoLastValue -= renkoPips;
                renkos.Insert(0, new Brick 
                {
                    Open = renkoLastValue + renkoPips / 2,
                    Close = renkoLastValue - renkoPips / 2
                });
                if (renkos.Count() > BricksToShow)
                    renkos.RemoveRange(BricksToShow, renkos.Count() - BricksToShow);
                if (IsLastBar)
                    UpdateHistory(index);
            }

            bool isNewBar = MarketSeries.Close.Count > lastCount;

            if (IsLastBar && isNewBar)
            {
                UpdateHistory(index);

                Open[index - BricksToShow] = double.NaN;
                High[index - BricksToShow] = double.NaN;
                Low[index - BricksToShow] = double.NaN;
                Close[index - BricksToShow] = double.NaN;

                lastCount = MarketSeries.Close.Count;
            }

            if (IsRealTime)
                UpdateLive(index);
        }

        private void UpdateHistory(int index)
        {
            for (int i = 0; i < BricksToShow - 1 && i < renkos.Count() - 1; i++)
            {
                var color = renkos[i].Open < renkos[i].Close ? colorBull : colorBear;

                ChartObjects.DrawLine(string.Format("renko.Last({0})", i + 1), index - i - 1, renkos[i].Open, index - i - 1, renkos[i].Close, color, thickness, LineStyle.Solid);

                Open[index - i - 1] = renkos[i].Open;
                High[index - i - 1] = Math.Max(renkos[i].Open, renkos[i].Close);
                Low[index - i - 1] = Math.Min(renkos[i].Open, renkos[i].Close);
                Close[index - i - 1] = renkos[i].Close;
            }
        }

        private void UpdateLive(int index)
        {
            double y1, y2;
            var top = Math.Max(renkos[0].Open, renkos[0].Close);
            var bottom = Math.Min(renkos[0].Open, renkos[0].Close);

            if (closeLastValue > top)
                y1 = top;
            else if (closeLastValue < bottom)
                y1 = bottom;
            else
                y1 = closeLastValue;

            y2 = closeLastValue;

            var colorLive = y1 < y2 ? colorBull : colorBear;

            ChartObjects.DrawLine("renko.Live", index, y1, index, y2, colorLive, thickness, LineStyle.Solid);

            Open[index] = y1;
            High[index] = y1 > y2 ? y1 : y2;
            Low[index] = y1 < y2 ? y1 : y2;
            Close[index] = y2;
        }
    }
}


MR
mrhung.tvt

Joined on 10.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Renko.algo
  • Rating: 0
  • Installs: 597
  • Modified: 11/12/2022 03:14
Comments
Log in to add a comment.
No comments found.