Multi Time Frame OnBar Events

Created at 16 Jun 2020, 13: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!
TA

TakeProfit

Joined 15.08.2017

Multi Time Frame OnBar Events
16 Jun 2020, 13:07


I need to access several TimeFrames for when a new Bar opens for example in 1h, 30m and 15m Chart.

Since there was only one dedicated protected override OnBar() Event in the API, I went the with the follow workaround.

protected override void OnTick()
        {
            int currentIndex_m15Timeframe = m15Timeframe.OpenPrices.Count - 1;
            if (currentIndex_m15Timeframe > lastIndex_m15Timeframe)
            {
                OnBar_m15Timeframe(currentIndex_m15Timeframe);
                lastIndex_m15Timeframe = currentIndex_m15Timeframe;
            }

            int currentIndex_m30Timeframe = m30Timeframe.OpenPrices.Count - 1;
            if (currentIndex_m30Timeframe > lastIndex_m30Timeframe)
            {
                OnBar_m30Timeframe(currentIndex_m30Timeframe);
                lastIndex_m30Timeframe = currentIndex_m30Timeframe;
            }

            // And so on
        }

Since API 3.7 we can subscribe to Events with Bars.

I was wondering if it is possible to do something like this:

private void OnBar1h(BarOpenedEventArgs.Hour1 args)
{
            
}

private void OnBar2h(BarOpenedEventArgs.Hour2 args)
{

}

// And so on

 


@TakeProfit
Replies

PanagiotisCharalampous
16 Jun 2020, 14:13

Hi TakeProfit.

See an example below on how you can subscribe to ticks and bar changes from Bar collections coming from other symbols and timeframes

using System;
using System.Linq;
using System.Text;
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 MultisymbolBacktesting : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        public Symbol[] MySymbols;

        protected override void OnStart()
        {
            // We get a symbol array for the following for symbols
            MySymbols = Symbols.GetSymbols("EURUSD", "GBPUSD", "USDJPY", "USDCHF");
            // We subscribe to the tick event for each symbol
            foreach (var symbol in MySymbols)
            {
                symbol.Tick += Symbol_Tick;
            }
            // We subscribe to the bar event for each symbol for the current timeframe
            foreach (var symbol in MySymbols)
            {
                var bars = MarketData.GetBars(TimeFrame, symbol.Name);
                bars.BarOpened += Bars_BarOpened;
            }
        }

        private void Bars_BarOpened(BarOpenedEventArgs obj)
        {
            // When a bar opens we check the previous bar. If the bar is bullish, we send a buy order, else we send a sell order.
            if (obj.Bars.Last(1).Close > obj.Bars.Last(1).Open)
            {
                ExecuteMarketOrder(TradeType.Buy, obj.Bars.SymbolName, 1000, "", 2, 2);
            }
            else
            {
                ExecuteMarketOrder(TradeType.Sell, obj.Bars.SymbolName, 1000, "", 2, 2);
            }
        }

        private void Symbol_Tick(SymbolTickEventArgs obj)
        {
            // On each symbol tick, we print the symbol's bid/ask prices on the chart
            var sb = new StringBuilder();
            foreach (var symbol in MySymbols)
            {
                var textLine = string.Format("{0} {1} {2}", symbol.Name, symbol.Bid, symbol.Ask);
                sb.AppendLine(textLine);
            }
            Chart.DrawStaticText("symbols", sb.ToString(), VerticalAlignment.Top, HorizontalAlignment.Left, Chart.ColorSettings.ForegroundColor);
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

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

 

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

TakeProfit
16 Jun 2020, 16:28

RE:

PanagiotisCharalampous said:

Hi TakeProfit.

See an example below on how you can subscribe to ticks and bar changes from Bar collections coming from other symbols and timeframes

using System;
using System.Linq;
using System.Text;
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 MultisymbolBacktesting : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        public Symbol[] MySymbols;

        protected override void OnStart()
        {
            // We get a symbol array for the following for symbols
            MySymbols = Symbols.GetSymbols("EURUSD", "GBPUSD", "USDJPY", "USDCHF");
            // We subscribe to the tick event for each symbol
            foreach (var symbol in MySymbols)
            {
                symbol.Tick += Symbol_Tick;
            }
            // We subscribe to the bar event for each symbol for the current timeframe
            foreach (var symbol in MySymbols)
            {
                var bars = MarketData.GetBars(TimeFrame, symbol.Name);
                bars.BarOpened += Bars_BarOpened;
            }
        }

        private void Bars_BarOpened(BarOpenedEventArgs obj)
        {
            // When a bar opens we check the previous bar. If the bar is bullish, we send a buy order, else we send a sell order.
            if (obj.Bars.Last(1).Close > obj.Bars.Last(1).Open)
            {
                ExecuteMarketOrder(TradeType.Buy, obj.Bars.SymbolName, 1000, "", 2, 2);
            }
            else
            {
                ExecuteMarketOrder(TradeType.Sell, obj.Bars.SymbolName, 1000, "", 2, 2);
            }
        }

        private void Symbol_Tick(SymbolTickEventArgs obj)
        {
            // On each symbol tick, we print the symbol's bid/ask prices on the chart
            var sb = new StringBuilder();
            foreach (var symbol in MySymbols)
            {
                var textLine = string.Format("{0} {1} {2}", symbol.Name, symbol.Bid, symbol.Ask);
                sb.AppendLine(textLine);
            }
            Chart.DrawStaticText("symbols", sb.ToString(), VerticalAlignment.Top, HorizontalAlignment.Left, Chart.ColorSettings.ForegroundColor);
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

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

 

Best Regards,

Panagiotis 

Join us on Telegram

 

Hi Panagiotis,

thank you for your quick reply!

 

The example helped a lot.

I just implemented and tested it and it works exactly as I wanted it to!

 

Best regards


@TakeProfit

cW22Trader
29 Jul 2021, 22:17 ( Updated at: 29 Jul 2021, 22:18 )

Could you also provide a simple example with an indicator used for multiple symbols at the same time?

Is it still initialized in OnStart? Do I need one per symbol or is this handled automatically?

Kind regards


@cW22Trader

amusleh
30 Jul 2021, 09:40

RE:

cW22Trader said:

Could you also provide a simple example with an indicator used for multiple symbols at the same time?

Is it still initialized in OnStart? Do I need one per symbol or is this handled automatically?

Kind regards

Please check the example on Market Data API references.


@amusleh

cW22Trader
03 Aug 2021, 00:27

Thanks for the answer.

Unfortunately, this example does not use any indicators it is code for an indicator. But how does it work in a bot using an indicator (e.g. an EMA)?


@cW22Trader