Skip to content

Commit

Permalink
fix NewOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
hadrianl committed Aug 9, 2021
1 parent 4ab61bb commit da8ceb5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
10 changes: 5 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1093,9 +1093,9 @@ func (ic *IbClient) PlaceOrder(orderID int64, contract *Contract, order *Order)
order.AllOrNone,
handleEmpty(order.MinQty),
handleEmpty(order.PercentOffset),
order.ETradeOnly,
order.FirmQuoteOnly,
handleEmpty(order.NBBOPriceCap),
false, // order.ETradeOnly
false, // order.FirmQuoteOnly
handleEmpty(UNSETFLOAT),
order.AuctionStrategy,
handleEmpty(order.StartingPrice),
handleEmpty(order.StockRefPrice),
Expand Down Expand Up @@ -1308,11 +1308,11 @@ func (ic *IbClient) PlaceOrder(orderID int64, contract *Contract, order *Order)
}

if ic.serverVersion >= mMIN_SERVER_VER_DURATION {
fields = append(fields, order.Duration)
fields = append(fields, handleEmpty(order.Duration))
}

if ic.serverVersion >= mMIN_SERVER_VER_POST_TO_ATS {
fields = append(fields, order.PostToAts)
fields = append(fields, handleEmpty(order.PostToAts))
}

msg := makeMsgBytes(fields...)
Expand Down
54 changes: 51 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestClient(t *testing.T) {

ic := NewIbClient(new(Wrapper))

if err := ic.Connect("localhost", 4002, 100); err != nil {
if err := ic.Connect("localhost", 7497, 100); err != nil {
log.Panic("failed to connect", zap.Error(err))
}

Expand Down Expand Up @@ -194,7 +194,7 @@ func TestClientWithContext(t *testing.T) {
ibwrapper := new(Wrapper)
ic := NewIbClient(ibwrapper)
ic.SetContext(ctx)
err = ic.Connect("localhost", 4002, 0)
err = ic.Connect("localhost", 7497, 0)
if err != nil {
log.Panic("failed to connect", zap.Error(err))
}
Expand All @@ -211,7 +211,8 @@ func TestClientWithContext(t *testing.T) {
ic.ReqAccountUpdates(true, "")
// ic.ReqExecutions(ic.GetReqID(), ExecutionFilter{})

hsi := Contract{ContractID: 415314929, Symbol: "HSI", SecurityType: "FUT", Exchange: "HKFE"}
hsi := Contract{ContractID: 500007591, Symbol: "HSI", SecurityType: "FUT", Exchange: "HKFE"}
// hsi := Contract{Symbol: "HSI", SecurityType: "FUT", Exchange: "HKFE"}
// ic.ReqMktDepth(ic.GetReqID(), &hsi1909, 5, true, nil)
ic.ReqContractDetails(ic.GetReqID(), &hsi)
// ic.ReqAllOpenOrders()
Expand Down Expand Up @@ -284,3 +285,50 @@ func BenchmarkCopySlice(b *testing.B) {
_ = newSlice
}
}

func TestPlaceOrder(t *testing.T) {
SetAPILogger(zap.NewDevelopmentConfig())
log := GetLogger()
defer log.Sync()
runtime.GOMAXPROCS(4)
var err error

ibwrapper := new(Wrapper)
ic := NewIbClient(ibwrapper)

err = ic.Connect("localhost", 7497, 0)
if err != nil {
log.Panic("failed to connect", zap.Error(err))
}

ic.SetConnectionOptions("+PACEAPI")
err = ic.HandShake()
if err != nil {
log.Panic("failed to hand shake", zap.Error(err))
}
ic.Run()

ic.ReqCurrentTime()
// ic.ReqAutoOpenOrders(true)
ic.ReqAccountUpdates(true, "")
// ic.ReqExecutions(ic.GetReqID(), ExecutionFilter{})

// hsi := Contract{ContractID: 500007591, Symbol: "HSI", SecurityType: "FUT", Exchange: "HKFE"}
aapl := Contract{ContractID: 265598, Symbol: "AAPL", SecurityType: "STK", Exchange: "NYSE"}
ic.ReqContractDetails(ic.GetReqID(), &aapl)

ic.ReqMktData(ic.GetReqID(), &aapl, "", false, false, nil)

lmtOrder := NewLimitOrder("BUY", 144, 1)
// mktOrder := NewMarketOrder("BUY", 1)
ic.PlaceOrder(ibwrapper.GetNextOrderID(), &aapl, lmtOrder)

ic.LoopUntilDone(
func() {
<-time.After(time.Second * 25)
ic.Disconnect()
})

fmt.Println(err)

}
7 changes: 5 additions & 2 deletions order.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ type Order struct {
ParentPermID int64

UsePriceMgmtAlgo bool
Duration int64
PostToAts int64
Duration int64 `default:"UNSETINT"`
PostToAts int64 `default:"UNSETINT"`

SoftDollarTier SoftDollarTier
}
Expand Down Expand Up @@ -325,6 +325,9 @@ func NewOrder() *Order {
order.LimitPriceOffset = UNSETFLOAT

order.CashQty = UNSETFLOAT
order.FilledQuantity = UNSETFLOAT
order.Duration = UNSETINT
order.PostToAts = UNSETINT

return order
}
Expand Down

1 comment on commit da8ceb5

@hadrianl
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix #32

Please sign in to comment.