Backtrader Community

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

    YE

    @YE

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

    YE Unfollow Follow

    Latest posts made by YE

    • Time zone for plotting

      I get the correct time zone in print, but plot shows UTC. How do I get my time zone in plotting?

      Adding ticker TQQQ using 15Min timeframe at 15 minutes.
      .
      .
      datetime :2020-11-03 06:30:00
      datetime :2020-11-03 06:45:00
      datetime :2020-11-03 07:00:00
      .
      .
      .
      P/L: $0.0
      

      Figure_0.png

      from datetime import datetime,timezone
      import backtrader as bt
      import pytz
      from local_settings import alpaca_paper
      import alpaca_backtrader_api as alpaca
      
      ALPACA_KEY_ID = alpaca_paper['api_key']
      ALPACA_SECRET_KEY = alpaca_paper['api_secret']
      ALPACA_PAPER = True
      
      fromdate = datetime(2020,11,1)
      todate = datetime(2020,11,3)
      timezone = pytz.timezone('America/Los_Angeles')
      
      tickers = ['TQQQ']
      timeframes = {
          '15Min':15,
      
      }
      
      
              
      class MyAlgo(bt.Strategy):
          # list of parameters which are configurable for the strategy
          params = dict(
              pfast=5,  # period for the fast moving average
              pslow=10   # period for the slow moving average
          )
      
          def __init__(self):
              self.sma1 = bt.ind.SMA(self.datas[0].close,period=self.p.pfast)  # fast moving average
              self.sma2 = bt.ind.SMA(self.datas[0].close,period=self.p.pslow)  # slow moving average
             
        
          def next(self):
              # if not self.position:
              #     if self.stochrsi < 0.30:
              #         self.buy(size=100)
              # else:
              #     if self.stochrsi > 0.70 and self.position.price <= self.data.close[0]:
              #         self.sell(size=100)
              #dt_utc = datetime.fromtimestamp(self.data.datetime, timezone.utc)
              #dt_eastern = dt_utc.astimezone(pytz.timezone("US/Eastern"))
              print(f'time :{self.data.datetime.time()} ')        
      
      
      cerebro = bt.Cerebro()  # create a "Cerebro" engine instance
      cerebro.broker.set_cash(100_000)
      cerebro.addtz(pytz.timezone('America/Los_Angeles'))
      store = alpaca.AlpacaStore(
          key_id=ALPACA_KEY_ID,
          secret_key=ALPACA_SECRET_KEY,
          paper=ALPACA_PAPER
      )
      
      if not ALPACA_PAPER:
          print(f"LIVE TRADING")
          broker = store.getbroker()
          cerebro.setbroker(broker)
      
      DataFactory = store.getdata
      
      for ticker in tickers:
          for timeframe, minutes in timeframes.items():
              print(f'Adding ticker {ticker} using {timeframe} timeframe at {minutes} minutes.')
      
              data = DataFactory(
                  dataname=ticker,
                  tz=pytz.timezone('America/Los_Angeles'), #
                  timeframe=bt.TimeFrame.Minutes,
                  compression=minutes,
                  fromdate=fromdate,
                  todate=todate,
                  historical=True)
              cerebro.adddata(data)
      
      
      cerebro.addstrategy(MyAlgo)  # Add the trading strategy
      
      cerebro.run()  # run it all
      #Get final portfolio Value
      portvalue = cerebro.broker.getvalue()
      pnl = portvalue - 100_000
      
      #Print out the final result
      print('Final Portfolio Value: ${}'.format(portvalue))
      print('P/L: ${}'.format(pnl))
      
      cerebro.plot(style='candlestick', barup='green',volume=False, bardown='red',tz=pytz.timezone('America/Los_Angeles'))  # and plot it with a single command
      posted in General Code/Help
      Y
      YE