For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Alpaba Backtrader API - key issue



  • Hello
    I am new to Backtrader and API
    I have just followed the steps in the following link
    I have done
    (import backtrader as bt
    import alpaca_backtrader_api
    import pandas as pd)

    but I dont know how to enter API keys with backtrader,
    Could you please help me about how to enter API Keys for Alpaca backratder API? how should be the code let say for the following example?

    https://pypi.org/project/alpaca-backtrader-api/
    ALPACA_API_KEY = <123>
    ALPACA_SECRET_KEY = <123>
    ALPACA_PAPER = True

    Also, should I change the API everyday? is there anyway to enter as my Python environment variable and use it for each strategy?

    thanks

    https://medium.com/automation-generation/introducing-alpaca-backtrader-integration-898d8cabffdc



  • This code looks just like the example in the link you provided. You should be able to replace the <123> with the actual key and the secret key.

    Did you find your secret key?



  • the linke has the following, is it APCA SECRET KEY or ALPACA
    $ export APCA_API_KEY_ID=xxx
    $ export APCA_API_SECRET_KEY=yyy

    1- I am really confused about the format, if my api key is AAA
    and my secrett key is BBB, is the following format is correct?
    do i need any " ' " or any "> " in the format

    import backtrader as bt
    import alpaca_backtrader_api
    import pandas as pd

    ALPACA_API_KEY = AAA
    ALPACA_SECRET_KEY = BBB
    APCA_API_BASE_URL=https://paper-api.alpaca.markets
    ALPACA_PAPER = True

    class SmaCross(bt.SignalStrategy):

    2- I am receiving the following error when I try the above
    this bit took my attention, do I need another process other than copying the API key from my alpaca account

    "ValueError: ('Key ID must be given to access Alpaca trade API', ' (env: APCA_API_KEY_ID)')"

    ValueError Traceback (most recent call last)
    <ipython-input-33-9151770ca3db> in <module>
    21 cerebro.addstrategy(SmaCross)
    22
    ---> 23 store = alpaca_backtrader_api.AlpacaStore()
    24
    25 if is_live:

    C:\Anaconda3\envs\AlpacaBT\lib\site-packages\alpaca_backtrader_api\alpacastore.py in call(cls, *args, **kwargs)
    165 if cls._singleton is None:
    166 cls._singleton = (
    --> 167 super(MetaSingleton, cls).call(*args, **kwargs))
    168
    169 return cls._singleton

    C:\Anaconda3\envs\AlpacaBT\lib\site-packages\backtrader\metabase.py in call(cls, *args, **kwargs)
    86 _obj, args, kwargs = cls.donew(*args, **kwargs)
    87 _obj, args, kwargs = cls.dopreinit(_obj, *args, **kwargs)
    ---> 88 _obj, args, kwargs = cls.doinit(_obj, *args, **kwargs)
    89 _obj, args, kwargs = cls.dopostinit(_obj, *args, **kwargs)
    90 return _obj

    C:\Anaconda3\envs\AlpacaBT\lib\site-packages\backtrader\metabase.py in doinit(cls, _obj, *args, **kwargs)
    76
    77 def doinit(cls, _obj, *args, **kwargs):
    ---> 78 _obj.init(*args, **kwargs)
    79 return _obj, args, kwargs
    80

    C:\Anaconda3\envs\AlpacaBT\lib\site-packages\alpaca_backtrader_api\alpacastore.py in init(self)
    232 self._oenv = self._ENVLIVE
    233 self.p.base_url = self._ENV_LIVE_URL
    --> 234 self.oapi = API(self.p.key_id,
    235 self.p.secret_key,
    236 self.p.base_url,

    C:\Anaconda3\envs\AlpacaBT\lib\site-packages\alpaca_trade_api\rest.py in init(self, key_id, secret_key, base_url, api_version, oauth)
    65 oauth=None
    66 ):
    ---> 67 self._key_id, self._secret_key, self._oauth = get_credentials(
    68 key_id, secret_key, oauth)
    69 self._base_url = base_url or get_base_url()

    C:\Anaconda3\envs\AlpacaBT\lib\site-packages\alpaca_trade_api\common.py in get_credentials(key_id, secret_key, oauth)
    17 key_id = key_id or os.environ.get('APCA_API_KEY_ID')
    18 if key_id is None and oauth is None:
    ---> 19 raise ValueError('Key ID must be given to access Alpaca trade API',
    20 ' (env: APCA_API_KEY_ID)')
    21

    ValueError: ('Key ID must be given to access Alpaca trade API', ' (env: APCA_API_KEY_ID)')

    Thanks



  • @yanke_zulu said in Alpaba Backtrader API - key issue:

    ALPACA_API_KEY = AAA
    ALPACA_SECRET_KEY = BBB
    APCA_API_BASE_URL=https://paper-api.alpaca.markets
    ALPACA_PAPER = True

    I think you are going to want:

    ALPACA_API_KEY = "AAA"
    ALPACA_SECRET_KEY = "BBB"
    APCA_API_BASE_URL= "https://paper-api.alpaca.markets"
    ALPACA_PAPER = True
    



Log in to reply
 

});