我正在尝试使用ib API的 Go 端口连接到我的 Interactive Brokers Trader 工作站。
我可以从 API 连接和读取数据,但是当我尝试在模拟交易账户上下订单时,我收到以下错误:
&{1 321 Error validating request:-'bB' : cause - Cannot set VOL attribute on non-VOL order.}
但我不相信我在请求中设置了 VOL 属性。重现错误的最小程序是:
package main
import (
"fmt"
"math"
"time"
"github.com/gofinance/ib"
)
func main() {
eng, err := ib.NewEngine(ib.EngineOptions{
DumpConversation: true,
Gateway: "127.0.0.1:1122",
})
if err != nil {
fmt.Printf("Connection error: %v", err)
}
// Note: you have to make sure the connection has been fully established
// before attempting to do any requests to the TWS. Failure to do so will
// result in the TWS closing the connection. Typically this can be done by
// waiting for a callback from an event and the end of the initial connection
// handshake, such as IBApi.EWrapper.nextValidId or IBApi.EWrapper.managedAccounts
// https://interactivebrokers.github.io/tws-api/connection.html
// TODO take apart engine.go and make it wait properly.
time.Sleep(1 * time.Second)
defer eng.Stop()
// printInstrument(eng)
reqID := eng.NextRequestID()
o := make(chan ib.Reply)
eng.Subscribe(o, reqID)
order := ib.Order{
Action: "BUY",
OrderType: "LMT",
TotalQty: 100,
LimitPrice: 95.94,
}
contract := ib.Contract{
SecurityType: "STK",
Symbol: "MSFT",
Exchange: "SMART",
PrimaryExchange: "NASDAQ",
Currency: "USD",
}
req := &ib.PlaceOrder{
Order: order,
Contract: contract,
}
req.SetID(reqID)
if err := eng.Send(req); err != nil {
fmt.Printf("Send error: %v", err)
}
fmt.Println("Waiting for reply...")
reply := <-o
fmt.Println(reply)
}
程序的输出是:
$ go run main.go
2> '71-1-2-'
2< &{15 1} &{[DU1029297]}
2< &{9 1} &{1}
2< &{4 2} &{-1 2104 Market data farm connection is OK:usfuture}
2< &{4 2} &{-1 2104 Market data farm connection is OK:cashfarm}
2< &{4 2} &{-1 2104 Market data farm connection is OK:usfarm.us}
2< &{4 2} &{-1 2104 Market data farm connection is OK:usfarm}
2< &{4 2} &{-1 2106 HMDS data farm connection is OK:ilhmds}
2< &{4 2} &{-1 2106 HMDS data farm connection is OK:euhmds}
2< &{4 2} &{-1 2106 HMDS data farm connection is OK:fundfarm}
2< &{4 2} &{-1 2106 HMDS data farm connection is OK:ushmds}
2> '3-42-1-0-MSFT-STK--0---SMART-NASDAQ-USD-----BUY-100-LMT-95.94-0-----0--0-0-0-0-0-0-0-0--0-------0--0-0---0-0-0-0-0-0-0-0-0-0-0-0-0-0-0--0-0-0-0-0-0-0-0-----0---0-0--0--'
Waiting for reply...
2< &{4 2} &{1 321 Error validating request:-'bB' : cause - Cannot set VOL attribute on...
&{1 321 Error validating request:-'bB' : cause - Cannot set VOL attribute on non-VOL order.}
请特别注意来自 ib 引擎的转储3-42-1-0-MSFT-STK--0---SMART-NASDAQ-USD-----BUY-100-LMT-95.94-0-----0--0-0-0-0-0-0-0-0--0-------0--0-0---0-0-0-0-0-0-0-0-0-0-0-0-0-0-0--0-0-0-0-0-0-0-0-----0---0-0--0--
。也许熟悉 IB API 的人可以告诉我我在这里做错了什么?