Bar number display indicator

alecs.spinu since: 20 Apr 2021;

  30 Apr 2021, 11:22
Bar number display indicator

Hi guys

can you help me with this indicator please?

it's written in C# for another platform and I would like to make it work for cTRader for bar counting

I have tried to make it work but got the error: No algo source was found in BAR COUNTING.csproj

Thank youu!

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator
{
    [UpdateOnEveryTick(false), SameAsSymbol(true)]
    public class Bar_Counter : IndicatorObject {
        public Bar_Counter(object _ctx):base(_ctx){
            Initialvalue = 0; //Default value
            Fontsize = 7; //Default value
            Distancebelowbar = 2; //Default value
            Zerotime = "13:30:00"; //Default value
        }
        private ITextObject plot1;
        
        private int Initialvalue;
        
        [Input]
        public int Distancebelowbar { get; set; }
        
        [Input]
        public int Fontsize { get; set; }
        
        [Input]
        public string Zerotime { get; set; }


        protected override void Create() {
            // create variable objects, function objects, plot objects etc.
        }
        protected override void StartCalc() {
            // assign inputs 
        }
        protected override void CalcBar(){
            // indicator logic
            
            Initialvalue ++;
            plot1 = DrwText.Create(new ChartPoint(Bars.Time[0], Bars.Low[0] - Distancebelowbar),"");
            plot1.Color = Color.Gray;
            plot1.Size = Fontsize;

            string Mytime = Bars.Sessions[0].StartTime.ToString();
            string Timenow = Bars.Time[0].ToString("HH:mm:ss");
            
            if (Timenow == Zerotime)
            {
                Initialvalue = 1;
            }
            
            plot1.Text = Initialvalue.ToString();

        }
    }
}

amusleh's avatar

amusleh since: 01 Mar 2021;

  01 May 2021, 13:39

Hi,

The Bars.Count gives you the number of available bars, check this sample indicator:

using cAlgo.API;

namespace cAlgo
{
    /// <summary>
    /// This sample indicator shows how to show the number of bars
    /// </summary>
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class BarsCountSample : Indicator
    {
        private TextBlock _countTextBlock;

        protected override void Initialize()
        {
            var grid = new Grid(1, 2)
            {
                BackgroundColor = Color.Gold,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
            };

            var style = new Style();

            style.Set(ControlProperty.Padding, 1);
            style.Set(ControlProperty.Margin, 3);
            style.Set(ControlProperty.BackgroundColor, Color.Black);

            grid.AddChild(new TextBlock { Text = "Bars #", Style = style }, 0, 0);

            _countTextBlock = new TextBlock { Text = Bars.Count.ToString(), Style = style };

            grid.AddChild(_countTextBlock, 0, 1);

            Chart.AddControl(grid);
        }

        public override void Calculate(int index)
        {
            if (!IsLastBar) return;

            _countTextBlock.Text = Bars.Count.ToString();
        }
    }
}
Community Developer | Spotware

alecs.spinu since: 20 Apr 2021;

  01 May 2021, 15:10

Thank you for sharing this ideea

I've tried to add it but does not help

It displayes the number of bars on the chart in a small box in the top left corner of the chart (a sum of the bars)

amusleh's avatar

amusleh since: 01 Mar 2021;

  02 May 2021, 10:06
RE:

alecs.spinu said:

Thank you for sharing this ideea

I've tried to add it but does not help

It displayes the number of bars on the chart in a small box in the top left corner of the chart (a sum of the bars)

It display the number of bars, if new bars come it will change the number, you can change the code to show the historical bars number too:

using cAlgo.API;

namespace cAlgo
{
    /// <summary>
    /// This sample indicator shows how to show the number of bars
    /// </summary>
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class BarsCountSample : Indicator
    {
        private TextBlock _countTextBlock;
        
        private int _barCount;

        protected override void Initialize()
        {
            var grid = new Grid(1, 2)
            {
                BackgroundColor = Color.Gold,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
            };

            var style = new Style();

            style.Set(ControlProperty.Padding, 1);
            style.Set(ControlProperty.Margin, 3);
            style.Set(ControlProperty.BackgroundColor, Color.Black);

            grid.AddChild(new TextBlock { Text = "Bars #", Style = style }, 0, 0);

            _countTextBlock = new TextBlock { Text = "0", Style = style };

            grid.AddChild(_countTextBlock, 0, 1);

            Chart.AddControl(grid);
        }

        public override void Calculate(int index)
        {
            if (index == _barCount) return;
        
            _barCount = index;
        
            _countTextBlock.Text = _barCount.ToString();
        }
    }
}

The above example is equivalent of your code.

Community Developer | Spotware