Я пытаюсь установить сеанс FIX 4.2 для fix.gdax.com (документы: https://docs.gdax.com/#fix-api или https://docs.prime.coinbase.com/?python#logon-a) с использованием Python 3.5 и Stunnel. Все работает, за исключением моего сообщения о входе в систему, которое было отклонено, и сеанс был закрыт сервером без ответа, что затрудняет отладку того, что происходит не так. Мой код Python выглядит следующим образом:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", 4197)) # address and port specified in stunnel config file
# generate a signature according to the gdax protocol for signing a message:
timestamp = str(time.time())
message = [timestamp, "A", "0", "f3e85389ffb809650c367d42b37e0a80", "Coinbase", "password-goes-here"] # these are the components of the pre-hash string as specified in the docs for a logon message
message = bytes("|".join(message), 'utf-8') # add the field separator
hmac_key = base64.b64decode(r"api-secret-goes-here")
signature = hmac.new(hmac_key, message, hashlib.sha256)
sign_b64 = base64.b64encode(signature.digest()).decode()
# in the above line the .decode() is not included when used to authenticate messages to the REST API and those are working successfully.
#The reason I've included it here is to allow a string to be passed into the variable 'body' below:
msgType = "A"
t = str(datetime.utcnow()).replace("-","").replace(" ", "-")[:-3] # format the timestamp into YYYYMMDD-HH:MM:SS.sss as per the FIX standard
body = '34=1|52=%s|49=f3e85389ffb809650c367d42b37e0a80|56=Coinbase|98=0|108=30|554=password-goes-here|96=%s|8013=Y|' % (t, sign_b64)
bodyLength = len(body.encode('utf-8')) # length of the message in bytes
header = '8=FIX.4.2|9=%s|35=%s|' % (bodyLength, msgType)
msg = header + body
# generate the checksum:
def check_sum(s):
sum = 0
for char in msg:
sum += ord(char)
sum = str(sum % 256)
while len(sum) < 3:
sum = '0' + sum
return sum
c_sum = check_sum(msg)
logon = msg + "10=%s" % c_sum # append the check sum onto the message
logon = logon.encode('ascii') # create a bytes object to send over the socket
print(logon)
s.sendall(logon)
print(s.recv(4096))
Результаты этих двух операторов печати:
b'8=FIX.4.2|9=159|35=A|34=1|52=20171104-11:13:53.331|49=f3e85389ffb809650c367d42b37e0a80|56=Coinbase|98=0|108=30|554=password-goes-here|96=G7yeX8uQqsCEhAjWDWHoBiQz9lZuoE0Q8+bLJp4XnPY=|8013=Y|10=212'
b''
Здесь есть много переменных, которые могут быть неправильными, и процесс проб и ошибок становится немного утомительным. Может ли кто-нибудь увидеть, что не так с сообщением о входе в систему?
|
в качестве разделителя полей. Разделителем полей FIX является символ ASCII 1 (я полагаю, что это\x01
в Python), поэтому ваши сообщения не соответствуют протоколу FIX, что может объяснить отсутствие ответа. Я настоятельно рекомендую использовать существующую библиотеку FIX, а не пытаться реализовать свою собственную. - person Iridium   schedule 04.11.2017