Setting a Stop loss and target in a strategy (missing piece in a sample)
I want to test a simple strategy with
- Signal Generation ( many examples available)
- Set Buy/Sell order (examples available)
- Set Stop Loss and Profit Target (Couldn't find this piece, closest was https://github.com/mementum/backtrader/blob/master/samples/order_target/order_target.py
but the stop loss piece is missing, please advise.
Stop Loss could be : Based on ATR OR Based on fixed value/percent of price etc.
Profit Target could be : Based on Fixed value/percent, price of an instrument etc.
The Buy Signal is generated at price of $100:
- Enter the trade by buying at $100 or better, set a stop loss of $90 and Target of $120
- Exit if Stop Loss is hit, i.e. price moves below $90 OR Exit if Target is achieved, i.e. price moves above $120
The Sell Signal is generated at $100
- Enter the trade by Selling at $100 or better, set a stop loss of $110 and Target of $80
- Exit if Stop Loss is hit, i.e. price moves above $1100 OR Exit if Target is achieved, i.e. price moves below $80
Such a sample already exists. It is ATR-Based
(Of course, it is included in the sources)
Thanks a lot!
Somehow it was not showing in search results and the code on git repo didn't have init part where all the magic is happening
Checked the repository and the strategy in the aforementioned sample has a complete
__init__. Should there be a problem, don't hesitate to share the details.
Ok I got it,
This source has complete code (and I couldn't find this earlier)
While this one which has same comment in strategy def, doesn't have init part
may be this is meant for something else
Thanks for such a prompt and highly useful response
order_target_percentis not aimed at stop-loss. The
order_target_xxxfamily of methods allow to size an order using for example expected value or percentage of value but don't set stop losses.
There is no need for
__init__in that sample. The logic for the orders is 100% in
nextand is explained here: