how to put a multi time frame condition

Created at 23 Sep 2020, 05:03
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!
SA

samuel.jus.cornelio

Joined 19.03.2020

how to put a multi time frame condition
23 Sep 2020, 05:03


Hello, I am trying to put conditions of multiple graphical times in my bot but I am not having success, someone could help me with this.

My idea is to place various business conditions on the indicators at various graphical times. the business being executed only when all these conditions are met at the same time.
Below is an example of an attempt to operate on an hour and a day chart at the same time

 

 

 

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.ESouthAmericaStandardTime, AccessRights = AccessRights.None)]
    public class bot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

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

        [Parameter("Periods", DefaultValue = 14)]
        public int Periods { get; set; }

        [Parameter("Stop Loss (pips)", DefaultValue = 35, MinValue = 10)]
        public double StopLoss { get; set; }

        [Parameter("Volume", DefaultValue = 10000, MinValue = 1000)]
        public int Volume { get; set; }


        [Parameter("Take_Profit", DefaultValue = 75, MinValue = 10)]
        public double TakeProfit { get; set; }







        public RelativeStrengthIndex _rsi;
        public RelativeStrengthIndex _rsi2;
        public RelativeStrengthIndex _rsi3;
        public RelativeStrengthIndex _rsi4;
        public RelativeStrengthIndex _rsi1d;
        public RelativeStrengthIndex _rsi2d;
        public RelativeStrengthIndex _rsi3d;



        private SimpleMovingAverage _sma;
        private SimpleMovingAverage _sma2;









        protected override void OnStart()
        {









        }



        protected override void OnTick()
        {















            MarketData.GetSeries(Symbol, TimeFrame.Daily);
            _rsi1d = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 10);
            _rsi2d = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 5);
            _rsi3d = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 2);




            var day = Server.Time.DayOfWeek;



            _rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 50);
            _rsi2 = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 24);
            _rsi3 = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 12);
            _rsi4 = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 6);


            _sma = Indicators.SimpleMovingAverage(Bars.ClosePrices, 1);
            _sma2 = Indicators.SimpleMovingAverage(Bars.ClosePrices, 4);




            {



                if (_rsi1d.Result.LastValue < 40)
                    if (_rsi2d.Result.LastValue < 40)
                        if (_rsi3d.Result.LastValue < 40)
                            if (_rsi.Result.LastValue < 49)
                                if (_rsi2.Result.LastValue < 49)
                                    if (_rsi3.Result.LastValue < 49)
                                        if (_rsi4.Result.LastValue < 40)




                                            if (Symbol.Ask > _sma2.Result.LastValue)






                                                if (day == DayOfWeek.Monday || day == DayOfWeek.Tuesday || day == DayOfWeek.Wednesday || day == DayOfWeek.Thursday)

                                                    ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "bot", StopLoss, TakeProfit);

            }


            if (_rsi1d.Result.LastValue > 60)
                if (_rsi2d.Result.LastValue > 60)
                    if (_rsi3d.Result.LastValue > 60)


                        if (_rsi.Result.LastValue < 51)
                            if (_rsi.Result.LastValue > 51)

                                if (_rsi2.Result.LastValue > 51)
                                    if (_rsi3.Result.LastValue > 51)
                                        if (_rsi4.Result.LastValue > 60)

                                            if (Symbol.Ask < _sma2.Result.LastValue)



                                                if (day == DayOfWeek.Monday || day == DayOfWeek.Tuesday || day == DayOfWeek.Wednesday || day == DayOfWeek.Thursday)



                                                    ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "bot", StopLoss, TakeProfit);










        }
    }
}
 


@samuel.jus.cornelio
Replies

samuel.jus.cornelio
23 Sep 2020, 17:13

RE:

samuel.jus.cornelio said:

Hello, I am trying to put conditions of multiple graphical times in my bot but I am not having success, someone could help me with this.

My idea is to place various business conditions on the indicators at various graphical times. the business being executed only when all these conditions are met at the same time.
Below is an example of an attempt to operate on an hour and a day chart at the same time

 

 

 

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.ESouthAmericaStandardTime, AccessRights = AccessRights.None)]
    public class bot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

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

        [Parameter("Periods", DefaultValue = 14)]
        public int Periods { get; set; }

        [Parameter("Stop Loss (pips)", DefaultValue = 35, MinValue = 10)]
        public double StopLoss { get; set; }

        [Parameter("Volume", DefaultValue = 10000, MinValue = 1000)]
        public int Volume { get; set; }


        [Parameter("Take_Profit", DefaultValue = 75, MinValue = 10)]
        public double TakeProfit { get; set; }







        public RelativeStrengthIndex _rsi;
        public RelativeStrengthIndex _rsi2;
        public RelativeStrengthIndex _rsi3;
        public RelativeStrengthIndex _rsi4;
        public RelativeStrengthIndex _rsi1d;
        public RelativeStrengthIndex _rsi2d;
        public RelativeStrengthIndex _rsi3d;



        private SimpleMovingAverage _sma;
        private SimpleMovingAverage _sma2;









        protected override void OnStart()
        {









        }



        protected override void OnTick()
        {















            MarketData.GetSeries(Symbol, TimeFrame.Daily);
            _rsi1d = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 10);
            _rsi2d = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 5);
            _rsi3d = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 2);




            var day = Server.Time.DayOfWeek;



            _rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 50);
            _rsi2 = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 24);
            _rsi3 = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 12);
            _rsi4 = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 6);


            _sma = Indicators.SimpleMovingAverage(Bars.ClosePrices, 1);
            _sma2 = Indicators.SimpleMovingAverage(Bars.ClosePrices, 4);




            {



                if (_rsi1d.Result.LastValue < 40)
                    if (_rsi2d.Result.LastValue < 40)
                        if (_rsi3d.Result.LastValue < 40)
                            if (_rsi.Result.LastValue < 49)
                                if (_rsi2.Result.LastValue < 49)
                                    if (_rsi3.Result.LastValue < 49)
                                        if (_rsi4.Result.LastValue < 40)




                                            if (Symbol.Ask > _sma2.Result.LastValue)






                                                if (day == DayOfWeek.Monday || day == DayOfWeek.Tuesday || day == DayOfWeek.Wednesday || day == DayOfWeek.Thursday)

                                                    ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "bot", StopLoss, TakeProfit);

            }


            if (_rsi1d.Result.LastValue > 60)
                if (_rsi2d.Result.LastValue > 60)
                    if (_rsi3d.Result.LastValue > 60)


                        if (_rsi.Result.LastValue < 51)
                            if (_rsi.Result.LastValue > 51)

                                if (_rsi2.Result.LastValue > 51)
                                    if (_rsi3.Result.LastValue > 51)
                                        if (_rsi4.Result.LastValue > 60)

                                            if (Symbol.Ask < _sma2.Result.LastValue)



                                                if (day == DayOfWeek.Monday || day == DayOfWeek.Tuesday || day == DayOfWeek.Wednesday || day == DayOfWeek.Thursday)



                                                    ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "bot", StopLoss, TakeProfit);










        }
    }
}
 

 

 

I tried this command to put the conditions of the daily chart in the 1 hour bot, but I was not successful

 

            var _rsi1dseries = MarketData.GetSeries(Symbol, TimeFrame.Daily);
            var _rsi2dseries = MarketData.GetSeries(Symbol, TimeFrame.Daily);
            var _rsi3dseries = MarketData.GetSeries(Symbol, TimeFrame.Daily);

            _rsi1d = Indicators.RelativeStrengthIndex(_rsi1dseries.ClosePrices, 10);
            _rsi2d = Indicators.RelativeStrengthIndex(_rsi2dseries.ClosePrices, 5);
            _rsi3d = Indicators.RelativeStrengthIndex(_rsi3dseries.ClosePrices, 2);


@samuel.jus.cornelio

genappsforex
19 Oct 2020, 20:39

RE: RE:

looks like you're an MT4/5 programmer.
You'd be better of taking a good look at the cTrader samples and structure. Only then you might beable to write a reliable EA in cTrader.

 


@genappsforex

prosteel1
23 Oct 2020, 18:27

While I don't do indicators, I run the same code on multiple timeframes using an array of integers Frames[], I iterate through that array to have multiple timeframes. And I repeatedly run "Bars series = MarketData.GetBars(Frames[a]);" to get the latest data in that timeframe - mostly to check if there is a new bar or a new high or low. 

Once the bot gets past the On Start() method the 0 bar being the most distant bar is always the same - this becomes the datum on that timeframe and is the same always (unless the bot is running in realtime and you zoom out or scroll back).

When I started writing my code I named the timeframes using different names like Bars series0 = MarketData.GetBars(Frames[0]), Bars series1 = MarketData.GetBars(Frames[1]), Bars series2 = MarketData.GetBars(Frames[2]), but then needed multiple copies of my code for series0, series1, series2 etc. My code got a bit long and I couldn't figure out how to index the series0 as series[0] (although that almost makes sense atm lol). My solution isn't great but it works for me so far.

I use the Frames integer as an index to update the values on various timeframes. Hope this helps :)

Fell free to ask more questions :)

You code might be better, but just for funzies Day of week can also be an int like this: 

int day = (int)Server.Time.DayOfWeek;

Same thing your doing I think just using the int of the day of week rather than the name.

 

 

 


@prosteel1

prosteel1
26 Oct 2020, 16:38

@samuel.jus.cornelio how did you go? Please ask any more questions you need answered :)

I do have an update to post to my linked post which may be of help but might take a few days. I do like Multi timeframe so feel free to ask :)

 


@prosteel1