cTrader API question - get bar info from chart object time index

Created at 01 Nov 2022, 11:24
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!
WA

waym77

Joined 22.07.2021

cTrader API question - get bar info from chart object time index
01 Nov 2022, 11:24


Hi,

I was wondering if it were possible for me to get the Bars info (eg. OpenPrices, ClosePrices, etc.) from a ChartObject's (such as a vertical line) Time index?

Any help is appreciated, thanks.


@waym77
Replies

Waxy
01 Nov 2022, 21:04

RE:

 

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

namespace cAlgo
{
    [Indicator(AccessRights = AccessRights.None)]
    public class NewIndicator : Indicator
    {
        private ChartVerticalLine _verticalLine;

        /*
         -- I was wondering if it were possible for me to get the Bars info (eg. OpenPrices, ClosePrices, etc.) from a ChartObject's (such as a vertical line) Time index?           
         -- Any help is appreciated, thanks.
         */

        protected override void Initialize()
        {
            _verticalLine = Chart.DrawVerticalLine("VerticalLine", Bars.OpenTimes.LastValue, Color.Red);

            var index = Bars.OpenTimes.GetIndexByTime(_verticalLine.Time);

            var bar = Bars[index];
            
            Print("Open: {0}, High: {1}, Low: {2}, Close: {3}", bar.Open, bar.High, bar.Low, bar.Close);
        }

        public override void Calculate(int index)
        {
            // Calculate value at specified index
            // Result[index] = 
        }
    }
}

 


@Waxy

waym77
02 Nov 2022, 14:21

RE: RE:

Waxy said:

 

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

namespace cAlgo
{
    [Indicator(AccessRights = AccessRights.None)]
    public class NewIndicator : Indicator
    {
        private ChartVerticalLine _verticalLine;

        /*
         -- I was wondering if it were possible for me to get the Bars info (eg. OpenPrices, ClosePrices, etc.) from a ChartObject's (such as a vertical line) Time index?           
         -- Any help is appreciated, thanks.
         */

        protected override void Initialize()
        {
            _verticalLine = Chart.DrawVerticalLine("VerticalLine", Bars.OpenTimes.LastValue, Color.Red);

            var index = Bars.OpenTimes.GetIndexByTime(_verticalLine.Time);

            var bar = Bars[index];
            
            Print("Open: {0}, High: {1}, Low: {2}, Close: {3}", bar.Open, bar.High, bar.Low, bar.Close);
        }

        public override void Calculate(int index)
        {
            // Calculate value at specified index
            // Result[index] = 
        }
    }
}

 

Hi, thanks for this!

I was also wondering if there is any way to check at which bar index the price crosses a certain number?

Thanks,


@waym77

Waxy
04 Nov 2022, 08:42

RE: RE: RE:

That's different, not sure but something like:
 

var checkPrice = 1.5;
var crossedBar = Bars.Select((bar, index) => (bar, index)).LastOrDefault(x => x.bar.Low <= checkPrice && x.bar.High >= checkPrice);

if (crossedBar.index != 0)
  Print($"Bar Index is {crossedBar.index}");

 


@Waxy