Skip to content

Repository files navigation

OpenApiPy

PyPI versionversionsGitHub license

A Python package for interacting with cTrader Open API.

This package uses Twisted and it works asynchronously.

Features

  • Works asynchronously by using Twisted

  • Methods return Twisted deferreds

  • It contains the Open API messages files so you don't have to do the compilation

  • Makes handling request responses easy by using Twisted deferreds

Insallation

pip install ctrader-open-api

Usage

fromctrader_open_apiimportClient, Protobuf, TcpProtocol, Auth, EndPointsfromctrader_open_api.messages.OpenApiCommonMessages_pb2import*fromctrader_open_api.messages.OpenApiCommonMessages_pb2import*fromctrader_open_api.messages.OpenApiMessages_pb2import*fromctrader_open_api.messages.OpenApiModelMessages_pb2import*fromtwisted.internetimportreactorhostType=input("Host (Live/Demo): ")
host=EndPoints.PROTOBUF_LIVE_HOSTifhostType.lower() =="live"elseEndPoints.PROTOBUF_DEMO_HOSTclient=Client(host, EndPoints.PROTOBUF_PORT, TcpProtocol)
defonError(failure): # Call back for errorsprint("Message Error: ", failure)
defconnected(client): # Callback for client connectionprint("\nConnected")
# Now we send a ProtoOAApplicationAuthReqrequest=ProtoOAApplicationAuthReq()
request.clientId="Your application Client ID"request.clientSecret="Your application Client secret"# Client send method returns a Twisted deferreddeferred=client.send(request)
# You can use the returned Twisted deferred to attach callbacks# for getting message response or error backs for getting error if something went wrong# deferred.addCallbacks(onProtoOAApplicationAuthRes, onError)deferred.addErrback(onError)
defdisconnected(client, reason): # Callback for client disconnectionprint("\nDisconnected: ", reason)
defonMessageReceived(client, message): # Callback for receiving all messagesprint("Message received: \n", Protobuf.extract(message))
# Setting optional client callbacksclient.setConnectedCallback(connected)
client.setDisconnectedCallback(disconnected)
client.setMessageReceivedCallback(onMessageReceived)
# Starting the client serviceclient.startService()
# Run Twisted reactorreactor.run()

Please check documentation or samples for a complete example.

Dependencies

About

A Python package for interacting with cTrader Open API

Resources

Contributing

Stars

188 stars

Watchers

10 watching

Forks

Releases

Used by

Contributors

Languages