How to get outside regular trading hours data? (OpenApiPy)
Created at 10 Mar 2022, 11:27
PR
How to get outside regular trading hours data? (OpenApiPy)
10 Mar 2022, 11:27
I have this code (from jupyter sample):
from ctrader_open_api import Client, Protobuf, TcpProtocol, Auth, EndPoints from ctrader_open_api.messages.OpenApiCommonMessages_pb2 import * from ctrader_open_api.messages.OpenApiCommonMessages_pb2 import * from ctrader_open_api.messages.OpenApiMessages_pb2 import * from ctrader_open_api.messages.OpenApiModelMessages_pb2 import * from twisted.internet import reactor import json import datetime
credentialsFile = open("credentials-dev.json") credentials = json.load(credentialsFile)
host = EndPoints.PROTOBUF_LIVE_HOST if credentials["HostType"].lower() == "live" else EndPoints.PROTOBUF_DEMO_HOST client = Client(host, EndPoints.PROTOBUF_PORT, TcpProtocol)
symbolName = "GERMANY 40"
dailyBars = []
def transformTrendbar(trendbar): openTime = datetime.datetime.fromtimestamp(trendbar.utcTimestampInMinutes * 60) openPrice = (trendbar.low + trendbar.deltaOpen) / 100000.0 highPrice = (trendbar.low + trendbar.deltaHigh) / 100000.0 lowPrice = trendbar.low / 100000.0 closePrice = (trendbar.low + trendbar.deltaClose) / 100000.0 return [openTime, openPrice, highPrice, lowPrice, closePrice, trendbar.volume]
def trendbarsResponseCallback(result): print("\nTrendbars received") trendbars = Protobuf.extract(result) barsData = list(map(transformTrendbar, trendbars.trendbar)) global dailyBars dailyBars.clear() dailyBars.extend(barsData) print("\ndailyBars length:", len(dailyBars)) print("\Stopping reactor...") reactor.stop() def symbolsResponseCallback(result): print("\nSymbols received") symbols = Protobuf.extract(result) global symbolName symbolsFilterResult = list(filter(lambda symbol: symbol.symbolName == symbolName, symbols.symbol)) if len(symbolsFilterResult) == 0: raise Exception(f"There is symbol that matches to your defined symbol name: {symbolName}") elif len(symbolsFilterResult) > 1: raise Exception(f"More than one symbol matched with your defined symbol name: {symbolName}, match result: {symbolsFilterResult}") symbol = symbolsFilterResult[0] request = ProtoOAGetTrendbarsReq() request.symbolId = symbol.symbolId request.ctidTraderAccountId = credentials["AccountId"] request.period = ProtoOATrendbarPeriod.H1 # We set the from/to time stamps to 50 weeks, you can load more data by sending multiple requests # Please check the ProtoOAGetTrendbarsReq documentation for more detail request.fromTimestamp = int((datetime.datetime.utcnow() - datetime.timedelta(weeks=1)).timestamp()) * 1000 request.toTimestamp = int(datetime.datetime.utcnow().timestamp()) * 1000 deferred = client.send(request) deferred.addCallbacks(trendbarsResponseCallback, onError) def accountAuthResponseCallback(result): print("\nAccount authenticated") request = ProtoOASymbolsListReq() request.ctidTraderAccountId = credentials["AccountId"] request.includeArchivedSymbols = False deferred = client.send(request) deferred.addCallbacks(symbolsResponseCallback, onError)
def applicationAuthResponseCallback(result): print("\nApplication authenticated") request = ProtoOAAccountAuthReq() request.ctidTraderAccountId = credentials["AccountId"] request.accessToken = credentials["AccessToken"] deferred = client.send(request) deferred.addCallbacks(accountAuthResponseCallback, onError) def onError(client, failure): # Call back for errors print("\nMessage Error: ", failure) def disconnected(client, reason): # Callback for client disconnection print("\nDisconnected: ", reason) def onMessageReceived(client, message): # Callback for receiving all messages if message.payloadType in [ProtoHeartbeatEvent().payloadType, ProtoOAAccountAuthRes().payloadType, ProtoOAApplicationAuthRes().payloadType, ProtoOASymbolsListRes().payloadType, ProtoOAGetTrendbarsRes().payloadType]: return print("\nMessage received: \n", Protobuf.extract(message)) def connected(client): # Callback for client connection print("\nConnected") request = ProtoOAApplicationAuthReq() request.clientId = credentials["ClientId"] request.clientSecret = credentials["Secret"] deferred = client.send(request) deferred.addCallbacks(applicationAuthResponseCallback, onError) # Setting optional client callbacks client.setConnectedCallback(connected) client.setDisconnectedCallback(disconnected) client.setMessageReceivedCallback(onMessageReceived)
# Starting the client service client.startService() # Run Twisted reactor, we imported it earlier reactor.run()
df = pd.DataFrame(np.array(dailyBars), columns=['Time', 'Open', 'High', 'Low', 'Close', 'Volume']) df["Open"] = pd.to_numeric(df["Open"]) df["High"] = pd.to_numeric(df["High"]) df["Low"] = pd.to_numeric(df["Low"]) df["Close"] = pd.to_numeric(df["Close"]) df["Volume"] = pd.to_numeric(df["Volume"])
df
I get values starting with "03:00:00" or "08:00:00", but I need data starting from 00:00:00. How can I get these data?
prenven570
10 Mar 2022, 13:00
RE:
prenven570 said:
(solved)
It depends by the broker you use
@prenven570