Short selling - Strategy versus Signals
hans last edited by
(firstly: I am new to backtrader, so forgive me if I overread smth in the documentation)
I am interested in forex trading (working primarily with tick data). I installed backtrader and ran some of its examples successfully, so I started with the basic example (from tutrial) which I am modifying to play around with backtrader.
When the strategy indicates a sell, I'd like to turn the long position into a short position. For example: when my indicator signals a reversal, I want to close a current (long) position, and switch to a short position (and vice versa). I could not figure out how to do this with a Strategy (hints/solutions welcome!), so I dug into the documentation and read about the section "strategy with signals". That seems approprate. As a matter of fact: it seems to be working quite well. However...
Everything concerning buying and selling appears "in the dark", so I attempted to intercept buy/sell signals and trade position, much like the logging in the tutorial. I am yet to find out how to get notified when a signal appears. In the first place, I'd like to just log a line to the console for each buy and sell, just like the logging in the tuturial.
Can anyone point me in the right position to get this done (either by means of a Strategy or using plain signals)? Thanks in advance!
backtrader administrators last edited by
Reversing a position is for sure no rocket science (unclear why this may be in the dark)
```python if mustgolong: self.buy(size=2*stake) # enter long else: self.sell(size=2*stake) # enter short ```
You may also use 2 commands to avoid duplicating the stake:
```python self.close() # closes existing position - no matter in which direction if mustgolong: self.buy(size=stake) # enter long else: self.sell(size=stake) # enter short ```
The reference about
closecan be seen at:
and some extra information on what happens (which order is created) is at:
There are additional approaches like using a
Sizerwhich automatically calculates the stake. This blog post shows how to use a sizer to execute reversals:
Those are for a standard
Strategybased subclass. The signal based approach is meant to handle everything automatically without intervention.
I am yet to find out how to get notified when a signal appears. In the first place, I'd like to just log a line to the console for each buy and sell, just like the logging in the tuturial.
signals are not notified. When you pass a signal to a
SignalStrategybased subclass it is used to (depending on for what was actually inserted)
signalsare just like
indicators. If you keep a reference to the signal you can read the value of the signal at anytime (current value is at inde
SignalStrategybased subclasses, just like any other
Strategycan override ` different methods (just like in the tutorial)
notify_order: which will get notifications about the order status:
Margin(this is also in the 2 links above)
notify_trade: which will of course notify trades (if you issue 2 buys, both accumulate logically as a single trade, until the position goes to
0or below because a reversal has been executed`
- Documented here: https://www.backtrader.com/docu/trade.html?highlight=trade
next: in a
SignalStrategybased subclass, this method can also be overridden. It will though be called after signals have been read and orders executed.
An example of long/short approaches with a subclass of
The source code contains additional examples, for example in the samples:
hans last edited by
Thanks a lot for this detailed and helpful explanation!