Incorrect values Indicator to Bot via Registry

Created at 15 May 2020, 15:49
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

Incorrect values Indicator to Bot via Registry
15 May 2020, 15:49


Hello,

I am trying to pass values from Indicator to a bot. I use to save the values to the PCs registry, and read them from the bot. The values are saved correctly to the registry, but the bot return something completely wrong. Below i am attaching both Indicator and Bot codes.

In order to be on the same page  here is how I test it.  Backtest on BTCUSD 1 min, date only 04/03/ visual, 1000$, tick data from server. Then I add Indicator TEMA_triCOPY2 to the chart, let testing run for a while, then PAUSE and check numbers from the registry.  .... And bot shows different values for tf11, tf22 and tf33.

My question is why the bot is returning wrong values. Thank you.

------Indicator-------

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.Registry)]
    public class TEMA_triCOPY2 : 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 string Reztf1;
        public string Reztf2;
        public string Reztf3;

        public double PMA;
        public double tf1perc;
        public double tf2perc;
        public double tf3perc;
        //  public double tf3a6;
        //  public string tf2rezS;
        //  public string tf3rezS;

        DataSeries stf1, stf2, stf3;
        Bars bar1, bar2, bar3;
        protected override void Initialize()
        {
            bar1 = MarketData.GetBars(tf1);
            bar2 = MarketData.GetBars(tf2);
            bar3 = MarketData.GetBars(tf3);

            stf1 = bar1.ClosePrices;
            stf2 = bar2.ClosePrices;
            stf3 = bar3.ClosePrices;

            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-------------
            RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\asa", true);

            if (RunningMode != RunningMode.Optimization)
            {
                while (double.IsNaN(bar1.ClosePrices[bar1.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])]))
                {
                    bar1.LoadMoreHistory();
                }
                while (double.IsNaN(bar2.ClosePrices[bar2.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])]))
                {
                    bar2.LoadMoreHistory();
                }
                while (double.IsNaN(bar3.ClosePrices[bar3.OpenTimes.GetIndexByTime(Bars.OpenTimes[index])]))
                {
                    bar3.LoadMoreHistory();
                }
            }


            var charttime = MarketSeries.OpenTime[index];
            //var charttime = MarketSeries.TimeFrame.OpenTime[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 tf1a0 = (Math.Round(temaA.Last(0), 2));
            double tf1a6 = 0;
            string tf1rez = "";

            for (int i = 1; i <= 15; i++)
            {
                tf1a6 = (Math.Round(temaA.Last(i), 2));
                if (tf1a6 != tf1a0)
                {
                    tf1perc = Math.Round(1 * (tf1a0 - tf1a6), 2);
                    var tf11 = tf1perc;
                    key.SetValue("tf11", tf11);
                    tf1rez = "tf1(" + i + ") " + tf1a6 + "   (0) " + tf1a0 + " i=" + i + "  " + tf1perc + " %   ";
                    break;
                }
            }

//-----calc tf2-------------------
            double tf2a0 = (Math.Round(temaB.Last(0), 2));
            double tf2a6 = 0;
            string tf2rez = "";

            for (int i = 1; i <= 15; i++)
            {
                tf2a6 = (Math.Round(temaB.Last(i), 2));
                if (tf2a6 != tf2a0)
                {
                    tf2perc = Math.Round(1 * (tf2a0 - tf2a6), 2);
                    var tf22 = tf2perc;
                    key.SetValue("tf22", tf22);
                    tf2rez = "tf2(" + i + ") " + tf2a6 + "   (0) " + tf2a0 + " i=" + i + "  " + tf2perc + " %   ";
                    break;
                }
            }

//-----calc tf3-------------------
            double tf3a0 = (Math.Round(temaC.Last(0), 2));
            double tf3a6 = 0;
            string tf3rez = "";

            for (int i = 1; i <= 15; i++)
            {
                tf3a6 = (Math.Round(temaC.Last(i), 2));
                if (tf3a6 != tf3a0)
                {
                    PMA = (tf1perc + tf2perc + tf3perc);
                    tf3perc = Math.Round(1 * (tf3a0 - tf3a6), 2);
                    var tf33 = tf3perc;
                    key.SetValue("tf33", tf33);
                    key.SetValue("PMA", PMA);
                    tf3rez = "tf3(" + i + ") " + tf3a6 + "   (0) " + tf3a0 + " i=" + i + "  " + tf3perc + " %   " + "\n\t\t\t_triCOPY2 PMA. =" + PMA;
                    break;
                }
            }
            key.Close();
//--end tf3---------------------

// PERCENT    //  var diffPerc2FR = Math.Round(10000 * (tf2diff / tf20), 3);
            //  double tf1a0 = (Math.Round(tema.Last(0), 2));

            if (RunningMode != RunningMode.Optimization)
            {
                Chart.DrawStaticText("tf1", " " + tf1rez, VerticalAlignment.Top, HorizontalAlignment.Center, Color.Yellow);
                Chart.DrawStaticText("tf2", "\n " + tf2rez, VerticalAlignment.Top, HorizontalAlignment.Center, Color.Red);
                Chart.DrawStaticText("tf3", "\n\n " + tf3rez, VerticalAlignment.Top, HorizontalAlignment.Center, Color.Aqua);
            }
        }
    }
}

 

------Bot----------

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using Microsoft.Win32;
namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        private TEMA_triCOPY2 _tema;

        double PMA = 0;
        double tf11 = 0;
        double tf22 = 0;
        double tf33 = 0;

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


//------onTick-----
        protected override void OnTick()
        {
            //  double PMA = 0;
            //  double tf11 = 0;
            //  double tf22 = 0;
            //  double tf33 = 0;

            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);

            using (RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\asa"))
            {
                try
                {
                    PMA = Convert.ToDouble(key.GetValue("PMA"));
                    tf11 = Convert.ToDouble(key.GetValue("tf11"));
                    tf22 = Convert.ToDouble(key.GetValue("tf22"));
                    tf33 = Convert.ToDouble(key.GetValue("tf33"));
                    //   Print("--PMA " + PMA + "  " + tf11 + "%  " + tf22 + "%  " + "%  " + tf33 + "%  ");
                    //  PMA = tf11 + tf22 + tf33;
                } catch (Exception e)
                {
                    Print("EXEpt   ", e);
                }
            }

            if (RunningMode != RunningMode.Optimization)
            {
                Chart.DrawStaticText("tf1", "BOT." + "  " + tf11, VerticalAlignment.Top, HorizontalAlignment.Right, Color.Yellow);
                Chart.DrawStaticText("tf2", "\nBOT." + "  " + tf22, VerticalAlignment.Top, HorizontalAlignment.Right, Color.Red);
                Chart.DrawStaticText("tf3", "\n\nBOT. " + "  " + tf33, VerticalAlignment.Top, HorizontalAlignment.Right, Color.Aqua);
                Chart.DrawStaticText("PMA", "\n\n\nPMA. " + PMA, VerticalAlignment.Top, HorizontalAlignment.Right, Color.Aqua);
            }

        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

---------end-----------


@office3131
Replies

genappsforex
17 May 2020, 13:49

RE:

I think the update and cache methods of the registry are not designed for this kind of use;
Have you tried just ordinairy share fileaccess with readonly open. If you use it like this it'll stay in cache probably the whole time so not much speedloss there.

 


@genappsforex

office3131
17 May 2020, 14:02

RE: RE:thanks for answering

genappsforex said:

I think the update and cache methods of the registry are not designed for this kind of use;
Have you tried just ordinairy share fileaccess with readonly open. If you use it like this it'll stay in cache probably the whole time so not much speedloss there.

 

Hello, 

Its a little bit unclair, what do you mean by "" ordinairy share fileaccess with readonly open"?

Can you give me some small example? Thank you 


@office3131