mhallsmoore / qstrader

QuantStart.com - QSTrader backtesting simulation engine.
https://www.quantstart.com/qstrader/
MIT License
2.84k stars 851 forks source link

Question about risk management #336

Closed snexus closed 5 months ago

snexus commented 3 years ago

Hi,

Thank you for this package, it is great to see new version!

Question regarding risk management - does it make sense to implement it on a different schedule to re-balance? Arguably risk should be managed on continuous basis, for example I would like to get rid of all positions BETWEEN re-balances, if certain conditions are met. I've tried to get around it by implementing risk logic directly in my instance of AlphaModel and re-balance daily, but it doesn't work smoothly.

mhallsmoore commented 3 years ago

Hi @snexus,

This is certainly an interesting point. Your suggestion motivates the idea of an additional 'schedule' in addition to the three that are currently carried out (simulation clock, statistics and re-balancing). Perhaps one way to go about implementing this would be to have the most granular simulation clock always running and then allow multiple 'Schedule' entities to post events at periodic subsets of this clock. Your approach would require this to be done dynamically, that is when 'certain conditions are met'. In order for that to work it would be necessary to run risk calculations at the same frequency as the sim clock (as you suggested) since the desired conditions may be met at any time.

I can certainly see a use-case, particularly for a strategy that does not rebalance frequently (i.e. Tactical Asset Allocation style strategies) where it might be necessary to liquidate the account rapidly due to unforeseen market circumstances.

While this feature might not appear in a recent release, it is certainly something I would like to add to the roadmap.

I'm happy to have more of a discussion around it in this Issue thread if you have futher ideas on the topic.

Kind regards,

Mike.

snexus commented 3 years ago

Hi Mike,

In order for that to work it would be necessary to run risk calculations at the same frequency as the sim clock (as you suggested) since the desired conditions may be met at any time.

Perhaps to leave it as a choice to user to define his/her own schedule, to allow for flexible definition of "when conditions are met" for specific use-case? If the would strike a balance between flexibility and performance.

I can certainly see a use-case, particularly for a strategy that does not rebalance frequently (i.e. Tactical Asset Allocation style strategies) where it might be necessary to liquidate the account rapidly due to unforeseen market circumstances.

That exactly what I was trying to achieve. Tried to implement a momentum strategy (30 day re-balance) with risk management using tactical asset allocation (daily). The momentum part went smoothly (thanks for flexible framework), but got stuck with the risk part.

Additional minor inconvenience - is there a way to make AlphaModel aware of current position allocations (guess it is coming from Broker class?). If user decides to implement risk management directly in AlphaModel, it would be useful to just return current positions if there is no risk (unless it is forbidden by design, since it is violating separation of concepts). There might be other use cases where outcome of re-balance is a function of current open positions.

Kind Regards, Denis