Backtrader Community

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

    Zantox

    @Zantox

    0
    Reputation
    16
    Profile views
    7
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    Zantox Unfollow Follow

    Latest posts made by Zantox

    • Help with VWMA Indicator

      Re: Help with Volume-Weigthed Moving Average (VWMA) indicator needed

      When I copy paste this code I get this error: class VolumeWeightedAveragePrice(bt.Indicator):
      AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Indicat' object has no attribute '_addobserver'

      I saw an earlier Post to this error but it didnt really help me. I also cant see where the error in the code is because for me it all makes sense. Thank you in Advance.

      posted in Indicators/Strategies/Analyzers
      Z
      Zantox
    • RE: Simple Ma Crossover

      Okay I found the Error. Sorry for the inconvenience

      posted in General Code/Help
      Z
      Zantox
    • RE: Simple Ma Crossover

      Okay I hopefully dont annoy anybody but I found something else. I think the problem was that it was intraday but I am not sure. The Log still seems weird, because as I understannd it, it buys and than there should be a postion in the postion log. But it comes a bit too late, but the price where it opens the postion now fits the cross after I changed the Timeframe from my csv to daily.
      Sale 9264.5 shares
      --- Position Begin

      • Size: 0
      • Price: 0.0
      • Price orig: 0.0
      • Closed: 0
      • Opened: 0
      • Adjbase: None
        --- Position End
        Buy 10195.5 shares
        --- Position Begin
      • Size: -1
      • Price: 9264.5
      • Price orig: 0.0
      • Closed: 0
      • Opened: -1
      • Adjbase: 10195.5
        --- Position End
        Sale 6685.5 shares
        --- Position Begin
      • Size: 1
      • Price: 10195.5
      • Price orig: 9264.5
      • Closed: 1
      • Opened: 1
      • Adjbase: 6685.5
        --- Position End
      posted in General Code/Help
      Z
      Zantox
    • RE: Simple Ma Crossover

      Unbenannt.png
      okay I found out how to upload it

      posted in General Code/Help
      Z
      Zantox
    • RE: Simple Ma Crossover

      I tried now the example Crossover Code from the Backtrader Home.
      https://www.backtrader.com/home/helloalgotrading/

      I got everything in the same python File and I didnt change the code and this comes out. Its so weird. I dont know what could be wrong.
      https://imgur.com/a/tZgAzPj
      It didnt work to include the picture here so I included the link. Hopefully its no Problem

      posted in General Code/Help
      Z
      Zantox
    • RE: Simple Ma Crossover

      I increased the Portfolio Value to 1000000, so it should be enough, but it isnt working. I also added the target size which was a good improvment. But still I get this problem as stated above.

      class SmaCross(bt.Strategy):
          # list of parameters which are configurable for the strategy
          params = dict(
              pfast=50,  # period for the fast moving average
              pslow=200   # period for the slow moving average
          )
      
          def __init__(self):
              sma1 = bt.ind.SMA(period=self.p.pfast)  # fast moving average
              sma2 = bt.ind.SMA(period=self.p.pslow)  # slow moving average
              self.crossover = bt.ind.CrossOver(sma1, sma2)  # crossover signal
      
          def next(self):
             
              if self.crossover > 0:  # if fast crosses slow to the upside
                  self.order_target_size(target=1)
                  print("Buy {} shares".format( self.data.close[0]))
                  print(self.position)
                      
       
              elif self.crossover < 0:  # in the market & cross to the downside
                  self.order_target_size(target=-1)
                  print("Sale {} shares".format(self.data.close[0]))
                  print(self.position)
      

      Than the Log has still the same errors. Should I use another csv file? But this cant be the problem in my opinion.

      Error Log:Starting Portfolio Value: 1000000.00
      Sale 8617.5 shares
      --- Position Begin

      • Size: 0
      • Price: 0.0
      • Price orig: 0.0
      • Closed: 0
      • Opened: 0
      • Adjbase: None
        --- Position End
        Buy 8973.0 shares
        --- Position Begin
      • Size: -1
      • Price: 8718.5
      • Price orig: 0.0
      • Closed: 0
      • Opened: -1
      • Adjbase: 8973.0
        --- Position End
        Sale 9174.0 shares
        --- Position Begin
      • Size: 1
      • Price: 8897.0
      • Price orig: 8718.5
      • Closed: 1
      • Opened: 1
      • Adjbase: 9174.0
        --- Position End

      And thats my code where I start the strategy.

      cerebro = backtrader.Cerebro()
      cerebro.adddata(data)
      cerebro.addstrategy(SmaCross)
      cerebro.broker.setcash(1000000.0)
      print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      cerebro.run()
      print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
      
      posted in General Code/Help
      Z
      Zantox
    • Simple Ma Crossover

      Hello Guys, I tried this simple cross-over strategy but it isnt working as I expect it to. I want to switch Postions when a crossover happens.
      The thing is it Buys at the exact Price it should buy or sell, but I can see in the Postiontext, that the price where it opens the postion, is an completely different price and I dont understand why.

      class SmaCross(bt.Strategy):
          # list of parameters which are configurable for the strategy
          params = dict(
              pfast=50,  # period for the fast moving average
              pslow=200   # period for the slow moving average
          )
      
          def __init__(self):
              sma1 = bt.ind.SMA(period=self.p.pfast)  # fast moving average
              sma2 = bt.ind.SMA(period=self.p.pslow)  # slow moving average
              self.crossover = bt.ind.CrossOver(sma1, sma2)  # crossover signal
      
          def next(self):
             
              if self.crossover > 0:  # if fast crosses slow to the upside
              
                  self.close()
                  print(self.position)
                  self.buy() # enter long
                  print("Buy {} shares".format( self.data.close[0]))
                  print(self.position)
                      
       
              elif self.crossover < 0:  # in the market & cross to the downside
                 
                  self.close()# close long position
                  print(self.position)
                  self.sell()
                  print("Sale {} shares".format(self.data.close[0]))
                  print(self.position)
                  
      

      Sale 8617.5 shares
      --- Position Begin

      • Size: 0
      • Price: 0.0
      • Price orig: 0.0
      • Closed: 0
      • Opened: 0
      • Adjbase: None
        --- Position End
        --- Position Begin
      • Size: -1
      • Price: 8718.5
      • Price orig: 0.0
      • Closed: 0
      • Opened: -1
      • Adjbase: 8973.0

      and Sometimes it doesnt even switch
      Buy 9622.5 shares
      --- Position Begin

      • Size: -1
      • Price: 9174.0
      • Price orig: 0.0
      • Closed: 0
      • Opened: -1
      • Adjbase: 9622.5
        --- Position End
        --- Position Begin
      • Size: 0
      • Price: 0.0
      • Price orig: 9174.0
      • Closed: 1
      • Opened: 0
      • Adjbase: 9685.0
        --- Position End

      Thank you in advance

      posted in General Code/Help
      Z
      Zantox