ONBAR

Created at 18 Nov 2016, 02:07
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!
CE

cedric.boudreau

Joined 02.03.2016

ONBAR
18 Nov 2016, 02:07


On we can use  on cbots  of m30 frame the data value form m1 or m15 bar value :

 

onbar (m1)

command..

onbar (m15)

command..

onbard(m30) 

command..


@cedric.boudreau
Replies

cTKit
18 Nov 2016, 11:18

Hi Cedric

OnBar is called when a new bar opens for the MarketSeries for the chart your bot is attached to.  To get data for other time frames you would need to use one of the MarketData.GetSeries() methods in your OnStart method to create a new MarketSeries for each additional time frame you want to access.  If all you want is the last value then things are relatively simple, but if you need anything else you are going to have to calculate the correct index to reference the additional timeframe data.

The following example should get you started with access the last value.

 

private MarketSeries m1Series;
private MarketSeries m15Series;

protected override void OnStart()
{
    m1Series = MarketData.GetSeries(TimeFrame.Minute);
    m15Series = MarketData.GetSeries(TimeFrame.Minute15);
}

protected override void OnBar()
{
    var m1Value = m1Series.Close.LastValue;
    var m15Value = m15Series.Close.LastValue;
   
    // do something with values
}

 


@cTKit

Jiri
18 Nov 2016, 18:02

Hi, there isn't built-in function called on each bar except from the market series of the chart that you can override. However, you can write your own function for each series.

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class OnBar : Robot
    {
        private MarketSeries seriesMinute1, seriesMinute15, seriesMinute30;
        private int lastIndexMinute1, lastIndexMinute15, lastIndexMinute30;

        protected override void OnStart()
        {
            seriesMinute1 = MarketData.GetSeries(TimeFrame.Minute);
            seriesMinute15 = MarketData.GetSeries(TimeFrame.Minute15);
            seriesMinute30 = MarketData.GetSeries(TimeFrame.Minute15);
        }

        protected override void OnTick()
        {
            int currentIndexMinute1 = seriesMinute1.Open.Count - 1;
            int currentIndexMinute15 = seriesMinute15.Open.Count - 1;
            int currentIndexMinute30 = seriesMinute30.Open.Count - 1;

            if (currentIndexMinute1 > lastIndexMinute1)
            {
                OnBarMinute1(currentIndexMinute1);
                lastIndexMinute1 = currentIndexMinute1;
            }

            if (currentIndexMinute15 > lastIndexMinute15)
            {
                OnBarMinute15(currentIndexMinute15);
                lastIndexMinute15 = currentIndexMinute15;
            }

            if (currentIndexMinute30 > lastIndexMinute30)
            {
                OnBarMinute30(currentIndexMinute30);
                lastIndexMinute30 = currentIndexMinute30;
            }
        }

        private void OnBarMinute1(int index)
        {
            // Called on each new bar of Minute1 dataseries
        }

        private void OnBarMinute15(int index)
        {
            // Called on each new bar of Minute15 dataseries
        }

        private void OnBarMinute30(int index)
        {
            // Called on each new bar of Minute30 dataseries
        }
    }
}

 


@Jiri

cedric.boudreau
20 Nov 2016, 17:22

RE:

thanks for that anwser , it is exactly whats i needed. 


@cedric.boudreau

driftingprogrammer
31 Jan 2020, 12:59

Thanks a lot

Thanks so much for this, this is such an integral need for a robot that it should be built into the API.

 

Jiri said:

Hi, there isn't built-in function called on each bar except from the market series of the chart that you can override. However, you can write your own function for each series.

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class OnBar : Robot
    {
        private MarketSeries seriesMinute1, seriesMinute15, seriesMinute30;
        private int lastIndexMinute1, lastIndexMinute15, lastIndexMinute30;

        protected override void OnStart()
        {
            seriesMinute1 = MarketData.GetSeries(TimeFrame.Minute);
            seriesMinute15 = MarketData.GetSeries(TimeFrame.Minute15);
            seriesMinute30 = MarketData.GetSeries(TimeFrame.Minute15);
        }

        protected override void OnTick()
        {
            int currentIndexMinute1 = seriesMinute1.Open.Count - 1;
            int currentIndexMinute15 = seriesMinute15.Open.Count - 1;
            int currentIndexMinute30 = seriesMinute30.Open.Count - 1;

            if (currentIndexMinute1 > lastIndexMinute1)
            {
                OnBarMinute1(currentIndexMinute1);
                lastIndexMinute1 = currentIndexMinute1;
            }

            if (currentIndexMinute15 > lastIndexMinute15)
            {
                OnBarMinute15(currentIndexMinute15);
                lastIndexMinute15 = currentIndexMinute15;
            }

            if (currentIndexMinute30 > lastIndexMinute30)
            {
                OnBarMinute30(currentIndexMinute30);
                lastIndexMinute30 = currentIndexMinute30;
            }
        }

        private void OnBarMinute1(int index)
        {
            // Called on each new bar of Minute1 dataseries
        }

        private void OnBarMinute15(int index)
        {
            // Called on each new bar of Minute15 dataseries
        }

        private void OnBarMinute30(int index)
        {
            // Called on each new bar of Minute30 dataseries
        }
    }
}

 

 


@driftingprogrammer

bishbashbosh
05 Feb 2020, 11:40

RE: Thanks a lot

driftingprogrammer said:

Thanks so much for this, this is such an integral need for a robot that it should be built into the API.

 

Jiri said:

Hi, there isn't built-in function called on each bar except from the market series of the chart that you can override. However, you can write your own function for each series.

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class OnBar : Robot
    {
        private MarketSeries seriesMinute1, seriesMinute15, seriesMinute30;
        private int lastIndexMinute1, lastIndexMinute15, lastIndexMinute30;

        protected override void OnStart()
        {
            seriesMinute1 = MarketData.GetSeries(TimeFrame.Minute);
            seriesMinute15 = MarketData.GetSeries(TimeFrame.Minute15);
            seriesMinute30 = MarketData.GetSeries(TimeFrame.Minute15);
        }

        protected override void OnTick()
        {
            int currentIndexMinute1 = seriesMinute1.Open.Count - 1;
            int currentIndexMinute15 = seriesMinute15.Open.Count - 1;
            int currentIndexMinute30 = seriesMinute30.Open.Count - 1;

            if (currentIndexMinute1 > lastIndexMinute1)
            {
                OnBarMinute1(currentIndexMinute1);
                lastIndexMinute1 = currentIndexMinute1;
            }

            if (currentIndexMinute15 > lastIndexMinute15)
            {
                OnBarMinute15(currentIndexMinute15);
                lastIndexMinute15 = currentIndexMinute15;
            }

            if (currentIndexMinute30 > lastIndexMinute30)
            {
                OnBarMinute30(currentIndexMinute30);
                lastIndexMinute30 = currentIndexMinute30;
            }
        }

        private void OnBarMinute1(int index)
        {
            // Called on each new bar of Minute1 dataseries
        }

        private void OnBarMinute15(int index)
        {
            // Called on each new bar of Minute15 dataseries
        }

        private void OnBarMinute30(int index)
        {
            // Called on each new bar of Minute30 dataseries
        }
    }
}

 

 

This would be a good use case for Reactive Extensions..

EDIT: btw, good news for you - it IS now part of the API - checkout the new Bars interface, specifically the BarOpened event.


@bishbashbosh