skip to Main Content

I am creating a program to simulate stock trading with an Alpaca paper trade account but I’m not able to get my program to work with Alpaca.

I have installed the module the documentation says to install using pip3 install alpaca_py it says it is successful, but then when I try to import it and use my API keys it gives an error that the module is not found.

Here is the code I’m trying to use (only with my actual keys):

import alpaca_py as tradeapi

# Set your Alpaca API key and secret
api_key= "api_key_here"
api_secret = "api_secret_here"

# Set the base URL for paper trading
base_url = "https://paper-api.alpaca.markets"

# Create an Alpaca API connection
api = tradeapi.REST(api_key, api_secret, base_url=base_url, api_version='v2') 

This is the error I’m getting:

Traceback (most recent call last):
  File "/home/ubuntu/environment/final_play.py", line 2, in <module>
    import alpaca_py as tradeapi
ModuleNotFoundError: No module named 'alpaca_py'

I found a couple other articles about this, but they both mentioned it being an issue with a file sharing the name of the module. I do not have a file named alpaca.py, just final_play.py

pip install alpaca-py doesn’t work to install. the output when I use this is below:

ubuntu:~/environment $ pip install alpaca-py
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Defaulting to user installation because normal site-packages is not writeable
ERROR: Could not find a version that satisfies the requirement alpaca-py (from versions: none)
ERROR: No matching distribution found for alpaca-py

If I use pip3 install alpaca-py I get the following:

ubuntu:~/environment $ pip3 install alpaca-py
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: alpaca-py in /home/ubuntu/.local/lib/python3.10/site-packages (0.13.4)
Requirement already satisfied: sseclient-py<2.0.0,>=1.7.2 in /home/ubuntu/.local/lib/python3.10/site-packages (from alpaca-py) (1.8.0)
Requirement already satisfied: pandas>=1.5.3 in /home/ubuntu/.local/lib/python3.10/site-packages (from alpaca-py) (2.1.4)
Requirement already satisfied: websockets<12.0.0,>=11.0.3 in /home/ubuntu/.local/lib/python3.10/site-packages (from alpaca-py) (11.0.3)
Requirement already satisfied: msgpack<2.0.0,>=1.0.3 in /home/ubuntu/.local/lib/python3.10/site-packages (from alpaca-py) (1.0.7)
Requirement already satisfied: pydantic<3.0.0,>=2.0.3 in /home/ubuntu/.local/lib/python3.10/site-packages (from alpaca-py) (2.5.2)
Requirement already satisfied: requests<3.0.0,>=2.30.0 in /home/ubuntu/.local/lib/python3.10/site-packages (from alpaca-py) (2.31.0)
Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from pandas>=1.5.3->alpaca-py) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in /usr/lib/python3/dist-packages (from pandas>=1.5.3->alpaca-py) (2022.1)
Requirement already satisfied: tzdata>=2022.1 in /home/ubuntu/.local/lib/python3.10/site-packages (from pandas>=1.5.3->alpaca-py) (2023.3)
Requirement already satisfied: numpy<2,>=1.22.4 in /home/ubuntu/.local/lib/python3.10/site-packages (from pandas>=1.5.3->alpaca-py) (1.26.2)
Requirement already satisfied: annotated-types>=0.4.0 in /home/ubuntu/.local/lib/python3.10/site-packages (from pydantic<3.0.0,>=2.0.3->alpaca-py) (0.6.0)
Requirement already satisfied: pydantic-core==2.14.5 in /home/ubuntu/.local/lib/python3.10/site-packages (from pydantic<3.0.0,>=2.0.3->alpaca-py) (2.14.5)
Requirement already satisfied: typing-extensions>=4.6.1 in /usr/local/lib/python3.10/dist-packages (from pydantic<3.0.0,>=2.0.3->alpaca-py) (4.7.1)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/lib/python3/dist-packages (from requests<3.0.0,>=2.30.0->alpaca-py) (1.26.5)
Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python3/dist-packages (from requests<3.0.0,>=2.30.0->alpaca-py) (2020.6.20)
Requirement already satisfied: charset-normalizer<4,>=2 in /home/ubuntu/.local/lib/python3.10/site-packages (from requests<3.0.0,>=2.30.0->alpaca-py) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/lib/python3/dist-packages (from requests<3.0.0,>=2.30.0->alpaca-py) (3.3)
Requirement already satisfied: six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil>=2.8.2->pandas>=1.5.3->alpaca-py) (1.16.0)

2

Answers


  1. Chosen as BEST ANSWER

    After further research, it seems the module alpaca-py isn't functional. I was able to install alpaca_trade_api using pip3 install alpaca_trade_api and the following code to get the connection I needed.

    import alpaca_trade_api as tradeapi
    
    # Set your Alpaca API key and secret
    api_key= "#api-key-here#"
    api_secret = "#secret-key-here#"
    
    # Set the base URL for paper trading
    base_url = "https://paper-api.alpaca.markets"
    
    # Create an Alpaca API connection
    api = tradeapi.REST(api_key, api_secret, base_url=base_url, api_version='v2')
    

  2. The alpaca-py module is functional. In the code you provided, it isn’t being called in the proper manner.

    After it is installed with pip3 install alpaca-py, the name of the module is just ‘alpaca’.

    When called in the proper manner, you can access the trade api like so:

    from alpaca.trading.client import TradingClient
    
    trading_api = TradingClient(
        api_key='<KEY_ID>',
        secret_key='<SECRET_KEY>',
        paper=True
    )
    

    Documentation Link: https://alpaca.markets/sdks/python/trading.html

    I would recommend using the alpaca-py module since it is the new Python SDK that Alpaca has created.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search