OHLC Current Bar

Created at 05 Oct 2021, 21:56
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!
CH

charltonadams38

Joined 05.10.2021

OHLC Current Bar
05 Oct 2021, 21:56


Hi

How do I get the OHCL data of the current bar?

I know how to access these values (Bars.Last(0).XXXX) but they all give me the same value for example:

  • 29/09/2021 21:00:00.000 | CurrentHigh: 1,15974
  • 29/09/2021 21:00:00.000 | CurrentOpen: 1,15974

Can anyone please advise? Thanks


@charltonadams38
Replies

amusleh
06 Oct 2021, 09:04

Hi,

When a new bar opens all of its OHLC prices are same as its open price, but when new ticks are coming the high, low, and close will start to develop and change.

If you access the last bar OHLC values inside OnBar method it will be all same, because the OnBar method is called when a new bar opens and the new bar OHLC are all same.

Try this:

using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        protected override void OnTick()
        {
            Print("Open: {0} | High: {1} | Low: {2} | Close: {3}", Bars.OpenPrices.LastValue, Bars.HighPrices.LastValue, Bars.LowPrices.LastValue, Bars.ClosePrices.LastValue);
        }
    }
}

If you change the OnTick to OnBar you will see whenever a new bar opens the method will be called and all OHLC prices will be same.


@amusleh

charltonadams38
06 Oct 2021, 11:39

RE:

Hi amusleh

First, thank you for your reply and valuable input.

Second, I would like to buy using onTick() will I be able to access the hourly data of a daily candle right before a new candle opens?

 

For example, I would like to get the High just before a new daily candle starts to ensure the currentHigh is as accurate as possible.

 

I am new to programming trading bots and I've been reading through the documentation but there is so much to read through it is hard to find the information I am looking for, hence the reason I have been using OnBar().

 

Thank you

 


@charltonadams38

charltonadams38
06 Oct 2021, 11:39 ( Updated at: 06 Oct 2021, 13:32 )

RE:

amusleh said:

Hi,

When a new bar opens all of its OHLC prices are same as its open price, but when new ticks are coming the high, low, and close will start to develop and change.

If you access the last bar OHLC values inside OnBar method it will be all same, because the OnBar method is called when a new bar opens and the new bar OHLC are all same.

Try this:

using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        protected override void OnTick()
        {
            Print("Open: {0} | High: {1} | Low: {2} | Close: {3}", Bars.OpenPrices.LastValue, Bars.HighPrices.LastValue, Bars.LowPrices.LastValue, Bars.ClosePrices.LastValue);
        }
    }
}

If you change the OnTick to OnBar you will see whenever a new bar opens the method will be called and all OHLC prices will be same.

Hi amusleh

First, thank you for your reply and valuable input.

Second, I would like to buy using onTick() will I be able to access the hourly data of a daily candle right before a new candle opens?

For example, I would like to get the High just before a new daily candle starts to ensure the dailyHigh is as accurate as possible.

I am new to programming trading bots and I've been reading through the documentation but there is so much to read through it is hard to find the information I am looking for, hence the reason I have been using OnBar().

Thank you


@charltonadams38

amusleh
07 Oct 2021, 19:59

Hi,

You can use Bars inside OnTick method to get access to bars data, Bars.HighPrices.Last(1) will give you the latest closed bar high value and Bars.HighPrices.LastValue will give you the current open bar high.


@amusleh