How to get bar's high and low? Help please

Created at 23 Jun 2020, 16:29
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!
Capt.Z-Partner's avatar

Capt.Z-Partner

Joined 09.05.2020

How to get bar's high and low? Help please
23 Jun 2020, 16:29


Hello,

I want to get last bar's high and low prices in my cBots, but the print always gives the same value. 

Can anyone help to have a look, where is the problem?

Thanks,

Lei


using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class RayLei : Robot
    {
        protected override void OnBar()
        {
            Print("LastHigh: " + Bars.LastBar.High + " LastLow: " + Bars.LastBar.Low);
        }
    }
}

@Capt.Z-Partner
Replies

Capt.Z-Partner
23 Jun 2020, 16:43

OK, I got the answer by searching google. Sorry for disturbing.


using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class RayLei : Robot
    {
        protected override void OnBar()
        {
            Print("High: {0}, Low: {1}", Bars.HighPrices.Last(1), Bars.LowPrices.Last(1));
        }
    }
}

 


@Capt.Z-Partner

PanagiotisCharalampous
23 Jun 2020, 16:43

Hi Delphima,

See below

        protected override void OnBar()
        {
            Print("LastHigh: " + Bars.HighPrices.Last(1) + " LastLow: " + Bars.LowPrices.Last(1));
        }

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

Capt.Z-Partner
23 Jun 2020, 16:45

RE:

PanagiotisCharalampous said:

Hi Delphima,

See below

        protected override void OnBar()
        {
            Print("LastHigh: " + Bars.HighPrices.Last(1) + " LastLow: " + Bars.LowPrices.Last(1));
        }

Best Regards,

Panagiotis 

Join us on Telegram

 

Thanks Panagiotis,

I may have more questions to put to the forum, this is a great place to learn and having fun.

Thanks again.


@Capt.Z-Partner

prosteel1
27 Jun 2020, 16:07

RE:

Delphima said:

Hello,

I want to get last bar's high and low prices in my cBots, but the print always gives the same value. 

Can anyone help to have a look, where is the problem?

Thanks,

Lei


using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class RayLei : Robot
    {
        protected override void OnBar()
        {
            Print("LastHigh: " + Bars.LastBar.High + " LastLow: " + Bars.LastBar.Low);
        }
    }
}

The issue is that the OnBar() is being triggered on the open of a new bar - so the High and Low of the last bar is actually the current just opened bar, so the high and low is the same, as it just opened 1 tick ago and it is giving the high and low of the bid price.

When I started, I tried using Bars.HighPrices.Last(1) but found it too confusing to work with when accessing previous bars as the index number of previous bars keeps changing. I ditched Bars.HighPrices.Last(1) which has a constantly moving index where the most recient bar is always 1, in favor of the Bars.HighPrices[series.Count - 2] where the oldest bar is 1 and the most recient bar starts at 2000 or so and keeps incrementing in it's count as each new bar opens.

 

I run a cbot that can work on multiple timeframes and each time I call "Bars series = MarketData.GetBars(TimeFrame);" I can use an index of "series.Count - 2" to get the index of the PREVIOUS Bar on that timeframe that just closed while the index 1 bar is always the same most historical bar that only changes if I scroll back and make it load more bars. This gives me a fixed Datum.

 

I think it is the High and low of the previous bar that you want and personally I find it easier to have an origin (Datum) bar with an index of 1 as the most historical bar and each new bar increments by one.

 

It may not be as confusing in your cbot, but Bars.HighPrices.Last(1) almost did my head in when I started and almost gave up thinking what I wanted to do wasn't possible - until I found the way I currently use lol.

Basic Example:

        protected override void OnBar()
        {
            Print("Count - 2 = " + (Bars.Count - 2) + ", Previous Bar High: " + Bars.HighPrices[Bars.Count - 2] + ", Previous Bar Low: " + Bars.LowPrices[Bars.Count - 2]);
        }

Multi Timeframe example:

protected override void OnBar()
{
       Bars series = MarketData.GetBars(TimeFrame);
       Print("Count - 2 = " + (series.Count - 2) + ", Previous Bar High: " + series.HighPrices[series.Count - 2] + ", Previous Bar Low: " + series.LowPrices[series.Count - 2]);
}


@prosteel1