Get custom value from indicator. Return 0

Created at 07 May 2020, 06:19
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!
OF

office3131

Joined 24.05.2019

Get custom value from indicator. Return 0
07 May 2020, 06:19


Hello

I have a 3 frame indicator based on TEMA. I want to get the value PMA from the indicator, in order to use it on my bot. Unfortunately the result is always 0.

I have read all instruction available in this site, but didn`t find an answer how to fix this. I would appreciate very much any help. Than you.

Here is the indicator. The bot is on bottom

using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using Microsoft.Win32;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class TEMA_triOLD : Indicator
    {
        [Parameter("Timeframe 1", DefaultValue = "Minute")]
        public TimeFrame tf1 { get; set; }
        public int Period = 14;

        [Parameter("Timeframe 2", DefaultValue = "Minute5")]
        public TimeFrame tf2 { get; set; }
        public int Periodtf2 = 14;

        [Parameter("Timeframe 3", DefaultValue = "Minute15")]
        public TimeFrame tf3 { get; set; }
        public int Periodtf3 = 14;

        [Output("TF A", LineColor = "yellow")]
        public IndicatorDataSeries temaA { get; set; }
        [Output("TF B", LineColor = "red")]
        public IndicatorDataSeries temaB { get; set; }
        [Output("TF C", LineColor = "blue")]
        public IndicatorDataSeries temaC { get; set; }

        private ExponentialMovingAverage ema1;
        private ExponentialMovingAverage ema2;
        private ExponentialMovingAverage ema3;

        private ExponentialMovingAverage ema1tf2;
        private ExponentialMovingAverage ema2tf2;
        private ExponentialMovingAverage ema3tf2;

        private ExponentialMovingAverage ema1tf3;
        private ExponentialMovingAverage ema2tf3;
        private ExponentialMovingAverage ema3tf3;

        public bool tfUp;
        public bool tfDown;
        public string tf2rez;
        public string Reztf1;
        public string Reztf2;
        public string Reztf3;
        public double PMA;

        public double tf2diff;
        public string tf2rezS;
        public string tf3rezS;

        public double tf1perc;
        public double tf2perc;
        public double tf3perc;

        DataSeries stf1, stf2, stf3;

        protected override void Initialize()
        {
            stf1 = MarketData.GetSeries(tf1).Close;
            stf2 = MarketData.GetSeries(tf2).Close;
            stf3 = MarketData.GetSeries(tf3).Close;

            ema1 = Indicators.ExponentialMovingAverage(stf1, Period);
            ema2 = Indicators.ExponentialMovingAverage(ema1.Result, Period);
            ema3 = Indicators.ExponentialMovingAverage(ema2.Result, Period);

            ema1tf2 = Indicators.ExponentialMovingAverage(stf2, Periodtf2);
            ema2tf2 = Indicators.ExponentialMovingAverage(ema1tf2.Result, Periodtf2);
            ema3tf2 = Indicators.ExponentialMovingAverage(ema2tf2.Result, Periodtf2);

            ema1tf3 = Indicators.ExponentialMovingAverage(stf3, Periodtf3);
            ema2tf3 = Indicators.ExponentialMovingAverage(ema1tf3.Result, Periodtf3);
            ema3tf3 = Indicators.ExponentialMovingAverage(ema2tf3.Result, Periodtf3);

        }

        public override void Calculate(int index)
        {
//------REGISTRY set-------------
            //get time MarketSeries.TimeFrame.
            var charttime = MarketSeries.OpenTime[index];
            //var charttime = MarketSeries.TimeFrame.OpenTime[index];

//MarketSeries.
            //get index
            var idx1 = MarketData.GetSeries(tf1).OpenTime.GetIndexByTime(charttime);
            var idx2 = MarketData.GetSeries(tf2).OpenTime.GetIndexByTime(charttime);
            var idx3 = MarketData.GetSeries(tf3).OpenTime.GetIndexByTime(charttime);

            temaA[index] = 3 * ema1.Result[idx1] - 3 * ema2.Result[idx1] + ema3.Result[idx1];
            temaB[index] = 3 * ema1tf2.Result[idx2] - 3 * ema2tf2.Result[idx2] + ema3tf2.Result[idx2];
            temaC[index] = 3 * ema1tf3.Result[idx3] - 3 * ema2tf3.Result[idx3] + ema3tf3.Result[idx3];

//--------calc tf1-----------------
            double tA0 = (Math.Round(temaA.Last(0), 2));
            double tA1 = (Math.Round(temaA.Last(1), 2));
            double tA2 = (Math.Round(temaA.Last(2), 2));
            double tA3 = (Math.Round(temaA.Last(3), 2));
            // /*   
            double tf1a1 = (Math.Round(temaA.Last(1), 2));
            double tf1a6 = 0;
            double tf1perc = 0;
            string tf1rez = "" + tf1a1;
            string tf1rezS = "";
            for (int i = 1; i <= 15; i++)
            {
                tf1a6 = (Math.Round(temaA.Last(i), 2));
                if (tf1a6 != tf1a1)
                {
                    if (tf1a6 > tf1a1)
                    {
                        tf1perc = Math.Round(1 * (tf1a1 - tf1a6), 2);
                        tf1rez = "tf1 -DOWN---(" + i + ") " + tf1a6 + "   (1) " + tf1a1 + " i=" + i + " %=" + tf1perc;
                        tf1rezS = "tf1 down";
                        Reztf1 = "down";
                        break;
                    }
                    if (tf1a6 < tf1a1)
                    {
                        tf1perc = Math.Round(1 * (tf1a1 - tf1a6), 2);
                        tf1rez = "tf1 -UP---(" + i + ") " + tf1a6 + "   (1) " + tf1a1 + " i=" + i + " %=" + tf1perc;
                        tf1rezS = "tf1 up";
                        Reztf1 = "up";
                        break;
                    }
                }
            }

//-----calc tf2-------------------
            double tB0 = (Math.Round(temaB.Last(0), 2));
            double tB1 = (Math.Round(temaB.Last(1), 2));
            double tB3 = (Math.Round(temaB.Last(3), 2));
            double tB9 = (Math.Round(temaB.Last(9), 2));
            //    /*
            double tf2a1 = (Math.Round(temaB.Last(1), 2));
            double tf2a6 = 0;
            double tf2perc = 0;
            string tf2rez = "" + tf2a1;
            string tf2rezS = "";
            for (int i = 1; i <= 15; i++)
            {
                tf2a6 = (Math.Round(temaB.Last(i), 2));
                if (tf2a6 != tf2a1)
                {
                    if (tf2a6 > tf2a1)
                    {
                        tf2perc = Math.Round(1 * (tf2a1 - tf2a6), 2);
                        tf2rez = "tf2 -DO---(" + i + ") " + tf2a6 + "   (1) " + tf2a1 + " i=" + i + " %=" + tf2perc;
                        tf2rezS = "tf2 down";
                        Reztf2 = "down";
                        break;
                    }
                    if (tf2a6 < tf2a1)
                    {
                        tf2perc = Math.Round(1 * (tf2a1 - tf2a6), 2);
                        tf2rez = "tf2 -UP---(" + i + ") " + tf2a6 + "   (1) " + tf2a1 + " i=" + i + " %=" + tf2perc;
                        tf2rezS = "tf2 up";
                        Reztf2 = "up";
                        break;
                    }
                }
            }

//-----end  tf2----------------------
//-----calc tf3-------------------
            double tC0 = (Math.Round(temaC.Last(0), 2));
            double tC1 = (Math.Round(temaC.Last(1), 2));
            double tC5 = (Math.Round(temaC.Last(5), 2));
            double tC15 = (Math.Round(temaC.Last(15), 2));

            double PMA = 0;
            double tf3a6 = 0;
            double tf3perc = 0;
            double tf3a1 = (Math.Round(temaC.Last(1), 2));
            string tf3rez = "" + tf3a1;
            string tf3rezS = "";
            for (int i = 1; i <= 15; i++)
            {
                tf3a6 = (Math.Round(temaC.Last(i), 2));
                if (tf3a6 != tf3a1)
                {
                    PMA = (tf1perc + tf2perc + tf3perc);
                    if (tf3a6 > tf3a1)
                    {

                        tf3perc = Math.Round(1 * (tf3a1 - tf3a6), 2);
                        PMA = (tf1perc + tf2perc + tf3perc);

                        tf3rez = "tf3 -DO---(" + i + ") " + tf3a6 + "   (1) " + tf3a1 + " i=" + i + " %=" + tf3perc + "\n\t\t\t_triOLD PMA. =" + PMA;
                        tf3rezS = "tf3 down";
                        Reztf3 = "down";
                        break;
                    }
                    if (tf3a6 < tf3a1)
                    {
                        tf3perc = Math.Round(10 * (tf3a1 - tf3a6), 2);
                        PMA = (tf1perc + tf2perc + tf3perc);

                        tf3rez = "tf3 -UP---(" + i + ") " + tf3a6 + "   (1) " + tf3a1 + " i=" + i + " %=" + tf3perc + "\n\t\t\t_triOLD PMA. =" + PMA;
                        tf3rezS = "tf3 up";
                        Reztf3 = "up";
                        break;
                    }
                }
            }

//--end tf3---------------------

//-----calc tema2 diff

            var tf26 = Math.Round(temaB.Last(6), 2);
            var tf20 = Math.Round(temaB.Last(0), 2);
            var tf2diff = tf20 - tf26;
            bool tfUp = tf2diff >= 9 ? true : false;
            bool tfDown = tf2diff <= -9 ? true : false;
            var diffPerc2FR = Math.Round(10000 * (tf2diff / tf20), 3);
            var ColRSI = "Yellow";
            if (RunningMode != RunningMode.Optimization)
            {
                //  Chart.DrawStaticText("AlertBuy", "tf26 " + tf26 + "   " + "  tf20 " + tf20 + "\n\n\n\ntfDown " + tfDown + "   tfUp" + tfUp, VerticalAlignment.Top, HorizontalAlignment.Center, Color.Yellow);
                Chart.DrawStaticText("tf1", "IND " + tf1rez, VerticalAlignment.Top, HorizontalAlignment.Center, Color.Yellow);
                Chart.DrawStaticText("tf2", "\nIND " + tf2rez, VerticalAlignment.Top, HorizontalAlignment.Center, Color.Red);
                Chart.DrawStaticText("tf3", "\n\nIND " + tf3rez, VerticalAlignment.Top, HorizontalAlignment.Center, Color.Aqua);
            }
//---end calc diff--------------
        }
    }
}

 

----------- BOT-----------------------------------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

using System.Threading;
using System.Threading.Tasks;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class v2zII : Robot
    {
        [Parameter(DefaultValue = "Test Bot")]
        public string Label { get; set; }

        [Parameter(Group = "TEMA", DefaultValue = 13)]
        public int TEMAPeriod { get; set; }

        private TEMA_triOLD _tema;


        protected override void OnStart()
        {
            _tema = Indicators.GetIndicator<TEMA_triOLD>(TimeFrame.Minute, TimeFrame.Minute3, TimeFrame.Minute15);
        }


        protected override void OnTick()
        {
            var t20 = _tema.temaA.Last(0) - _tema.temaA.Last(1);
            t20 = Math.Round(t20, 2);
            var t40 = _tema.temaA.Last(1) - _tema.temaA.Last(2);
            t40 = Math.Round(t40, 2);

            string tf1rezS = _tema.Reztf1;
            string tf2rezS = _tema.Reztf2;
            string tf3rezS = _tema.Reztf3;
            var PMA = _tema.PMA;
            Print(" ----t40 " + t40 + "--PMA " + PMA);
        }

        protected override void OnBar()
        {

        }


        protected override void OnStop()
        {

        }
    }
}

 


@office3131
Replies

PanagiotisCharalampous
07 May 2020, 10:34

Hi office3131,

There is a local declaration of PMA variable inside the Calculate function which overrides the global one.

            double PMA = 0;
            double tf3a6 = 0;
            double tf3perc = 0;

You need to delete it.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

office3131
07 May 2020, 16:46

RE:

PanagiotisCharalampous said:

Hi office3131,

There is a local declaration of PMA variable inside the Calculate function which overrides the global one.

            double PMA = 0;
            double tf3a6 = 0;
            double tf3perc = 0;

You need to delete it.

Best Regards,

Panagiotis 

Join us on Telegram

Than you for replaying Panagiotis,

Deleting double PMA, doesnt change nothing. I still have PMA=0 in the bot.

Deleting   double tf3a6 = 0; double tf3perc = 0; gives me Error CS0103: The name 'tf3a6' does not exist in the current context


@office3131

PanagiotisCharalampous
08 May 2020, 08:35 ( Updated at: 21 Dec 2023, 09:22 )

Hi office3131,

I don't

here is my indicator code

using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using Microsoft.Win32;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class TEMA_triOLD : Indicator
    {
        [Parameter("Timeframe 1", DefaultValue = "Minute")]
        public TimeFrame tf1 { get; set; }
        public int Period = 14;

        [Parameter("Timeframe 2", DefaultValue = "Minute5")]
        public TimeFrame tf2 { get; set; }
        public int Periodtf2 = 14;

        [Parameter("Timeframe 3", DefaultValue = "Minute15")]
        public TimeFrame tf3 { get; set; }
        public int Periodtf3 = 14;

        [Output("TF A", LineColor = "yellow")]
        public IndicatorDataSeries temaA { get; set; }
        [Output("TF B", LineColor = "red")]
        public IndicatorDataSeries temaB { get; set; }
        [Output("TF C", LineColor = "blue")]
        public IndicatorDataSeries temaC { get; set; }

        private ExponentialMovingAverage ema1;
        private ExponentialMovingAverage ema2;
        private ExponentialMovingAverage ema3;

        private ExponentialMovingAverage ema1tf2;
        private ExponentialMovingAverage ema2tf2;
        private ExponentialMovingAverage ema3tf2;

        private ExponentialMovingAverage ema1tf3;
        private ExponentialMovingAverage ema2tf3;
        private ExponentialMovingAverage ema3tf3;

        public bool tfUp;
        public bool tfDown;
        public string tf2rez;
        public string Reztf1;
        public string Reztf2;
        public string Reztf3;
        public double PMA;

        public double tf2diff;
        public string tf2rezS;
        public string tf3rezS;

        public double tf1perc;
        public double tf2perc;
        public double tf3perc;

        DataSeries stf1, stf2, stf3;

        protected override void Initialize()
        {
            stf1 = MarketData.GetSeries(tf1).Close;
            stf2 = MarketData.GetSeries(tf2).Close;
            stf3 = MarketData.GetSeries(tf3).Close;

            ema1 = Indicators.ExponentialMovingAverage(stf1, Period);
            ema2 = Indicators.ExponentialMovingAverage(ema1.Result, Period);
            ema3 = Indicators.ExponentialMovingAverage(ema2.Result, Period);

            ema1tf2 = Indicators.ExponentialMovingAverage(stf2, Periodtf2);
            ema2tf2 = Indicators.ExponentialMovingAverage(ema1tf2.Result, Periodtf2);
            ema3tf2 = Indicators.ExponentialMovingAverage(ema2tf2.Result, Periodtf2);

            ema1tf3 = Indicators.ExponentialMovingAverage(stf3, Periodtf3);
            ema2tf3 = Indicators.ExponentialMovingAverage(ema1tf3.Result, Periodtf3);
            ema3tf3 = Indicators.ExponentialMovingAverage(ema2tf3.Result, Periodtf3);

        }

        public override void Calculate(int index)
        {
//------REGISTRY set-------------
            //get time MarketSeries.TimeFrame.
            var charttime = MarketSeries.OpenTime[index];
            //var charttime = MarketSeries.TimeFrame.OpenTime[index];

//MarketSeries.
            //get index
            var idx1 = MarketData.GetSeries(tf1).OpenTime.GetIndexByTime(charttime);
            //Print("idx1: " + idx1);
            var idx2 = MarketData.GetSeries(tf2).OpenTime.GetIndexByTime(charttime);
            var idx3 = MarketData.GetSeries(tf3).OpenTime.GetIndexByTime(charttime);

            temaA[index] = 3 * ema1.Result[idx1] - 3 * ema2.Result[idx1] + ema3.Result[idx1];
            //   Print("Tema A " + temaA[index]);
            temaB[index] = 3 * ema1tf2.Result[idx2] - 3 * ema2tf2.Result[idx2] + ema3tf2.Result[idx2];
            //   Print("Tema B " + temaA[index]);
            temaC[index] = 3 * ema1tf3.Result[idx3] - 3 * ema2tf3.Result[idx3] + ema3tf3.Result[idx3];
            //    Print("Tema C " + temaA[index]);

            //--------calc tf1-----------------
            double tA0 = (Math.Round(temaA.Last(0), 2));
            double tA1 = (Math.Round(temaA.Last(1), 2));
            double tA2 = (Math.Round(temaA.Last(2), 2));
            double tA3 = (Math.Round(temaA.Last(3), 2));
            // /*   
            double tf1a1 = (Math.Round(temaA.Last(1), 2));
            double tf1a6 = 0;
            double tf1perc = 0;
            string tf1rez = "" + tf1a1;
            string tf1rezS = "";
            for (int i = 1; i <= 15; i++)
            {
                tf1a6 = (Math.Round(temaA.Last(i), 2));
                if (tf1a6 != tf1a1)
                {
                    if (tf1a6 > tf1a1)
                    {
                        tf1perc = Math.Round(1 * (tf1a1 - tf1a6), 2);
                        tf1rez = "tf1 -DOWN---(" + i + ") " + tf1a6 + "   (1) " + tf1a1 + " i=" + i + " %=" + tf1perc;
                        tf1rezS = "tf1 down";
                        Reztf1 = "down";
                        break;
                    }
                    if (tf1a6 < tf1a1)
                    {
                        tf1perc = Math.Round(1 * (tf1a1 - tf1a6), 2);
                        tf1rez = "tf1 -UP---(" + i + ") " + tf1a6 + "   (1) " + tf1a1 + " i=" + i + " %=" + tf1perc;
                        tf1rezS = "tf1 up";
                        Reztf1 = "up";
                        break;
                    }
                }
            }

//-----calc tf2-------------------
            double tB0 = (Math.Round(temaB.Last(0), 2));
            double tB1 = (Math.Round(temaB.Last(1), 2));
            double tB3 = (Math.Round(temaB.Last(3), 2));
            double tB9 = (Math.Round(temaB.Last(9), 2));
            //    /*
            double tf2a1 = (Math.Round(temaB.Last(1), 2));
            double tf2a6 = 0;
            double tf2perc = 0;
            string tf2rez = "" + tf2a1;
            string tf2rezS = "";
            for (int i = 1; i <= 15; i++)
            {
                tf2a6 = (Math.Round(temaB.Last(i), 2));
                if (tf2a6 != tf2a1)
                {
                    if (tf2a6 > tf2a1)
                    {
                        tf2perc = Math.Round(1 * (tf2a1 - tf2a6), 2);
                        tf2rez = "tf2 -DO---(" + i + ") " + tf2a6 + "   (1) " + tf2a1 + " i=" + i + " %=" + tf2perc;
                        tf2rezS = "tf2 down";
                        Reztf2 = "down";
                        break;
                    }
                    if (tf2a6 < tf2a1)
                    {
                        tf2perc = Math.Round(1 * (tf2a1 - tf2a6), 2);
                        tf2rez = "tf2 -UP---(" + i + ") " + tf2a6 + "   (1) " + tf2a1 + " i=" + i + " %=" + tf2perc;
                        tf2rezS = "tf2 up";
                        Reztf2 = "up";
                        break;
                    }
                }
            }

//-----end  tf2----------------------
//-----calc tf3-------------------
            double tC0 = (Math.Round(temaC.Last(0), 2));
            double tC1 = (Math.Round(temaC.Last(1), 2));
            double tC5 = (Math.Round(temaC.Last(5), 2));
            double tC15 = (Math.Round(temaC.Last(15), 2));

         //   double PMA = 1;
            double tf3a6 = 0;
            double tf3perc = 0;
            double tf3a1 = (Math.Round(temaC.Last(1), 2));
            string tf3rez = "" + tf3a1;
            string tf3rezS = "";
            for (int i = 1; i <= 15; i++)
            {
                tf3a6 = (Math.Round(temaC.Last(i), 2));
                if (tf3a6 != tf3a1)
                {
                    PMA = (tf1perc + tf2perc + tf3perc);
                    if (tf3a6 > tf3a1)
                    {

                        tf3perc = Math.Round(1 * (tf3a1 - tf3a6), 2);
                        PMA = (tf1perc + tf2perc + tf3perc);

                        tf3rez = "tf3 -DO---(" + i + ") " + tf3a6 + "   (1) " + tf3a1 + " i=" + i + " %=" + tf3perc + "\n\t\t\t_triOLD PMA. =" + PMA;
                        tf3rezS = "tf3 down";
                        Reztf3 = "down";
                        break;
                    }
                    if (tf3a6 < tf3a1)
                    {
                        tf3perc = Math.Round(10 * (tf3a1 - tf3a6), 2);
                        PMA = (tf1perc + tf2perc + tf3perc);

                        tf3rez = "tf3 -UP---(" + i + ") " + tf3a6 + "   (1) " + tf3a1 + " i=" + i + " %=" + tf3perc + "\n\t\t\t_triOLD PMA. =" + PMA;
                        tf3rezS = "tf3 up";
                        Reztf3 = "up";
                        break;
                    }
                }
            }

//--end tf3---------------------

//-----calc tema2 diff

            var tf26 = Math.Round(temaB.Last(6), 2);
            var tf20 = Math.Round(temaB.Last(0), 2);
            var tf2diff = tf20 - tf26;
            bool tfUp = tf2diff >= 9 ? true : false;
            bool tfDown = tf2diff <= -9 ? true : false;
            var diffPerc2FR = Math.Round(10000 * (tf2diff / tf20), 3);
            var ColRSI = "Yellow";
            if (RunningMode != RunningMode.Optimization)
            {
                //  Chart.DrawStaticText("AlertBuy", "tf26 " + tf26 + "   " + "  tf20 " + tf20 + "\n\n\n\ntfDown " + tfDown + "   tfUp" + tfUp, VerticalAlignment.Top, HorizontalAlignment.Center, Color.Yellow);
                Chart.DrawStaticText("tf1", "IND " + tf1rez, VerticalAlignment.Top, HorizontalAlignment.Center, Color.Yellow);
                Chart.DrawStaticText("tf2", "\nIND " + tf2rez, VerticalAlignment.Top, HorizontalAlignment.Center, Color.Red);
                Chart.DrawStaticText("tf3", "\n\nIND " + tf3rez, VerticalAlignment.Top, HorizontalAlignment.Center, Color.Aqua);
            }
//---end calc diff--------------
        }
    }
}

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous