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/

    Help. Live Trade Alpaca: 'Account' object has no attribute 'portfolio_value'

    General Code/Help
    2
    3
    35
    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.
    • W
      wizardmichael last edited by

      Hi. I have some issue connecting to my alpaca account using alpaca_backtrader_api. For examples, I would like to check amount of cash and portfolio value in my alpaca live account. With the following code:

      import alpaca_backtrader_api
      import backtrader as bt
      from alpaca_account import paper_account, money_account
      
      ALPACA_API_KEY = money_account['api_key']
      ALPACA_SECRET_KEY = money_account['api_secret']
      
      if __name__ == '__main__':
      
          cerebro = bt.Cerebro()
          store = alpaca_backtrader_api.AlpacaStore(
              key_id=ALPACA_API_KEY,
              secret_key=ALPACA_SECRET_KEY,
              paper= False
          )
          
          broker = store.getbroker()  # or just alpaca_backtrader_api.AlpacaBroker()
          cerebro.setbroker(broker)
      
          print('Available Cash: %.2f' % cerebro.broker.getcash())
          print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
      

      It gave me an error. AttributeError: 'Account' object has no attribute 'portfolio_value', and Available Cash: 0.00.

      However, if I use alpaca_trade_api, with the following code,

      
      import alpaca_trade_api as tradeapi
      from alpaca_account import paper_account, money_account
      
      ALPACA_API_KEY = money_account['api_key']
      ALPACA_SECRET_KEY = money_account['api_secret']
      
      
      api = tradeapi.REST(ALPACA_API_KEY, ALPACA_SECRET_KEY, 'https://api.alpaca.markets')
      account = api.get_account()
      print('${} is available as buying power.'.format(account.buying_power))
      

      There would be no error, and the result shows "$1000 is available as buying power." Here are the versions of my packages:

      alpaca-backtrader-api=0.13.1=pypi_0
      alpaca-trade-api=0.52.0=pypi_0
      backtrader=1.9.76.123=pypi_0
      matplotlib=3.2.2=pypi_0
      numpy=1.19.4=pypi_0
      pandas=1.1.5=pypi_0
      python=3.6.12=h5500b2f_2
      scipy=1.5.4=pypi_0
      websocket-client=0.58.0=pypi_0
      websockets=8.1=pypi_0

      I believe that it should be a small issue, but I got stuck for several days, and could not find solutions online. I greatly appreciate if someone could help me out. Thank you!

      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

        @wizardmichael said in Help. Live Trade Alpaca: 'Account' object has no attribute 'portfolio_value':

        cerebro.setbroker(broker)

        CEREBRO stores information at startup to be used when running. For example, I could load 3 strategies in cerebro and they would just sit there as class files waiting to be used once cerebro runs.

        I believe your issue is you are just putting your broker class in a list in cerebro, and it hasn't been run.

        W 1 Reply Last reply Reply Quote 1
        • W
          wizardmichael @run-out last edited by

          @run-out Thanks!

          1 Reply Last reply Reply Quote 1
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
          $(document).ready(function () { app.coldLoad(); }); }