QuantConnect / Lean

Lean Algorithmic Trading Engine by QuantConnect (Python, C#)
https://lean.io
Apache License 2.0
9.8k stars 3.26k forks source link

MaximumDrawdownPercentPerSecurity Looks to be MaximumUnrealizedProfitPercentPerSecurity #6841

Closed ari62 closed 1 year ago

ari62 commented 1 year ago

I was just looking at the code for https://github.com/QuantConnect/Lean/blob/master/Algorithm.Framework/Risk/MaximumDrawdownPercentPerSecurity.py and I may be mistaken but I think that should be renamed MinimumUnrealizedProfitPercentPerSecurity, as it doesn't seem to use drawdown. For drawdown, i think you would need to keep track of min UnrealizedLossPercent as done here: https://github.com/IlshatGaripov/Lean/blob/c12868fa3f3c8145a752c86ebd05932adf983359/Algorithm.Framework/Risk/TrailingStopRiskManagementModel.py Not sure if the proper correction is to rename it or implement drawdown.

ari62 commented 1 year ago

you can see the similarity to https://github.com/QuantConnect/Lean/blob/aaba566954e995053eb97656b05e06a3984fc9fb/Algorithm.Framework/Risk/MaximumUnrealizedProfitPercentPerSecurity.py as well

jaredbroad commented 1 year ago

Closing as doesn't seem like a bug. Implementing the drawdown percent. Happy to reopen if can explain further the issue.

ari62 commented 1 year ago

Hi @jaredbroad The main functionality is:

maximumDrawdownPercent = -0.05
pnl = security.Holdings.UnrealizedProfitPercent
if pnl < self.maximumDrawdownPercent:
          # liquidate
          targets.append(PortfolioTarget(security.Symbol, 0))

This script uses unrealized profit, which has as its base the holdings value at time of purchase. Drawdown has as its base the highest holdings value (highest unrealized profit + original purchase holding value). This script does not keep track of nor use the highest holdings value. Hopefully that clarifies.