@run-out Hi, thank you very much for the reply.
Let me try to explain what my story is here, and please correct me if I'm wrong with my understanding of bt:
From my understanding, bt recognises 'stocklike' and 'futurelike' assets and treats them differently, i.e., trade 'on-margin' or not, when managing their transaction, and 'cash-value' of the portfolio.
At the moment you buy or sell a futurelike asset, you have a reduction in cash since you need margin for both long and short positions, and the value of your position is recorded as a positive value in 'pos_value_unlevered ', which gets its value from the margin. When holding a position in a futurelike asset, bt 'marks-to-market' at every bar so that the pnl from holding the position shows up in 'cash' (considering multiplier and leverage) and the 'pos_value_unlevered ' is locked as the amount of margin requirement from the moment the position is being created. When closing such a position, you close at the 'open price' (position.price), and the value is refunded back to your cash. PS: I also found a bug in bt in the cash-value calculation in this setup, which was discovered and solved in an earlier thread.
Basically, for future-like instruments:
Δ_margin = 0
Δ_cash = absolute_pnl
where, Δ_margin =Δ_pos_value, and absolute_pnl = Δ_price*size*mult
This is a reasonable setup for futures, although from where I trade, there are other complications (commission rules are vastly different for positions from yesterday and positions opened/closed today). But this is another topic. I'll open a new thread on this soon.
For stock-like instruments, things are straight forward: you simply record and update the true value of your position and deduct/add cash from your account at the moment you complete a transaction.
For options, the rules are much more complicated.
Options 1) trade on margin (dynamically calculated) when you short; and 2) have multipliers. They behave neither future-like nor stock-like, but a bit of both.
When you sell an option, you record a negative pos_value and you receive option premium in cash of equal amount (this is a stock-like behaviour). This cash, although added to your account, is not available to you, until the option expires or you close your position. When holding a short position in in-the-money options, you are required a margin (this is a future-like behaviour). This margin does not represent equal your pos_value_unlevered (as apposed to what bt does), it's simply the amount of cash frozen by your broker. The value of your position shall be updated at every new bar, but your total cash does not change (if you don't trade). The available cash on the other hand is updated at every bar according to the margin change.
So when you have option shorts, your broker.value() should return (available_cash + margin + unrealised_PnL + pos_value). This is similar to what bt is doing now with futures, except 1) pos_value is negative for short positions, 2) you have unrealisedPnL recorded as cash, 3)margin is dynamically updated according to some complicated rule, 4) the sum of the available_cash, margin and unrealised_PnL does not change if you don't trade 4) and pos_value changes.
When you buy an option, you should record a positive position value and you have your cash deducted. You also don't have margin requirement. So buying an option is just like buying a stock, only with a multiplier.
basically, what I need bt to do now for option commission setup is to
- know whether you are opening or closing a position
- know whether you have a long or short position
- know meta info on this option, i.e. put/call type
- know some time-series variables calculated dynamically according to the bar data of the options AND the underlying asset
What I'm doing now is by-passing all the above complications with a stock-like setup and calculating the margin in strategy and feeding the value to a customised observer so I can plot and analyse it post-backtesting. This can do the job, but not realistic or ideal.
I know this is a lot of words. Please let me know if you have any questions so we can discuss further.
A lot of work is needed to make backtrader option-friendly. This is only a beginning. But for sure this is an interesting project.