Can't get Multi Timeframe/range to work in Indicator

Created at 01 Feb 2024, 05:13
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!
EM

emeeder1

Joined 04.02.2016

Can't get Multi Timeframe/range to work in Indicator
01 Feb 2024, 05:13


I have a version of the RMO indicator that I'm trying to turn into multi timeframe.  technically a multi range frame. id like to run my chart on R3 and then have timframe showing R10 or R20.  I realize it will look a bit choppy unless it runs at realtime.  But right now its not looking correct at all.

I'm not sure what i have wrong.

Does anyone have any suggestion?

Much appreciated

 

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Levels(-7,7)]
    [Indicator(AccessRights = AccessRights.None)]
    public class RMOMultiTF:Indicator
    {
        
        private SimpleMovingAverage _sma;
        private SimpleMovingAverage _sma2;
        private SimpleMovingAverage _sma3;
        private SimpleMovingAverage _sma4;
        private SimpleMovingAverage _sma5;
        private SimpleMovingAverage _sma6;
        private SimpleMovingAverage _sma7;
        private SimpleMovingAverage _sma8;
        private SimpleMovingAverage _sma9;
        private SimpleMovingAverage _sma10;
        private ExponentialMovingAverage _ema1;
        private ExponentialMovingAverage _ema2;
        private ExponentialMovingAverage _ema3;
        private ExponentialMovingAverage _ema4;
        private ExponentialMovingAverage _ema5;
        private ExponentialMovingAverage _ema6;
        
        private SimpleMovingAverage _smaTF1;
        private SimpleMovingAverage _sma2TF1;
        private SimpleMovingAverage _sma3TF1;
        private SimpleMovingAverage _sma4TF1;
        private SimpleMovingAverage _sma5TF1;
        private SimpleMovingAverage _sma6TF1;
        private SimpleMovingAverage _sma7TF1;
        private SimpleMovingAverage _sma8TF1;
        private SimpleMovingAverage _sma9TF1;
        private SimpleMovingAverage _sma10TF1;
        private ExponentialMovingAverage _ema1TF1;
        private ExponentialMovingAverage _ema2TF1;
        private ExponentialMovingAverage _ema3TF1;
        private ExponentialMovingAverage _ema4TF1;
        private ExponentialMovingAverage _ema5TF1;
        private ExponentialMovingAverage _ema6TF1;

        private IndicatorDataSeries iSeries1;
        private IndicatorDataSeries iSeries4;
        private IndicatorDataSeries iSeries1TF1;
        private IndicatorDataSeries iSeries4TF1;
       
        
        [Output("ST2", LineColor = "Red")]
        public IndicatorDataSeries ST2 { get; set; }

        [Output("ST3", LineColor = "Green")]
        public IndicatorDataSeries ST3 { get; set; }
   
        [Output("ST4", LineColor = "Orange")]
        public IndicatorDataSeries ST4 { get; set; }   
        
        [Output("ST2TF1", LineColor = "Red")]
        public IndicatorDataSeries ST2TF1 { get; set; }

        [Output("ST3TF1", LineColor = "Green")]
        public IndicatorDataSeries ST3TF1 { get; set; }
   
        [Output("ST4TF1", LineColor = "Orange")]
        public IndicatorDataSeries ST4TF1 { get; set; }           
        
        
        [Parameter(DefaultValue = 2)]
        public int Len1 { get; set; }

        [Parameter(DefaultValue = 10)]
        public int Len2 { get; set; }
        
        [Parameter(DefaultValue = 30)]
        public int Len3 { get; set; }
        
        [Parameter(DefaultValue = 81)]
        public int Len4 { get; set; }
        
        [Parameter(DefaultValue = 8)]
        public int LineLevel { get; set; }    
        
        [Parameter("Use TF1", DefaultValue = true)]
        public bool UseTF1 { get; set; }        

        [Parameter("Timeframe1", DefaultValue = "Range20")]
        public TimeFrame Timeframe1 { get; set; }


        
        [Output("Bearish", LineColor = "Red", PlotType = PlotType.Histogram)]
        public IndicatorDataSeries BearBuffer { get; set; }
        [Output("Bullish", LineColor = "Green", PlotType = PlotType.Histogram)]
        public IndicatorDataSeries BullBuffer { get; set; }
        [Output("Neutral", LineColor = "Gray", PlotType = PlotType.Histogram)]
        public IndicatorDataSeries NeutralBuffer { get; set; }
        
        [Output("BearishTF1", LineColor = "Red", PlotType = PlotType.Histogram)]
        public IndicatorDataSeries BearBufferTF1 { get; set; }
        [Output("BullishTF1", LineColor = "Green", PlotType = PlotType.Histogram)]
        public IndicatorDataSeries BullBufferTF1 { get; set; }
        [Output("NeutralTF1", LineColor = "Gray", PlotType = PlotType.Histogram)]
        public IndicatorDataSeries NeutralBufferTF1 { get; set; }
        
      


        private Bars barsTF1;



        protected override void Initialize()
        {
        
            
            // Chart TF
        
            iSeries1 = CreateDataSeries();            
            iSeries4 = CreateDataSeries();


            _sma = Indicators.SimpleMovingAverage(Bars.ClosePrices, Len1);
            _sma2 = Indicators.SimpleMovingAverage(_sma.Result, Len1);
            _sma3 = Indicators.SimpleMovingAverage(_sma2.Result, Len1);
            _sma4 = Indicators.SimpleMovingAverage(_sma3.Result, Len1);
            _sma5 = Indicators.SimpleMovingAverage(_sma4.Result, Len1);
            _sma6 = Indicators.SimpleMovingAverage(_sma5.Result, Len1);
            _sma7 = Indicators.SimpleMovingAverage(_sma6.Result, Len1);
            _sma8 = Indicators.SimpleMovingAverage(_sma7.Result, Len1);
            _sma9 = Indicators.SimpleMovingAverage(_sma8.Result, Len1);
            _sma10 = Indicators.SimpleMovingAverage(_sma9.Result, Len1);

            _ema1 = Indicators.ExponentialMovingAverage(iSeries1, Len3);
            _ema2 = Indicators.ExponentialMovingAverage(_ema1.Result, Len3);

            _ema3 = Indicators.ExponentialMovingAverage(ST2, Len3);
            _ema4 = Indicators.ExponentialMovingAverage(_ema3.Result, Len3);
            
            _ema5 = Indicators.ExponentialMovingAverage(iSeries1, Len4);
            _ema6 = Indicators.ExponentialMovingAverage(_ema3.Result, Len4);
            
            
            // TF1
            
            barsTF1 = MarketData.GetBars(Timeframe1);
            
            iSeries1TF1 = CreateDataSeries();            
            iSeries4TF1 = CreateDataSeries();


            _smaTF1 = Indicators.SimpleMovingAverage(barsTF1.ClosePrices, Len1);
            _sma2TF1 = Indicators.SimpleMovingAverage(_smaTF1.Result, Len1);
            _sma3TF1 = Indicators.SimpleMovingAverage(_sma2TF1.Result, Len1);
            _sma4TF1 = Indicators.SimpleMovingAverage(_sma3TF1.Result, Len1);
            _sma5TF1 = Indicators.SimpleMovingAverage(_sma4TF1.Result, Len1);
            _sma6TF1 = Indicators.SimpleMovingAverage(_sma5TF1.Result, Len1);
            _sma7TF1 = Indicators.SimpleMovingAverage(_sma6TF1.Result, Len1);
            _sma8TF1 = Indicators.SimpleMovingAverage(_sma7TF1.Result, Len1);
            _sma9TF1 = Indicators.SimpleMovingAverage(_sma8TF1.Result, Len1);
            _sma10TF1 = Indicators.SimpleMovingAverage(_sma9TF1.Result, Len1);

            _ema1TF1 = Indicators.ExponentialMovingAverage(iSeries1TF1, Len3);
            _ema2TF1 = Indicators.ExponentialMovingAverage(_ema1TF1.Result, Len3);

            _ema3TF1 = Indicators.ExponentialMovingAverage(ST2TF1, Len3);
            _ema4TF1 = Indicators.ExponentialMovingAverage(_ema3TF1.Result, Len3);
            
            _ema5TF1 = Indicators.ExponentialMovingAverage(iSeries1TF1, Len4);
            _ema6TF1 = Indicators.ExponentialMovingAverage(_ema3TF1.Result, Len4);            
            
            
            
         

        }

        public override void Calculate(int index)
        {
           if (index < 4)
                return;

            double fix = Bars.HighPrices.Maximum(Len2 - 1) - Bars.LowPrices.Minimum(Len2 - 1);

            if (Math.Abs(fix - 0) < double.Epsilon)
                fix = 1;

            iSeries1[index] = 100*(Bars.ClosePrices[index] -
                                   (_sma.Result[index] + _sma2.Result[index] + _sma3.Result[index]
                                    + _sma4.Result[index] + _sma5.Result[index] + _sma6.Result[index]
                                    + _sma7.Result[index] + _sma8.Result[index] + _sma9.Result[index]
                                    + _sma10.Result[index])/10)/fix;
            
            ST2[index] = 2*_ema1.Result[index] - _ema2.Result[index];

            ST3[index] = 2*_ema3.Result[index] - _ema4.Result[index];

            iSeries4[index] = 2*_ema5.Result[index] - _ema6.Result[index];
            
            ST4[index] = ((ST3[index-1]-ST2[index-1]) - (ST3[index]-ST2[index]))*1;
            
           
            
            if (ST4[index] > 0)
                ST4[index] = ST4[index] + 7;
            if (ST4[index] < 0 )
               ST4[index] = ST4[index] -7;  
            
  

            if (iSeries4[index] > 0 && ST2[index] > 0 && ST3[index] > 0)
                BullBuffer[index] = iSeries4[index];
            else if (iSeries4[index] < 0 && ST2[index] < 0 && ST3[index] < 0)
                BearBuffer[index] = iSeries4[index];
            else
                NeutralBuffer[index] = iSeries4[index];
                
           // TF1
           if (UseTF1)
            {
            var indexTF1 = barsTF1.OpenTimes.GetIndexByTime(Bars.OpenTimes[index]); 
            


            double fixTF1 = barsTF1.HighPrices.Maximum(Len2 - 1) - barsTF1.LowPrices.Minimum(Len2 - 1);

            if (Math.Abs(fixTF1 - 0) < double.Epsilon)
                fixTF1 = 1;

            iSeries1TF1[index] = 100*(barsTF1.ClosePrices[indexTF1] -
                                   (_smaTF1.Result[indexTF1] + _sma2TF1.Result[indexTF1] + _sma3TF1.Result[indexTF1]
                                    + _sma4TF1.Result[indexTF1] + _sma5TF1.Result[indexTF1] + _sma6TF1.Result[indexTF1]
                                    + _sma7TF1.Result[indexTF1] + _sma8TF1.Result[indexTF1] + _sma9TF1.Result[indexTF1]
                                    + _sma10TF1.Result[indexTF1])/10)/fixTF1;
            
            ST2TF1[index] = 2*_ema1TF1.Result[indexTF1] - _ema2TF1.Result[indexTF1];

            ST3TF1[index] = 2*_ema3TF1.Result[indexTF1] - _ema4TF1.Result[indexTF1];

            iSeries4TF1[index] = 2*_ema5TF1.Result[indexTF1] - _ema6TF1.Result[indexTF1];
            
            ST4TF1[index] = ((ST3TF1[index-1]-ST2TF1[index-1]) - (ST3TF1[index]-ST2TF1[index]))*1;
            
           
            
            if (ST4TF1[index] > 0)
                ST4TF1[index] = ST4TF1[index] + 7;
            if (ST4TF1[index] < 0 )
               ST4TF1[index] = ST4TF1[index] -7;  
            
  

            if (iSeries4TF1[index] > 0 && ST2TF1[index] > 0 && ST3TF1[index] > 0)
                BullBufferTF1[index] = iSeries4TF1[index];
            else if (iSeries4TF1[index] < 0 && ST2TF1[index] < 0 && ST3TF1[index] < 0)
                BearBufferTF1[index] = iSeries4TF1[index];
            else
                NeutralBufferTF1[index] = iSeries4TF1[index];            
            }

        }

    }
}

 

 


@emeeder1