Skip to content

Py-Alpaca-Api is a python package aimed at simplifying communication with Alpaca Markets REST API

License

Notifications You must be signed in to change notification settings

TexasCoding/py-alpaca-api

Repository files navigation

project-logo

PY-ALPACA-API

Empower Seamless Trading with Comprehensive API Control

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

Developed with the software and tools below.

precommit Poetry Python GitHub%20Actions pandas Pytest NumPy


Table of Contents

Overview

The py-alpaca-api project offers a comprehensive interface for algorithmic trading and data retrieval using Alpacas APIs. It centralizes key functionalities such as managing accounts, assets, orders, positions, and watchlists, while also supporting market screening, historical data analysis, and efficient HTTP request handling. By integrating streamlined configurations and continuous testing workflows, py-alpaca-api ensures a reliable, robust, and consistent trading experience for developers, enhancing their financial market engagements with seamless programmatic access and transaction management. Designed for interoperability, the project fosters smooth trading operations, accommodating diverse financial strategies and requirements.


Features

Feature Description
βš™οΈ Architecture The project follows a modular architecture with isolated components for various API interactions such as orders, positions, and market data. The structure is organized under the py_alpaca_api/src/ directory, reflecting single-responsibility principles.
πŸ”© Code Quality Code maintains high standards with tools like black, isort, and flake8 enforcing coding style, formatting, and linting rules. The repositories structure and coding practices foster readability and maintainability.
πŸ“„ Documentation The repository includes pyproject.toml and README.md with good initial documentation. However, additional inline documentation and comprehensive examples could benefit users, enhancing understandability.
πŸ”Œ Integrations Integrates with Alpaca Trading and Data APIs, supporting diverse functionalities related to stock market data and account management. Uses Github Actions for CI/CD and includes dependencies like requests and pandas.
🧩 Modularity Highly modular with components such as account, asset, and order encapsulated separately for improved reusability. Each component performs discrete tasks consistent with single-responsibility principles.
πŸ§ͺ Testing Utilizes pytest and pytest-mock for testing, ensuring reliability with unit tests and mocking capabilities. Continuous integration is set up in .github/workflows/test-package.yml ensuring automated testing on commits.
⚑️ Performance The project leverages efficient data structures provided by pandas for performance in data retrieval and manipulation. Network operations are facilitated by efficient HTTP request handling techniques minimizing latency.
πŸ›‘οΈ Security Secure access to Alpaca APIs is handled through customizable credential management enabling differentiating between live and paper trading. Data interactions are confinably validated and authenticated with reliable libraries.
πŸ“¦ Dependencies Relies on several key libraries like requests, pandas, numpy, pytz, and python-dateutil for its core operations, ensuring extensive functionality dealing with web requests, timezones, and data manipulation.
πŸš€ Scalability Designed to scale by handling increased API requests for market data and trade executions. Its modular architectural decisions enable easy upgrades and horizontal scaling capabilities that mitigate bottlenecks efficiently.

Repository Structure

└── py-alpaca-api/
    β”œβ”€β”€ 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
    β”‚       β”œβ”€β”€ requests.py
    β”‚       β”œβ”€β”€ screener.py
    β”‚       └── watchlist.py
    └── tests
        β”œβ”€β”€ __init__.py
        β”œβ”€β”€ test_account.py
        β”œβ”€β”€ test_alpaca.py
        β”œβ”€β”€ test_asset.py
        β”œβ”€β”€ test_historical_data.py
        β”œβ”€β”€ test_market.py
        β”œβ”€β”€ test_orders.py
        β”œβ”€β”€ test_position2.py
        β”œβ”€β”€ test_positions.py
        β”œβ”€β”€ test_screener.py
        └── test_watchlist.py

Modules

.
File Summary
requirements.txt Define and manage package dependencies for the py-alpaca-api project. Highlight compatibility requirements with Python versions between 3.12 and 4.0, ensuring smooth operation and interoperability of necessary packages for executing various functionalities, such as data manipulation, HTTP requests, and time zone management, within the APIs framework.
pyproject.toml Defines the configuration for the py-alpaca-api Python package, including metadata, dependencies, and development tools. It enables streamlined building, testing, and documenting, forming the backbone for package management and ensuring consistency and standardization across the development and deployment processes within the repository.
py_alpaca_api
File Summary
alpaca.py Centralizes access to Alpacas trading and data APIs, encapsulating various modules such as accounts, assets, orders, and markets. Simplifies interaction by setting up API credentials and differentiating live from paper trading environments, providing a unified interface for all functions required for algorithmic trading and data retrieval.
py_alpaca_api.src
File Summary
watchlist.py Facilitates the management of watchlists within the Alpaca trading API, enabling functionalities such as creating, retrieving, updating, and deleting watchlists. Supports handling of assets in watchlists, including adding or removing symbols, and performing API requests to ensure appropriate data retrieval and manipulation.
screener.py Provide functionality for filtering stocks including identifying gainers and losers by leveraging stock prices, volumes, trade counts, and percentage changes. Utilizes the Alpaca Data API and integrates market data to empower stock screening and investment decision-making within the repositorys architecture.
requests.py Implement a robust utility for handling HTTP requests. The utility incorporates retry logic and provides methods for GET, POST, and DELETE requests, ensuring stable communication with endpoints across the py-alpaca-api library, which supports Alpacas trading platform. This component enhances interaction reliability with the API infrastructure.
position.py Manages and interacts with user trading positions, enabling retrieval of position data, closing specific or all positions, and converting this information into structured data formats. Integral to handling Alpaca trading account portfolios dynamically, ensuring up-to-date position reporting and trading actions within the broader API architecture.
order.py Define functionality for managing various types of orders within the Alpaca API, including market, limit, stop, and trailing stop orders. Include methods for retrieving and canceling orders by ID or all at once, ensuring comprehensive order validation and appropriate handling of request parameters for accurate order processing within the trading system.
market.py Facilitates access to Alpacas market calendar and clock services, enabling users to retrieve market schedules and current market status efficiently. It plays a critical role in the repository by providing reliable methods to interact with Alpacas API, ensuring accurate and timely trading data.
history.py Provide historical stock data retrieval capabilities within the repositorys architecture, ensuring data validation, fetching, and preprocessing for various timeframes and conditions, enabling seamless integration and usage in broader API functionalities demonstrated through structured, efficient methods.
data_classes.py Data class definitions for core entities (Clock, Position, Order, Asset, Account, Watchlist) facilitate structured data representation and conversion from dictionary formats. Key utilities streamline extraction and processing of data fields, enhancing internal readability and maintainability within the overall API architecture.
asset.py Facilitates retrieval and processing of asset information from the Alpaca API, converting it into structured formats for further use. Ensures adjustable querying capabilities for asset status, class, and exchange specifics, and provides an interface for detailed symbol-specific asset information.
account.py Handle Alpaca API interactions related to account management, activity retrieval, and portfolio history. Retrieve specified account activities, current account details, and portfolio history. Return structured data as pandas DataFrames for further analysis and use in financial trading operations.
.github.workflows
File Summary
test-package.yml Enables continuous integration by defining the automated testing workflows for the py-alpaca-api project, ensuring code changes are properly tested before being merged. It specifies the testing environment and dependencies to maintain code quality and reliability through systematic test execution on each commit.

Getting Started

System Requirements:

  • Python: version 3.12.3

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:
$ pip install -r requirements.txt
# OR USE POETRY (Recommended)
$ poetry install --sync

Usage

From source

Run py-alpaca-api using the command below:

from py_alpaca_api.alpaca import PyAlpacaApi

api = PyAlpacaApi(api_key='YOUR_API_KEY', api_secret='YOUR_API_SECRET',)
api.order.market(symbol='AAPL', qty=1)

Tests

Run the test suite using the command below:

$ pytest

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 MIT License. For more details, refer to the LICENSE file.


Acknowledgments

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

Return