Problem with multiple requests

Created at 11 Jan 2022, 17:24
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!
TI

tippy

Joined 02.07.2021

Problem with multiple requests
11 Jan 2022, 17:24


Hi,

I have a problem with receiving responses from multiple requests in Python (eg. ProtoOAGetTrendbarsReq and ProtoOAGetTrendbarsRes). The example on the Github works fine with a single request (https://github.com/spotware/OpenApiPy/blob/main/samples/ConsoleSample/main.py). But, if you add multiple requests it is messing up and onMessageReceived callback does not work properly anymore. To get it just duplicate row 284 to execute command "commands[command](*parameters)" twice. Any recommendations?


@tippy
Replies

amusleh
12 Jan 2022, 09:12

Hi,

I just tested the console app, I created a new copy of sendProtoOAGetTrendbarsReq method and called it from original one, the code looks like this (line 186):

    def sendProtoOAGetTrendbarsReq(weeks, period, symbolId, clientMsgId = None):
        request = ProtoOAGetTrendbarsReq()
        request.ctidTraderAccountId = currentAccountId
        request.period = ProtoOATrendbarPeriod.Value(period)
        request.fromTimestamp = int((datetime.datetime.utcnow() - datetime.timedelta(weeks=int(weeks))).timestamp()) * 1000
        request.toTimestamp = int(datetime.datetime.utcnow().timestamp()) * 1000
        request.symbolId = int(symbolId)
        deferred = client.send(request, clientMsgId = clientMsgId)
        deferred.addErrback(onError)
        sendProtoOAGetTrendbarsReq2(weeks, period, symbolId, clientMsgId)

    def sendProtoOAGetTrendbarsReq2(weeks, period, symbolId, clientMsgId = None):
        request = ProtoOAGetTrendbarsReq()
        request.ctidTraderAccountId = currentAccountId
        request.period = ProtoOATrendbarPeriod.Value(period)
        request.fromTimestamp = int((datetime.datetime.utcnow() - datetime.timedelta(weeks=int(weeks))).timestamp()) * 1000
        request.toTimestamp = int(datetime.datetime.utcnow().timestamp()) * 1000
        request.symbolId = int(symbolId)
        deferred = client.send(request, clientMsgId = clientMsgId)
        deferred.addErrback(onError)

Then I sent a ProtoOAGetTrendbarsReq with these parameters: 1 D1 1

Here is the response:

Message received: 
 ctidTraderAccountId: 22415253
period: D1
timestamp: 1641938400000
trendbar {
  volume: 90020
  low: 112770
  deltaOpen: 69
  deltaClose: 355
  deltaHigh: 694
  utcTimestampInMinutes: 27355560
}
trendbar {
  volume: 98597
  low: 112847
  deltaOpen: 273
  deltaClose: 139
  deltaHigh: 471
  utcTimestampInMinutes: 27357000
}
trendbar {
  volume: 85986
  low: 112901
  deltaOpen: 63
  deltaClose: 712
  deltaHigh: 747
  utcTimestampInMinutes: 27358440
}
trendbar {
  volume: 91747
  low: 112851
  deltaOpen: 709
  deltaClose: 430
  deltaHigh: 757
  utcTimestampInMinutes: 27362760
}
trendbar {
  volume: 87745
  low: 113130
  deltaOpen: 122
  deltaClose: 555
  deltaHigh: 621
  utcTimestampInMinutes: 27364200
}
symbolId: 1



Message received: 
 ctidTraderAccountId: 22415253
period: D1
timestamp: 1641938400000
trendbar {
  volume: 90020
  low: 112770
  deltaOpen: 69
  deltaClose: 355
  deltaHigh: 694
  utcTimestampInMinutes: 27355560
}
trendbar {
  volume: 98597
  low: 112847
  deltaOpen: 273
  deltaClose: 139
  deltaHigh: 471
  utcTimestampInMinutes: 27357000
}
trendbar {
  volume: 85986
  low: 112901
  deltaOpen: 63
  deltaClose: 712
  deltaHigh: 747
  utcTimestampInMinutes: 27358440
}
trendbar {
  volume: 91747
  low: 112851
  deltaOpen: 709
  deltaClose: 430
  deltaHigh: 757
  utcTimestampInMinutes: 27362760
}
trendbar {
  volume: 87745
  low: 113130
  deltaOpen: 122
  deltaClose: 555
  deltaHigh: 621
  utcTimestampInMinutes: 27364200
}
symbolId: 1

As you can see I received two responses.


@amusleh

tippy
13 Jan 2022, 01:40

RE:

thanks for response. I have noticed that first run is ok, problem occurs after another execution. Could you try execute sendProtoOAGetTrendbarsReq multiple times and check if it is ok?

 

I have another issue, is it normal that some of  trendbars are missing in historical data for EURUSD on M1? I am using a demo account from the IC Markets.


@tippy

amusleh
13 Jan 2022, 09:03

RE: RE:

tippy said:

thanks for response. I have noticed that first run is ok, problem occurs after another execution. Could you try execute sendProtoOAGetTrendbarsReq multiple times and check if it is ok?

 

I have another issue, is it normal that some of  trendbars are missing in historical data for EURUSD on M1? I am using a demo account from the IC Markets.

Hi,

I tested sendProtoOAGetTrendbarsReq by calling it 5 times in a row, here is the result:

Message received:
 ctidTraderAccountId: 22415253
period: D1
timestamp: 1642024800000
trendbar {
  volume: 98597
  low: 112847
  deltaOpen: 273
  deltaClose: 139
  deltaHigh: 471
  utcTimestampInMinutes: 27357000
}
trendbar {
  volume: 85986
  low: 112901
  deltaOpen: 63
  deltaClose: 712
  deltaHigh: 747
  utcTimestampInMinutes: 27358440
}
trendbar {
  volume: 91747
  low: 112851
  deltaOpen: 709
  deltaClose: 430
  deltaHigh: 757
  utcTimestampInMinutes: 27362760
}
trendbar {
  volume: 87745
  low: 113130
  deltaOpen: 122
  deltaClose: 555
  deltaHigh: 621
  utcTimestampInMinutes: 27364200
}
trendbar {
  volume: 94871
  low: 113547
  deltaOpen: 126
  deltaClose: 866
  deltaHigh: 980
  utcTimestampInMinutes: 27365640
}
symbolId: 1


Message received:
 ctidTraderAccountId: 22415253
period: D1
timestamp: 1642024800000
trendbar {
  volume: 98597
  low: 112847
  deltaOpen: 273
  deltaClose: 139
  deltaHigh: 471
  utcTimestampInMinutes: 27357000
}
trendbar {
  volume: 85986
  low: 112901
  deltaOpen: 63
  deltaClose: 712
  deltaHigh: 747
  utcTimestampInMinutes: 27358440
}
trendbar {
  volume: 91747
  low: 112851
  deltaOpen: 709
  deltaClose: 430
  deltaHigh: 757
  utcTimestampInMinutes: 27362760
}
trendbar {
  volume: 87745
  low: 113130
  deltaOpen: 122
  deltaClose: 555
  deltaHigh: 621
  utcTimestampInMinutes: 27364200
}
trendbar {
  volume: 94871
  low: 113547
  deltaOpen: 126
  deltaClose: 866
  deltaHigh: 980
  utcTimestampInMinutes: 27365640
}
symbolId: 1

Message received:
 ctidTraderAccountId: 22415253
period: D1
timestamp: 1642024800000
trendbar {
  volume: 98597
  low: 112847
  deltaOpen: 273
  deltaClose: 139
  deltaHigh: 471
  utcTimestampInMinutes: 27357000
}
trendbar {
  volume: 85986
  low: 112901
  deltaOpen: 63
  deltaClose: 712
  deltaHigh: 747
  utcTimestampInMinutes: 27358440
}
trendbar {
  volume: 91747
  low: 112851
  deltaOpen: 709
  deltaClose: 430
  deltaHigh: 757
  utcTimestampInMinutes: 27362760
}
trendbar {
  volume: 87745
  low: 113130
  deltaOpen: 122
  deltaClose: 555
  deltaHigh: 621
  utcTimestampInMinutes: 27364200
}
trendbar {
  volume: 94871
  low: 113547
  deltaOpen: 126
  deltaClose: 866
  deltaHigh: 980
  utcTimestampInMinutes: 27365640
}
symbolId: 1

Message received:
 ctidTraderAccountId: 22415253
period: D1
timestamp: 1642024800000
trendbar {
  volume: 98597
  low: 112847
  deltaOpen: 273
  deltaClose: 139
  deltaHigh: 471
  utcTimestampInMinutes: 27357000
}
trendbar {
  volume: 85986
  low: 112901
  deltaOpen: 63
  deltaClose: 712
  deltaHigh: 747
  utcTimestampInMinutes: 27358440
}
trendbar {
  volume: 91747
  low: 112851
  deltaOpen: 709
  deltaClose: 430
  deltaHigh: 757
  utcTimestampInMinutes: 27362760
}
trendbar {
  volume: 87745
  low: 113130
  deltaOpen: 122
  deltaClose: 555
  deltaHigh: 621
  utcTimestampInMinutes: 27364200
}
trendbar {
  volume: 94871
  low: 113547
  deltaOpen: 126
  deltaClose: 866
  deltaHigh: 980
  utcTimestampInMinutes: 27365640
}
symbolId: 1

Message received:
 ctidTraderAccountId: 22415253
period: D1
timestamp: 1642024800000
trendbar {
  volume: 98597
  low: 112847
  deltaOpen: 273
  deltaClose: 139
  deltaHigh: 471
  utcTimestampInMinutes: 27357000
}
trendbar {
  volume: 85986
  low: 112901
  deltaOpen: 63
  deltaClose: 712
  deltaHigh: 747
  utcTimestampInMinutes: 27358440
}
trendbar {
  volume: 91747
  low: 112851
  deltaOpen: 709
  deltaClose: 430
  deltaHigh: 757
  utcTimestampInMinutes: 27362760
}
trendbar {
  volume: 87745
  low: 113130
  deltaOpen: 122
  deltaClose: 555
  deltaHigh: 621
  utcTimestampInMinutes: 27364200
}
trendbar {
  volume: 94871
  low: 113547
  deltaOpen: 126
  deltaClose: 866
  deltaHigh: 980
  utcTimestampInMinutes: 27365640
}
symbolId: 1

The code I used:

    def sendProtoOAGetTrendbarsReq(weeks, period, symbolId, clientMsgId = None, numberOfCalls = 0):
        if numberOfCalls == 5:
            return
        request = ProtoOAGetTrendbarsReq()
        request.ctidTraderAccountId = currentAccountId
        request.period = ProtoOATrendbarPeriod.Value(period)
        request.fromTimestamp = int((datetime.datetime.utcnow() - datetime.timedelta(weeks=int(weeks))).timestamp()) * 1000
        request.toTimestamp = int(datetime.datetime.utcnow().timestamp()) * 1000
        request.symbolId = int(symbolId)
        deferred = client.send(request, clientMsgId = clientMsgId)
        deferred.addErrback(onError)
        numberOfCalls += 1
        sendProtoOAGetTrendbarsReq(weeks, period, symbolId, clientMsgId, numberOfCalls)

Regarding missing data, no, its not normal, most probably something is wrong with your code.

Please check the limits on documentation: https://spotware.github.io/open-api-docs/messages/#protooagettrendbarsreq

If you ask for more data than allowed you have to send multiple requests.


@amusleh

tippy
13 Jan 2022, 12:07

RE: RE: RE:

ok, I have checked it. That recurrency works fine for the first time and response comes immediately. Run it again from the menu and response does come randomly.

 

Regarding missing trendbars please try this one:

Execution: ProtoOAGetTrendbarsReq 1 M1 1    

def sendProtoOAGetTrendbarsReq(weeks, period, symbolId, clientMsgId = None):
        request = ProtoOAGetTrendbarsReq()
        request.ctidTraderAccountId = currentAccountId
        request.period = ProtoOATrendbarPeriod.Value(period)
        request.fromTimestamp = 1578362820*1000  # 2020-01-07 02:07:00 GMT
        request.toTimestamp = 1578363000*1000  # 2020-01-07 02:10:00 GMT
        request.symbolId = int(symbolId)
        deferred = client.send(request, clientMsgId = clientMsgId)
        deferred.addErrback(onError)

The "2020-01-07 02:09:00" is missing in my response.

Message received:
period: M1
timestamp: 1578363000000
trendbar {
  volume: 40
  low: 111925
  deltaOpen: 4
  deltaClose: 0
  deltaHigh: 5
  utcTimestampInMinutes: 26306047 //<- 2020-01-07 02:07:00
}
trendbar {
  volume: 14
  low: 111924
  deltaOpen: 2
  deltaClose: 0
  deltaHigh: 2
  utcTimestampInMinutes: 26306048 //<- 2020-01-07 02:08:00
}
trendbar {
  volume: 20
  low: 111924
  deltaOpen: 1
  deltaClose: 1
  deltaHigh: 1
  utcTimestampInMinutes: 26306050 //<- 2020-01-07 02:10:00
}
symbolId: 1

 


@tippy

amusleh
13 Jan 2022, 12:49

Hi,

I send several requests by using console app and it's working fine on my system:

 


@amusleh

tippy
13 Jan 2022, 13:08

RE:

ok, it seems to work well on your system.

 

Could you please check missing trendbar with timestamps?

        request.fromTimestamp = 1578362820*1000  # 2020-01-07 02:07:00 GMT
        request.toTimestamp = 1578363000*1000  # 2020-01-07 02:10:00 GMT

@tippy

amusleh
13 Jan 2022, 14:47

RE: RE:

tippy said:

ok, it seems to work well on your system.

 

Could you please check missing trendbar with timestamps?

        request.fromTimestamp = 1578362820*1000  # 2020-01-07 02:07:00 GMT
        request.toTimestamp = 1578363000*1000  # 2020-01-07 02:10:00 GMT

The bar data can be missed if there was no incoming price during that bar period, so it looks like the price didn't changed from previous bar close to this bar close.


@amusleh