Multi Time Frame OnBar Events

TakeProfit since: 15 Aug 2017;

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

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
PanagiotisCharalampous's avatar

PanagiotisCharalampous since: 13 Jan 2017;

  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

Head of Community Management at cTrader

TakeProfit since: 15 Aug 2017;

  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

cW22Trader since: 16 Mar 2021;

  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

amusleh's avatar

amusleh since: 01 Mar 2021;

  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.

Community Developer | Spotware | Telegram: @algodeveloper

cW22Trader since: 16 Mar 2021;

  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)?