Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. SkullTech
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    • Profile
    • Following 0
    • Followers 0
    • Topics 3
    • Posts 5
    • Best 0
    • Controversial 0
    • Groups 0

    SkullTech

    @SkullTech

    0
    Reputation
    70
    Profile views
    5
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    SkullTech Unfollow Follow

    Latest posts made by SkullTech

    • 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?

      posted in General Code/Help
      SkullTech
      SkullTech
    • RE: Taking the sign on a indicator signal

      Thanks for the help @Robin-Dhillon @ab_trader !!!

      You're right, it was as simple as declaring

      self.indicator3 = indicator1 > indicator2
      

      I was getting confused because it wasn't showing up in the plot, and because of that I was thinking that I was doing something wrong. I had to do this for the plot to show up.

      posted in General Code/Help
      SkullTech
      SkullTech
    • Taking the sign on a indicator signal

      How can I create an indicator in the __init__ of my strategy which just outputs the sign (positive or negative, +1 or -1) of another indicator?

      posted in General Code/Help
      SkullTech
      SkullTech
    • RE: Creating only buy orders but still portfolio value seeing a net increase, why // how is this happening?

      @backtrader I'm only buying stocks, not selling anything. As I'm only buying, it would seem obvious that my portfolio balance should go down, not up. Am I missing something here?

      posted in General Code/Help
      SkullTech
      SkullTech
    • Creating only buy orders but still portfolio value seeing a net increase, why // how is this happening?

      This is the code I'm using. Nothing fancy, just followed the tutorial ---

      import os.path
      import sys
      
      import backtrader as bt
      import backtrader.feeds as btfeeds
      
      
      class GoogleDriveData(btfeeds.GenericCSVData):
          params = (
              ('dtformat', ('%Y%m%d')),
              ('tmformat', ('%H:%M')),
              ('datetime', 1),
              ('time', 2),
              ('high', 3),
              ('low', 4),
              ('open', 5),
              ('close', 6),
              ('volume', 7),
              ('openinterest', -1),
              ('timeframe', bt.TimeFrame.Minutes)
          )
      
      
      class TestStrategy(bt.Strategy):
          def log(self, txt, dt=None):
              dt = dt or self.datas[0].datetime.datetime(0)
              print(f'{dt}, {txt}')
      
          def __init__(self):
              self.dataclose = self.datas[0].close
      
          def next(self):
              self.log(f'Close, {self.dataclose[0]}')
      
              if self.dataclose[0] < self.dataclose[-1] < self.dataclose[-2]:
                  self.log(f'BUY CREATE, {self.dataclose[0]}')
                  self.buy()
      
      
      if __name__ == '__main__':
          cerebro = bt.Cerebro()
          cerebro.addstrategy(TestStrategy)
      
          cerebro.broker.setcash(1000000.0)
      
          modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
          datapath = os.path.join(modpath, 'TATAMOTORS.txt')
          data = GoogleDriveData(dataname=datapath)
          cerebro.adddata(data)
      
          print(f'Starting Portfolio Value: {cerebro.broker.getvalue()}')
          cerebro.run()
          print(f'Final Portfolio Value: {cerebro.broker.getvalue()}')
      

      And the final portfolio value is ---

      Final Portfolio Value: 1004834.0999999973
      
      posted in General Code/Help
      SkullTech
      SkullTech