Lumiwealth / lumibot

Backtesting and Trading Bots Made Easy for Crypto, Stocks, Options, Futures, FOREX and more
GNU General Public License v3.0
882 stars 159 forks source link

Alpaca broker fails to authorize and/or connect... #331

Open andreylisovskiy opened 9 months ago

andreylisovskiy commented 9 months ago

I have copied initial example set up for Alpaca form "Getting Started" of Lumibot documentation. Have copied over my API and secret key, however, the connection to trading broker fails at this line of lumibot example code: strategy = MyStrategy( broker=broker, parameters= { "symbol": "SPY" })

With this error message: File "C:\kingston_drive\Pycharm_Projects\stocks_tensorflow\modules\lumibot_trading.py", line 67, in lumibot_example strategy = MyStrategy( File "C:\Python3_10\lib\site-packages\lumibot\strategies\_strategy.py", line 199, in __init__ self.update_broker_balances() File "C:\Python3_10\lib\site-packages\lumibot\strategies\_strategy.py", line 367, in update_broker_balances broker_balances = self.broker._get_balances_at_broker(self.quote_asset) File "C:\Python3_10\lib\site-packages\lumibot\brokers\alpaca.py", line 253, in _get_balances_at_broker response = self.api.get_account() File "C:\Python3_10\lib\site-packages\alpaca\trading\client.py", line 417, in get_account response = self.get("/account") File "C:\Python3_10\lib\site-packages\alpaca\common\rest.py", line 221, in get return self._request("GET", path, data, **kwargs) File "C:\Python3_10\lib\site-packages\alpaca\common\rest.py", line 129, in _request return self._one_request(method, url, opts, retry) File "C:\Python3_10\lib\site-packages\alpaca\common\rest.py", line 205, in _one_request raise APIError(error, http_error) alpaca.common.exceptions.APIError: {"code":40110000,"message":"request is not authorized"}

But I have zero issues accessing my paper trading account with same API and secret key using alpaca_trade_api python library like so: api = tradeapi.rest.REST(APCA_API_KEY_ID, APCA_API_SECRET_KEY, APCA_API_BASE_URL, api_version='v2')

I do not know why lumibot method fails. Please help!

Thanks,

grzesir commented 9 months ago

Thank you for bringing this to my attention! The docs need to be updated. Instead of:

ALPACA_CONFIG = {

Put your own Alpaca key here:

"API_KEY": "YOUR_ALPACA_API_KEY",
# Put your own Alpaca secret here:
"API_SECRET": "YOUR_ALPACA_SECRET",
# If you want to go live, you must change this. It is currently

set for paper trading "ENDPOINT": "https://paper-api.alpaca.markets"}

It should be:

ALPACA_CONFIG = {

Put your own Alpaca key here:

"API_KEY": "YOUR_ALPACA_API_KEY",
# Put your own Alpaca secret here:
"API_SECRET": "YOUR_ALPACA_SECRET",
# If you want to go live, you must change this. It is currently

set for paper trading "PAPER": True}

Let me know if this works for you.

Robert Grzesik 347-635-3416

On Thu, Dec 14, 2023 at 3:13 AM andrewlis @.***> wrote:

I have copied initial example set up for Alpaca form "Getting Started" of Lumibot documentation. Have copied over my API and secret key, however, the connection to trading broker fails at this line: strategy = MyStrategy( broker=broker, parameters= { "symbol": "SPY" })

With this error message: File "C:\kingston_drive\Pycharm_Projects\stocks_tensorflow\modules\lumibot_trading.py", line 67, in lumibot_example strategy = MyStrategy( File "C:\Python3_10\lib\site-packages\lumibot\strategies_strategy.py", line 199, in init self.update_broker_balances() File "C:\Python3_10\lib\site-packages\lumibot\strategies_strategy.py", line 367, in update_broker_balances broker_balances = self.broker._get_balances_at_broker(self.quote_asset) File "C:\Python3_10\lib\site-packages\lumibot\brokers\alpaca.py", line 253, in _get_balances_at_broker response = self.api.get_account() File "C:\Python3_10\lib\site-packages\alpaca\trading\client.py", line 417, in get_account response = self.get("/account") File "C:\Python3_10\lib\site-packages\alpaca\common\rest.py", line 221, in get return self._request("GET", path, data, **kwargs) File "C:\Python3_10\lib\site-packages\alpaca\common\rest.py", line 129, in _request return self._one_request(method, url, opts, retry) File "C:\Python3_10\lib\site-packages\alpaca\common\rest.py", line 205, in _one_request raise APIError(error, http_error) alpaca.common.exceptions.APIError: {"code":40110000,"message":"request is not authorized"}

But I have zero issues accessing my paper trading account with same API and secret key using alpaca_trade_api python library like so: api = tradeapi.rest.REST(APCA_API_KEY_ID, APCA_API_SECRET_KEY, APCA_API_BASE_URL, api_version='v2')

I do not know why lumibot method fails. Please help!

Thanks,

— Reply to this email directly, view it on GitHub https://github.com/Lumiwealth/lumibot/issues/331, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIYQK4V3IZEBSXFDKODIK3YJKYKZAVCNFSM6AAAAABAUNM7Y2VHI2DSMVQWIX3LMV43ASLTON2WKOZSGA2DCMJXGEZTMMI . You are receiving this because you are subscribed to this thread.Message ID: @.***>

andreylisovskiy commented 9 months ago

That did do it! It worked! Thank you.

grzesir commented 9 months ago

No problem. I will update the docs as well

Robert Grzesik 347-635-3416

On Thu, Dec 14, 2023 at 5:30 PM andrewlis @.***> wrote:

That did do it! It worked! Thank you.

— Reply to this email directly, view it on GitHub https://github.com/Lumiwealth/lumibot/issues/331#issuecomment-1856815103, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIYQK4H7572SWP4TXOJV23YJN4Z7AVCNFSM6AAAAABAUNM7Y2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNJWHAYTKMJQGM . You are receiving this because you commented.Message ID: @.***>