Indicator stops drawing when using multiple timeframes

Created at 30 Jan 2016, 02:18
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!
CA

carltiden

Joined 30.01.2016

Indicator stops drawing when using multiple timeframes
30 Jan 2016, 02:18


I've created an indicator that uses the minute timeframe to calculate where the resulting dot should be placed on a bigger timeframe's candle (the "main" timeframe).

When I run the indicator it only draws dots for about the 180 last candles (1h timeframe). Why is this?


@carltiden
Replies

cyfer
30 Jan 2016, 02:22

I'm responding blindly now because you didn't post any code 

Chartobjects.DrawText("theDot"+index.toString() , the Text , the position , the color)

 


@cyfer

carltiden
31 Jan 2016, 22:57

Very sorry, here's the code:

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TFV : Indicator
    {
        [Parameter("exponent", DefaultValue = 2.0, MinValue = 0.2)]
        public double EXPO { get; set; }

        [Output("Main", PlotType = PlotType.Points, Color = Colors.Orange, Thickness = 4)]
        public IndicatorDataSeries Result { get; set; }

        private MarketSeries m1;
        private int startIndex;
        private int lastIndex;
        private double prevBid;
        private double avgBid;
        private int bidCount = 0;

        protected override void Initialize()
        {
            m1 = MarketData.GetSeries(TimeFrame.Minute);

            startIndex = MarketSeries.Open.Count - 1;
            lastIndex = startIndex;
        }

        public override void Calculate(int index)
        {
            if (index < startIndex)
            {
                OldCows(index);
            }
            else
            {
                // calculate for last/current bar
                int minuteIndex = GetIndexByDate(m1, MarketSeries.OpenTime[index]);
                double candle = Symbol.Bid;

                // the birth of a candle
                if (index != lastIndex)
                {
                    bidCount = 1;

                    // start candle with last minute's median + current price
                    Result[index] = avgBid = prevBid = (m1.Median[minuteIndex - 1] + EXPO * Symbol.Bid) / (EXPO + 1);

                    lastIndex = index;
                }
                // not the birth of new candle
                else
                {
                    // continue the current candle
                    if (Symbol.Bid != prevBid)
                    {
                        // bid price differs from previous
                        avgBid = ((bidCount * avgBid) + (EXPO * Symbol.Bid)) / (bidCount + EXPO);
                        prevBid = Symbol.Bid;
                        bidCount++;
                        candle = avgBid;
                    }
                    Result[index] = avgBid;
                }
            }
        }

        private void OldCows(int index)
        {
            // Calculate value at specified index
            // Result[index] = ...
            if (MarketSeries.TimeFrame == TimeFrame.Minute)
            {
                Result[index] = MarketSeries.Median[index];
            }
            else
            {
                var firstMinBarIndex = m1.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index]);

                // we need to know how many minute bars fit
                // in a big bar
                var timeSpan = MarketSeries.OpenTime[index] - MarketSeries.OpenTime[index - 1];
                double numberOfSeconds = timeSpan.TotalSeconds;
                int numberOfMinutes = (int)numberOfSeconds / 60;

                double resultForBar = 0;
                double divisor = 0;
                for (int i = 0; i < numberOfMinutes; i++)
                {
                    // add the minute bars value
                    resultForBar += m1.Close[firstMinBarIndex + i] * Math.Pow(EXPO, i);
                    divisor += Math.Pow(EXPO, i);
                }
                resultForBar = resultForBar / divisor;

                Result[index] = resultForBar;
            }
        }

        private int GetIndexByDate(MarketSeries series, DateTime time)
        {
            for (int i = series.Close.Count - 1; i > 0; i--)
            {
                if (time == series.OpenTime[i])
                    return i;
            }
            return -1;
        }
    }
}

It's pretty much a remake/modified copy of jeex's indicator temporary fair value.

On each bar it uses the minute candles' values for plotting the dot on the main TF.


@carltiden

Spotware
05 Feb 2016, 14:39

Dear Trader,

We would like to inform you that we do not provide coding assistance services. We more than glad to assist you with specific questions about cAlgo.API. You can contact one of our Partners or post a job in Development Jobs section for further coding assistance.

However, we can recommend you a very simple yet very useful way to troubleshoot your code.

1) You could write simple Print("Hello"); Methods in some sections of your code to see if your code reaches this section.

2) Once you locate the part which is not reachable you should check the conditions you entered and investigate why they are not filled. You can do this again by printing the values in the log file (or console).

3) Verify that the results of your calculations are correct.


@Spotware

MaVe
05 Feb 2016, 21:41

Dear Spotware,

Do you have an example for that?

Thank you.


@MaVe