Replies

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