Python client for AGuAlpha Investment Platform API
Project description
AGuAlpha Python Client
Official Python library for accessing AGuAlpha Investment Platform data.
Installation
pip install agualpha
For async support:
pip install agualpha[async]
For pandas integration:
pip install agualpha[pandas]
Quick Start
Synchronous Usage
from agualpha import AGuAlphaClient
# Initialize client
client = AGuAlphaClient(api_key="sk-your-api-key-here")
# Get positions data
positions = client.get_positions()
print(f"Total positions: {positions.total}")
for position in positions.data:
print(f"Date: {position.date}, Outstanding: {position.outstanding}")
# Get ideas data
ideas = client.get_ideas(status="active")
print(f"Total active ideas: {ideas.total}")
for idea in ideas.data:
print(f"Symbol: {idea.ticker_symbol}, Status: {idea.status}")
# Close the connection
client.close()
Using Context Manager
from agualpha import AGuAlphaClient
with AGuAlphaClient(api_key="sk-your-api-key-here") as client:
positions = client.get_positions(
start_date="2024-01-01",
end_date="2024-12-31"
)
print(f"Found {positions.total} positions")
Asynchronous Usage
import asyncio
from agualpha import AGuAlphaAsyncClient
async def main():
async with AGuAlphaAsyncClient(api_key="sk-your-api-key-here") as client:
# Fetch data concurrently
positions, ideas = await asyncio.gather(
client.get_positions(),
client.get_ideas(status="active")
)
print(f"Positions: {positions.total}, Ideas: {ideas.total}")
asyncio.run(main())
Pandas Integration
from agualpha import AGuAlphaClient
from agualpha.utils import positions_to_dataframe, export_to_csv
client = AGuAlphaClient(api_key="sk-your-api-key-here")
# Get positions and convert to DataFrame
positions_response = client.get_positions()
df = positions_to_dataframe(positions_response.data)
# Export to CSV
export_to_csv(positions_response.data, "positions.csv")
CSI Weights
Read the full csi_weights table from the prices database. The data is returned
as a pandas DataFrame for easy analysis.
from agualpha import AGuAlphaClient
with AGuAlphaClient(api_key="sk-your-api-key-here") as client:
df = client.get_csi_weights_dataframe()
print(df.head())
print(fShape: {df.shape})
If you don't need pandas, you can get the raw list of dicts instead:
from agualpha import AGuAlphaClient
with AGuAlphaClient(api_key="sk-your-api-key-here") as client:
rows = client.get_csi_weights()
print(f"Total records: {len(rows)}")
for row in rows:
print(row)
API Reference
AGuAlphaClient
__init__(api_key: str, base_url: str = "http://www.agualpha.cn:8090/api")
Initialize the client with your API key.
get_positions(start_date: Optional[str] = None, end_date: Optional[str] = None) -> PositionsResponse
Get position data from subscribed analysts.
Parameters:
start_date(str): Filter by start date (YYYY-MM-DD format)end_date(str): Filter by end date (YYYY-MM-DD format)
Returns: PositionsResponse
get_ideas(status: Optional[str] = None, direction: Optional[str] = None) -> IdeasResponse
Get trade ideas from subscribed analysts.
Parameters:
status(str): Filter by status ("active", "closed")direction(str): Filter by direction ("long", "short")
Returns: IdeasResponse
get_csi_weights() -> list
Get all CSI weights data from the prices database (csi_weights table).
Returns: list of dicts, each representing a row from the csi_weights table.
get_csi_weights_dataframe() -> pandas.DataFrame
Convenience wrapper that returns the CSI weights data directly as a pandas DataFrame. Requires the pandas extra (pip install agualpha[pandas]).
Returns: pandas.DataFrame
get_revision_fy2() -> list
Get all revision_fy2 data from the cn_af database (revision_fy2 table).
Returns: list of dicts, each representing a row from the revision_fy2 table.
get_revision_fy2_dataframe() -> pandas.DataFrame
Convenience wrapper that returns the revision_fy2 data directly as a pandas DataFrame. Requires the pandas extra (pip install agualpha[pandas]).
Returns: pandas.DataFrame
Response Models
PositionsResponse
success(bool): Request success statustotal(int): Total number of recordsdata(List[Position]): List of position objectserror(str, optional): Error message if failed
IdeasResponse
success(bool): Request success statustotal(int): Total number of recordsdata(List[Idea]): List of idea objectserror(str, optional): Error message if failed
Error Handling
from agualpha import AGuAlphaClient
from agualpha.exceptions import APIError, AuthenticationError, RateLimitError
try:
client = AGuAlphaClient(api_key="invalid-key")
positions = client.get_positions()
except AuthenticationError:
print("Invalid API key")
except RateLimitError as e:
print(f"Too many requests: {e}")
except APIError as e:
print(f"API error: {e}")
Rate Limiting
The data API limits each API key to 60 requests per minute (sliding window). When
the limit is exceeded, the server returns HTTP 429 Too Many Requests with a
Retry-After header indicating how many seconds to wait. The SDK surfaces this as a
RateLimitError (subclass of APIError), so you can catch it and back off:
import time
from agualpha import AGuAlphaClient
from agualpha.exceptions import RateLimitError
client = AGuAlphaClient(api_key="your-api-key")
while True:
try:
data = client.get_positions()
break
except RateLimitError as e:
time.sleep(5)
Requirements
- Python 3.8+
- requests 2.28.0+
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file agualpha-1.3.3.tar.gz.
File metadata
- Download URL: agualpha-1.3.3.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bd42b37d59b8a6d5456426c56a373ac7087517d039a92d1fd60b6fdc2c4927a3
|
|
| MD5 |
001945f6d9e2f62acf027f4e69631023
|
|
| BLAKE2b-256 |
4c9acb891344da4f765ed24619c005271ab0a402427b4715135889933d8e7d53
|
File details
Details for the file agualpha-1.3.3-py3-none-any.whl.
File metadata
- Download URL: agualpha-1.3.3-py3-none-any.whl
- Upload date:
- Size: 12.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8cafa7cde1981fafef84f40bc817f149ef2665549b08a66f16bba7da9b2c1a5c
|
|
| MD5 |
4b37682e62dc9be9cfa48f053e914ee5
|
|
| BLAKE2b-256 |
a7bec9372308d315c769de96b12ccd474b082be1779050aabd30b57d6bf615e3
|