For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Hedging support



  • Hi guys,

    I'm wondering does backtrader have any support for hedging?

    For example being in a long and short position at the same time?

    There are many reasons/use cases for this; such as:

    • a market neutral strategy (trading on the same market), or
    • building into a net long/short position over time, etc.

    I cant seem to find any examples or any search results when searching hedging.

    Breaking it down- I'm essentially looking for the ability to have buy market and sell market orders open at the same time.

    Thanks again, fantastic community here and great product.



  • If you try to get in the long and short position at the same time in the same instrument in the same account, you will have no position? Or these are positions in different instruments or different accounts? In latter case it does't matter is it hedging or not, bt will open positions according to signals.


  • administrators

    Cannot say more than what @ab_trader already said



  • Hi guys thanks for the replies.

    As @ab_trader said, this is what I have found when using Backtrader-
    you can be either in a LONG or SHORT position or FLAT (no position).

    My question was in relation to going LONG and SHORT (both at market), on the same instrument, on the same account. <-- in essence that is the most basic form of hedging.. (as opposed to multiple instruments/different accounts)

    I've attached an example of this:
    Build Example: (https://imgur.com/WI83gWk)

    However- the root of the question lies in the ability to have each order being independant of each other. i.e

    With the ability to have orders independent (and consequently allow hedging), some more use cases would include (i.e questions- can these be done?):

    1. Allowing each order have its own stop loss/profit target

    2. Having the ability to select a specific order from an order book (where multiple orders exist on the same instrument)

    3. Have multiple strategies trading on the same instrument
    i.e Strategy 1 - BUY
    i.e Strategy 2 - BUY
    i.e Strategy 3 - BUY
    i.e Strategy 4 - BUY

    I hope this makes sense, happy to explain in more detail if necessary.

    Thanks guys, appreciate the fast response.


  • administrators

    @btalgo said in Hedging support:

    As @ab_trader said, this is what I have found when using Backtrader-
    you can be either in a LONG or SHORT position or FLAT (no position).

    That's not because you are using Backtrader. That's the reality of life.

    @btalgo said in Hedging support:

    However- the root of the question lies in the ability to have each order being independant of each other. i.e

    Order are always independent. The problem is that you want to issue orders which undo the actions of other orders

    @btalgo said in Hedging support:

    1. Allowing each order have its own stop loss/profit target

    Orders do not have stop loss/profit target. You can have orders which allow you reach a profit or loss target when you already have an open position. You can use bracket orders and have stop loss/profit target orders be activated if a position is entered.

    @btalgo said in Hedging support:

    1. Having the ability to select a specific order from an order book (where multiple orders exist on the same instrument)

    Call your broker and ask it if you have can multiple conflicting orders on the same asset and only select and execute those which are of your interest.

    @btalgo said in Hedging support:

    1. Have multiple strategies trading on the same instrument
      i.e Strategy 1 - BUY
      i.e Strategy 2 - BUY
      i.e Strategy 3 - BUY
      i.e Strategy 4 - BUY

    That's already built-in from day 1 in backtrader

    @btalgo said in Hedging support:

    I hope this makes sense, happy to explain in more detail if necessary.

    Most of it doesn't make any sense at all.



  • hi @backtrader thanks for the response.
    I'll try and expand and be clearer on some points:

    @btalgo said in Hedging support:

    As @ab_trader said, this is what I have found when using Backtrader-
    you can be either in a LONG or SHORT position or FLAT (no position).

    That's not because you are using Backtrader. That's the reality of life.

    @btalgo said in Hedging support:

    However- the root of the question lies in the ability to have each order being independant of each other. i.e

    Order are always independent. The problem is that you want to issue orders which undo the actions of other orders

    I understand what you are saying here (a SELL order closes a LONG order and vice versa).
    However in other platforms a SELL order is a market order independent of a LONG order. This is where hedging comes in- you can have a LONG and SHORT order open at the same time.

    Q: Is there any future scope, or any way to modify the current cerebro engine to allow this?

    @btalgo said in Hedging support:

    1. Allowing each order have its own stop loss/profit target

    Orders do not have stop loss/profit target. You can have orders which allow you reach a profit or loss target when you already have an open position. You can use bracket orders and have stop loss/profit target orders be activated if a position is entered.

    I am aware the orders do not have stops/profit targets- they are set with respective (separate) stop and limit orders. bracket orders solves this, sorry missed this in documentation, thanks!

    @btalgo said in Hedging support:

    1. Having the ability to select a specific order from an order book (where multiple orders exist on the same instrument)

    Call your broker and ask it if you have can multiple conflicting orders on the same asset and only select and execute those which are of your interest.

    many brokers do allow this but more specifically in the spot FX and CFD space. Futures/stockbrokers rare to find due to FIFO rules (in the same account)

    But this feature (only for backtesting) is very handy for various types of strategies where the goal would be to deploy across multiple accounts.

    @btalgo said in Hedging support:

    1. Have multiple strategies trading on the same instrument
      i.e Strategy 1 - BUY
      i.e Strategy 2 - BUY
      i.e Strategy 3 - BUY
      i.e Strategy 4 - BUY

    That's already built-in from day 1 in backtrader

    @btalgo said in Hedging support:

    I hope this makes sense, happy to explain in more detail if necessary.

    Most of it doesn't make any sense at all.

    Thanks again, look forward to your reply.


  • administrators

    @btalgo said in Hedging support:

    a SELL order closes a LONG order and vice versa

    That's wrong. When you issue a Sell order, the broker looks at the current position held for the target asset. If the position is positive (Long), the sell order (which has a negative size) will sell as many contracts as possible. If the negative size is greater than the positive existing size, the broker will keep on selling (you will go Short)

    @btalgo said in Hedging support:

    Futures/stockbrokers rare to find due to FIFO rules (in the same account)

    Exactly. Keep your own book as you would have to do in almost all of the brokers (I guess anyone reading this would be glad to a pointer to the broker who lets you have simultaneous conflicting Market orders parked on the same asset and on the same account)

    @btalgo said in Hedging support:

    However in other platforms a SELL order is a market order independent of a LONG order.

    Sell orders are executed with the execution type you choose and not with simply with Market. What other platforms may or may not do is not relevant here. If you send conflicting Market orders to the same asset you will have a conflicting behavior.

    @btalgo said in Hedging support:

    Q: Is there any future scope, or any way to modify the current cerebro engine to allow this?

    Cerebro has nothing to do with it. The key here is that you send order on the same account targeted at the same assets and the orders contradict each other.

    You can put effort in developing a multi-account broker and you may execute conflicting orders against as many accounts as you wish.



  • This hedging support offered by FX Brokers and CFD is really a bad idea and I am glad that backtrader is handling orders the way it is doing it at the moment. Which is the way to do it.

    I am not sure what benefits do you get from hedging. If you have a loss, you can always exit it and re-enter, do need to supposedly hedge it by entering the opposite direction. The result is the same.