Python example
Created at 22 Nov 2020, 22:20
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)
Replies
NFZ-Trader
28 Jul 2023, 20:04
perfeito, funcionou pra mim, obrigado
perfeito, funcionou pra mim, obrigado
@NFZ-Trader
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:
@wiktor.kisielewski