Navigation

    Backtrader Community

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

    hobart-liu

    @hobart-liu

    1
    Reputation
    776
    Profile views
    37
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    hobart-liu Unfollow Follow

    Best posts made by hobart-liu

    • RE: indicator slicing

      to close this question:

      import backtrader as bt
      import numpy as np
      import datetime
      
      class A(bt.Indicator):
          lines = ('avg',)
          def __init__(self):
              self.addminperiod(3)
          def next(self):
              self.lines.avg[0] = np.mean(self.datas[0].close.get(size=3).tolist())
      
      
      class B(bt.Indicator):
          lines = ('avg',)
          def __init__(self):
              self.addminperiod(5)
          def next(self):
              self.lines.avg[0] = np.mean(self.datas[0].close.get(size=5).tolist())
      
      class C(bt.Indicator):
          lines =('xxx',)
          def __init__(self):
              pass
          def next(self):
              print([a-b for a,b in zip(self.datas[0].avg.get(size=3).tolist(), self.datas[1].avg.get(size=3).tolist())])
      
      class TestStrategy(bt.Strategy):
          def __init__(self):
              self.avg3 = A(self.datas[0])
              self.avg5 = B(self.datas[0])
              self.delta = C(self.avg3, self.avg5)
      
          def next(self):
              pass
      
      cerebro = bt.Cerebro()
      cerebro.addstrategy(TestStrategy)
      data = bt.feeds.YahooFinanceCSVData(
          dataname='orcl-1995-2014.txt',
          fromdate=datetime.datetime(2000, 1, 1),
          todate=datetime.datetime(2000, 1, 31),
          reverse=False)
      cerebro.adddata(data)
      cerebro.run()
      
      posted in General Code/Help
      H
      hobart-liu

    Latest posts made by hobart-liu

    • Convert a Line from float to integer

      It seems the code below doesn't work

      i = int(f)
      
      posted in General Code/Help
      H
      hobart-liu
    • RE: Portfolio

      Many thanks @tracy , @backtrader

      posted in General Discussion
      H
      hobart-liu
    • Portfolio

      Say if I have a pool of 300-500 stocks, I would like to sort out top 10 stocks by ranking algo, and compute the portfolio and then test win/loss. Do we have example of this?

      posted in General Discussion
      H
      hobart-liu
    • Initialize Line

      In some cases I need to set different status in a line.
      For example

      s1 = indicator > 0.2
      s2 = indicator < -0.2
      signal = s1 - s2 
      

      the value of signal will be

       [1, nan, nan, -1, -1, nan, -1, 1, nan, nan...]
      

      I would like to know how to avoid nan in this case?

      Today I use the code below

      signal = bt.Max(0, 0)
      singal = b - s
      

      I wonder is there an efficient way to initialize signal with value 0?

      posted in General Code/Help
      H
      hobart-liu
    • RE: express 2 conditions in a efficient way

      BTW, how to initialize a line with 0?

      posted in General Code/Help
      H
      hobart-liu
    • RE: express 2 conditions in a efficient way

      thanks! so fast! :-)

      posted in General Code/Help
      H
      hobart-liu
    • express 2 conditions in a efficient way

      Say if I try to check, trix.trix cross trix.signal and trix.trix < 0, How to write it in an efficient way.
      I know I can use CrossOver(trix.trix, trix.signal), but how to add another condition, trix < 0

      posted in General Code/Help
      H
      hobart-liu
    • RE: multi processes

      thanks, I will try

      posted in General Code/Help
      H
      hobart-liu
    • multi processes

      Does backtrader support multi-process? I suppose this shall be supported. I see backtrader use multi-process when do optimizing, but I don't find how to use multi-process examples. I am asking for this because I want to scan on stocks. The strategy is same for all, but need different data feeds.

      posted in General Code/Help
      H
      hobart-liu
    • RE: Zigzag indicator

      I try to use Zigzag function to check W-bottom, M-top. A quick question, it seems you are setting indicator value by future data. For example, at day 11, you detected day 10 is valley-pivot and mark day 10 as valley.

      posted in Indicators/Strategies/Analyzers
      H
      hobart-liu