如何使用 Python 向 GDAX/Coinbase 发送 FIX 登录消息

2023-11-25

我正在尝试建立一个到 fix.gdax.com 的 FIX 4.2 会话(文档:https://docs.gdax.com/#fix-api or 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''

这里有很多变量可能是错误的,并且反复试验的过程变得有点乏味。谁能看出登录消息有什么问题吗?


我对您的代码做了一些修改,并在与您的代码不同的地方添加了注释(更正您的错误):

import socket
import base64
import time, datetime
import hmac
import hashlib

PASSPHRASE = "your passphrase"
API_KEY = "your api key"
API_SECRET = "your secret"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", 4197))

seq_num = "1" # Correction: using the same MsgSeqNum for signed text and for the field 34


# Correction: t is the same in both signed RawData and in SendingTime (52)
timestamp = str(time.time())
t = str(datetime.datetime.utcnow()).replace("-","").replace(" ", "-")[:-3]

# Correction: '|' is not a valid separator for FIX, it must be '\u0001'
message   = "\u0001".join([t, "A", seq_num, API_KEY, "Coinbase", PASSPHRASE]).encode("utf-8")

hmac_key  = base64.b64decode(API_SECRET)
signature = hmac.new(hmac_key, message, hashlib.sha256)
sign_b64  = base64.b64encode(signature.digest()).decode()

msgType = "A"

body = "34={}|52={}|49={}|56=Coinbase|98=0|108=30|554={}|96={}|8013=Y|".format(seq_num, t, API_KEY, PASSPHRASE, sign_b64) # using the same time (t) and seq_num as in signed text

# Correction: bodyLength is the number of characters, not bytes, also it must include everything after "8=FIX.4.2|9={}|" i.e. the "35=A|" part of the header
bodyLength = len("35={}|".format(msgType)) + len(body)
header     = "8=FIX.4.2|9={}|35={}|".format(bodyLength, msgType)
msg        = header + body

msg = msg.replace('|', '\u0001') # Correction: '|' is not a valid separator for FIX, it must be '\u0001'

# 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={}\u0001".format(c_sum)
logon = logon.encode('ascii')
print(logon)

s.sendall(logon)
print(s.recv(4096))

对我来说,这个更正后的代码现在返回来自服务器的登录消息,而不是像您的情况那样仅返回 0 字节。您能否确认它也适用于您并且登录完成后您可以成功发送其他交易?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Python 向 GDAX/Coinbase 发送 FIX 登录消息 的相关文章

随机推荐

  • Android 中的向前或向后兼容性?

    我想知道 Android 是否提供任何类型的兼容性 即向前或向后 这意味着 就像在 Blackberry 中一样 如果使用 JDE 4 2 开发应用程序 那么该应用程序将在任何具有 OS 4 2 或更高版本的手机上运行 这意味着它具有向前兼
  • xslt 有 split() 函数吗?

    如何根据某些分隔符拆分字符串 给定一个字符串Topic1 Topic2 Topic3 我想根据分割字符串 生成 Topic1 Topic2 Topic3 在 XSLT 1 0 中 您必须构建一个递归模板 这个样式表
  • 如何在网站上使用表情符号字体?

    我已经下载了谷歌的 诺托彩色表情符号 字体 但无法使其工作 我对 Noto Sans Regular 等字体没有任何问题 但使用 Noto Color Emoji 字体时 我在 Firefox 在 Windows 10 上 中收到以下错误
  • WPF 窗口设置焦点

    我有一个 WPF 窗口 我只创建一次 然后 Show 和 Hide 多次 现在我正在寻找一种方法来将焦点设置在每个 Show 上的元素上 我可以在哪里以及如何执行此操作 只需对要聚焦的元素调用 Element Focus 即可 如果您的意思
  • 在 Java 中使用另一个类的私有方法

    我有两节课 public class Class1 public class Class2 private void simpleMethod In Class2我有私人方法simpleMethod 我想用它Class1在同一个项目中 我不
  • 在apache服务器上导入Pandas导致超时错误

    我有一个在 Apache 服务器上运行的 Django 项目 我安装了 pandas 并想用它来开始操作数据 然而发生了一些奇怪的事情 每当我使用import pandas在生产环境中 服务器会挂起并 一段时间后 抛出 408 超时错误 我
  • 使用自定义 URL 参数启动 VS2010 silverlight 调试会话

    当我调试 Silverlight 应用程序时 Visual Studio 将仅打开页面 没有额外的 URL 参数 因此 网址将如下所示 http localhost 65351 MyApp 但是 当有人导航到 URL 中带有额外参数的页面时
  • 使用 Room @Transaction 函数时 Android 仪器测试未运行结束

    我正在使用 AS 3 4 1 和运行 Android 9 的模拟器进行测试 当我使用以下测试时 以下测试将无法运行Room Dao Function annotated with Transaction in it class Recurr
  • SQL Server 视图可以有主键和外键吗?

    是否可以在 Microsoft SQL Server Management Studio 中定义数据库视图的主键和外键 如何 我正在尝试创建一个 ADO NET 实体数据模型来读取四个我无法修改的旧的 格式不正确的数据库表 我只创建了我需要
  • ASP.NET 4.0环境下使用C#实现多文件上传

    我正在寻找上传多个文件的解决方案 单击浏览按钮 然后使用 Shift 键选择多个文件 我看到几个解决方案需要通过点击浏览按钮一一上传 然后点击提交按钮 但我需要允许用户同时选择多个文件 设置属性 AllowMultiple true 如下所
  • numpy.fft.fft 和 numpy.fft.fftfreq 有什么区别

    我正在分析时间序列数据 希望提取 5 个主要频率分量并用作训练机器学习模型的特征 我的数据集是921 x 10080 每行是一个时间序列 总共有 921 个 在探索可能的方法时 我遇到了各种功能 包括numpy fft fft numpy
  • 删除 CSS 文件

    我使用 spring MVC 和 jsp 页面进行演示 假设我有三个选项卡A B and C在一个jsp页面中 单击 A 选项卡时 会显示 css 文件 例如 aa css 并在带有相应 div 的 head 标签中加载 单击 B 和 C
  • 如何访问jasperreports中数据源的根元素

    我有一份由 MyJavaBean 集合支持的报告 在此报告中 我 当然 可以获得 MyJavaBean 的属性 在字段中声明它们并在详细信息带上使用它 到目前为止一切顺利 现在我希望能够将此 MyJavaBean 作为子报表的参数传递 看
  • FFMPEG“字体配置错误:无法加载默认配置文件”错误 Windows

    我正在尝试用 FFMPEG 制作一个短视频 其中一个单词在视频持续时间 0 5 秒 内停留在屏幕上 我的 FFMPEG 代码如下所示 ffmpeg f lavfi i color c white s 320x240 d 0 5 vf dra
  • Visual Studio 中的“范围突出显示”功能? (与 BlueJ for java 中看到的相同)

    我想知道 因为到目前为止我未能在 VS 的选项中找到这样的功能 如果有一个功能允许范围突出显示 与 BlueJ IDE for java 中看到的相同 这是一个例子 请注意代码块是如何用颜色突出显示的 当使用 blueJ 学习 java 时
  • Firefox 和 IE 使用 utf 8 网络字体时出现特殊字符问题

    我的字符有问题 使用 UTF 8 编码和字体 Lato 和 Open Sans 使用 safari 和 chrome 没有问题 但是当我在 Windows 或 Mac 或 IE 上使用 Firefox 时 问题可以在这里看到 Lato 和
  • 设计一个支持海量数据存储和查询的系统

    面试官要求我设计一个系统来存储千兆字节的数据 并且该系统还必须支持某种查询 描述 IDC中会产生海量的记录 每条记录由一个url 访问该url的IP以及访问发生的时间组成 该记录可能可以表述为这样的结构 但我不确定应该选择哪种数据类型来表示
  • axios从URL获取文件并上传到s3

    我正在尝试使用 axios get 从网站获取文件 然后将其直接上传到 S3 但是 文件已损坏或编码不正确 上传后无法打开 文件类型范围从 jpg png 到 pdf 这是我的代码 axios get URL responseEncodin
  • 本机库 sqljdbc_auth.dll 已加载到另一个类加载器中

    我有 2 个 Java Web 应用程序需要使用 Windows 集成身份验证连接到 SQL Server 数据库 加载的第一个工作正常 但第二个会引发异常 Native Library sqljdbc auth dll already l
  • 如何使用 Python 向 GDAX/Coinbase 发送 FIX 登录消息

    我正在尝试建立一个到 fix gdax com 的 FIX 4 2 会话 文档 https docs gdax com fix api or https docs prime coinbase com python logon a 使用 P