Thursday 9 July 2015

The death of global variables


I've updated the code on github used for the series of posts on using swigibpy with python to talk to the interactive brokers API, of which this is the first.

Key changes:

  • Global variables are no longer used. They make the code very hard to debug and are considered a generally bad thing. Instead I pass data between the client and server using the callback instance. I never liked using globals, but when I originally started looking at this stuff I didn't have the confidence to start messing with the IBWrapper class. Plus they worked. But now they are dead.
  • While loops are now clearer and more consistent with their treatment of error, finished and time out conditions.
  • The code is now more careful about honoring the use of request ID's, and storing data for different ID's in different places. Although this wasn't necessary for the simple example it's probably safer if people are copying and pasting the code without understanding the potential dangers.
  • Specific error messages are passed upstream from the server wrapper in case you'd like to handle them yourself.
  • I've made some simplifications; removing the bar data from the third example, and combining the separate client functions for portfolio and account data in the final example.
  • The handling of fills versus a request for execution is done more robustly in the 4th example.

The blog posts have been updated to reflect the new code.  If you're playing with this stuff it might be worth having a look at the new version, or even downloading it from scratch.

Enjoy

3 comments:

  1. Global variables are nasty - good job!

    ReplyDelete
  2. Thanks! Currently girding myself for the much bigger job of refactoring my own code in the same way...

    ReplyDelete
  3. Good stuff even though it's super old now lol. I can only guess the future code will be even better.

    I am an aggressive refactorer myself. Big believer in the DRY (Don't Repeat Yourself) so the moment I have code doing the same thing in >1 places that's where I start to extract aggressively.

    ReplyDelete

Comments are moderated. So there will be a delay before they are published. Don't bother with spam, it wastes your time and mine.