Basic question

Created at 07 Apr 2021, 12:55
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!
AN

animate44x

Joined 06.04.2021

Basic question
07 Apr 2021, 12:55


I understand that the "OnBar" method executes on each bar close. I could not find which bar close it refers to.

How do I go about specifying that I want it to be called on every "m1" or "h1" bar close. Can I use multiple "OnBar" methods in the same robot and have them be called on different timeframe candles.


@animate44x
Replies

amusleh
07 Apr 2021, 13:17

Hi,

OnBar/Tick methods are called based on current chart symbol and time frame, if you want to receive on bar/tick events of other timeframes or symbols yopu have to first get that symbol/timeframe Bars object by calling the MarketData.GetBars method and passing the time frame and symbol name.

After you got the Bars then you can subscribe to its BarOpened and Tick events:

using cAlgo.API;
using System;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class BarsSample : Indicator
    {
        private TextBlock _barTicksNumberTextBlock, _barsStateTextBlock;

        [Output("Range", LineColor = "RoyalBlue")]
        public IndicatorDataSeries Range { get; set; }

        [Output("Body", LineColor = "Yellow")]
        public IndicatorDataSeries Body { get; set; }

        protected override void Initialize()
        {
            // Bars events
            Bars.BarOpened += Bars_BarOpened;

            Bars.Tick += Bars_Tick;

            Bars.HistoryLoaded += Bars_HistoryLoaded;

            Bars.Reloaded += Bars_Reloaded;

            var grid = new Grid(2, 2) 
            {
                BackgroundColor = Color.DarkGoldenrod,
                HorizontalAlignment = HorizontalAlignment.Right,
                VerticalAlignment = VerticalAlignment.Top,
                Opacity = 0.5
            };

            grid.AddChild(new TextBlock 
            {
                Text = "Bar Ticks #",
                Margin = 5
            }, 0, 0);

            _barTicksNumberTextBlock = new TextBlock 
            {
                Text = "0",
                Margin = 5
            };

            grid.AddChild(_barTicksNumberTextBlock, 0, 1);

            grid.AddChild(new TextBlock 
            {
                Text = "Bars State",
                Margin = 5
            }, 1, 0);

            _barsStateTextBlock = new TextBlock 
            {
                Margin = 5
            };

            grid.AddChild(_barsStateTextBlock, 1, 1);

            IndicatorArea.AddControl(grid);
        }

        private void Bars_Reloaded(BarsHistoryLoadedEventArgs obj)
        {
            _barsStateTextBlock.Text = "Reloaded";
        }

        private void Bars_HistoryLoaded(BarsHistoryLoadedEventArgs obj)
        {
            _barsStateTextBlock.Text = "History Loaded";
        }

        private void Bars_Tick(BarsTickEventArgs obj)
        {
            _barTicksNumberTextBlock.Text = Bars.TickVolumes.LastValue.ToString();
        }

        private void Bars_BarOpened(BarOpenedEventArgs obj)
        {
            _barsStateTextBlock.Text = "New Bar Opened";
        }

        public override void Calculate(int index)
        {
            Range[index] = Bars.HighPrices[index] - Bars.LowPrices[index];
            Body[index] = Math.Abs(Bars.ClosePrices[index] - Bars.OpenPrices[index]);
        }
    }
}

 

Getting Bars of another timeframe/symbol:

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MarketDataSample : Indicator
    {
        private Bars _bars;

        private Ticks _ticks;

        private MarketDepth _marketDepth;

        [Parameter("Use Current Symbol", DefaultValue = true)]
        public bool UseCurrentSymbol { get; set; }

        [Parameter("Other Symbol Name", DefaultValue = "GBPUSD")]
        public string OtherSymbolName { get; set; }

        [Parameter("Use Current TimeFrame", DefaultValue = true)]
        public bool UseCurrentTimeFrame { get; set; }

        [Parameter("Other TimeFrame", DefaultValue = "Daily")]
        public TimeFrame OtherTimeFrame { get; set; }

        protected override void Initialize()
        {
            var symbol = UseCurrentSymbol ? Symbol : Symbols.GetSymbol(OtherSymbolName);
            var timeframe = UseCurrentTimeFrame ? TimeFrame : OtherTimeFrame;

            // You can use GetBarsAsync instead of GetBars
            _bars = MarketData.GetBars(timeframe, symbol.Name);
            // You can use GetTicksAsync instead of GetTicks
            _ticks = MarketData.GetTicks(symbol.Name);

            _marketDepth = MarketData.GetMarketDepth(symbol.Name);
        }

        public override void Calculate(int index)
        {
        }
    }
}

 


@amusleh