Skip to main content

Python package, for communicating with Alpaca Markets REST API.

Project description

project-logo

PY-ALPACA-API

Empowering traders with precision and control.

GitHub Actions Workflow Status license last-commit repo-top-language repo-language-count


Table of Contents

Overview

Py-alpaca-api is a robust Python library facilitating seamless interaction with the Alpaca Markets REST API. It empowers developers to manage accounts, assets, market data, orders, and positions efficiently. By leveraging key modules like position, order, and history, users can access historical market data, analyze trends, and make informed trading decisions. With a focus on modularity and ease of use, this open-source project delivers essential functionalities for enhancing trading platforms and enabling strategic decision-making within the financial realm.


Features

Feature Description
โš™๏ธ Architecture The project follows a modular architecture design, allowing for separate components such as Account, Asset, Market, and Order handling. It initializes with API Key, Secret, and Paper Trading options.
๐Ÿ”ฉ Code Quality The codebase maintains good code quality with consistent style and formatting. It integrates tools like Black for code formatting and Flake8 for linting.
๐Ÿ“„ Documentation The project has extensive documentation covering various modules such as Account, Asset, and History. It provides detailed explanations of methods, parameters, and usage instructions.
๐Ÿ”Œ Integrations Key integrations include requests for HTTP requests, pandas for data manipulation, and pytest for testing. It also leverages Alpaca Markets REST API for financial data interaction.
๐Ÿงฉ Modularity The codebase is highly modular, with separate modules for different functionalities like Position, Order, and Market data retrieval. This promotes code reusability and maintainability.
๐Ÿงช Testing The project utilizes pytest for unit testing and requests-mock for mocking HTTP requests. GitHub Actions automate testing workflows for ensuring code integrity.
โšก๏ธ Performance The codebase efficiently handles data retrieval and processing, ensuring smooth interaction with the Alpaca API endpoints. Performance optimizations are achieved through streamlined historical data retrieval and market status updates.
๐Ÿ›ก๏ธ Security Security measures include handling authentication securely through API Key and Secret. The project emphasizes data protection and access control by managing account and asset information securely.
๐Ÿ“ฆ Dependencies Key external libraries and dependencies include requests, pandas, numpy for data manipulation, and pytest for testing. Additional tools like pre-commit and poetry enhance development workflows.

Repository Structure

โ””โ”€โ”€ py-alpaca-api/
    โ”œโ”€โ”€ .github
    โ”‚   โ”œโ”€โ”€ ISSUE_TEMPLATE
    โ”‚   โ”‚   โ”œโ”€โ”€ bug_report.md
    โ”‚   โ”‚   โ”œโ”€โ”€ custom.md
    โ”‚   โ”‚   โ””โ”€โ”€ feature_request.md
    โ”‚   โ””โ”€โ”€ workflows
    โ”‚       โ””โ”€โ”€ test-package.yml
    โ”œโ”€โ”€ CODE_OF_CONDUCT.md
    โ”œโ”€โ”€ CONTRIBUTING.md
    โ”œโ”€โ”€ LICENSE
    โ”œโ”€โ”€ README.md
    โ”œโ”€โ”€ SECURITY.md
    โ”œโ”€โ”€ docs
    โ”‚   โ”œโ”€โ”€ Makefile
    โ”‚   โ”œโ”€โ”€ make.bat
    โ”‚   โ”œโ”€โ”€ rtd_requirements.txt
    โ”‚   โ””โ”€โ”€ source
    โ”‚       โ”œโ”€โ”€ conf.py
    โ”‚       โ”œโ”€โ”€ index.md
    โ”‚       โ””โ”€โ”€ notebooks
    โ”œโ”€โ”€ poetry.lock
    โ”œโ”€โ”€ py_alpaca_api
    โ”‚   โ”œโ”€โ”€ __init__.py
    โ”‚   โ”œโ”€โ”€ alpaca.py
    โ”‚   โ””โ”€โ”€ src
    โ”‚       โ”œโ”€โ”€ __init__.py
    โ”‚       โ”œโ”€โ”€ account.py
    โ”‚       โ”œโ”€โ”€ asset.py
    โ”‚       โ”œโ”€โ”€ data_classes.py
    โ”‚       โ”œโ”€โ”€ history.py
    โ”‚       โ”œโ”€โ”€ market.py
    โ”‚       โ”œโ”€โ”€ order.py
    โ”‚       โ””โ”€โ”€ position.py
    โ”œโ”€โ”€ pyproject.toml
    โ”œโ”€โ”€ requirements.txt
    โ””โ”€โ”€ tests
        โ”œโ”€โ”€ __init__.py
        โ”œโ”€โ”€ test_account.py
        โ”œโ”€โ”€ test_alpaca.py
        โ”œโ”€โ”€ test_asset.py
        โ”œโ”€โ”€ test_historical_data.py
        โ”œโ”€โ”€ test_market.py
        โ”œโ”€โ”€ test_orders.py
        โ””โ”€โ”€ test_positions.py

Modules

.
File Summary
requirements.txt Ensures Python dependencies are managed for the repository, restricting versions to specific ranges. Facilitates compatibility and stable functioning by defining required packages for the Alpaca API project.
pyproject.toml Enables interaction with Alpaca Markets REST API. Manages account, asset, market data, orders, and positions. Supports Python 3.12, pandas, requests, and numpy. Integrates testing, linting, and documentation tools.
py_alpaca_api
File Summary
alpaca.py Defines PyAlpacaApi class, initializing with API Key, Secret, and Paper Trading option. Sets URLs based on trading mode and initializes Account, Asset, History, Position, Order, and Market objects for interacting with Alpaca API endpoints.
py_alpaca_api.src
File Summary
position.py Retrieves and processes account positions from Alpaca API. Includes functions to get all positions as a DataFrame, retrieve a specific position, close all positions, and close a specific position by symbol or ID.
order.py SummaryThis code file, history.py, plays a crucial role in the py-alpaca-api repository by providing essential functionalities related to historical market data retrieval and analysis within the Alpaca API ecosystem. By leveraging this module, developers can efficiently access and process historical data for strategic decision-making, enhancing the overall capabilities of the trading platform.
market.py Retrieves market clock status via HTTP GET request, handling successful and failed responses. Integrated with Alpaca API for real-time market data.
history.py Retrieve historical stock data from Alpaca API based on specified parameters. Validates asset information, handles timeframe conversions, sends API request with required parameters, and converts response to a structured DataFrame. Ensures error handling for data availability, asset validation, and API response.
data_classes.py This code file, data_classes.py, plays a critical role in defining the data structures and classes essential for modeling various financial assets and market data within the py-alpaca-api repository. By encapsulating the core data elements and relationships, this module sets the foundation for consistent and organized representation of data throughout the project.
asset.py Retrieves asset information from the Alpaca API based on the provided symbol. Parses the response into an AssetClass object containing essential details like ID, exchange, status, and more. Signals errors if retrieval fails.
account.py Retrieves account information from Alpaca API, mapping it to an AccountClass object in JSON format. Handles successful and failed responses gracefully within the repositorys modular architecture.
.github.workflows
File Summary
test-package.yml Tests package dependencies and ensures code integrity through automated workflows. Orchestrates testing for account, asset, historical data, market, orders, and positions modules. Facilitates CI/CD integration for robust repository maintenance.

Getting Started

System Requirements:

  • Python: version x.y.z

Installation

From source

  1. Clone the py-alpaca-api repository:
$ git clone https://github.com/TexasCoding/py-alpaca-api
  1. Change to the project directory:
$ cd py-alpaca-api
  1. Install the dependencies:
$ poetry install

Usage

From source

Run py-alpaca-api using the command below:

from py_alpaca_api import PyAlpacaApi
api = PyAlpacaApi(api_key='YOUR_KEY', api_secret='YOUR_SECRET', api_paper=True)
positions = api.position.get_all()
orders = api.order.get_all()
# Create new order
order = api.order.market(symbol='AAPL', qty=1.034, side='buy')

Tests

Run the test suite using the command below:

$ pytest

Project Roadmap

  • โ–บ Create functionality for all Alpaca API resources

Contributing

Contributions are welcome! Here are several ways you can contribute:

Contributing Guidelines
  1. Fork the Repository: Start by forking the project repository to your github account.
  2. Clone Locally: Clone the forked repository to your local machine using a git client.
    git clone https://github.com/TexasCoding/py-alpaca-api
    
  3. Create a New Branch: Always work on a new branch, giving it a descriptive name.
    git checkout -b new-feature-x
    
  4. Make Your Changes: Develop and test your changes locally.
  5. Commit Your Changes: Commit with a clear message describing your updates.
    git commit -m 'Implemented new feature x.'
    
  6. Push to github: Push the changes to your forked repository.
    git push origin new-feature-x
    
  7. Submit a Pull Request: Create a PR against the original project repository. Clearly describe the changes and their motivations.
  8. Review: Once your PR is reviewed and approved, it will be merged into the main branch. Congratulations on your contribution!
Contributor Graph


License

This project is protected under the SELECT-A-LICENSE License. For more details, refer to the LICENSE file.


Acknowledgments

  • List any resources, contributors, inspiration, etc. here.

Return


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_alpaca_api-0.4.4.tar.gz (24.9 kB view hashes)

Uploaded Source

Built Distribution

py_alpaca_api-0.4.4-py3-none-any.whl (26.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page