True TMA Bands

Created at 07 Aug 2023, 10:12
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!
CZ

Czung

Joined 07.08.2023

True TMA Bands
07 Aug 2023, 10:12


Hi

I am trying to convert TMA bands from TV into ctrader but have one problem.

If (i<= j) I am getting results that I want but only on historical data. It doesnt calculate present price action. 

if (i >= j) its working on actual data but. Doesn't return data that I want. 

I dont know what to do bc on TV its working perfectly fine with (i >= j). And gives perfect results.

 

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 TMABandsv22 : Indicator
    {
        [Parameter(DefaultValue = 12, MinValue = 0)]
        public int HalfLength { get; set; }

        [Parameter(DefaultValue = 2)]
        public double ATRMultiplier { get; set; }

        [Parameter(DefaultValue = 130)]
        public int ATRPeriod { get; set; }

        [Output("Upper", LineColor = "blue")]
        public IndicatorDataSeries Upper { get; set; }

        [Output("Lower", LineColor = "blue")]
        public IndicatorDataSeries Lower { get; set; }
        
        [Output("TMAC", LineColor = "green")]
        public IndicatorDataSeries TMAC { get; set; }

        private IndicatorDataSeries _haOpen;
        private IndicatorDataSeries _haHigh;
        private IndicatorDataSeries _haLow;
        private IndicatorDataSeries _haClose;
        
        protected override void Initialize()
        {
            _haOpen = CreateDataSeries();
            _haHigh = CreateDataSeries();
            _haLow = CreateDataSeries();
            _haClose = CreateDataSeries();
            
        }
        
       
        public override void Calculate(int index)
        {
        
         double Prices(int x)
{
         double price = (Bars.ClosePrices.Last(x) + Bars.ClosePrices.Last(x) + Bars.HighPrices.Last(x) + Bars.LowPrices.Last(x)) /4;
         return price;    
}
        
            ///////////////////////////////////////////

            var open = Bars.OpenPrices[index];
            var high = Bars.HighPrices[index];
            var low = Bars.LowPrices[index];
            var close = Bars.ClosePrices[index];
            
            var haClose = (open + high + low + close) / 4;
            var haOpen = (index > 0) ? (_haOpen[index - 1] + _haClose[index - 1]) / 2 : (open + close) / 2;
            var haHigh = Math.Max(Math.Max(high, haOpen), haClose);
            var haLow = Math.Min(Math.Min(low, haOpen), haClose);
            
            _haOpen[index] = haOpen;
            _haHigh[index] = haHigh;
            _haLow[index] = haLow;
            _haClose[index] = haClose;

             ///////////////////////////////////////////
            ///////////////////////////////////////////
        
         
            // MAIN
            
for (int i = HalfLength; i >= 0; i--)
{
    // ATR
    double atr = 0.0;
    for (int j = 0; j < ATRPeriod; j++)
    {
        atr += Math.Max(_haHigh[i + j + 10], _haClose[i + j + 11]) - Math.Min(_haLow[i + j + 10], _haClose[i + j + 11]);
    }
    atr /= ATRPeriod;

    // BANDS
    double sum = (HalfLength + 1) * Prices(i); 
    
    double sumw = (HalfLength + 1);
    
    int k = HalfLength;
    
    for (int j = 1; j <= HalfLength; j++, k--)
    {
       if (i <= j)
       
            {
            sum += k * Prices(i - j);
            sumw += k;
            }
            
            sum += k * Prices(i + j);
            sumw += k; 
          
    }
    
    double tmac = sum / sumw ;
    Upper[index] = tmac + ATRMultiplier * atr;
    Lower[index] = tmac - ATRMultiplier * atr;
    TMAC[index] = tmac;
}
         
    }     
           
        }
                 
            }

 


@Czung