Why does it calculate the print twice on each index?

Created at 24 Apr 2023, 20:21
LU

Luiss

Joined 18.04.2023

Why does it calculate the print twice on each index?
24 Apr 2023, 20:21


It’s the indicator I use, a simple moving average. In which I have a print.

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 _Simple_Moving_Average : Indicator
    {
        [Parameter(DefaultValue = 14)]
        public int period { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        public double sum;
   
        protected override void Initialize()
        {
            // Initialize and create nested indicators
   
        }

        public override void Calculate(int index)
        {
            // Calculate value at specified index
            // Result[index] = ...
            sum = 0;

            for (int i = 0; i < period; i++)
            {
                sum += (Bars.HighPrices[index - i] + Bars.LowPrices[index - i]) / 2;

            }

            Print("Result[index]= " + sum / period + "    Index: " + index + "    Time: " + Bars.OpenTimes);

            Result[index] = sum / period;
        }
    }
}

And this is the bot, where I’m calling from.

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Proof : Robot
    {

        [Parameter(DefaultValue = 14)]
        public int period { get; set; }


        private _Simple_Moving_Average sma;

        double valor;


        protected override void OnStart()
        {
            // Put your initialization logic here

            sma = Indicators.GetIndicator<_Simple_Moving_Average>(period);
        }

        protected override void OnBar()
        {
            // Put your core logic here
            Print("sma: " + sma.Result.Last(0));
            valor = sma.Result.Last(0);
        }

        protected override void OnTick()
        {
            // Put your core logic here

        }

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

Here goes part of the output:

As you see, it calculates on each bar, twice the Result indicator. Does anyone know what is happening?

 

 


@Luiss
Replies

firemyst
25 Apr 2023, 09:20

When you do this:

valor = sma.Result.Last(0);

it calls the Calculate method in the indicator.

You have a print statement in that method in your indicator.


@firemyst