alpacahq / alpacahq/Momentum-Trading-Example
Calculating shares_to_buy within handle_second_bar()
- Langage dominant
- Python
- Étoiles
- 714
- Forks
- 221
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Description
When calculating the number of shares to buy, I think the formula you use should be
```python
shares_to_buy = portfolio_value * risk // data.close
```
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:
```python
portfolio_value = 200000
risk = 0.001
close = 100
stop_price = 95
# new calculation of shares_to_buy
shares_to_buy = portfolio_value * risk // close
print('portfolio_value * risk: ' + str(portfolio_value*risk))
print('share price: ' + str(close))
print('shares_to_buy: ' + str(shares_to_buy))
print('shares_to_buy * close: ' + str(shares_to_buy*close))
print('shares_to_buy*close/portfolio_value: ' + str(shares_to_buy*close/portfolio_value))
```
>portfolio_value * risk: 200.0
>share price: 100
>shares_to_buy: 2.0
>shares_to_buy * close: 200.0
>shares_to_buy*close/portfolio_value: 0.001
```python
# original calculation of shares_to_buy
shares_to_buy = portfolio_value * risk // (close-stop_price)
print('portfolio_value * risk: ' + str(portfolio_value*risk))
print('share price: ' + str(close))
print('shares_to_buy: ' + str(shares_to_buy))
print('shares_to_buy * close: ' + str(shares_to_buy*close))
# exceeds risk threshold
print('shares_to_buy*close/portfolio_value: ' + str(shares_to_buy*close/portfolio_value))
```
>portfolio_value * risk: 200.0
>share price: 100
>shares_to_buy: 40.0
>shares_to_buy * close: 4000.0
>shares_to_buy*close/portfolio_value: 0.02
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
Guide de contribution
Aucun guide de contribution indexé pour ce dépôt
Évaluation
Cette issue n'a pas encore été évaluée.