An issue with multi timeframe code

Created at 22 Oct 2024, 06:06
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

samyelzallat

Joined 03.10.2024

An issue with multi timeframe code
22 Oct 2024, 06:06


I was trying to access adx stock indicator in another time frame other than the one Iam running my cbot on , it didn't seem to work as it did with the moving average indicator.
So I made an other adx that works just fine, but when I try to reference it in my cbot to use it, it keeps giving 0 as its value, when I initialize the indicator without trying to access an other timeframe it also works okay.

Here are the snippets from the Cbot


      //adx
       [Parameter("Period", DefaultValue = 1, MinValue = 1)]
        public int AdxPeriod { get; set; }

        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType AdxMaType { get; set; }

      
      AdxS _Adx;
      
       protected override void OnStart() {
      
        var WeeklyTf = MarketData.GetBars(TimeFrame.Weekly);

     
         _Adx = Indicators.GetIndicator < AdxS > (WeeklyTf, AdxPeriod , AdxMaType);

and this is the indicator

 

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

namespace cAlgo
{
    [Indicator("Custom ADX", IsOverlay = false, AccessRights = AccessRights.None)]
    public class AdxS : Indicator
    {
        [Parameter("Period", DefaultValue = 14, MinValue = 1)]
        public int Period { get; set; }

        [Parameter("MA Type", DefaultValue = MovingAverageType.WilderSmoothing)]
        public MovingAverageType MaType { get; set; }

        [Output("ADX", Color = Colors.Cyan)]
        public IndicatorDataSeries ADX { get; set; }

        [Output("DI+", Color = Colors.Green)]
        public IndicatorDataSeries DiPlus { get; set; }

        [Output("DI-", Color = Colors.Red)]
        public IndicatorDataSeries DiMinus { get; set; }

        private DataSeries high, low, close;
        private IndicatorDataSeries tr, dmPlus, dmMinus;
        private MovingAverage maTr, maDmPlus, maDmMinus;

        protected override void Initialize()
        {
            high = Bars.HighPrices;
            low = Bars.LowPrices;
            close = Bars.ClosePrices;

            tr = CreateDataSeries();
            dmPlus = CreateDataSeries();
            dmMinus = CreateDataSeries();

            maTr = Indicators.MovingAverage(tr, Period, MaType);
            maDmPlus = Indicators.MovingAverage(dmPlus, Period, MaType);
            maDmMinus = Indicators.MovingAverage(dmMinus, Period, MaType);
        }

        public override void Calculate(int index)
        {
            if (index == 0)
            {
                tr[0] = high[0] - low[0];
                dmPlus[0] = 0;
                dmMinus[0] = 0;
            }
            else
            {
                tr[index] = Math.Max(Math.Max(high[index] - low[index], 
                                    Math.Abs(high[index] - close[index - 1])), 
                                    Math.Abs(low[index] - close[index - 1]));
                
                dmPlus[index] = (high[index] - high[index - 1] > low[index - 1] - low[index]) 
                    ? Math.Max(high[index] - high[index - 1], 0) 
                    : 0;

                dmMinus[index] = (low[index - 1] - low[index] > high[index] - high[index - 1]) 
                    ? Math.Max(low[index - 1] - low[index], 0) 
                    : 0;

                DiPlus[index] = (maTr.Result[index] == 0) ? 0 : (100 * maDmPlus.Result[index] / maTr.Result[index]);
                DiMinus[index] = (maTr.Result[index] == 0) ? 0 : (100 * maDmMinus.Result[index] / maTr.Result[index]);

                if (DiPlus[index] + DiMinus[index] != 0)
                {
                    ADX[index] = 100 * Math.Abs((DiPlus[index] - DiMinus[index]) / 
                        (DiPlus[index] + DiMinus[index]));
                }
                else
                {
                    ADX[index] = 0;
                }
            }
        }
    }
}

@samyelzallat
Replies

PanagiotisCharalampous
22 Oct 2024, 06:31

Hi there,

Your indicator only declares two parameters while you are passing three. The code does not make much sense.

Best regards,

Panagiotis


@PanagiotisCharalampous

samyelzallat
22 Oct 2024, 07:06

RE: An issue with multi timeframe code

PanagiotisCharalampous said: 

Hi there,

Your indicator only declares two parameters while you are passing three. The code does not make much sense.

Best regards,

Panagiotis

Hey Panagiotis,
But isn't this how it works?

How Do I declare a custom indicator to get data from another tf?


@samyelzallat