polakowo / vectorbt

Find your trading edge, using the fastest engine for backtesting, algorithmic trading, and research.
https://vectorbt.dev
Other
4.21k stars 609 forks source link

Feature request: Margin trades #42

Open Ziink opened 4 years ago

Ziink commented 4 years ago

I'm testing a strategy for a single stock where I buy more shares or sell some of the shares. I've set the init_capital to 1000000 so that all buy orders are recorded. In this scenario, the stats are not very helpful because most of the funds are not really being used.

It would be great to be able to specify margin limit (or simply assume infinity) and interest

polakowo commented 4 years ago

Margin trading is currently difficult to integrate as the whole portfolio simulation logic is built around cash accounts. It would require some memory of previous actions and tracking their status at each data point, which in turn would negatively impact performance. It certainly can be done, and I will mark this as an enhancement for future releases.

In the meantime I can make some sort of "flexible cash" where you set init_capital to np.inf, run your simulation, and the initial funds will be automatically set to the highest amount of cash you spent during this simulation. The only problem with this is that different columns will have different initial funds -> portfolio value -> returns, and thus less comparable, and vectorbt is all about comparing different configurations.

Right now I'm working on combined portfolios and already made more options for calculating returns. One of them is "active returns", which are not calculated from portfolio value but from holding value and cash flows of transactions. This way, returns will be the same as if you invested all your cash into the security and thus you can set initial capital to infinity and still be able to compare columns.

Ziink commented 4 years ago

"Active returns" sounds like it'll be quite useful. Any idea when you might be able to release it?

polakowo commented 4 years ago

I will try to release v0.14 by the end of the next week..

TheSnowGuru commented 4 years ago

Great idea! For margin trading you need to add some varibles:

Maxleverage: LotSize: PipSize Swaps: FreeMargin etc

Backtesting result of the account equity net worth col of all positions at every timestamp.

*In margin trading positions can be hedged.

On Fri, 28 Aug 2020, 02:17 Oleg Polakow, notifications@github.com wrote:

I will try to release v0.14 by the end of the next week..

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/polakowo/vectorbt/issues/42#issuecomment-682238315, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABIRHQ2VSPITN7OC5KLNX73SC3SPRANCNFSM4QNHY6VA .

guzuomuse commented 3 years ago

@polakowo thanks for your effort on this elegant framework. i want to know ,is there any plane on this feature .margin with hedge support.

polakowo commented 3 years ago

@guzuomuse I have currently no plans on implementing this feature as I have less time for open-source projects and I don't use margin trading very much, but it may be implemented somewhere in the near future. Contributions are very welcome.