nautechsystems / nautilus_trader

A high-performance algorithmic trading platform and event-driven backtester
https://nautilustrader.io
GNU Lesser General Public License v3.0
1.7k stars 398 forks source link

Add index instrument support for Interactive Brokers #1703

Closed rsmb7z closed 3 weeks ago

rsmb7z commented 3 weeks ago

Pull Request

Add support for Index instrument.

Type of change

How has this change been tested?

Included some tests.

cjdsellers commented 3 weeks ago

Hey @rsmb7z thanks for the PR!

Some thoughts here:

rsmb7z commented 3 weeks ago
  • Should this be a type of Instrument if the products aren't tradable?
  • If not tradable, then should we treat it similar to SyntheticInstrument which is a type of Data?

Although the InstrumentContract is not tradable, it does not fall under the category of SyntheticInstrument since it originates directly from the trading venue rather than being internally generated by Nautilus. It should indeed be treated as a type of Instrument because, similar to tradable instruments, it would be managed via data subscriptions in the same way. We have already categorized it as asset_class=AssetClass.INDEX, which provides a clear distinction. However, adding a new enumeration value to InstrumentClass could help further clarify its classification, ensuring it is properly recognized within Nautilus.

  • What does the data look like coming from IB?

In the ibapi, definition is structured in a IBContract object, similar to how stocks, futures, and other financial instruments are represented.

  • If not tradable, then do we need the size precision and increment?

Although the instrument is not tradable, the size precision and increment parameters are indeed necessary for processing index data precision. I have included these details because they are part of the IBContract and are required attributes for the Instrument class. This inclusion ensures consistency and completeness in our handling of all instruments, whether tradable or not, facilitating accurate data processing and analysis.

cjdsellers commented 3 weeks ago

Although the InstrumentContract is not tradable, it does not fall under the category of SyntheticInstrument since it originates directly from the trading venue rather than being internally generated by Nautilus. It should indeed be treated as a type of Instrument because, similar to tradable instruments, it would be managed via data subscriptions in the same way.

We also use SyntheticInstrument(Data) in the DataEngine to manage data feeds, and its not a tradable instrument.

Although the instrument is not tradable, the size precision and increment parameters are indeed necessary for processing index data precision.

Interesting, so the reason I ask is because I would have just expected prices for an index, are there sizes too? I would have thought these would indicate trades? [edit: I suppose there could be traded volume associated with an index].

I'm also happy to merge as you've added support into the Interactive Brokers adapter which definitely seems necessary. We can potentially tweak / refine the index instrument after merge based on your answers to the above.