Python example

Created at 22 Nov 2020, 22:20
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!
mywebsidekicks's avatar

mywebsidekicks

Joined 12.11.2020

Python example
22 Nov 2020, 22:20


Hey people

Anyone have a working FIX API Python example?

 

I've spent like 10 days already trying to connect. But for the love of me, I just don't receive a reply from server, no matter what I do...

I've successfully connected via C# Sample from Spotware, using all the same credentials and stuff in my Python code.

Here's my code:

import socket
import datetime


### 1 Custom variables: ###
u = '1234567'
heartbeat_interval = 30
user = r'%s' %u
password = r'password'
SenderCompID = r'icmarkets.%s' %u
TargetCompID = r'cServer'
MsgSeqNum = 1
utct = datetime.datetime.utcnow()
SendingTime = utct.strftime("%Y%m%d-%H:%M:%S")



### 2 Constructing Body ###
body = {
'encryption_scheme': r'98=0',
'heartbeat_interval': r'108=%s' %heartbeat_interval,
'username': r"553=%s" %user,
'password': r"554=%s" %password
}
bodyStr = ''
for e in body:
    bodyStr += body[e]+'|'



### 3 Constructing Header ###
header = {
'8-BeginString': r"8=FIX.4.4",
'9-BodyLength':  r"9={message_length}",
'35-MsgType':  r"35=A",
'49-SenderCompID':  r"49=%s" %SenderCompID,
'56-TargetCompID': r"56=%s" % TargetCompID,
'57-TargetSubID': r"57=TRADE",
'50-SenderSubID': r"50=TRADE",
'34-MsgSeqNum':  r"34=%s" %MsgSeqNum,
'52-SendingTime': r"52=%s" %SendingTime,
}

headerStr = ''
for e in header:
    headerStr += header[e] +'|'

message = headerStr + bodyStr
message_length = len(message.replace('8=FIX.4.4|', ''))
message = message.replace('{message_length}', str(message_length))



### 4 Calculating checksum ###
arr = bytes(message.replace('|', '\x01'), 'ascii')
s = 0
for byte in arr:
    s += byte

cs = s % 256

if len(str(cs)) == 2:
    cs = '0%s' % cs

print("Checksum = %s" % cs)



### 5 Adding checksum to the message ###
message = message + '10=%s|' %cs
print(message)
message = message.replace('|', '\x01')
print(message)



### 6 Sending message ###
hostname = 'h50.p.ctrader.com'

message = message.encode('ascii')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((hostname, 5202))
s.sendall(message)



### 7 Receiving message ###
data = s.recv(1024)
print("received message: ", data)

 


@mywebsidekicks
Replies

wiktor.kisielewski
04 Dec 2020, 20:16 ( Updated at: 04 Dec 2020, 20:17 )

Hey,
I suggest using this to create messages:
 

this is my logon sample:
 

from simplefix import FixMessage, FixParser
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((auth.SocketConnectHost, auth.Q_SocketConnectPort))

parser = FixParser()
message = FixMessage()

message.append_pair(8, "FIX.4.4")
message.append_pair(35, "A")
message.append_pair(34, #SEQ_NUMBER_HERE)
message.append_pair(49, auth.SenderCompID)
message.append_utc_timestamp(52)
message.append_pair(56, auth.TargetCompID)
message.append_pair(57, 'QUOTE')
message.append_pair(98, "0")
message.append_pair(141, "Y")
message.append_pair(108, "30")
message.append_pair(553, auth.Username)
message.append_pair(554, auth.Password)

parser.append_buffer(message.encode())
sent = parser.get_message()
#print("logon request ", sent)

s.sendall(message.encode())
response = s.recv(5201)
parser.append_buffer(response)
out = parser.get_message()
#print("logon response2", out)

Uncomment the print statements to see what's coming out an in

Best of luck, python rules!

@wiktor.kisielewski

NFZ-Trader
28 Jul 2023, 20:04

perfeito, funcionou pra mim, obrigado

perfeito, funcionou pra mim, obrigado


@NFZ-Trader