Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    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

    General Code/Help
    2
    2
    282
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • SkullTech
      SkullTech last edited by SkullTech

      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?

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        Yes, you close blindly when entering the market.

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors