OnBar() data incorrect / wrong / bad

Created at 28 Oct 2019, 09: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!
BU

buccinator

Joined 04.08.2019

OnBar() data incorrect / wrong / bad
28 Oct 2019, 09:59


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 NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

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

        protected override void OnBar()
        {
            int index = MarketSeries.Close.Count - 1;
            int Bot_IndexLast = index - 1;

            // Put your core logic here
            Print("-----------------");
            Print("MarketSeries.Close[{0}] = {1}", Bot_IndexLast, MarketSeries.Close[Bot_IndexLast]);
            Print("MarketSeries.Close[{0}] = {1}", index, MarketSeries.Close[index]);

        }

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

 

as you can see the last index doesnt match. i havent used cAlgo in a few months (mainly as my last post was never replied to). i come back to today and see new behaviour above. please advise.


@buccinator
Replies

PanagiotisCharalampous
29 Oct 2019, 08:51

Hi buccinator,

Your values do not match because you are printing the close value of the current bar which will probably change when the bar closes. See below how you should have implemented this test

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 NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

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

        protected override void OnBar()
        {
            int index = MarketSeries.Close.Count - 2;
            int Bot_IndexLast = index - 1;

            // Put your core logic here
            Print("-----------------");
            Print("MarketSeries.Close[{0}] = {1}", Bot_IndexLast, MarketSeries.Close[Bot_IndexLast]);
            Print("MarketSeries.Close[{0}] = {1}", index, MarketSeries.Close[index]);

        }

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

Best Regards,

Panagiotis


@PanagiotisCharalampous

buccinator
30 Oct 2019, 23:51

i see, my bad, thank you for the confirmation. i only raised this because i when i last coded (few months ago) i did not notice this behaviour. it seemed to be that the next/current OnBar() was already closed. i understand now that the next/current OnBar() is not closed until the next cycle. once again, thank you for the confirmation.


@buccinator