lbilli / rib

An R implementation of Interactive Brokers API
GNU General Public License v3.0
34 stars 7 forks source link

Messages not getting decoded properly? #17

Closed YuriWerewolf closed 1 year ago

YuriWerewolf commented 1 year ago

I get warnings when sending requests/receiving messages when using your example/other requests:

I have checked Decoder class but still not sure what's wrong, just getting 'default implementation' message...

ic$reqContractDetails(11, contract)

more requests can go here...

Parse responses

Might need to be called several times to exhaust all messages

ic$checkMsg() [1] 49 There were 49 warnings (use warnings() to see them)

Find results in

wrap$context NULL warnings() Warning messages: 1: In (function (orderId, contract, order, orderstate) ... : default implementation 2: In (function (orderId, status, filled, remaining, avgFillPrice, ... : default implementation 3: In (function (orderId, contract, order, orderstate) ... : default implementation 4: In (function (orderId, status, filled, remaining, avgFillPrice, ... : default implementation 5: In (function (orderId, contract, order, orderstate) ... : default implementation

Just to clarify, the client does get connected and I get responses from the TWS:

ic$connect(port=7496, clientId=1) server version and timestamp: 175 20220919 10:37:43 British Summer Time

ic$checkMsg() Managed Accounts: U***** Next OrderId: 1 Error: -1 2104 Market data farm connection is OK:cafarm
Error: -1 2104 Market data farm connection is OK:hfarm
Error: -1 2104 Market data farm connection is OK:usfuture.il
Error: -1 2104 Market data farm connection is OK:eufarmnj
Error: -1 2104 Market data farm connection is OK:cashfarm
Error: -1 2104 Market data farm connection is OK:usfuture.

lbilli commented 1 year ago

Thanks for the report.

Though a bit cryptic, the meaning of the warnings is that the wrap provided doesn't contain customized methods to handle certain responses, which in this case are related to existing pending orders in TWS.

I'm guessing that you used the very first example in README to define wrap. That's rather a barebone self contained template that the user is supposed to customize in order to fit their needs.

I'd suggest to try again with this wrap definition instead:

wrap <- IBWrapSimple$new()

which handles many responses by simply printing out or storing in wrap$context their content.

You can look at IBWrapSimple's implementation as a template to write your own.

YuriWerewolf commented 1 year ago

Yes, you are correct. Works with IBWrapSimple and I will look into extending this class definition.

Is the only way to write it inside the main code I guess?

In the installed 'rib' package folder I only see .rdb and .rdx files, no source files.

lbilli commented 1 year ago

You can look at the code right here on Github or clone the repo on your disk, no reason to mess with the installed package.

IBWrapSimple is defined here.

Once you get the feeling of it, just add in your own project something along these lines:

MyCustomWrap <- R6::R6Class("MyCustomWrap",
  class=      FALSE,
  cloneable=  FALSE,
  lock_class= TRUE,

  inherit= IBWrap,

  # And the customized method you need: e.g.

  error=            function(id, errorCode, errorString, advancedOrderRejectJson)
                      cat("Error:", id, errorCode, errorString, advancedOrderRejectJson, "\n"),

  nextValidId=      function(orderId)
                      cat("Next OrderId:", orderId, "\n"),

  managedAccounts=  function(accountsList)
                      cat("Managed Accounts:", accountsList, "\n"),

  contractDetails= ...,

  orderStatus= ...,

  openOrder= ...,

  # etc
  )
)

A class like this, i.e. derived from IBWrap, is a prerequisite in order to instantiate an IBClient.

Later on you'll have:

wrap <- MyCustomWrap$new()
ic   <- IBClient$new(wrap)

ic$connect(...)

# etc

When responses from TWS are processed, the appropriate function defined in MyCustomWrap is invoked.

If such a function has not been provided inMyCustomWrap, the warning "default implementation" is issued.

YuriWerewolf commented 1 year ago

That's how I will proceed from here. Thanks for the replies, very useful.