Bot development in Python for use in cTrader

Created at 03 Dec 2017, 14:04
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!
R.

r.houlahan

Joined 03.12.2017

Bot development in Python for use in cTrader
03 Dec 2017, 14:04


Hi there,

I am developing a non-C# algorithmic trading program and am wondering how I can use the Open API to interface with cTrader. I am not interested in using cAlgo to write C# bots, as the bulk of my code is written in Python. I cannot seem to find any documention on how to connect the Open API to cTrader - could someone link me any resources so that I can learn about this? 

Thanks in advance. 


@r.houlahan
Replies

ap11
04 Dec 2017, 10:44

RE:

Hi,

https://connect.spotware.com/

Here you can find documentation and examples for Open API. Unfortunately there is no examples for Python.

Kind Reagrds,
Andrey

 

r.houlahan said:

Hi there,

I am developing a non-C# algorithmic trading program and am wondering how I can use the Open API to interface with cTrader. I am not interested in using cAlgo to write C# bots, as the bulk of my code is written in Python. I cannot seem to find any documention on how to connect the Open API to cTrader - could someone link me any resources so that I can learn about this? 

Thanks in advance. 

 


@ap11

ap11
04 Dec 2017, 11:13

There is no way to create cBot using Python code. cBots can be created only using C#.

If you want to integrate python app into cTrader there are 2 possible workarounds:

  • Create .Net dll using IronPython and use it from cBot
  • Communicate with Python app from cBot

In both cases you need to create cBot using C#.

 

Best Regards,
Andrey


@ap11

depot051
20 Feb 2018, 15:00

Protocol buffer doesn't support Python properly, you have to modify it manually and install pure python protocol buffer implementation (because C++ scoping rules ruins Python) to get them work.

pip uninstall protobuf

pip install --no-binary protobuf protobuf

And compile those proto files seperately. I don't know why that's the case.

protoc --proto_path=ctrader/open-api-proto-lib --python_out=ctrader/proto_gen ctrader/open-api-proto-lib/OpenApiModelMessages.proto ctrader/open-api-proto-lib/OpenApiMessages.proto

protoc --proto_path=ctrader/open-api-proto-lib --python_out=ctrader/proto_gen ctrader/open-api-proto-lib/CommonModelMessages.proto ctrader/open-api-proto-lib/CommonMessages.proto

The last problem is how generated Python code import model modules isn't correct. Add relative path by hand to import code as belows

from . import ... as ...

That's how I get it work


@depot051

irmscher9
11 Nov 2020, 22:04

That's interesting, can't believe Python gets overlooked to so massive extent...


@irmscher9

irmscher9
11 Nov 2020, 22:04

RE:

r.houlahan said:

Hi there,

I am developing a non-C# algorithmic trading program and am wondering how I can use the Open API to interface with cTrader. I am not interested in using cAlgo to write C# bots, as the bulk of my code is written in Python. I cannot seem to find any documention on how to connect the Open API to cTrader - could someone link me any resources so that I can learn about this? 

Thanks in advance. 

So have you managed to make it work eventually?


@irmscher9

honeybadger
16 Nov 2020, 01:39

It seems to work with Python

depot051 said:

Protocol buffer doesn't support Python properly, you have to modify it manually and install pure python protocol buffer implementation (because C++ scoping rules ruins Python) to get them work.

Hi,

The above post is already 2 years old, so perhaps it was true at that time. But if you acknowledge that Protobuf is from Google and a company that smart and big is not super likely to cause major portability issues, then a bit of digging, hard work and reading protocol references gets you onto a promising path. Please see my post on formatting protobuf messages in Python for a first lead.

@irmscher9, the main building blocks for a Python app here seem to be the TCP Socket1, and the Protobuf classes2 with proper formatting and wrapping (see my linked post) - then you essentially have a communication interface with the server and what you do internally with that communication is entirely up to you. I am still at the beginning of my superpower-python-conquer-the-world-trading-app but at least I have managed to send a request to the server, and get a reply in accordance with the Open API reference.

Note that for the beginning and for testing, you can handle the OAuth23 part manually - you can implement an HTTP client and proper redirects at a later stage. Still, once you have your credentials, your app should authenticate with the server using the ProtoOAApplicationAuthReq() and the ProtoOAAccountAuthReq() message.

Hope this helps,

h.


1 Python Socket basics: https://docs.python.org/3.7/howto/sockets.html 
2 More information on Protobuf for Python: https://developers.google.com/protocol-buffers/docs/reference/python-generated
3 OAuth2 basics: https://aaronparecki.com/oauth-2-simplified/, and for Python: https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html
 


@honeybadger

mywebsidekicks
16 Nov 2020, 14:33

RE: It seems to work with Python

@honeybadger

Right. That's some valuable info, thanks a lot!

So how did you install protobuffs on Python, with pip or?

Also, do you find Open API really any better than Fix API for trading?

PS. LOL, nice nickname by the way. I'm a big fan of HoneyBadger =)


@mywebsidekicks

honeybadger
17 Nov 2020, 02:27

RE: RE: It seems to work with Python

mywebsidekicks said:

So how did you install protobuffs on Python, with pip or?

Also, do you find Open API really any better than Fix API for trading?

Hi there,

Thanks for your comments. You do not need to install protobuf for Python. Instead, you need the appropriate protobuf compiler for your system architecture which is available from Google (more detailed info is available under the link I posted above). The compiler generates Python classes for each type of protobuf message that you can import into your project and work with - and all classes come with the methods needed for handling them.

I can't really comment on your second question as I have no experience with FIX API - its functionality simply doesn't fit my needs.

Cheers,

h.


@honeybadger

mywebsidekicks
26 Nov 2020, 22:18 ( Updated at: 27 Nov 2020, 20:47 )

RE: RE: RE: It seems to work with Python

Hey mate, thanks a lot for sending me in the right direction! I was able to install ProtoBuffs and compile Spotware protos to Python files!

One more question if you don't mind:

import OpenApiCommonMessages_pb2 as CommonMessages
import OpenApiCommonModelMessages_pb2 as CommonModelMessages
import OpenApiMessages_pb2 as Messages
import OpenApiModelMessages_pb2 as ModelMessages


clientId = r"myclientId"
clientSecret = r"myclientSecret"
token = r"mytoken"

accAuth = Messages.ProtoOAAccountAuthReq()

accAuth.ctidTraderAccountId = 1234567
accAuth.accessToken = token

# checks if all the required fields have been set.
print(accAuth.IsInitialized())

# returns a human-readable representation of the message, particularly useful for debugging.
print(accAuth.__str__())

# serializes the message and returns it as a string.
print(accAuth.SerializeToString())

 

So how do I actually send the data?

It needs to be sent to 'live.ctraderapi.com:5035' Right?

Via regular Python socket interface or with SSL wrapper?


@mywebsidekicks

honeybadger
11 Dec 2020, 02:14

RE: RE: RE: RE: It seems to work with Python

Hi there,

happy to hear it worked.

mywebsidekicks said:

So how do I actually send the data?

It needs to be sent to 'live.ctraderapi.com:5035' Right?

Yes, as it is documented in the Open API Reference.

 

Via regular Python socket interface or with SSL wrapper?

I don't know if it works via a bare TCP Socket as I would never send my Client Secret unencrypted. TLS (more specifically: the TLS certificate) is the only piece in this setup ensuring that nobody will hijack your account and your money. 

My advice for these kinds of questions, however: just try it out! Even if your attempt does not work, you will gain valuable programming experience and help avoiding these kind of problems in the future.

Stay safe (off- and online),

h.


@honeybadger

amusleh
16 Dec 2021, 16:03

Hi,

For anyone who is reading this thread in future, you can use our new Python package for cTrader open API: OpenApiPy (spotware.github.io)


@amusleh