Support for futures #28
Replies: 4 comments 2 replies
|
Here's what I'm thinking. Would be great to get your input. I'd like to do this with the least drift from your design and implementation. Contract specification: The backtesting position manager needs a way to know the price increment, price multiplier, and maintenance margin of the futures contract. Seems like this could be a subclass of PairInfo and/or Pair. Maintenance margin requirements: balance on hold could be adapted to persist until the order is completed/cancelled. Since there may be a difference between intraday and overnight margin, strategies may need to specify active hours. When an order is submitted, balance on hold increase if the absolute value of the position increases. When an order is filled or cancelled, balance on hold is released if absolute value of the position decreases. Looks like this might require some work in Account Balances and Position Manager. Position management: This seems a little more complicated, but I might be missing some simplifications. When you buy/sell futures, you're just going positive or negative a number of contracts rather than a credit/debit to your account of the price * quantity. In my POC implementation, I added an OrderAction (OPEN/CLOSE) to indicate the intent for the position. For example, buy 1 @ market to open and sell 1 OCO @ stop or limit to close. There's probably a way to manage positions without needing the notion of open/close. Still thinking about it. Open to suggestions! Leverage: informed by contract specification, used in account balance updates and position management. Still working on deeper integration with position management, balance on hold, and balance updates. OCO orders: adapt the structures and methods from the binance implementation to add support for OCO orders in backtesting. External: Databento as a data source, Interactive Brokers as a Data Source and Order/Position Manager. Order Book: adapt binance event handling for order book into IBKR and backtesting (databento provides futures MBO and TBBS datasets). |
|
John, at the moment any big changes I'd like to develop those myself, and in the short term I won't be working on this. With respect to your design ideas, I'd have to educate myself a little bit more on futures trading in order to better respond. A couple of comments anyways: The position manager is a user level abstraction. Its not part of the library. I'm not sure how much stock trading and futures trading have in common, in terms of different order types, how the order book works, how orders get processed and matched with one another, etc. Lastly, I've been working on support for loans and the backtesting exchange and account balances where updated. You may want to take a look at that: https://github.com/gbeced/basana/tree/feature/loans |
|
hey @jxstanford - sounds like you're trying trade stock/commodity futures, not crypto, right? I'm interested in using this for crypto futures with leverage, and for long/short strategies. So I too need the concept of open and close, not just buy and sell. Perhaps we could collaborate a bit? or maybe I'll take a peek at what you have so far. |
|
Support for margin accounts has been finished and will be available in the next version. Here is what it looks like from the users side https://github.com/gbeced/basana/blob/f77db83c9529e523744eff578830d9db2ef4530b/samples/backtest_bbands.py#L46L56 |
Uh oh!
There was an error while loading. Please reload this page.
Hi, i really appreciate the quality and the work that went into this package. i've been working on adding support for futures trading. So far, I'd say the changes required will be fairly significant. I have a mostly working working backtest that accounts for leverage. It still needs work in the margin requirements area. I may be able to take advantage of the "on hold" concept manage margin requirements, but still need to assess that.
I'm curious whether this type of change is something you would be interested in taking into basana, or if it's not aligned with your vision for the package. Let me know what you think.
All reactions