Open DanOKeefe opened 4 years ago
The formula, as is, is correct if you're defining risk to be the proportion of the portfolio at risk cannot be greater than 0.001.
But the comment when defining risk says:
# How much of our portfolio to allocate to any one position
risk = 0.001
So I think either the comment or the code is incorrect
I think it makes sense to include the stop loss in this equation while keeping that same risk level because you only stand to lose the difference between the buy price and the stop-loss. So if you buy 40 shares at $100 a piece, that's 2% of your respectable $200k portfolio. However, if your stop-loss is $95, you only stand to lose $5 on each share, or $200 total - which would be .001 of your portfolio. I assume that's how risk is calculated here.
When calculating the number of shares to buy, I think the formula you use should be
instead of
https://github.com/alpacahq/Momentum-Trading-Example/blob/be4352838eebdd2d124eedd341e31fbae8774f3d/algo.py#L253-L255
I think that the number of shares you buy should be independent of the difference between the current stock price and your stop loss.
For example:
The above order would make up 2% of your portfolio, higher than the 0.1% of your portfolio that you said you would allocate to any one position