For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:
2019-10-02: The community is currently in read-only mode

Short selling - Strategy versus Signals

  • (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!

  • administrators

    Reversing a position is for sure no rocket science (unclear why this may be in the dark)

    if mustgolong:*stake)  # enter long
        self.sell(size=2*stake)  # enter short

    You may also use 2 commands to avoid duplicating the stake:

    self.close()  # closes existing position - no matter in which direction
    if mustgolong:  # enter long
        self.sell(size=stake)  # enter short

    The reference about buy, sell and close can be seen at:

    and some extra information on what happens (which order is created) is at:

    There are additional approaches like using a Sizer which automatically calculates the stake. This blog post shows how to use a sizer to execute reversals:

    Those are for a standard Strategy based 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 SignalStrategy based subclass it is used to (depending on for what was actually inserted) buy, sell and close

    • signals are 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 [0])

    • SignalStrategy based subclasses, just like any other Strategy can override ` different methods (just like in the tutorial)

      • notify_order: which will get notifications about the order status: Submitted, Accepted, Partial, Completed, Canceled, Rejected, 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 0 or below because a reversal has been executed`

      • next: in a SignalStrategy based 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 SignalStrategy:

    The source code contains additional examples, for example in the samples:

    • pyfolio2
    • kselrsi
    • sigsmacross
    • strategy-selection

  • Thanks a lot for this detailed and helpful explanation!

Log in to reply