Cannot fetch last trendbar

Created at 11 Jul 2023, 11:33
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!
AN

antonioritucci336

Joined 15.02.2023

Cannot fetch last trendbar
11 Jul 2023, 11:33


Hi there!

I'm connecting to cTrader to openApiPy, when i call for a trendbar i got all the bars except the last one. On every timeframe i send the ProtoOAGetTrendbarsReq with toTimestamp = int(calendar.timegm(datetime.datetime.utcnow().utctimetuple())) * 1000   that is the current timestamp, i get all the bars, except the last one, is it an expected behaviour? For example calling it after 10.02AM with timeframe 30m give me as last candle the one at 9.00AM (expected 9.30AM) , calling at 10.02AM with timeframe 15m give me as last candle the one at 9.30AM (expecting 9.45AM 


@antonioritucci336
Replies

PanagiotisChar
12 Jul 2023, 10:06

Hi there,

ProtoOAGetTrendbarsReq returns only historical data. Use ProtoOASubscribeLiveTrendbarReq to get the current live trendbar.

Aieden Technologies

Need help? Join us on Telegram

 


@PanagiotisChar

antonioritucci336
12 Jul 2023, 10:38

RE:

PanagiotisChar said:

Hi there,

ProtoOAGetTrendbarsReq returns only historical data. Use ProtoOASubscribeLiveTrendbarReq to get the current live trendbar.

Aieden Technologies

Need help? Join us on Telegram

 

 

Sorry for continue asking, but for example, when is 10.02AM on timeframe 30 minutes, I thought 9.30AM-10.00AM was an historic bar and 10.00AM-10.02AM is the live bar. Isn't it correct? 


@antonioritucci336

PanagiotisChar
12 Jul 2023, 11:02

Hi there,

Indeed. Does this happen on all symbols? How can we reproduce it? Symbol? Broker?

Aieden Technologies

Need help? Join us on Telegram


@PanagiotisChar

antonioritucci336
12 Jul 2023, 11:10

RE:

PanagiotisChar said:

Hi there,

Indeed. Does this happen on all symbols? How can we reproduce it? Symbol? Broker?

Aieden Technologies

Need help? Join us on Telegram

It happens on all symbols, using cTrader Pepperstone. If necessary I'll extract a snippet from the code. Basically to reproduce I do the ProtoOAGetTrendbarsReq with toTimestamp = int(calendar.timegm(datetime.datetime.utcnow().utctimetuple())) * 1000

Thanks


@antonioritucci336

PanagiotisChar
13 Jul 2023, 08:48 ( Updated at: 21 Dec 2023, 09:23 )

Hi there,

I just tried this with the Open API .Net Sample and a Pepperstone account and looks correct to me

It seems like a bug in the Python code instead of the API itself. I hope this helps you debug this issue

Aieden Technologies

Need help? Join us on Telegram


@PanagiotisChar

antonioritucci336
13 Jul 2023, 11:57 ( Updated at: 21 Dec 2023, 09:23 )

Debugging

Everything seems right in python code:

 

    def trendbarsResponseCallback(self, result):

        # print("\nTrendbars received")

        trendbars = Protobuf.extract(result)

        print(f"Trendabars received: {trendbars}")

        barsData = list(map(self.transformTrendbar, trendbars.trendbar))

        #barsData = list(map(self.transformTrendbar, result))

        barsData = {"bars": barsData}

        return json.dumps(barsData)

   

    def sendProtoOAGetTrendbarsReq(self,

                                currentAccountId: int,

                                symbolId: int,

                                timeframe: str,

                                since_ms: int,

                                until_ms: int):

        request = ProtoOAGetTrendbarsReq()

        request.symbolId = symbolId

        request.ctidTraderAccountId = currentAccountId

        request.period = self.timeframe_to_proto_period(timeframe)

        request.fromTimestamp = since_ms

        request.toTimestamp = until_ms

        request.count = 3

        print(f"Requesting trendbars symbolId: {request.symbolId}")

        print(f"Requesting trendbars period: {request.period}")

        print(f"Requesting trendbars fromTimestamp: {request.fromTimestamp}")

        print(f"Requesting trendbars toTimestamp: {request.toTimestamp}")

        print(f"Requesting trendbars count: {request.count}")

        deferred = self.client.send(request)

        deferred.addCallbacks(self.trendbarsResponseCallback, onError)

        return deferred

 

 

this is are logs showed 

 

sending with toTimestamp 1689237969000 that is actually 10:46am

 

the last bar received is at 28153920 minutes that is 1689235200 that is 10.00am

 

 


@antonioritucci336

antonioritucci336
13 Jul 2023, 12:13

Found out my fault

Now I found out! Everything is fine, just the timestamp is the starting and not the ending timestamp of the bar so i received the 10.00am bar that is actually the last from 10.00am to 10.30am! I apologize, it is mentioned in the documentation here 

 

Thank you for your kind support!

Antonio


@antonioritucci336