hadrianl / ibapi

Interactive Brokers API - GoLang Implement
MIT License
127 stars 59 forks source link

Can't place a order #36

Open kshv190 opened 1 year ago

kshv190 commented 1 year ago

contract := new(ibapi.Contract) contract.Symbol = "MSFT" contract.Exchange = "SMART" contract.Currency = "USD" lmt := ibapi.NewLimitOrder("BUY", 244.26, 100) ic.PlaceOrder(newOrderId(), contract, lmt)

The order doesn't show on TWS nor get a openorder message

{"level":"info","ts":1670503751.6900978,"caller":"ibapi@v0.0.0-20210809041850-da8ceb5942c1/wrapper.go:656","msg":"","reqID":0,"errCode":10149,"errString":"Invalid order id: 0"}

JohnAllen commented 1 year ago

The reason you can't is right there: invalid order id: 0. You have to start from 1 I think. And you have to increment by one each order you submit. @kshv190

hadrianl commented 1 year ago

The valid order id would be returned via IbWrapper.NextValidID once the client handshaked with ib gateway successfully.

francdoc commented 10 months ago

Hi, I have the same issue. I'm trying to buy an AAPL stock but the operation does not show on the IBGateway log or TWS.

package main

import (
    "context"
    "fmt"
    "time"

    . "github.com/hadrianl/ibapi"
    "go.uber.org/zap"
)

func main() {
    var err error
    SetAPILogger(zap.NewDevelopmentConfig()) // log is default for production(json encode, info level), set to development(console encode, debug level) here
    log := GetLogger().Sugar()
    defer log.Sync()
    ibwrapper := &Wrapper{}

    ctx, _ := context.WithTimeout(context.Background(), time.Second*30)

    ic := NewIbClient(ibwrapper)
    ic.SetContext(ctx)
    err = ic.Connect("127.0.0.1", 7497, 0) // 7497 for TWS, 4002 for IB Gateway
    if err != nil {
        log.Panic("Connect failed:", err)
    }

    ic.SetConnectionOptions("+PACEAPI")

    err = ic.HandShake()
    if err != nil {
        log.Panic("HandShake failed:", err)
    }

    ic.Run() // start ibclient's message loop

    fmt.Println("Going to place order...")
    aapl := Contract{ContractID: 265598, Symbol: "AAPL", SecurityType: "STK", Exchange: "NYSE"}
    ic.ReqContractDetails(ic.GetReqID(), &aapl)
    lmtOrder := NewLimitOrder("BUY", 144, 1)
    ic.PlaceOrder(ibwrapper.GetNextOrderID(), &aapl, lmtOrder)

    err = ic.LoopUntilDone()

    log.Info(err)
}

Thanks in advance

icecolbeveridge commented 9 months ago

Franco, I may have a similar problem to you -- if you call ic.ReqOpenOrders(), does your order appear?

For me, it shows up in the (frustratingly under-documented) APIPending state with no indication of what might have caused that.

Further information: orders were working fine for me about a week before Franco's post (mid-August), but not when I came back from vacation last week (mid-September). My colleagues report that python library ibinsync continues to work as expected.

icecolbeveridge commented 9 months ago

Further to discussion on the TWS API list (hi Franco!), the problem seems to be that the highest server version available in ibapi is significantly behind the most up-to-date one. As I understand it, one difference is that IB currently requires Decimal types rather than float64s.

hadrianl commented 9 months ago

I would try to catch up with the highest server version

francdoc commented 9 months ago

Hi everyone, I managed to execute MKT and LMT - BUY/SELL orders. I branched from master (stable version) for sharing purposes. Here is the link: https://github.com/francdoc/ibapi/tree/placeOrder_twsapi.10.19.01. This probably needs more work though.

Hope this helps.

hadrianl commented 9 months ago

@francdoc Thanks!

francdoc commented 9 months ago

Your welcome, @hadrianl. Awesome work on your Go implementation by the way. If anyone is curious, to make the update to hadrianl's source code I used the twsapi.10.19.01 C++ source files. That is how I found the missing fields for the placeOrder function in Go.

francdoc commented 9 months ago

Franco, I may have a similar problem to you -- if you call ic.ReqOpenOrders(), does your order appear?

For me, it shows up in the (frustratingly under-documented) APIPending state with no indication of what might have caused that.

Further information: orders were working fine for me about a week before Franco's post (mid-August), but not when I came back from vacation last week (mid-September). My colleagues report that python library ibinsync continues to work as expected.

Hi @icecolbeveridge, yes. The order appears now. Check out the link I posted here, hope it helps.

icecolbeveridge commented 9 months ago

Nice work, Franco! Thanks :-)

On Wed, 27 Sept 2023 at 07:07, francdoc @.***> wrote:

Franco, I may have a similar problem to you -- if you call ic.ReqOpenOrders(), does your order appear?

For me, it shows up in the (frustratingly under-documented) APIPending state with no indication of what might have caused that.

Further information: orders were working fine for me about a week before Franco's post (mid-August), but not when I came back from vacation last week (mid-September). My colleagues report that python library ibinsync continues to work as expected.

Hi @icecolbeveridge https://github.com/icecolbeveridge, yes. The order appears now. Check out the link I posted below, hope it helps.

— Reply to this email directly, view it on GitHub https://github.com/hadrianl/ibapi/issues/36#issuecomment-1736759386, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJGYIUOPAJRQEAQ4ZF4WTLX4O7B5ANCNFSM6AAAAAASYCZ22Y . You are receiving this because you were mentioned.Message ID: @.***>

francdoc commented 9 months ago

Your welcome Colin!

francdoc commented 9 months ago

Hey @hadrianl, if you want I'd like to create a dedicated branch to make your source code fully compatible with the current stable twsapi (version 10.19.01). Do you think it's best to branch from 'master' or 'dev'? Or create a new github repo with a link to the original one as reference? Personally, I've been working more with 'master' lately, so I feel more comfortable working from there. Let me know what you think.

hadrianl commented 9 months ago

I think master is just fine

hadrianl commented 9 months ago

It seem that I`m missing the production release of tws api from 10.10 to 10.22

francdoc commented 9 months ago

Hi @hadrianl, ok. I’ll keep on working on a feature branch from master. When it becomes a stable release I will submit a pull request to your repo (master) so you can check it. Thanks.