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

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)
            self.co = bt.indicators.CrossOver(fast_ama, slow_ama)
    
        def next(self):
            dayend = self.data.datetime.time(0) > time(15, 15)
            if dayend and self.position:
                self.close()
    
            if self.co > 0:
                self.close()
                self.order = self.buy()
    
            elif self.co < 0:
                self.close()
                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

    0_1557925282520_Figure_0.png

    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.