Navigation

    Backtrader Community

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

    JAS2210

    @JAS2210

    2
    Reputation
    1
    Profile views
    11
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    JAS2210 Unfollow Follow

    Best posts made by JAS2210

    • RE: No error shown but no results, simply double crossover

      @tony-shacklock Hi , Thank you.
      Now it works fine. But I thought when it runs blank it suggests some problems with the condition of buy or sell or in position, but those were right . I fell like sometimes backtrader don' t want to work proprely ahah maybe is jupyter notebook.
      Anyway now I will do optimazation for this 4 parameters , I' ll let you know results.

      posted in General Code/Help
      J
      JAS2210
    • RE: Display also minutes in TestStrategy with intraday data feed passad by GenericCSVData

      @vladisld
      Thanks man!
      Maybe I should read all the docs first and then the repository.

      posted in General Code/Help
      J
      JAS2210

    Latest posts made by JAS2210

    • RE: Understand long revers short logic

      @run-out right thanks, it seems unknown the value between

      posted in General Code/Help
      J
      JAS2210
    • RE: Understand long revers short logic

      @run-out ahhh ok that's what I missed..
      And what value does crosover indicatore take from the moment when cross in one direction to the moment of crossing in the opposite direction? 0 ? or None?

      posted in General Code/Help
      J
      JAS2210
    • Understand long revers short logic

      Hello,
      I find this in a blog https://backtest-rookies.com/2017/08/22/backtrader-multiple-data-feeds-indicators/?unapproved=1976&moderation-hash=b8dac3607f1017db7f47ff98c5d042e6#comment-1976

      regardless the multiple data feed. I am not fully understand the logic of the next method :

      pos = self.getposition(d).size
      if not pos:  # no market / no orders
                      if self.inds[d]['cross'][0] == 1:
                          self.buy(data=d, size=1000)
                      elif self.inds[d]['cross'][0] == -1:
                          self.sell(data=d, size=1000)
      else:
                      if self.inds[d]['cross'][0] == 1:
                          self.close(data=d)
                          self.buy(data=d, size=1000)
                      elif self.inds[d]['cross'][0] == -1:
                          self.close(data=d)
                          self.sell(data=d, size=1000)
      

      Suppose that we are not in position and first signal of the crossover is a long signal, ok we go long.

      Than I don’ t understand this:
      If the next bar is stil in crossover = 1 ( fast ma is above) what stops the self.close() command to close the the long submitted in the previous bar while open another non sense long in the same time/bar?
      What am I missing here? :/

      thx, regards

      posted in General Code/Help
      J
      JAS2210
    • RE: Accessing indicator lines in multiple datafeeds

      @leggomaeggo I guess you are right. I mean the 'd' index makes that job already right?

      posted in General Code/Help
      J
      JAS2210
    • RE: No error shown but no results, simply double crossover

      @tony-shacklock Hi , Thank you.
      Now it works fine. But I thought when it runs blank it suggests some problems with the condition of buy or sell or in position, but those were right . I fell like sometimes backtrader don' t want to work proprely ahah maybe is jupyter notebook.
      Anyway now I will do optimazation for this 4 parameters , I' ll let you know results.

      posted in General Code/Help
      J
      JAS2210
    • No error shown but no results, simply double crossover

      Hello again friends,
      I tried a simple strategy with 4 moving averages . MA1 e MA2 for a buy crossover and MA3 and MA4 for a sell/closing crossover .
      The main reason is that I want to find eventually optimezed parameters of this 4 moving average.
      But when I run no errors and no results... Where I can put some feedback like print or log to see If it is doing backtest or not? I use def stop() but nothing in the output..
      code:

      # Create a Stratey
      class TestStrategy(bt.Strategy):
          params = (
              ('ma1period', 15),
              ('ma2period', 50),
              ('ma3period', 15),
              ('ma4period', 50))
      
          def log(self, txt, dt=None, doprint=False):
              ''' Logging function fot this strategy'''
              if self.params.printlog or doprint:
                  dt = dt or self.datas[0].datetime.date(0)
                  print('%s, %s' % (dt.isoformat(), txt))
      
          def __init__(self):
              # Keep a reference to the "close" line in the data[0] dataseries
              self.dataclose = self.datas[0].close
      
              # To keep track of pending orders and buy price/commission
              self.order = None
              self.buyprice = None
              self.buycomm = None
      
              # Add a MovingAverageSimple indicator
              sma1 = bt.indicators.SimpleMovingAverage(
                  self.datas[0], period=self.params.ma1period)
              sma2 = bt.indicators.SimpleMovingAverage(
                  self.datas[0], period=self.params.ma2period)
              sma3 = bt.indicators.SimpleMovingAverage(
                  self.datas[0], period=self.params.ma3period)
              sma4 = bt.indicators.SimpleMovingAverage(
                  self.datas[0], period=self.params.ma4period)          
              
              self.crossover_b= bt.indicators.CrossOver(sma1, sma2)
              self.crossover_s= bt.indicators.CrossOver(sma3, sma4)
      
          def next(self):
              print("sto facendo")
              # Simply log the closing price of the series from the reference
              self.log('Close, %.2f' % self.dataclose[0])
      
              # Check if an order is pending ... if yes, we cannot send a 2nd one
              if self.order:
                  return
      
              # Check if we are in the market
              if not self.position:
                  #if self.rsi[0]< self.params.rsi_treshold:
                      if self.crossover_b > 0:
                          
                          # Not yet ... we MIGHT BUY if ...
                          #if self.dataclose[0] > self.sma1[0]:
      
                          # BUY, BUY, BUY!!! (with all possible default parameters)
                          self.log('BUY CREATE, %.2f' % self.dataclose[0])
                          self.buy()
      
              else:
                  if self.crossover_s < 0:  # in the market & cross to the downside the other two moving average 
                      self.close()  # close long position
      
      if __name__ == '__main__':
          # Create a cerebro entity
          cerebro = bt.Cerebro()
      
          # Add a strategy
          strats = cerebro.optstrategy(
              TestStrategy,
              ma1period=range(10, 12),
              ma2period=range(20, 22), 
              ma3period=range(10, 12), 
              ma4period=range(20, 22)
              )
          # Add the Data Feed to Cerebro
          cerebro.adddata(data)
      
          # Set our desired cash start
          cerebro.broker.setcash(1000000)
          
          cerebro.addanalyzer(btanalyzers.SharpeRatio, _name = "sharpe")
          cerebro.addanalyzer(btanalyzers.DrawDown, _name = "drawdown")
          cerebro.addanalyzer(btanalyzers.Returns, _name = "returns")
      
          # Add a FixedSize sizer according to the stake
          cerebro.addsizer(bt.sizers.FixedSize, stake=0.5)
      
          # Set the commission
          cerebro.broker.setcommission(commission=0.001)
      
          # Run over everything
          back=cerebro.run()
      
      posted in General Code/Help
      J
      JAS2210
    • RE: Simply access some values like trade.pnl

      @vladisld Yes I suspected it.
      I just to know how to move from the values results in the iteretion of the cerebro engine to for example a dataframe in pandas or an array in numpy.
      Basically from lines object to an array if I am not wrong.

      Thanks for your time.

      posted in General Code/Help
      J
      JAS2210
    • Simply access some values like trade.pnl

      Hello everyone,
      I am following the official guide (first pages).

      I would like to store trade.pnl values in a list for example. And the for example I want to get the sum of all pnl of the positions.

      I cannot do it inside this function of the class because every time trade.pnl is different? Where I have to puy an empty list and append this values? In the class "class Name_of_strategy(bt.Strategy):" or outside of the class?

          def notify_trade(self, trade):
              if not trade.isclosed:
                  return
      
              self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' %
                       (trade.pnl, trade.pnlcomm))
      

      I am lacking the real function of cerebro.run I think...

      Thank you :)

      posted in General Code/Help
      J
      JAS2210
    • RE: Display also minutes in TestStrategy with intraday data feed passad by GenericCSVData

      @vladisld
      Thanks man!
      Maybe I should read all the docs first and then the repository.

      posted in General Code/Help
      J
      JAS2210
    • RE: Display also minutes in TestStrategy with intraday data feed passad by GenericCSVData

      @run-out
      WOW it works. Thank you a lot!!

      I try to read to code in the repository but is quite complex.
      So I don't have to pass
      self.datas[0].datetime.date(0)
      But what are the differences from self.data?

      Self.datas is created in the Cerebro class as his attribute with an empty list right?
      Instead self.data are what returns backtrader.feeds.GenericCSVData.
      Sorry for asking but I still miss the various structures because there are a lot of passages.
      Anyway thanks again!!

      posted in General Code/Help
      J
      JAS2210