简体   繁体   中英

How to fix Alpaca Trade Api Authentication Error in python

I would like to run this GitHub Source. I already created paper alpaca api key_id and secret key.

...

if __name__ == '__main__':
    ...
    parser.add_argument(
        '--key-id', type=str, default='PKZCNAMQV8VY7ME04TLK',
        help='API key ID',
    )
    parser.add_argument(
        '--secret-key', type=str, default='/Qe8LfRa8Q1749qLFQRR2o/clnyW46sGLbdpMNYp',
        help='API secret key',
    )
    parser.add_argument(
        '--base-url', type=str, default='https://paper-api.alpaca.markets',
        help='set https://paper-api.alpaca.markets if paper trading',
    )
    args = parser.parse_args()
    assert args.quantity >= 100
    run(args)
...

The issue looks like that.

WARNING:root:code = 1000 (OK), no reason
ERROR:asyncio:Task was destroyed but it is pending!
task: <Task pending coro=<StreamConn._ensure_ws() running at C:\ProgramData\Anaconda3\lib\site-packages\alpaca_trade_api\stream2.py:87>>
C:\ProgramData\Anaconda3\lib\asyncio\base_events.py:604: RuntimeWarning: coroutine 'StreamConn._ensure_ws' was never awaited
  self._ready.clear()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Traceback (most recent call last):
  File ".\tick_taker.py", line 265, in <module>
    run(args)
  File ".\tick_taker.py", line 236, in run
    ['trade_updates', tc, qc]
  File "C:\ProgramData\Anaconda3\lib\site-packages\alpaca_trade_api\stream2.py", line 159, in run
    loop.run_until_complete(self.subscribe(initial_channels))
  File "C:\ProgramData\Anaconda3\lib\asyncio\base_events.py", line 579, in run_until_complete
    return future.result()
  File "C:\ProgramData\Anaconda3\lib\site-packages\alpaca_trade_api\stream2.py", line 128, in subscribe
    await self._ensure_polygon()
  File "C:\ProgramData\Anaconda3\lib\site-packages\alpaca_trade_api\stream2.py", line 85, in _ensure_polygon
    await self.polygon.connect()
  File "C:\ProgramData\Anaconda3\lib\site-packages\alpaca_trade_api\polygon\streamconn.py", line 44, in connect
    if await self.authenticate():
  File "C:\ProgramData\Anaconda3\lib\site-packages\alpaca_trade_api\polygon\streamconn.py", line 70, in authenticate
    raise ValueError('Invalid Polygon credentials, '
ValueError: Invalid Polygon credentials, Failed to authenticate: {'ev': 'status', 'status': 'auth_failed', 'message': 'authentication failed'}

I think the issue related on the env but I am not sure. Any help is appreciated.

If it were the environment, usually it says your missing a module or something similar. The actual error is authentication: ValueError: Invalid Polygon credentials, Failed to authenticate: {'ev': 'status', 'status': 'auth_failed', 'message': 'authentication failed'}

If you look at the notes in the repository, it says you need a live account because it uses polygon's data/stream, which is a different provider than Alpaca. You mentioned above paper trading, which you can do, but you have to have a funded live account to access polygon through alpaca api keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM