Indicator Data does not seem to load

Created at 24 Aug 2020, 00:24
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

samuelbreezey

Joined 10.04.2019

Indicator Data does not seem to load
24 Aug 2020, 00:24


Hi guys

This may be simple but the following indicator when loaded does not seem to load properly. Is there anything in the coding which would effect this?

Code  below:

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

// (C) 2014 marekfx

//ICE USDX contract - https://www.theice.com/publicdocs/futures_us/USDX_Futures_Contract.pdf
//ICE EURX contract - https://www.theice.com/publicdocs/rulebooks/futures_us/24_ICE_Futures_EURO_Index.pdf

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AutoRescale = true, AccessRights = AccessRights.None)]
    public class USDXDollarIndex : Indicator
    {
        [Parameter("Show USDX", DefaultValue = true)]
        public bool ShowUSDX { get; set; }

        [Parameter("Show EURX", DefaultValue = false)]
        public bool ShowEURX { get; set; }

        [Output("USDX")]
        public IndicatorDataSeries USDX { get; set; }

        [Output("EURX")]
        public IndicatorDataSeries EURX { get; set; }

        [Output("MA", LineColor = "Blue", PlotType = PlotType.Line, Thickness = 2)]
        public IndicatorDataSeries MA { get; set; }

        [Parameter(DefaultValue = 10)]
        public int PeriodsUSDX { get; set; }

        private MovingAverage USDXMA;
        private Index _usdxIndex;
        private Index _eurxIndex;

        protected override void Initialize()
        {

            USDXMA = Indicators.MovingAverage(USDX, PeriodsUSDX, MovingAverageType.Simple);

            _usdxIndex = new Index 
            {
                Name = "USDX",
                Multiplier = 50.14348112,
                Constituents = new List<Constituent> 
                {
                    //wieght is negative when USD is not the base currency (EURUSD and GBPUSD)

                    new Constituent("EURUSD", -0.576),
                    new Constituent("USDJPY", 0.136),
                    new Constituent("GBPUSD", -0.119),
                    new Constituent("USDCAD", 0.091),
                    new Constituent("USDSEK", 0.042),
                    new Constituent("USDCHF", 0.036)
                }
            };

            _eurxIndex = new Index 
            {
                Name = "EURX",
                Multiplier = 34.38805726,
                Constituents = new List<Constituent> 
                {
                    new Constituent("EURUSD", 0.3155),
                    new Constituent("EURJPY", 0.1891),
                    new Constituent("EURGBP", 0.3056),
                    new Constituent("EURSEK", 0.0785),
                    new Constituent("EURCHF", 0.1113)
                }
            };
        }

        public override void Calculate(int index)
        {
            var date = MarketSeries.OpenTime[index];

            if (ShowUSDX)
            {
                USDX[index] = CalculateIndex(_usdxIndex, date);
            }

            if (ShowEURX)
            {
                EURX[index] = CalculateIndex(_eurxIndex, date);
            }

            MA[index] = USDXMA.Result[index];

        }

        private double CalculateIndex(Index index, DateTime date)
        {
            //index is calculated as a weighted geometric mean of its constituents' close prices

            double result = index.Multiplier;

            foreach (var weight in index.Constituents)
            {
                var series = MarketData.GetSeries(weight.Symbol, TimeFrame);
                if (series == null)
                {
                    return double.NaN;
                }
                double close = GetCloseByDate(date, series);
                result *= Math.Pow(close, weight.Weight);
            }
            return result;
        }

        private double GetCloseByDate(DateTime date, MarketSeries series)
        {
            var idx = series.OpenTime.GetIndexByExactTime(date);
            if (idx == -1)
            {
                return double.NaN;
            }
            return series.Close[idx];
        }
    }

    public class Index
    {
        public string Name { get; set; }

        /// <summary>
        /// Constant multiplier as defined in ICE contract spec
        /// </summary>
        public double Multiplier { get; set; }

        /// <summary>
        /// List of index constituents
        /// </summary>
        public List<Constituent> Constituents { get; set; }
    }

    public class Constituent
    {
        public Constituent(string symbol, double cx)
        {
            Symbol = symbol;
            Weight = cx;
        }

        public string Symbol { get; private set; }

        /// <summary>
        /// Constituent Weight
        /// </summary>
        public double Weight { get; private set; }
    }
}

 


@samuelbreezey
Replies

PanagiotisCharalampous
24 Aug 2020, 08:13

Hi Sam,

Please explain why do you think it is not loaded properly? What is the indicator supposed to do? What did you expect to see and what do you see instead?

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

samuelbreezey
24 Aug 2020, 21:14

RE:

PanagiotisCharalampous said:

Hi Sam,

Please explain why do you think it is not loaded properly? What is the indicator supposed to do? What did you expect to see and what do you see instead?

Best Regards,

Panagiotis 

Join us on Telegram

 

Hi Panagiotis 

Reason being no data from before 4th August 2020. I was expecting to see data before this date, however instead it was blank

Thanks
Sam


@samuelbreezey

PanagiotisCharalampous
25 Aug 2020, 07:48

Hi Sam,

This is probably caused by the fact that the series you retrieve do not have data before that date. You should try retrieving bars instead of series and use LoadMoreHistory() to retrieve adequate bars so that your indicator can display values for the days you wish to see.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

samuelbreezey
02 Sep 2020, 21:30

Thank you Panagiotis 

Can you help me out with the coding please?

Many thanks
Sam


@samuelbreezey

PanagiotisCharalampous
03 Sep 2020, 07:50

Hi Sam,

I am sorry but I cannot engage into custom development. If you cannot program this yourself, you should hire a professional.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous