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

Help with coding a position reversal strategy

  • This is the strategy I'm using

    class AMAStrat(bt.Strategy):
        def __init__(self):
            fast_ama = bt.indicators.AdaptiveMovingAverage(fast=2, slow=5)
            slow_ama = bt.indicators.AdaptiveMovingAverage(fast=4, slow=10)
   = bt.indicators.CrossOver(fast_ama, slow_ama)
        def next(self):
            dayend = > time(15, 15)
            if dayend and self.position:
            if > 0:
                self.order =
            elif < 0:
                self.order = self.sell()

    It is a position reversal strategy, so it's supposed to always stay in the market, and close all positions at the end of the day.

    And this is the result upon running the strategy on the data


    As you can see from the cash // value graph at the top, it was not in the market all the time, it exited after some time. Specifically, at the buy signal, it only closed the previous position, but did not buy any more and create a new position.

    Any clue why this might be happening?

  • administrators

    Yes, you close blindly when entering the market.

Log in to reply