Multiple values for the same time period?

Created at 14 Dec 2012, 17:59
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!
lec0456's avatar

lec0456

Joined 14.11.2012

Multiple values for the same time period?
14 Dec 2012, 17:59


I have a print statement in an indicator, calculate event:

 Print("{0,20:MM/dd/yyyy HH:mm}{1,25}{2,25}{3,25}{4,25}",opentime1,PlotCount,MA1_MA2_1,MA1Slope1,"Trend Up Buy");

 

How come when I run the robot, the log indicates 2 index values for the same time period.  in this case 14:24 has 2007 and 2008.  I have a count that simple counts up in the calculate event.

 PlotCount++;

 

But this makes no sence...

 


@lec0456
Replies

lec0456
14 Dec 2012, 22:48

Here is the robot which will print 2 indicator calculations for each on bar event
//#reference: C:\Users\lcespedes\Documents\cAlgo\Sources\Indicators\Sample SMA.algo
// -------------------------------------------------------------------------------
// -------------------------------------------------------------------------------
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
    [Robot]
    public class TestIndicatorBot : Robot
    {    
        private SampleSMA sma;
        
        protected override void OnStart()
        {
			sma = Indicators.GetIndicator<SampleSMA>(MarketSeries.Close,10);
        }
		protected override void OnBar()
        {
			if (Trade.IsExecuting) return;
//** Indicator calculations and Analysis		
			int t0 = MarketSeries.Close.Count-1;//** t0 results are not final because the bar has not completed
			int t1 = t0 - 1;
			int t2 = t1 - 1;
			
			if(t2<0){return;}//** prevent crash caused by posibly using a negetive index value
			
			DateTime opentime1 = MarketSeries.OpenTime[t1];
			
			if(double.IsNaN(sma.Result[t2])){return;} //** skip printing bar until moving average data is calculated

			decimal MA1t1 = (decimal) Math.Round(sma.Result[t1],7);
	        decimal MA1t2 = (decimal) Math.Round(sma.Result[t2],7);
			decimal MA1Slope1 = (MA1t1 - MA1t2)/(decimal)Symbol.PointSize;

if(!double.IsNaN(sma.Result[t1])){
Print("{0,20:MM/dd/yyyy HH:mm}{1,20}{2,20}{3,20}{4,20}",opentime1,t1,sma.Result[t1],MA1Slope1,"robot");
}
		}	
    }//** End of class **********
}//** End of namespace **********

 


@lec0456

lec0456
14 Dec 2012, 22:49

Here is the modified Sample SMA I am using
using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true)]
    public class SampleSMA : Indicator
    {
        [Parameter]
        public DataSeries Source { get; set; }

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

        [Output("Main", Color = Colors.Turquoise)]
        public IndicatorDataSeries Result { get; set; }
		private int PlotCount;
		
        protected override void Initialize()
        {
            PlotCount=0;
        }
        
     	public override void Calculate(int index)
        {
            double sum = 0.0;
			DateTime opentime1 = MarketSeries.OpenTime[index];
			
            for (int i = index - Periods + 1; i <= index; i++)
            {
                sum += Source[i];
            }
            Result[index] = sum / Periods;
            
            Print("{0,20:MM/dd/yyyy HH:mm}{1,20}{2,20}{3,20}{4,20:#.000000#}",opentime1,index, PlotCount, Result[index],"indicator");
            PlotCount++;
        }
    }
}

 


@lec0456

lec0456
14 Dec 2012, 23:02 ( Updated at: 21 Dec 2023, 09:20 )

Here is a better example of the problem:  by taking out the print statement in the  robot and replacing it with just printing "onBar".  You can see that at the same time interval the indicator is printing twice with different values, one before the onbar event and one after???

 


@lec0456