You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Gdax Sandbox Websocket API返回价格异常问题咨询

Fixing Sandbox Websocket Price Mismatch in gdax-python

I've run into this exact issue before with the old GDAX rebranding to Coinbase Pro, so let's break down what's going wrong and how to fix it:

The Core Issues

  1. Outdated Sandbox Websocket URL: The address wss://ws-feed-public.sandbox.gdax.com is legacy and no longer serves real test market data—it returns a hardcoded test value (10000 USD) for most products. The current valid Sandbox public websocket URL is wss://ws-feed-public.sandbox.pro.coinbase.com.
  2. Manual URL Override Timing: Your code modifies the URL in on_open(), but the gdax-python library has built-in handling for Sandbox mode that's more reliable than manual edits. Using the library's native parameter ensures all environment configurations (not just the URL) are switched correctly.

Corrected Code

Instead of manually overriding the URL, use the library's sandbox=True parameter to initialize the client. I've also added message type filtering to ensure you're only logging relevant price data:

import gdax, time

class myWebsocketClient(gdax.WebsocketClient):
    def on_open(self):
        self.products = ["LTC-USD"]
        self.message_count = 0
        print("Lets count the messages!")
    
    def on_message(self, msg):
        self.message_count += 1
        # Filter for only ticker (latest quote) or match (executed trade) messages
        if msg.get('type') in ['ticker', 'match'] and 'price' in msg:
            print(f"Message type: {msg['type']} \t@ {float(msg['price']):.3f}")
    
    def on_close(self):
        print("-- Goodbye! --")

# Initialize with sandbox=True to auto-use valid Sandbox endpoints
wsClient = myWebsocketClient(sandbox=True)
wsClient.start()
print(wsClient.url, wsClient.products)

while wsClient.message_count < 500:
    print(f"\nmessage_count = {wsClient.message_count} \n")
    time.sleep(1)

wsClient.close()

Key Changes Explained

  • sandbox=True: This tells the gdax-python library to automatically use all Coinbase Pro Sandbox endpoints (including the correct websocket URL) instead of production ones. No manual URL edits needed.
  • Message Type Filtering: The websocket feed sends multiple message types (heartbeats, subscription confirmations, etc.). By targeting only ticker and match messages, you ensure you're logging actual market prices rather than irrelevant data.
  • Updated URL: When using sandbox=True, the library will connect to wss://ws-feed-public.sandbox.pro.coinbase.com automatically, which serves the same test market data you see on the Sandbox web interface.

内容的提问来源于stack exchange,提问作者jimbbq

火山引擎 最新活动