gofinance / ib

Pure Go interface to Interactive Brokers IB API
392 stars 122 forks source link

Placing A Basic Order #13

Open dshanabrook opened 9 years ago

dshanabrook commented 9 years ago

I am trying to figure out how to place an order. Is this currently possible?

I found the PlaceOrder type, but am unclear on how to actually place the order. I found the .write method on it, but it doesn't seem to be used anywhere and is private.

dsouzae commented 9 years ago

Here is my example on how I do a buy (both market and limit orders):

func NewContract(symbol string) ib.Contract {
    return ib.Contract{
        Symbol:       symbol,
        SecurityType: "STK",
        Exchange:     "SMART",
        Currency:     "USD",
    }
}

func doBuy(engine *ib.Engine, symbol string, quantity uint64, market bool, price float64) {
    request := ib.PlaceOrder{
        Contract: NewContract(symbol),
    }

    request.Order, _ = ib.NewOrder()
    request.Order.Action = "BUY"
    request.Order.TotalQty = int64(quantity)

    if market {
        request.Order.OrderType = "MKT"
    } else {
        request.Order.OrderType = "LMT"
        request.Order.LimitPrice = price
    }
    request.SetID(NextOrderID())

    engine.Send(&request)
}

Function NextOrderID() is my own creation and should be set to the valid return value from the NextValidID object when doing: engine.Send(&ib.RequestIDs{})

I've got a functional command line programming using readline that I use for my current trading. If anyone is interesting I will work on making it a separate repository under my account.

saulshanabrook commented 9 years ago

@dsouzae It would be great to see that code open sourced. Even if I wouldn't use it myself, seeing an example of all this together would be really helpful.

uwe commented 9 years ago

I would also like to see the code. More examples are necessary, especially for getting started. It was your example @dsouzae in a ticket, that helped me getting started with gofinance/ib.

dsouzae commented 9 years ago

I have created repository for my current version. It needs some clean up and proper documentation. But It should be relatively easy to read.

https://github.com/dsouzae/ibstockcli

dshanabrook commented 9 years ago

@dsouzae Thanks for the repository. I am trying to place a trade with an advisor account. If I want to place an buy order with a specific AccountCode, should I just set the ClientID on the Order?

If not, how would I do it?