BarOpened event for a tick chart?

Created at 01 Mar 2021, 02:31
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!
MA

maxalta

Joined 01.03.2021

BarOpened event for a tick chart?
01 Mar 2021, 02:31


I'm really a newbie here, so please excuse me if I'm asking for something ridiculous...

How can I trigger if a new candle has started in a TICK chart (e.g in a T50 chart, where each candle has 50 ticks) ?

I see that a BarOpened event exists for time based charts, and I'm wondering if something similar exists for tick based charts...

Thank you very much for any help.


@maxalta
Replies

PanagiotisCharalampous
01 Mar 2021, 10:52

Hi maxalta,

BarOpened should trigger for all types of charts.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

maxalta
01 Mar 2021, 11:18

RE:

Thank you for your help Panagiotis, but really I need an example here... In one of your code suggestion here: 

protected override void OnStart()
{
    var bars = MarketData.GetBars(TimeFrame.Minute, "GBPUSD");
    bars.BarOpened += Bars_BarOpened;
}

private void Bars_BarOpened(BarOpenedEventArgs obj)
{
    Print("New bar created for " + obj.Bars.SymbolName + " at " + obj.Bars.Last(0).OpenTime);
}

I can see that bars are first retrieved passing a TimeFrame.Minute to MarketData.GetBars, but it doesn't accept a NO timeframe param, e.g. T50... How to reach the same thing using ticks?

Please an example would be very appreciated for a newbie like me...


@maxalta

PanagiotisCharalampous
01 Mar 2021, 11:27 ( Updated at: 21 Dec 2023, 09:22 )

Hi maxalta,

This has been added in cTrader v4.0 so you should have it anytime soon if your broker does not have this already.

In the meanwhile, a workaround is to pass the timeframe as a parameter. This way you will be able to choose tick based timeframes from the UI. See below

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter("Timeframe")]
        public TimeFrame Timeframe { get; set; }

        protected override void OnStart()
        {
            var bars = MarketData.GetBars(Timeframe, "GBPUSD");
            bars.BarOpened += Bars_BarOpened;
        }

        private void Bars_BarOpened(BarOpenedEventArgs obj)
        {
            Print("New bar created for " + obj.Bars.SymbolName + " at " + obj.Bars.Last(0).OpenTime);
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

maxalta
01 Mar 2021, 12:12

RE:

Panagiotis your suggestion works like a charm: thank you very much!

Very appreciated!


@maxalta