Multi time frame data

Created at 11 Sep 2021, 18:15
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!
HE

herculesone

Joined 02.11.2020

Multi time frame data
11 Sep 2021, 18:15


Hey there peeps.

I have been having a real issue using multi timeframe EMAs, and extracting their data. Although the indicators paint well there are a lot of NaN values, and some crazy results. 

Here is a function that does not return correct values, and I need to know what I am doing wrong. Your help is appreaciated.

Below is the function I have:

        private void EMAAnalysis()
        {

            Bars shortChart = MarketData.GetBars(ShortMATimeFrame);
            Bars longChart = MarketData.GetBars(LongMATimeFrame);

            int shortIndex = shortChart.OpenTimes.GetIndexByExactTime(Bars.OpenTimes.Last(0));
            int longIndex = longChart.OpenTimes.GetIndexByExactTime(Bars.OpenTimes.Last(0));

            MovingAverage shortEMA = Indicators.MovingAverage(shortChart.ClosePrices, ShortMALength, ShortMAType);
            MovingAverage longEMA = Indicators.MovingAverage(longChart.ClosePrices, LongMALength, LongMAType);

            Print("Short EMA = ", shortEMA.Result.Last(shortIndex));
            Print("Long EMA = ", longEMA.Result.Last(longIndex));

            if (shortEMA.Result.Last(shortIndex) > longEMA.Result.Last(longIndex))
            {
                _longtrend = true;
            }

            if (shortEMA.Result.Last(shortIndex) < longEMA.Result.Last(longIndex))
            {
                _longtrend = false;
            }

        }


@herculesone
Replies

herculesone
12 Sep 2021, 15:40

Solved

I was able to solve this as I finally understood how the index and time are related with these functions and I have chosen to do the following:

        private void EMAAnalysis(int index)
        {

            Bars shortChart = MarketData.GetBars(ShortMATimeFrame);
            Bars longChart = MarketData.GetBars(LongMATimeFrame);

            int shortIndex = shortChart.OpenTimes.GetIndexByExactTime(Bars.OpenTimes.Last(index));
            int longIndex = longChart.OpenTimes.GetIndexByExactTime(Bars.OpenTimes.Last(index));

            MovingAverage shortEMA = Indicators.MovingAverage(shortChart.ClosePrices, ShortMALength, ShortMAType);
            MovingAverage longEMA = Indicators.MovingAverage(longChart.ClosePrices, LongMALength, LongMAType);

            double shortResult = 0;
            double longResult = 0;

            for (int i = index; i < 100; i++)
            {
                shortResult = shortEMA.Result.Last(i);

                if (!double.IsNaN(shortResult) && shortResult != 0)
                {
                    Print("Short EMA = ", shortResult);
                    Print("Short Index = ", i);
                    break;
                }
            }

            for (int i = index; i < 100; i++)
            {
                longResult = longEMA.Result.Last(i);

                if (!double.IsNaN(longResult) && longResult != 0)
                {
                    Print("Long EMA = ", longResult);
                    Print("Long Index = ", i);
                    break;
                }
            }

            if (shortResult > longResult)
            {
                _longtrend = true;
            }

            if (shortResult < longResult)
            {
                _longtrend = false;
            }

        }

 

This way we look back at the real index of each bar until we get a number that is valid. Due to the way tick data is collected it seems that the NaN check needs to be there all the time, if not the values are sometimes all over the place as we get a lot of NaN and actual values.

Solved. Thank you!


@herculesone