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

Creating a Long/Short/Exit Strategy?



  • I'm relatively new to bt and have been having trouble trying to find out how to code in a long/short strategy.
    My strategy in pseudo code:
    if macd > signal & rsi > rsi_ma:
    long
    if macd < signal:
    exit long
    if macd < signal & rsi < rsi_ma:
    short
    if macd > 0:
    exit short

    I can't figure out how to exit a long position or short position given a indicator crossover.

    I see the self. position.size > 0 but need something more like self.position = long.

    Any help?


  • administrators

    @roony16 said in Creating a Long/Short/Exit Strategy?:

    I see the self. position.size > 0 but need something more like self.position = long.

    • > 0 is long (you have taken)
    • == 0 is no position
    • < 0 is short (you have given)

    Or, what are you expecting?

    Note:
    Please format your code (see the top of the forum). Paying attention to detail is what will make an algotrader ... a good one.



  • @backtrader Thank you - I figured the position.size was either 0 or > 0 indicating that you either had an open position or no position.


  • administrators

    The existence of a position (regardless of direction) is the opposite of pos == 0 and that's not > 0. The opposite is not pos or the (theoretically) less recommended pos != 0


Log in to reply
 

});