kieran-mackle / AutoTrader

A Python-based development platform for automated trading systems - from backtesting to optimisation to livetrading.
https://kieran-mackle.github.io/AutoTrader/
GNU General Public License v3.0
945 stars 217 forks source link

Risk per trade #14

Closed elijahho closed 2 years ago

elijahho commented 2 years ago

Is your feature request related to a problem? Please describe. Right now, if I were to set 1% risk per trade, I get a lot of cancelled trades if the stop losses is tight. What happens is it tries to max out the margin based on the absolute loss for that trade. The trade does not go through because my account's leverage is not high enough.

Describe the solution you'd like Can we modify the risk approach that does not max out the position sizing based on the 1% risk alone but take into account the leverage of the trading account. So the open trade could risk < 1% (based on my earlier example), and that's fine.

Describe alternatives you've considered Indicate a smaller risk per trade amount, which prevents margin from being maxed out.

Additional context N.A

kieran-mackle commented 2 years ago

The current implementation calculates the position size based on the risk_pc and the balance of the account at the time of ordering. That is, an amount risked is calculated according to balance * risk_pc / 100 (autobot.py line 808). To clarify, would the method you are suggesting set the amount risked to balance * risk_pc / 100 / leverage? Essentially, normalising the risk_pc by the leverage?

As another alternative, you can build a custom position size method in your strategy, and simply include the size parameter in your signal_dict, which will override any automatically calculated sizes.

rahulmr commented 2 years ago

@kieran-mackle How do we calculate quantity based on risk per trade? Suppose I have stop loss as 7 pips, risk per trade as Rs 500/- which is 1% of 50000/- and say I want to take leverage of 4x.

kieran-mackle commented 2 years ago

@rahulmr The size calculation method get_size can be found in autotrader.brokers.broker_utils module.

In your example, it would be calculated as follows:

price_per_pip = amount_risked / pip_stop_distance = 500 Rs / 7 pips = 71.4 Rs/pip
quantity = price_per_pip / pip_value = 71.4 [Rs/pip ] / 1 [Rs/pip] = 71.4 units

Note that the pip_value is dependent on the product being traded, and represents the value per unit of movement in price. In the example above, I have just assumed 1 Rs/pip. In that case, if the stop loss is hit, the trade will have a loss of 71.4 Rs. In some cases, the amount_risked will need to be converted from your account currency to the quote currency of the product being traded.

rahulmr commented 2 years ago

@kieran-mackle Thanks for the explanation.