Replies

orsica
10 Sep 2020, 13:50

RE:

Dear Panagiotis,

still have problems with indicator showing different results depending on symbol timeframe, could you check please (maybe I didn't understand well the index adaptation inside calculate):

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

namespace cAlgo.Indicators
{

    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TSI_ORIGINAL2 : Indicator
    {

        [Parameter("Long Period", Group = "TSI", DefaultValue = 25, MinValue = 1)]
        public int LongPeriod { get; set; }

        [Parameter("Short Period", Group = "TSI", DefaultValue = 15, MinValue = 1)]
        public int ShortPeriod { get; set; }

        [Parameter("Max-MIn Period", Group = "TSI", DefaultValue = 1000, MinValue = 1)]
        public int MaxMinPeriod { get; set; }

        [Parameter("Timeframe", Group = "TSI", DefaultValue = "Hour")]
        public TimeFrame timeFrame { get; set; }

        [Parameter("MA Type", Group = "TSI", DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType MaType { get; set; }

        [Output("EUR", Thickness = 1, LineColor = "Green")]
        public IndicatorDataSeries eurIdxOrig { get; set; }


        //******* DEFINISCO LE MEDIE MOBILI PER L'EURO*********
        private MovingAverage eur_Ema1;
        private MovingAverage eur_Ema2;
        private MovingAverage eur_Abs_Ema1;
        private MovingAverage eur_Abs_Ema2;
        //*****************************************************


        //******* DEFINISCO LE SERIE TIPO INDICATORE MOMENTUM VARI CAMBI CON L'EURO*********
        private IndicatorDataSeries mtm_eurUsd_series;
        private IndicatorDataSeries mtm_eurGbp_series;
        private IndicatorDataSeries mtm_eurJpy_series;
        private IndicatorDataSeries mtm_eurCad_series;
        private IndicatorDataSeries mtm_eurChf_series;
        private IndicatorDataSeries mtm_eurNzd_series;
        private IndicatorDataSeries mtm_eurAud_series;
        //**********************************************************************************

        //******* DEFINISCO LE SERIE TIPO INDICATORE MOMENTUM SINTETICO PER L'EURO**********
        private IndicatorDataSeries mtm_eur_series;
        private IndicatorDataSeries abs_mtm_eur_series;
        //**********************************************************************************

        //******* DEFINISCO LE SERIE DELLE BARRE DEI CAMBI CON L'EURO*********
        Bars eurUsd_series;
        Bars eurGbp_series;
        Bars eurJpy_series;
        Bars eurCad_series;
        Bars eurChf_series;
        Bars eurNzd_series;
        Bars eurAud_series;
        //********************************************************************

        //******* INIZIALIZZO LE VARIABILI PER LE LABEL DI PERCENTUALE A DESTRA (MAX,MIN,ACTUAL) PER L'EURO********
        double maxeurIdxOrig = 0;
        double mineurIdxOrig = 0;
        double labeurIdxOrig = 0;
        //*********************************************************************************************************



        protected override void Initialize()
        {
            //******* INIZIALIZZO LE SERIE DEI MOMENTUM DEI VARI CAMBI CON L'EURO*********
            mtm_eurUsd_series = CreateDataSeries();
            mtm_eurGbp_series = CreateDataSeries();
            mtm_eurJpy_series = CreateDataSeries();
            mtm_eurCad_series = CreateDataSeries();
            mtm_eurChf_series = CreateDataSeries();
            mtm_eurNzd_series = CreateDataSeries();
            mtm_eurAud_series = CreateDataSeries();
            //****************************************************************************

            //******* INIZIALIZZO LE SERIE DEL MOMENTUM SINTETICO PER L'EURO**********
            mtm_eur_series = CreateDataSeries();
            abs_mtm_eur_series = CreateDataSeries();
            //************************************************************************

            //******* CARICO I DATI DELLE SERIE DELLE BARRE DEI CAMBI CON L'EURO ***************
            eurUsd_series = MarketData.GetBars(timeFrame, "EURUSD");
            eurGbp_series = MarketData.GetBars(timeFrame, "EURGBP");
            eurJpy_series = MarketData.GetBars(timeFrame, "EURJPY");
            eurCad_series = MarketData.GetBars(timeFrame, "EURCAD");
            eurChf_series = MarketData.GetBars(timeFrame, "EURCHF");
            eurNzd_series = MarketData.GetBars(timeFrame, "EURNZD");
            eurAud_series = MarketData.GetBars(timeFrame, "EURAUD");
            //**********************************************************************************

            //******** INIZIALIZZO LE MEDIE MOBILI PER IL CALCOLO DEL TSI PER L'EURO *************************
            eur_Ema1 = Indicators.MovingAverage(mtm_eur_series, LongPeriod, MaType);
            eur_Ema2 = Indicators.MovingAverage(eur_Ema1.Result, ShortPeriod, MaType);
            eur_Abs_Ema1 = Indicators.MovingAverage(abs_mtm_eur_series, LongPeriod, MaType);
            eur_Abs_Ema2 = Indicators.MovingAverage(eur_Abs_Ema1.Result, ShortPeriod, MaType);
            //*************************************************************************************


        }

        public override void Calculate(int index)
        {
            //******** AZZERO IL VALORE DELL'INDICE SINTETICO DELL'EURO AL PRIMO FIX UTILE ***************
            if (index < 1)
            {
                eurIdxOrig[index] = 0;

                return;
            }
            //********************************************************************************************



            //******** POPOLO LE SERIE DEI MOMENTUM DEI VARI CAMBI CON L'EURO ****************************
            mtm_eurUsd_series[index] = (eurUsd_series.ClosePrices[eurUsd_series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])] - eurUsd_series.OpenPrices[eurUsd_series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])]) / (eurUsd_series.OpenPrices[eurUsd_series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])]);
            mtm_eurGbp_series[index] = (eurGbp_series.ClosePrices[eurGbp_series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])] - eurGbp_series.OpenPrices[eurGbp_series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])]) / (eurGbp_series.OpenPrices[eurGbp_series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])]);
            mtm_eurJpy_series[index] = (eurJpy_series.ClosePrices[eurJpy_series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])] - eurJpy_series.OpenPrices[eurJpy_series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])]) / (eurJpy_series.OpenPrices[eurJpy_series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])]);
            mtm_eurCad_series[index] = (eurCad_series.ClosePrices[eurCad_series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])] - eurCad_series.OpenPrices[eurCad_series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])]) / (eurCad_series.OpenPrices[eurCad_series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])]);
            mtm_eurChf_series[index] = (eurChf_series.ClosePrices[eurChf_series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])] - eurChf_series.OpenPrices[eurChf_series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])]) / (eurChf_series.OpenPrices[eurChf_series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])]);
            mtm_eurNzd_series[index] = (eurNzd_series.ClosePrices[eurNzd_series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])] - eurNzd_series.OpenPrices[eurNzd_series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])]) / (eurNzd_series.OpenPrices[eurNzd_series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])]);
            mtm_eurAud_series[index] = (eurAud_series.ClosePrices[eurAud_series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])] - eurAud_series.OpenPrices[eurAud_series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])]) / (eurAud_series.OpenPrices[eurAud_series.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])]);
            //********************************************************************************************


            //******** POPOLO LA SERIE DEL MOMENTUM SINTESI DEI VARI MOMENTUM PER L'EURO **************************************************************************************************************************************
            mtm_eur_series[index] = mtm_eurUsd_series[index] + mtm_eurGbp_series[index] + mtm_eurJpy_series[index] + mtm_eurCad_series[index] + mtm_eurChf_series[index] + mtm_eurNzd_series[index] + mtm_eurAud_series[index];
            abs_mtm_eur_series[index] = Math.Abs(mtm_eur_series[index]);
            //*****************************************************************************************************************************************************************************************************************


            //******** POPOLO LA SERIE DELL'INDICE SINTETICO DELL'EURO *******************************
            double eur_tsiDivisor = eur_Abs_Ema2.Result[index];
            double eur_tsiDividend = eur_Ema2.Result[index];

            if (Math.Abs(eur_tsiDivisor) < double.Epsilon)
            {
                eurIdxOrig[index] = 0;
            }
            else
            {
                eurIdxOrig[index] = 100.0 * eur_tsiDividend / eur_tsiDivisor;
            }
            //*****************************************************************************************


            //******** CERCO NELLA SERIE DELL'INDICE SINTETICO DELL'EURO IL MINIMO E IL MASSIMO NEI MAXMINPERIOD PERIODI ******
            maxeurIdxOrig = Functions.Maximum(eurIdxOrig, MaxMinPeriod);
            mineurIdxOrig = Functions.Minimum(eurIdxOrig, MaxMinPeriod);
            //*****************************************************************************************************************

            //******** CALCOLO LA PERCENTUALE ATTUALE RISPETTO AL MINIMO O AL MASSIMO PRECEDENTI ******************************
            if (eurIdxOrig[index] >= 0)
            {
                labeurIdxOrig = +(eurIdxOrig[index] * 100 / maxeurIdxOrig);
            }
            else
            {
                labeurIdxOrig = -(eurIdxOrig[index] * 100 / mineurIdxOrig);
            }
            //*****************************************************************************************************************


            //******** STAMPO MASSIMO,ATTUALE,MINIMO E PERCENTUALE ATTUALE ************************************************************************************************
            string eurIdxLabOrig = "\n" + "EUR : " + Convert.ToString(Math.Round(eurIdxOrig[index], 2)) + " ( " + Convert.ToString(Math.Round(labeurIdxOrig, 2)) + " % ) ";
            var text1 = IndicatorArea.DrawStaticText("eurIdx", eurIdxLabOrig, VerticalAlignment.Top, HorizontalAlignment.Right, Color.Green);
            //*************************************************************************************************************************************************************


        }

    }
}


@orsica