edtechre / pybroker

Algorithmic Trading in Python with Machine Learning
https://www.pybroker.com
Other
2.09k stars 262 forks source link

Orders and rebalancing #9

Closed IgorWounds closed 1 year ago

IgorWounds commented 1 year ago

Hello,

Thanks for building this framework!

We at AlgoTrading101 are planning to create a few videos on using a backtesting library and my idea was to use this one as it has a nice pythonic way of building out strategies. But, I do have some difficulties and questions.

For example, is there a way that I can execute an order for a specific ticker (e.g. TSLA) if the ctx that I'm currently on is another stock? For example, if I was doing a pairs trade strategy with assets A and B. Can I long asset A and short asset B at the same time or do I need to wait for the ctx symbols to switch out?

If I was to rebalance the portfolio is there a built-in method or do I need to loop through each ctx and rebalance based on that?

edtechre commented 1 year ago

Thanks for reaching out @IgorWounds!

For example, is there a way that I can execute an order for a specific ticker (e.g. TSLA) if the ctx that I'm currently on is another stock? For example, if I was doing a pairs trade strategy with assets A and B. Can I long asset A and short asset B at the same time or do I need to wait for the ctx symbols to switch out?

Yes, you can retrieve data for another ticker. Here is an example. You can go long on asset A by setting buy_shares when ctx.symbol == 'A' and go short on asset B by setting sell_shares when ctx.symbol == 'B'. This can be done either in the same execution function or a separate one. To see an example of running a short strategy alongside a long one, you can refer to this notebook.

If I was to rebalance the portfolio is there a built-in method or do I need to loop through each ctx and rebalance based on that?

Currently, PyBroker does not have a dedicated function to rebalance a portfolio. However, you can still rebalance your portfolio by fetching your current positions and buying or selling shares as needed.

Let me know if that helps!

IgorWounds commented 1 year ago

Thanks for the answer! Yes, that was my idea too. I've noticed that tickers sometimes pass in a strange pattern (A, A, A, B, B, A). That's why I was a bit worried about following the ctx.cymbol == "A" logic. This also potentially makes the rebalancing act a bit clunky?

Is there a way to trigger actions/signals in one execution chain from another one or are they localized?

edtechre commented 1 year ago

Hi @IgorWounds,

The order in which you see the symbols in the output does not reflect the execution order you are assuming. PyBroker runs an execution function for each symbol for every bar of data, in the order of the dates of the bars. The ctx.symbol attribute will always reflect the current symbol being run for the execution function.

For example, assuming that securities A and B have bars that share the same date:

def exec_fn(ctx):
   ctx.buy_shares = 100

strategy.add_execution(exec_fn, ['A', 'B'])

The exec_fn will run on the same date twice, once for security A and once for security B, respectively. So if ctx.buy_shares = 100 is set, it will trigger a buy order on both A and B, which will by default be placed on the next bar of data. Then, on the next bar of data, exec_fn runs again for each of A and B.

If you want to rebalance your positions based on other positions, you can fetch your current positions using ctx.positions(). You can then update ctx.buy_shares or ctx.sell_shares accordingly based on the currently executing ctx.symbol.

edtechre commented 1 year ago

Additionally, it's worth noting that you can set position sizes for multiple symbols using a pos_size_handler function when either buy_shares or sell_shares is triggered on them. This notebook has more information.

IgorWounds commented 1 year ago

Thanks for the answer! Yes, I had a very similar logic for approaching what we spoke about above. I'm glad that this framework is built and that it offers what we need. Thank you!

edtechre commented 1 year ago

Thank you for your feedback, @IgorWounds! Please feel free to let me know if you have any suggestions on how PyBroker can further improve.

edtechre commented 1 year ago

Hi @IgorWounds,

I have committed changes that add Strategy#set_before_exec and Strategy#set_after_exec methods for handling multiple symbol contexts at once. The change will be in the next v1.1.1 release.

edtechre commented 1 year ago

Hello @IgorWounds,

I wanted to share with you a notebook I have written that demonstrates how to rebalance a portfolio at the beginning of every month. You can find the notebook here. The notebook also includes an example of using portfolio optimization to construct a minimum risk portfolio.