Renko - cBot dont get all Bars

Created at 22 Aug 2024, 16:10
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
BL

BlackTiger83

Joined 19.08.2024

Renko - cBot dont get all Bars
22 Aug 2024, 16:10


I have a very simple script that should number each Renko Bars (because I need a certain value from them).
However, some renko Bars are not numbered. Does anyone have an idea why? 
(See Image)
Using Ctrader 5.0.20 MAC

 

For Example here is the 11180 missing
And here is the simple code:
US30 RE20

 

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SimpleRenkoColorNumber : Robot
    {
        [Parameter("Renko Block Size (Pips)", DefaultValue = 10)]
        public int RenkoBlockSize { get; set; }

        [Parameter("Left Shift (Bars)", DefaultValue = 1)]
        public int LeftShift { get; set; }

        private double _renkoStartPrice;
        private int _barCounter;

        protected override void OnStart()
        {
            _renkoStartPrice = Bars.ClosePrices.Last(1);
            _barCounter = 0;
        }

        protected override void OnBarClosed()
        {
            double currentPrice = Bars.ClosePrices.Last(1);
            double priceMovement = currentPrice - _renkoStartPrice;

            if (Math.Abs(priceMovement) >= RenkoBlockSize * Symbol.PipSize)
            {
                bool isBullish = priceMovement > 0;
                DrawBlock(isBullish ? Color.Green : Color.Red, _barCounter);
                DrawBarNumber(_barCounter);

                _barCounter++;
                _renkoStartPrice = currentPrice;
            }
        }

        private void DrawBlock(Color color, int blockNumber)
        {
            string objectName = "RenkoBlock_" + blockNumber;
            DateTime shiftedTime = GetShiftedTime(LeftShift);
            Chart.DrawIcon(objectName, ChartIconType.Square, shiftedTime, Bars.ClosePrices.Last(1), color);
        }

        private void DrawBarNumber(int barNumber)
        {
            string textName = "BarNumber_" + barNumber;
            string text = barNumber.ToString();
            DateTime shiftedTime = GetShiftedTime(LeftShift);
            Chart.DrawText(textName, text, shiftedTime, Bars.ClosePrices.Last(1), Color.White);
        }

        private DateTime GetShiftedTime(int shift)
        {
            int index = Math.Max(0, Bars.Count - 1 - shift);
            return Bars.OpenTimes[index];
        }
    }
}

@BlackTiger83
Replies

PanagiotisCharalampous
23 Aug 2024, 06:39

Hi there,

I can see 11180. Can you check again?

Best regards,

Panagiotis


@PanagiotisCharalampous

BlackTiger83
23 Aug 2024, 06:57 ( Updated at: 23 Aug 2024, 13:17 )

RE: Renko - cBot dont get all Bars

PanagiotisCharalampous said: 

Hi there,

I can see 11180. Can you check again?

Best regards,

Panagiotis

You are totally right, sorry. But I had this in several places. See here: 
I thought the “OnBar()” or “OnBarClosed()” gets every bar?
NAS 100 RE10

 

Code:

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SimpleRenkoColorNumber : Robot
    {
        private int previousIndex = -1; 
        protected override void OnBarClosed()
        {
            int index = Bars.Count - 1;

            // Überprüfen, ob der aktuelle Index genau um 1 größer ist als der vorherige
            if (previousIndex >= 0 && index != previousIndex + 1)
            {
                DrawWarningLine(Bars.OpenTimes[index]);
            }

            Chart.DrawText("BarNumber" + index, (index + 1).ToString(), Bars.OpenTimes[index], Bars.HighPrices[index] + Symbol.PipSize * 5, Color.White);

            Print("Bar closed: " + index);


            previousIndex = index;
        }

        private void DrawWarningLine(DateTime time)
        {
            string lineName = "WarningLine_" + time.ToString("yyyyMMddHHmmss");
            Chart.DrawVerticalLine(lineName, time, Color.Yellow, 2, LineStyle.Solid);
        }
    }
}

@BlackTiger83