Issue with GetIndexByTime

Created at 04 Apr 2020, 20:31
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!
AG

agent.xzxz

Joined 04.04.2020

Issue with GetIndexByTime
04 Apr 2020, 20:31


  

 

Hi,

 

 

When  I put the last bar time in GetIndexByTime like this:

int index = Bars.OpenTimes.GetIndexByTime(Bars.OpenTimes.LastValue);

and get the opening price of that bar like this:

Bars.OpenPrices.Last(index);

the price is not accurate, maybe because the index is not correct. The index should be zero since that's how the ctrader reads it(last bar is index zero)

 

 

 

Can someone help me with this?

 

 

 

 

Thank you.


@agent.xzxz
Replies

PanagiotisCharalampous
06 Apr 2020, 09:00

Hi agent.xzxz,

Can you provide us an example demonstrating this issue?

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

agent.xzxz
06 Apr 2020, 16:14

RE:

Hi,

 

Can you run this code on your side? Select Monthly Timeframe.

 

using System;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class IndexByTimeIssue : Robot
    {
        protected override void OnStart()
        {
            double lastBarOpenPrice = Bars.OpenPrices.LastValue;
            DateTime lastBarOpenTime = Bars.OpenTimes.LastValue;

            int index = Bars.OpenTimes.GetIndexByTime(lastBarOpenTime);

            double openPriceByIndex = Bars.OpenPrices.Last(index);
            Print("Equal?: ", lastBarOpenPrice == openPriceByIndex);
        }

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

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

 

Why lastBarOpenPrice is not equal to openPriceByIndex?

 

I only get the index by time right? They should be equal.


@agent.xzxz

PanagiotisCharalampous
07 Apr 2020, 08:42

Hi agent.xzxz,

You need to change 

double openPriceByIndex = Bars.OpenPrices.Last(index);

to

 double openPriceByIndex = Bars.OpenPrices[index];

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

agent.xzxz
08 Apr 2020, 19:52

RE:

PanagiotisCharalampous said:

Hi agent.xzxz,

You need to change 

double openPriceByIndex = Bars.OpenPrices.Last(index);

to

 double openPriceByIndex = Bars.OpenPrices[index];

Best Regards,

Panagiotis 

Join us on Telegram

Thank you!


@agent.xzxz