nofx icon indicating copy to clipboard operation
nofx copied to clipboard

[FEATURE] 关于币安网不支持测试网的修改--供大家参考

Open einstein33 opened this issue 1 year ago • 6 comments

当前版本的币安交易所界面是支持测试网勾选的,但是实际上链接的是主网,只能使用主网的api key,使用测试网的api key是不支持的,会不停的报错,我改动了里面的设置

1.config/config.go,在第22行后添加 BinanceTestnet bool json:"binance_testnet,omitempty"

2.trader/binance_futures.go,修改 NewFuturesTrader 函数 func NewFuturesTrader(apiKey, secretKey string, isTestnet bool) *FuturesTrader { log.Printf("🔧 创建Binance客户端 - 测试网模式: %v", isTestnet)

client := futures.NewClient(apiKey, secretKey)

// 正确的测试网设置方法
if isTestnet {
	// 使用SetApiEndpoint方法设置测试网端点
	client.SetApiEndpoint("https://testnet.binancefuture.com")
	log.Printf("🔧 设置测试网端点: https://testnet.binancefuture.com")
} else {
	log.Printf("🔧 使用主网端点")
}

return &FuturesTrader{
	client:        client,
	cacheDuration: 15 * time.Second,
}

}

3.manager/trader_manager.go,HyperliquidTestnet: exchangeCfg.Testnet, 后添加: BinanceTestnet: exchangeCfg.Testnet,

4.AutoTraderConfig BinanceSecretKey string 后添加:BinanceTestnet bool trader = NewFuturesTrader(config.BinanceAPIKey, config.BinanceSecretKey)改为trader = NewFuturesTrader(config.BinanceAPIKey, config.BinanceSecretKey, config.BinanceTestnet)

5.在币安测试网申请api key:https://testnet.binancefuture.com会自动跳转https://demo.binance.com/

6.修改后重新编译测试 docker-compose build docker-compose up 查看启动日志,应该看到: 🔧 创建Binance客户端 - 测试网模式: true 🔧 设置测试网端点: https://testnet.binancefuture.com

上面的修改比较繁琐,求更新到最新版中吧,太费人了

einstein33 avatar Nov 04 '25 17:11 einstein33

4.AutoTraderConfig BinanceSecretKey string 后添加:BinanceTestnet bool trader = NewFuturesTrader(config.BinanceAPIKey, config.BinanceSecretKey)改为trader = NewFuturesTrader(config.BinanceAPIKey, config.BinanceSecretKey, config.BinanceTestnet) 没看明白

zadishu avatar Nov 04 '25 19:11 zadishu

按照上面https://github.com/NoFxAiOS/nofx/pull/497/更改后,binance模拟交易接口报错:❌ 构建交易上下文失败: 获取账户余额失败: 获取账户信息失败: code=-1021, msg=Timestamp for this request was 1000ms ahead of the server's time.

lidx0820 avatar Nov 05 '25 07:11 lidx0820

现在这个改动还生效吗?

heyu3913 avatar Nov 06 '25 02:11 heyu3913

你这太麻烦了。直接使用 futures.UseTestnet = true就行了

yibitnet avatar Nov 06 '25 06:11 yibitnet

你这太麻烦了。直接使用 futures.UseTestnet = true就行了

细说老哥 怎么改可以让币安的测试网生效

heyu3913 avatar Nov 06 '25 06:11 heyu3913

按照einstein33老哥的方法,确实直接可以使用测试网了。 挺爽的。 先要拉取最新的dev分支,然后以下修改5个文件

Image Image Image Image Image

最后一个完整代码如下

Image
// NewFuturesTrader 创建合约交易器
func NewFuturesTrader(apiKey, secretKey string, isTestnet bool) *FuturesTrader {
    log.Printf("🔧 创建Binance客户端 - 测试网模式: %v", isTestnet)
    client := futures.NewClient(apiKey, secretKey)
    // 正确的测试网设置方法
    if isTestnet {
        // 优先尝试设置API端点方法;若库不支持则直接设置BaseURL
        // 部分版本提供 SetApiEndpoint / SetBaseURL,均尝试兼容
        type apiEndpointSetter interface{ SetApiEndpoint(string) }
        if setter, ok := any(client).(apiEndpointSetter); ok {
            setter.SetApiEndpoint("https://testnet.binancefuture.com")
        } else {
            // 回退:直接设置 BaseURL 字段(go-binance v2 支持)
            client.BaseURL = "https://testnet.binancefuture.com"
        }
        log.Printf("🔧 设置测试网端点: https://testnet.binancefuture.com")
    } else {
        log.Printf("🔧 使用主网端点")
    }
    // 同步时间,避免 Timestamp ahead 错误(基于当前端点)
    syncBinanceServerTime(client)
    trader := &FuturesTrader{
        client:        client,
        cacheDuration: 15 * time.Second, // 15秒缓存
    }
    // 设置双向持仓模式(Hedge Mode)
    // 这是必需的,因为代码中使用了 PositionSide (LONG/SHORT)
    if err := trader.setDualSidePosition(); err != nil {
        log.Printf("⚠️ 设置双向持仓模式失败: %v (如果已是双向模式则忽略此警告)", err)
    }
    return trader
}

LovenSar avatar Nov 07 '25 02:11 LovenSar