gofinance / ib

Pure Go interface to Interactive Brokers IB API
386 stars 120 forks source link

noob question: how can I access to my portfolio? #38

Open cocodrino opened 4 years ago

cocodrino commented 4 years ago

I've several stocks in my portfolio and I'd like to access them using go,

portfolio

right now I've the current code


account,err := ib.NewPrimaryAccountManager(engine)

if err != nil {
        fmt.Println("error getting account ",err)
    }

portfolio := account.Portfolio()

fmt.Println("Portfolio ", portfolio)

but I'm getting an empty map as portfolio

hope you can help me, thank you so much

dsouzae commented 4 years ago

In your case you need to wait for the request to the gateway to complete. Here is an example code you can use:

    account, err := ib.NewPrimaryAccountManager(engine)

    if err != nil {
        fmt.Println("error getting account ", err)
    }

    notify := account.Refresh()

    for {
        select {
        case <-notify:
            fmt.Printf("Portfolio\n")
            portfolio := account.Portfolio()
            for _, xc := range portfolio {
                fmt.Printf("%v\n", xc)
            }   
        }       
    }

You will get multiple calls to returned from the Manager.

Or you can use the following code if just want one time use:

    <-account.Refresh()
    fmt.Printf("Portfolio\n")
    portfolio := account.Portfolio()
    for _, xc := range portfolio {
        fmt.Printf("%v\n", xc)
    }