How to get the current price of an asset using openApiPy?
How to get the current price of an asset using openApiPy?
25 Feb 2022, 14:05
I call the sendProtoOASubscribeSpotsReq function after "Account authenticated",
In the output I can see the ask, bid prices and other informations, but I need to store in a variable the current price.
I'm trying to use ProtoOAGetTickDataRes payload type inside the onMessageReceived function, but it don't work,
This is my code:
def sendProtoOASubscribeSpotsReq(result):
symbolId = 22395
timeInSeconds = 2
subscribeToSpotTimestamp = False
clientMsgId = None
request = ProtoOASubscribeSpotsReq()
request.ctidTraderAccountId = currentAccountId
request.symbolId.append(int(symbolId))
request.subscribeToSpotTimestamp = subscribeToSpotTimestamp if type(subscribeToSpotTimestamp) is bool else bool(subscribeToSpotTimestamp)
deferred = client.send(request, clientMsgId = clientMsgId)
deferred.addErrback(onError)
#reactor.callLater(int(timeInSeconds), sendProtoOAUnsubscribeSpotsReq, symbolId)
def accountAuthResponseCallback(result):
print("\nAccount authenticated")
request = ProtoOASymbolsListReq()
request.ctidTraderAccountId = credentials["AccountId"]
request.includeArchivedSymbols = False
deferred = client.send(request)
deferred.addCallbacks(sendProtoOASubscribeSpotsReq, 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))
if message.payloadType == ProtoOAGetTickDataRes().payloadType:
print("Tick data received")
price_message = Protobuf.extract(message)
current_price = price_message.tickData
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)
I don' t know if i used the correct function and payload type to get the current price of an asset, my goal is to store the current price of an asset inside a variable.
Thanks
Replies
prenven570
25 Feb 2022, 19:15
RE:
amusleh said:
Hi,
You have to use ProtoOASpotEvent, you will receive that event after Spot subscription on each tick, not ProtoOAGetTickDataRes.
Thanks
@prenven570
william@hallsdesign.co.uk
04 Apr 2022, 18:09
RE: RE: Code to make this work?
prenven570 said:
amusleh said:
Hi,
You have to use ProtoOASpotEvent, you will receive that event after Spot subscription on each tick, not ProtoOAGetTickDataRes.
Thanks
Hi,
I have been going round in circles with this, and all I want is the current price of a pair I have. I have everything, all my code is placing a trade etc.. But only if I manually add the price in for my pair and run my code.
I have tried all the subscribing and ProtoOASpotEvent stuff. If you wouldnt mind helping me out with the exact code you did, that would be greatly appreciated. I just wish they would allow you to place a market order with a stopLoss and takeProfit without having to faff with all of this!!
Thanks in advance
William
@william@hallsdesign.co.uk
amusleh
05 Apr 2022, 09:44
RE: RE: RE: Code to make this work?
william@hallsdesign.co.uk said:
Hi,
I have been going round in circles with this, and all I want is the current price of a pair I have. I have everything, all my code is placing a trade etc.. But only if I manually add the price in for my pair and run my code.
I have tried all the subscribing and ProtoOASpotEvent stuff. If you wouldnt mind helping me out with the exact code you did, that would be greatly appreciated. I just wish they would allow you to place a market order with a stopLoss and takeProfit without having to faff with all of this!!Thanks in advance
William
Hi,
To receive live quotes of a symbol you have to send a ProtoOASubscribeSpotsReq, after that you will keep receiving ProtoOASpotEvent(s) which will contain the latest symbol bid/ask prices.
For code example please check the console sample of OpenApiPy: OpenApiPy/main.py at main · spotware/OpenApiPy (github.com)
For creating a new market order you have to send a ProtoOANewOrderReq: OpenApiPy/main.py at 50b00db0a2570d4cb62ab0a191501f49aa5ea191 · spotware/OpenApiPy (github.com)
You can set the stop loss and take profit in relative: Messages - cTrader Open API (spotware.github.io)
@amusleh
joel+ctrader
23 Jun 2022, 12:05
RE: RE: RE: RE: Code to make this work?
amusleh said:
william@hallsdesign.co.uk said:
Hi,
I have been going round in circles with this, and all I want is the current price of a pair I have. I have everything, all my code is placing a trade etc.. But only if I manually add the price in for my pair and run my code.
I have tried all the subscribing and ProtoOASpotEvent stuff. If you wouldnt mind helping me out with the exact code you did, that would be greatly appreciated. I just wish they would allow you to place a market order with a stopLoss and takeProfit without having to faff with all of this!!Thanks in advance
William
Hi,
To receive live quotes of a symbol you have to send a ProtoOASubscribeSpotsReq, after that you will keep receiving ProtoOASpotEvent(s) which will contain the latest symbol bid/ask prices.
For code example please check the console sample of OpenApiPy: OpenApiPy/main.py at main · spotware/OpenApiPy (github.com)
For creating a new market order you have to send a ProtoOANewOrderReq: OpenApiPy/main.py at 50b00db0a2570d4cb62ab0a191501f49aa5ea191 · spotware/OpenApiPy (github.com)
You can set the stop loss and take profit in relative: Messages - cTrader Open API (spotware.github.io)
Hi I have a question about the MARKET orders using the ProtoOANewOrderReq message.
On setting the orderType to MARKET (1), how set the stop loss and take profit? the stopLoss and stopPrice are not allowed to use.
using relativeStopLoss also gives an error: "Relative stop loss has invalid precision". (using int not float). So I have no clue how to set this up. Using the LIMIT order it works, but I want to open a MARKET order and SL and TP needs to be LIMIT orders.
I hope someone has a solution for this?
Jo
@joel+ctrader
vasile.peste
30 Jul 2022, 20:20
( Updated at: 21 Dec 2023, 09:22 )
Hi, I know it's about Python, but I just want to show how easy this could be using JavaScript/Mida.
You can check it here https://www.mida.org, there is a small community behind if you need help
@vasile.peste
amusleh
25 Feb 2022, 18:13 ( Updated at: 25 Feb 2022, 18:14 )
Hi,
You have to use ProtoOASpotEvent, you will receive that event after Spot subscription on each tick, not ProtoOAGetTickDataRes.
@amusleh