Wrong data when calling custom indicator from cBot

Created at 19 Sep 2022, 23:41
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!
EY

eynt

Joined 08.05.2020

Wrong data when calling custom indicator from cBot
19 Sep 2022, 23:41


Hello

 

While upgrading my bots and indicators from .net 4 to 6 I've noticed there was a problem when I called an indicator form a bot.

Basically, after calling the Bars.LoadMoreHistory() from a bot, the data which is stored in the Bars of the indicator is incorrect.

In order to illustrate that I've created a sample bot+indicator. The bot loads more history and then call the indicator to print the data of the first bar (date+high).

Notice for example that on SpotCrude 15/09/2022 15:44:26 (15 of September, time region is UTC+3, and the bot was running on 1 pip range live chart) the High printed by the indicator is 85.279 although as can be seen in the image it should be 87.250. This is just one example, the entire data printed is wrong.

In my real bot and indicator, I load history at the OnStart and then call methods from the indicator which calculates things according to the Highs/Lows. However, since this data is false all the calculations are incorrect. I've noticed that if I start my bot, stop it after a while and then restart it things are working well. I'm guessing it's related to something which i noticed and that is that when a bot is calling LoadMoreHistory, then stopped and then rerun, the data form the first run is already loaded.

Another important thing which might help. At .Net 4 I believe I had the same problem or something similar to that. What I did was Adding the following Refresh method to the indicator and then calling it from the bot before using the custom indicator. This work-around does not work on .net 6 neither calling RefreshData from the bot.

 

public void Refresh()
        {
                double d = this.Result.LastValue;
}

Please help on the subject

 

 

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

namespace cAlgo
{
    [Indicator(AccessRights = AccessRights.None)]
    public class PrintInd : Indicator
    {
        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        protected override void Initialize()
        {
        }

        public override void Calculate(int index)
        {
        }


        public void xxx()
        {
            int index = 0;

            Print(index + " " + Bars.HighPrices[index] + " " + Bars.OpenTimes[index] + " " + Bars.Count); // 0 > present
        }
    }
}

 

 

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

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = "Hello world!")]
        public string Message { get; set; }

        protected override void OnStart()
        {
            DateTime startDate = new DateTime(2022, 6, 28);
            PrintInd p = Indicators.GetIndicator<PrintInd>();

            Print("a " + Bars.Count + " " + Bars.OpenTimes[0] + " = " + startDate);

            while (Bars.OpenTimes[0] > startDate)
            {
                //Print("b " + Bars.Count + " " + Bars.OpenTimes[0] + " = " + startDate);
                p.xxx();
                Bars.LoadMoreHistory();
            }
            Print("c " + Bars.Count + " " + Bars.OpenTimes[0] + " = " + startDate);
        }

        protected override void OnTick()
        {
            // Handle price updates here



        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}
19/09/2022 23:06:04.401 | CBot instance [New cBot, SpotCrude, Ra1] started.
19/09/2022 23:06:05.558 | a 1001 19/09/2022 18:17:05 = 28/06/2022 00:00:00
19/09/2022 23:06:05.573 | 0 85.789 19/09/2022 18:17:05 1001
19/09/2022 23:06:05.917 | 0 85.452 19/09/2022 17:06:27 2001
19/09/2022 23:06:06.292 | 0 85.693 19/09/2022 16:17:43 3001
19/09/2022 23:06:06.480 | 0 84.833 19/09/2022 15:37:32 4001
19/09/2022 23:06:06.698 | 0 85.08 19/09/2022 15:05:47 5001
19/09/2022 23:06:06.886 | 0 84.903 19/09/2022 14:36:49 6001
19/09/2022 23:06:07.183 | 0 84.546 19/09/2022 14:14:04 7001
19/09/2022 23:06:07.417 | 0 84.234 19/09/2022 13:52:32 8001
19/09/2022 23:06:07.605 | 0 82.735 19/09/2022 13:32:56 9001
19/09/2022 23:06:08.011 | 0 82.581 19/09/2022 13:07:52 10001
19/09/2022 23:06:08.448 | 0 82.36 19/09/2022 12:35:01 11001
19/09/2022 23:06:08.761 | 0 82.964 19/09/2022 11:36:47 12001
19/09/2022 23:06:09.183 | 0 83.769 19/09/2022 10:28:21 13001
19/09/2022 23:06:09.464 | 0 84.131 19/09/2022 09:06:51 14001
19/09/2022 23:06:09.823 | 0 83.905 19/09/2022 08:20:17 15001
19/09/2022 23:06:10.245 | 0 84.488 19/09/2022 07:14:43 16001
19/09/2022 23:06:10.573 | 0 85.49 19/09/2022 05:30:26 17001
19/09/2022 23:06:10.901 | 0 86.174 19/09/2022 01:10:50 18001
19/09/2022 23:06:11.136 | 0 85.451 16/09/2022 20:03:19 19001
19/09/2022 23:06:11.339 | 0 85.554 16/09/2022 18:17:06 20001
19/09/2022 23:06:11.573 | 0 85.524 16/09/2022 17:21:34 21001
19/09/2022 23:06:11.808 | 0 85.629 16/09/2022 16:42:34 22001
19/09/2022 23:06:11.995 | 0 86.108 16/09/2022 15:58:55 23001
19/09/2022 23:06:12.214 | 0 86.561 16/09/2022 15:29:08 24002
19/09/2022 23:06:12.480 | 0 86.42 16/09/2022 15:08:11 25002
19/09/2022 23:06:12.698 | 0 85.997 16/09/2022 14:45:57 26002
19/09/2022 23:06:12.870 | 0 86.349 16/09/2022 14:25:03 27002
19/09/2022 23:06:13.058 | 0 85.896 16/09/2022 14:08:53 28002
19/09/2022 23:06:13.261 | 0 85.765 16/09/2022 13:50:34 29002
19/09/2022 23:06:13.636 | 0 85.664 16/09/2022 13:32:54 30002
19/09/2022 23:06:14.151 | 0 85.34 16/09/2022 13:12:40 31002
19/09/2022 23:06:14.511 | 0 85.158 16/09/2022 12:52:53 32002
19/09/2022 23:06:15.542 | 0 86.181 16/09/2022 12:10:00 33002
19/09/2022 23:06:16.433 | 0 85.511 16/09/2022 11:03:05 34002
19/09/2022 23:06:16.855 | 0 85.118 16/09/2022 10:02:06 35002
19/09/2022 23:06:17.198 | 0 85.017 16/09/2022 09:04:25 36002
19/09/2022 23:06:17.651 | 0 84.7 16/09/2022 08:22:24 37002
19/09/2022 23:06:18.058 | 0 85.466 16/09/2022 07:44:26 38002
19/09/2022 23:06:18.589 | 0 85.576 16/09/2022 06:45:14 39002
19/09/2022 23:06:19.214 | 0 85.309 16/09/2022 03:20:04 40002
19/09/2022 23:06:19.808 | 0 85.239 16/09/2022 01:08:15 41002
19/09/2022 23:06:20.151 | 0 85.08 15/09/2022 20:54:20 42002
19/09/2022 23:06:20.401 | 0 85.238 15/09/2022 18:35:20 43002
19/09/2022 23:06:20.558 | 0 85.379 15/09/2022 18:04:02 44002
19/09/2022 23:06:20.730 | 0 85.662 15/09/2022 17:14:04 45002
19/09/2022 23:06:20.901 | 0 85.354 15/09/2022 16:28:26 46002
19/09/2022 23:06:21.073 | 0 85.279 15/09/2022 15:44:26 47002
19/09/2022 23:06:21.230 | 0 84.961 15/09/2022 15:21:10 48002
19/09/2022 23:06:21.417 | 0 84.856 15/09/2022 14:57:50 49002
19/09/2022 23:06:21.573 | 0 85.43 15/09/2022 14:35:03 50002
19/09/2022 23:06:21.761 | 0 85.903 15/09/2022 14:09:20 51002
19/09/2022 23:06:21.933 | 0 85.873 15/09/2022 13:45:39 52002
19/09/2022 23:06:22.089 | 0 86.923 15/09/2022 13:25:32 53002
19/09/2022 23:06:22.261 | 0 87.049 15/09/2022 12:59:56 54002
19/09/2022 23:06:22.433 | 0 87.25 15/09/2022 12:19:41 55002
19/09/2022 23:06:22.605 | 0 87.432 15/09/2022 11:31:59 56002
19/09/2022 23:06:22.792 | 0 88.127 15/09/2022 10:34:00 57002
19/09/2022 23:06:22.964 | 0 88.469 15/09/2022 09:17:14 58002
19/09/2022 23:06:23.151 | 0 88.041 15/09/2022 08:32:07 59002
19/09/2022 23:06:23.355 | 0 89.073 15/09/2022 07:59:04 60002
19/09/2022 23:06:23.526 | 0 88.59 15/09/2022 06:20:53 61002
19/09/2022 23:06:23.714 | 0 88.731 15/09/2022 02:23:02 62002
19/09/2022 23:06:23.917 | 0 89.018 15/09/2022 00:38:55 63002
19/09/2022 23:06:24.089 | 0 88.358 14/09/2022 19:21:39 64002
19/09/2022 23:06:24.292 | 0 88.651 14/09/2022 18:21:31 65002
19/09/2022 23:06:24.480 | 0 89.547 14/09/2022 17:38:12 66002
19/09/2022 23:06:24.667 | 0 90.111 14/09/2022 16:42:29 67002
19/09/2022 23:06:24.855 | 0 89.336 14/09/2022 15:52:35 68002
19/09/2022 23:06:25.058 | 0 89.517 14/09/2022 15:23:34 69002
19/09/2022 23:06:25.230 | 0 89.235 14/09/2022 14:55:33 70002
19/09/2022 23:06:25.542 | 0 88.464 14/09/2022 14:33:00 71002
19/09/2022 23:06:25.730 | 0 89.436 14/09/2022 14:03:49 72002
19/09/2022 23:06:25.917 | 0 88.943 14/09/2022 13:37:48 73002
19/09/2022 23:06:26.120 | 0 88.557 14/09/2022 13:08:07 74002
19/09/2022 23:06:26.276 | 0 87.343 14/09/2022 12:36:37 75002
19/09/2022 23:06:26.464 | 0 86.759 14/09/2022 11:58:38 76002
19/09/2022 23:06:26.651 | 0 87.474 14/09/2022 11:10:38 77002
19/09/2022 23:06:26.823 | 0 87.893 14/09/2022 10:13:50 78002
19/09/2022 23:06:26.980 | 0 87.802 14/09/2022 09:15:18 79002
19/09/2022 23:06:27.151 | 0 87.358 14/09/2022 08:28:48 80002
19/09/2022 23:06:27.323 | 0 86.935 14/09/2022 07:47:07 81002
19/09/2022 23:06:27.511 | 0 86.966 14/09/2022 07:03:38 82002
19/09/2022 23:06:27.683 | 0 87.107 14/09/2022 05:32:40 83002
19/09/2022 23:06:27.870 | 0 87.792 14/09/2022 02:12:13 84002
19/09/2022 23:06:28.058 | 0 87.736 14/09/2022 00:00:19 85002
19/09/2022 23:06:28.230 | 0 87.797 13/09/2022 18:50:33 86002
19/09/2022 23:06:28.417 | 0 87.7 13/09/2022 18:17:55 87002
19/09/2022 23:06:28.605 | 0 87.47 13/09/2022 17:49:51 88002
19/09/2022 23:06:28.776 | 0 87.249 13/09/2022 17:31:10 89002
19/09/2022 23:06:28.948 | 0 86.221 13/09/2022 17:07:09 90002
19/09/2022 23:06:29.120 | 0 86.783 13/09/2022 16:24:54 91002
19/09/2022 23:06:29.292 | 0 86.547 13/09/2022 15:43:13 92002
19/09/2022 23:06:29.480 | 0 87.414 13/09/2022 15:21:14 93002
19/09/2022 23:06:29.667 | 0 86.703 13/09/2022 14:56:35 94002
19/09/2022 23:06:29.839 | 0 87.389 13/09/2022 14:37:26 95002
19/09/2022 23:06:30.011 | 0 87.725 13/09/2022 14:17:34 96002
19/09/2022 23:06:30.214 | 0 88.232 13/09/2022 13:59:57 97002
19/09/2022 23:06:30.386 | 0 88.427 13/09/2022 13:42:25 98002
19/09/2022 23:06:30.605 | 0 87.648 13/09/2022 13:25:56 99002
19/09/2022 23:06:30.855 | 0 87.277 13/09/2022 13:07:08 100002
19/09/2022 23:06:31.026 | 0 87.262 13/09/2022 12:52:41 101002
19/09/2022 23:06:31.198 | 0 88.57 13/09/2022 12:35:50 102002
19/09/2022 23:06:31.401 | 0 89.207 13/09/2022 11:55:00 103002
19/09/2022 23:06:31.558 | CBot instance [New cBot, SpotCrude, Ra1] aborted by timeout.


@eynt
Replies

eynt
21 Sep 2022, 08:47

RE:

Hello

 

Is there anything new on the subject? This issue is SUPER important, my entire project is stuck until this problem is solved


@eynt

PanagiotisCharalampous
21 Sep 2022, 09:31

Hi yuval.ein,

I cannot reproduce any problem. Why are you comparing an m1 chart to Ra1 log?

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous