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/

    calculating SMA for multiple datas

    Indicators/Strategies/Analyzers
    1
    2
    84
    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.
    • Daily Tasks
      Daily Tasks last edited by

      Greetings,

      I am adding data to cerebro in the following manner,

      url = "https://archives.nseindia.com/content/indices/ind_nifty50list.csv"
              # url = r"C:\Users\s13rw81\Desktop\ind_nifty50list.csv"
              symbols_list = pandas.read_csv(url, usecols=[2], names=["symbol"], header=0)
              symbols_list = symbols_list[:2]
              for index, row in symbols_list.iterrows():
                  dx = yfinance.download(tickers=row['symbol'] + ".NS", actions=False, period="1y", interval="1d", threads=True, progress=False)
                  cerebro.adddata(bt.feeds.PandasData(name=row['symbol'], dataname=dx, tz=timezone))
      

      How do i calculate SMA for each of these symbols and also calculate crossovers.

      My __init__() function looks like this,

      class Strategy(StandardStrategy):
          get_parameter = id.Scenario()
          params = get_parameter.all_param()
      
          def __init__(self):
              self.sma = bt.ind.SMA(period=44)
              self.crossover = bt.ind.CrossOver(self.sma, self.data.close)
      

      It looks like, the crossover is being calculated only for the first data added to the cerebro.

      Kindly help me with the above issue and also teach me how to check the crossovers.
      I need to check when

      1. the value of sma crosses above the close price of the symbol and
      2. the value of sma crosses below the close price of the symbol

      TIA

      1 Reply Last reply Reply Quote 0
      • Daily Tasks
        Daily Tasks last edited by

        if i calculate the moving average in the following manner, i do know know how to refer to the moving average for a particular date.

        def __init__(self):
                for d in self.datas:
                    self.sma[d.params.name] = bt.ind.SMA(period=44)
                    self.crossover[d.params.name] = bt.ind.CrossOver(self.sma[d], d.close)
        

        @backtrader could you kindly take a look at this.

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