Navigation

    Backtrader Community

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

    mudassar031

    @mudassar031

    12
    Reputation
    35
    Profile views
    45
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    mudassar031 Unfollow Follow

    Best posts made by mudassar031

    • RE: Add Supertrend Indicator to Backtrader

      @nicostro
      Here is Supertrend indicator
      https://github.com/mementum/backtrader/pull/374/files

      posted in Indicators/Strategies/Analyzers
      M
      mudassar031
    • RE: TimeFrame.Days works but TimeFrame.Minutes doesn't

      @Wayne-Filkins
      resample data means if you have minute of data and you want to convert it into 15 minutes period or 30 minutes period etc...Screen Shot 2020-06-30 at 3.10.24 PM.png

      posted in General Code/Help
      M
      mudassar031
    • RE: interpreting Heikin Ashi and SuperTrend Strategy to backtrader

      @vladisld
      ok i am checking thanks for your help

      posted in Indicators/Strategies/Analyzers
      M
      mudassar031
    • Compound value Calculation

      I am translating Thinkorswim strategy into to backtrader need help translating TOS built in function in backtrader

      https://github.com/sureshja/ThinkOrSwim/blob/master/HeikinAshiSuperTrendStrategy.ts

      input aggrPd1 = AggregationPeriod.FOUR_HOURS;
      def haop1 = CompoundValue(1, (haop1[1] + hacl1[1]) / 2, (open(period = aggrPd1)[1] + close(period = aggrPd1)[1]) / 2);

      compound value reference
      https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/CompoundValue

      posted in General Code/Help
      M
      mudassar031
    • RE: TimeFrame.Days works but TimeFrame.Minutes doesn't

      Here is code i have used it and its working perfectly also there resample it

      if not alpaca_paper:
        broker = store.getbroker()  # or just alpaca_backtrader_api.AlpacaBroker()
        cerebro.setbroker(broker)
      
      DataFactory = store.getdata  # or use alpaca_backtrader_api.AlpacaData
      data0 = DataFactory(
          dataname='AAPL',
          timeframe=bt.TimeFrame.TFrame("Minutes"),
          fromdate=pd.Timestamp('2018-11-15'),
          todate=pd.Timestamp('2018-11-17'),
          historical=True)
      cerebro.adddata(data0)
      #Resampler for 15 minutes
      cerebro.resampledata(data0,timeframe=bt.TimeFrame.Minutes,compression=15)
      
      
      posted in General Code/Help
      M
      mudassar031
    • RE: TimeFrame.Days works but TimeFrame.Minutes doesn't

      @mudassar031
      full code example using alpaca api with TimeFrame.minutes and resample data

      import alpaca_backtrader_api
      import backtrader as bt
      import pandas as pd
      from datetime import datetime
      from strategies.tos_strategy import TOS
      
      from dotenv import load_dotenv
      import os
      load_dotenv()
      
      api_key = os.getenv('API_KEY_ID')
      api_secret = os.getenv('API_SECRET')
      alpaca_paper = os.getenv('ALPACA_PAPER')
      
      cerebro = bt.Cerebro()
      cerebro.addstrategy(TOS)
      
      cerebro.broker.setcash(100000)
      cerebro.broker.setcommission(commission=0.0)
      cerebro.addsizer(bt.sizers.PercentSizer, percents=20)
      
      store = alpaca_backtrader_api.AlpacaStore(
          key_id=api_key,
          secret_key=api_secret,
          paper=alpaca_paper
      )
      
      if not alpaca_paper:
        broker = store.getbroker()  # or just alpaca_backtrader_api.AlpacaBroker()
        cerebro.setbroker(broker)
      
      DataFactory = store.getdata  # or use alpaca_backtrader_api.AlpacaData
      data0 = DataFactory(
          dataname='AAPL',
          timeframe=bt.TimeFrame.TFrame("Minutes"),
          fromdate=pd.Timestamp('2018-11-15'),
          todate=pd.Timestamp('2018-11-17'),
          historical=True)
      cerebro.adddata(data0)
      
      #Resampler for 15 minutes
      cerebro.resampledata(data0,timeframe=bt.TimeFrame.Minutes,compression=15)
      
      print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      cerebro.run()
      print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      cerebro.plot()
      
      
      posted in General Code/Help
      M
      mudassar031
    • RE: TimeFrame.Days works but TimeFrame.Minutes doesn't

      @Wayne-Filkins
      perfect

      posted in General Code/Help
      M
      mudassar031
    • RE: Compound value Calculation

      @run-out
      Thanks for your help i am checking and update you if needed something

      posted in General Code/Help
      M
      mudassar031
    • Compare next bar values

      I want to compare next bar data
      self.data.close[-1] > self.l.super_trend[-1] means previous values so how can i access next bar values ?

      i am trying like this

      if self.data.close[1] > self.l.super_trend[1] and self.data.close[0] < self.l.super_trend[0]:
                     self.log('BUY CREATE, %.2f' % self.data.close[0])
                     # adds buy to self.order so we can run the check above
                     self.order = self.buy()
      

      this code throwing error
      Thanks

      posted in General Code/Help
      M
      mudassar031
    • RE: Compare next bar values

      @vladisld
      thanks for your reply
      actually i have interpreted thinkorswim strategy in backtrader i am on last step buy or sell order and they are following same way as i have mentioned above, so i want to know one thing Backtrader support AI/ML to predict future values, as you mention "As a side note: using the information from the future is not a good idea in general ( it will definitely not work in real time trading)"

      posted in General Code/Help
      M
      mudassar031

    Latest posts made by mudassar031

    • RE: Compare next bar values

      @vladisld
      thanks for your reply
      actually i have interpreted thinkorswim strategy in backtrader i am on last step buy or sell order and they are following same way as i have mentioned above, so i want to know one thing Backtrader support AI/ML to predict future values, as you mention "As a side note: using the information from the future is not a good idea in general ( it will definitely not work in real time trading)"

      posted in General Code/Help
      M
      mudassar031
    • Compare next bar values

      I want to compare next bar data
      self.data.close[-1] > self.l.super_trend[-1] means previous values so how can i access next bar values ?

      i am trying like this

      if self.data.close[1] > self.l.super_trend[1] and self.data.close[0] < self.l.super_trend[0]:
                     self.log('BUY CREATE, %.2f' % self.data.close[0])
                     # adds buy to self.order so we can run the check above
                     self.order = self.buy()
      

      this code throwing error
      Thanks

      posted in General Code/Help
      M
      mudassar031
    • 4 hours of resample data

      i am using 4 hours of resampledata

      data0 = DataFactory(
              dataname=args.symbol or "MSFT",
              timeframe=bt.TimeFrame.TFrame("Minutes"),
              fromdate=pd.Timestamp('2020-07-27'),
              todate=pd.Timestamp('2020-07-29'),
              compression=1,
              historical=True)
          cerebro.adddata(data0)
      
          cerebro.resampledata(data0, timeframe=bt.TimeFrame.Minutes, compression=60 * 4)
      

      and i am fetching 4 hours resample data in def init(self):
      like this

      self.data4h = self.datas[1]  # 240 minute resampled data
      

      its giving me error

      File "4hresample.py", line 74, in __init__
          self.data4h = self.datas[1]  # 240 minute resampled data
      IndexError: list index out of range
      
      posted in General Code/Help
      M
      mudassar031
    • RE: Grab every 4th row in data

      @mudassar031
      I have resolved it actually
      this must be

      if len(self.data) % 240 == 0:
      

      Thanks

      posted in General Code/Help
      M
      mudassar031
    • RE: Grab every 4th row in data

      @ab_trader
      another question i am using 4 hours of resampledata

       data0 = DataFactory(
              dataname=args.symbol or "MSFT",
              timeframe=bt.TimeFrame.TFrame("Minutes"),
              fromdate=pd.Timestamp('2020-07-27'),
              todate=pd.Timestamp('2020-07-29'),
              compression=1,
              historical=True)
          cerebro.adddata(data0)
      
          cerebro.resampledata(data0, timeframe=bt.TimeFrame.Minutes, compression=60 * 4)
      

      and i am fetching 4 hours resample data in def init(self):
      like this

      self.data4h = self.datas[1]  # 240 minute resampled data
      

      its giving me error

      File "4hresample.py", line 74, in __init__
          self.data4h = self.datas[1]  # 240 minute resampled data
      IndexError: list index out of range
      
      posted in General Code/Help
      M
      mudassar031
    • RE: Grab every 4th row in data

      @ab_trader
      i have minutes of data and i want to fetch 4 hours our data but it always executing one minutes on data i want only to display row after 240th row

           if len(self.data) % 240 != 0:
      
              self.log(
                  'Open: %.2f '
                  'High: %.2f '
                  'Low: %.2f '
                  'Close: %.2f '
                  % (
                      self.data.open[0],
                      self.data.high[0],
                      self.data.low[0],
                      self.data.close[0]
                  ))
      
      posted in General Code/Help
      M
      mudassar031
    • RE: Grab every 4th row in data

      @ab_trader
      Thanks

      posted in General Code/Help
      M
      mudassar031
    • RE: Grab every 4th row in data

      @run-out
      can you please give me an code example to consider every fourth row in next function?

      posted in General Code/Help
      M
      mudassar031
    • RE: Grab every 4th row in data

      @mudassar031
      if we have minutes of data and use resampling with compression=60 so backtrader give us data after averaging 60 mins actually i want to skip data in between and want to grab specific row as i have explained above example Thanks

      posted in General Code/Help
      M
      mudassar031
    • Grab every 4th row in data

      I want to grab every 4th row in data without resampling in backtrader as i have done in python here is code

      ts = TimeSeries(key='av_api_key', output_format='pandas')
      data, meta_data = ts.get_intraday(symbol='MSFT', interval='60min', outputsize='full')
      # Convert the index to datetime.
      data.index = pd.to_datetime(data.index)
      data.columns = ['Open', 'High', 'Low', 'Close', 'Volume']
      # Grabs every 4th row
      data = data.iloc[::4, :]
      
      posted in General Code/Help
      M
      mudassar031