Multi time frame cBot

Created at 09 Jul 2020, 04: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!
VI

victor.major

Joined 20.11.2019

Multi time frame cBot
09 Jul 2020, 04:31


I am writing a cBot with two indicators, Stochastic and RSI.

I want them to run at separate time frames, for example RSI at h1, Stochastic at m30. How do I do this with the current API version? I found some examples from last year, but those functions now show as obsolete and I have been unable to achieve this using the current API version.

Thank you :)

 

V.

 


@victor.major
Replies

PanagiotisCharalampous
09 Jul 2020, 08:35

Hi Victor,

Here is an example

 var rsi = Indicators.RelativeStrengthIndex(MarketData.GetBars(TimeFrame.Hour).ClosePrices, 14);

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

victor.major
09 Jul 2020, 10:07

RE:

Thank you Panagiotis.

Sorry for the clearly basic following question, but I learn by doing...

How do I change 

(TimeFrame.Hour).ClosePrices

Into an expression that allows me to declare a user selectable  time frame and source from the cBot settings for example these:

[Parameter("Timeframe", Group = "RSI", DefaultValue = "Minute10")]
        public TimeFrame RSITimeFrame { get; set; }

[Parameter("Source", Group = "RSI")]
        public DataSeries Source { get; set; }

 

Thanks,

 

Victor

 

PanagiotisCharalampous said:

Hi Victor,

Here is an example

 var rsi = Indicators.RelativeStrengthIndex(MarketData.GetBars(TimeFrame.Hour).ClosePrices, 14);

Best Regards,

Panagiotis 

Join us on Telegram

 

 


@victor.major

PanagiotisCharalampous
09 Jul 2020, 10:15

Hi Victor,

it is not so straight forward for the Source part. The current parameter will not work. You will need to declare your own enum 

        public enum SourceType
        {
            Open,
            High,
            Low,
            Close
        }

use it as a variable type

        [Parameter("Source")]
        public SourceType Source { get; set; }

and then have a method to return the appropriate source from the bars object


        private DataSeries GetSeries(Bars bars, SourceType type)
        {
            // Getting the series based on the source type
            switch (type)
            {
                case SourceType.Open:
                    return bars.OpenPrices;

                case SourceType.High:
                    return bars.HighPrices;

                case SourceType.Low:
                    return bars.LowPrices;

                case SourceType.Close:
                    return bars.ClosePrices;
                default:
                    return bars.ClosePrices;
            }
        }

You can then use it for your indicator as below

var rsi = Indicators.RelativeStrengthIndex(GetSeries(MarketData.GetBars(RSITimeFrame), Source), 14);

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

victor.major
15 Jul 2020, 09:32

RE:

Thank you for this. It works very well and was easy to follow and implement. In fact I did it immediately and it worked, but did not want to create noise by replying :) Then I felt bad for not thinking you, so thank you.

 

V.

 


@victor.major