# pyHaasAPI
A comprehensive Python library for interacting with the HaasOnline Trading Bot API.
## ๐ Quick Start
```python
from pyHaasAPI import api
from pyHaasAPI.model import CreateLabRequest
# Authenticate
executor = api.RequestsExecutor(host="127.0.0.1", port=8090, state=api.Guest())
auth_executor = executor.authenticate(email="your_email", password="your_password")
# Create a lab with proper market and account assignment
from pyHaasAPI.model import CloudMarket
market = CloudMarket(category="SPOT", price_source="BINANCE", primary="BTC", secondary="USDT")
req = CreateLabRequest.with_generated_name(
script_id="your_script_id",
account_id="your_account_id",
market=market,
exchange_code="BINANCE",
interval=1,
default_price_data_style="CandleStick"
)
lab = api.create_lab(auth_executor, req)
print(f"Lab created: {lab.lab_id}")
```
## ๐ Documentation
- [Lab Workflows & Supported Workflows](./docs/lab_workflows.md) - Unified guide to all lab management, cloning, updating, parameter handling, and bulk workflows
- [Lab Management](./docs/lab_management.md) - (Legacy) guide to creating and managing labs
- [Market and Account Assignment Fix](./docs/MARKET_ACCOUNT_ASSIGNMENT_FIX.md) - Detailed explanation of the fix for market/account assignment issues
- [API Reference](./docs/api_reference.md) - Complete API documentation
- [Examples](./examples/) - Working examples and tutorials
## ๐ง Recent Fixes
### Market and Account Assignment Fix โ
**Issue**: Labs were being created with incorrect or empty market tags and account IDs, causing them to be queued with wrong market information.
**Solution**: Fixed HTTP method and data format issues in the API layer, ensuring proper market and account assignment.
**Key Changes**:
- Fixed POST request handling for lab updates
- Added proper JSON encoding for complex objects
- Fixed indentation and syntax errors
- Enhanced parameter handling for both dict and object types
**Verification**: The `examples/lab_full_rundown.py` script now successfully creates labs with correct market tags and account IDs.
## ๐ฏ Key Features
- **Lab Management**: Create, update, clone, and delete labs (see [Lab Workflows Guide](./docs/lab_workflows.md))
- **Market Operations**: Fetch markets, prices, and order books
- **Account Management**: Manage trading accounts and balances
- **Script Management**: Upload, edit, and manage trading scripts
- **Backtesting**: Run comprehensive backtests with parameter optimization
- **Bot Management**: Create and manage live trading bots
- **Order Management**: Place and manage trading orders
## ๐ฆ Installation
```bash
pip install pyHaasAPI
```
## ๐ Authentication
```python
from pyHaasAPI import api
# Create executor
executor = api.RequestsExecutor(
host="127.0.0.1", # HaasOnline API host
port=8090, # HaasOnline API port
state=api.Guest()
)
# Authenticate
auth_executor = executor.authenticate(
email="your_email@example.com",
password="your_password"
)
```
## ๐งช Examples
### Basic Lab Creation
```python
from pyHaasAPI import api
from pyHaasAPI.model import CreateLabRequest, CloudMarket
# Setup market and account
market = CloudMarket(category="SPOT", price_source="BINANCE", primary="BTC", secondary="USDT")
account_id = "your_account_id"
script_id = "your_script_id"
# Create lab with proper market assignment
req = CreateLabRequest.with_generated_name(
script_id=script_id,
account_id=account_id,
market=market,
exchange_code="BINANCE",
interval=1,
default_price_data_style="CandleStick"
)
lab = api.create_lab(auth_executor, req)
print(f"Lab created with market: {lab.settings.market_tag}")
```
### Running a Backtest
```python
from pyHaasAPI import lab
from pyHaasAPI.domain import BacktestPeriod
# Run a 30-day backtest
period = BacktestPeriod(period_type=BacktestPeriod.Type.DAY, count=30)
results = lab.backtest(auth_executor, lab.lab_id, period)
print(f"Backtest completed with {len(results.items)} configurations")
```
### Bulk Lab Creation
```python
from pyHaasAPI.market_manager import MarketManager
# Create labs for multiple trading pairs
market_manager = MarketManager(auth_executor)
trading_pairs = ['BTC/USDT', 'ETH/USDT', 'SOL/USDT']
for pair in trading_pairs:
validation = market_manager.validate_market_setup("BINANCE", pair.split('/')[0], pair.split('/')[1])
if validation["ready"]:
# Create lab using the working pattern
req = CreateLabRequest.with_generated_name(
script_id=script_id,
account_id=validation["account"].account_id,
market=validation["market"],
exchange_code="BINANCE",
interval=1,
default_price_data_style="CandleStick"
)
lab = api.create_lab(auth_executor, req)
print(f"Created lab for {pair}: {lab.lab_id}")
```
## ๐ Market History Sync Utility
Before creating a lab or running a backtest, ensure your market is fully synced and has enough historical data:
```python
from pyHaasAPI import api
success = api.ensure_market_history_ready(executor, "BINANCE_BTC_USDT_", months=36)
if success:
print("Market is ready for lab creation or backtesting!")
else:
print("Failed to prepare market history.")
```
Use this to automate and monitor history sync for any market. See [docs/api_reference.md](./docs/api_reference.md#market-history-sync-utility) for details.
## ๐ ๏ธ Development
### Running Tests
```bash
# Run the working example
python -m examples.lab_full_rundown
# Run specific tests
python -m pytest tests/
```
### Project Structure
```
pyHaasAPI/
โโโ pyHaasAPI/ # Core library
โ โโโ api.py # API client and functions
โ โโโ lab.py # Lab management functions
โ โโโ model.py # Data models and types
โ โโโ ...
โโโ examples/ # Working examples
โ โโโ lab_full_rundown.py # Complete workflow example
โ โโโ bulk_create_labs_for_pairs.py # Bulk lab creation
โ โโโ ...
โโโ docs/ # Documentation
โ โโโ lab_management.md
โ โโโ MARKET_ACCOUNT_ASSIGNMENT_FIX.md
โ โโโ ...
โโโ tests/ # Test suite
```
## ๐ค Contributing
1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests for new functionality
5. Ensure all tests pass
6. Submit a pull request
## ๐ License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## ๐ Support
- **Documentation**: Check the [docs](./docs/) directory
- **Examples**: See the [examples](./examples/) directory
- **Issues**: Report bugs and feature requests on GitHub
## ๐ Changelog
### Latest Changes
- โ
Fixed market and account assignment issues
- โ
Enhanced lab creation with proper market tag formatting
- โ
Improved parameter handling for both dict and object types
- โ
Added comprehensive documentation and examples
- โ
Fixed HTTP method and data format issues in API layer
For detailed information about recent fixes, see [CHANGES_SUMMARY.md](CHANGES_SUMMARY.md).
Raw data
{
"_id": null,
"home_page": "https://github.com/iamcos/pyHaasAPI",
"name": "pyHaasAPI",
"maintainer": "pyHaasAPI Team",
"docs_url": null,
"requires_python": ">=3.11",
"maintainer_email": null,
"keywords": "haasonline, trading, api, cryptocurrency, automation, backtesting",
"author": "pyHaasAPI Contributors",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/65/b0/2578b268b0a289f2fd1163a4a8a322379d28dd10d8990cfa255aeb41f57e/pyhaasapi-0.1.1.tar.gz",
"platform": null,
"description": "# pyHaasAPI\n\nA comprehensive Python library for interacting with the HaasOnline Trading Bot API.\n\n## \ud83d\ude80 Quick Start\n\n```python\nfrom pyHaasAPI import api\nfrom pyHaasAPI.model import CreateLabRequest\n\n# Authenticate\nexecutor = api.RequestsExecutor(host=\"127.0.0.1\", port=8090, state=api.Guest())\nauth_executor = executor.authenticate(email=\"your_email\", password=\"your_password\")\n\n# Create a lab with proper market and account assignment\nfrom pyHaasAPI.model import CloudMarket\nmarket = CloudMarket(category=\"SPOT\", price_source=\"BINANCE\", primary=\"BTC\", secondary=\"USDT\")\n\nreq = CreateLabRequest.with_generated_name(\n script_id=\"your_script_id\",\n account_id=\"your_account_id\", \n market=market,\n exchange_code=\"BINANCE\",\n interval=1,\n default_price_data_style=\"CandleStick\"\n)\n\nlab = api.create_lab(auth_executor, req)\nprint(f\"Lab created: {lab.lab_id}\")\n```\n\n## \ud83d\udcda Documentation\n\n- [Lab Workflows & Supported Workflows](./docs/lab_workflows.md) - Unified guide to all lab management, cloning, updating, parameter handling, and bulk workflows\n- [Lab Management](./docs/lab_management.md) - (Legacy) guide to creating and managing labs\n- [Market and Account Assignment Fix](./docs/MARKET_ACCOUNT_ASSIGNMENT_FIX.md) - Detailed explanation of the fix for market/account assignment issues\n- [API Reference](./docs/api_reference.md) - Complete API documentation\n- [Examples](./examples/) - Working examples and tutorials\n\n## \ud83d\udd27 Recent Fixes\n\n### Market and Account Assignment Fix \u2705\n\n**Issue**: Labs were being created with incorrect or empty market tags and account IDs, causing them to be queued with wrong market information.\n\n**Solution**: Fixed HTTP method and data format issues in the API layer, ensuring proper market and account assignment.\n\n**Key Changes**:\n- Fixed POST request handling for lab updates\n- Added proper JSON encoding for complex objects\n- Fixed indentation and syntax errors\n- Enhanced parameter handling for both dict and object types\n\n**Verification**: The `examples/lab_full_rundown.py` script now successfully creates labs with correct market tags and account IDs.\n\n## \ud83c\udfaf Key Features\n\n- **Lab Management**: Create, update, clone, and delete labs (see [Lab Workflows Guide](./docs/lab_workflows.md))\n- **Market Operations**: Fetch markets, prices, and order books\n- **Account Management**: Manage trading accounts and balances\n- **Script Management**: Upload, edit, and manage trading scripts\n- **Backtesting**: Run comprehensive backtests with parameter optimization\n- **Bot Management**: Create and manage live trading bots\n- **Order Management**: Place and manage trading orders\n\n## \ud83d\udce6 Installation\n\n```bash\npip install pyHaasAPI\n```\n\n## \ud83d\udd11 Authentication\n\n```python\nfrom pyHaasAPI import api\n\n# Create executor\nexecutor = api.RequestsExecutor(\n host=\"127.0.0.1\", # HaasOnline API host\n port=8090, # HaasOnline API port\n state=api.Guest()\n)\n\n# Authenticate\nauth_executor = executor.authenticate(\n email=\"your_email@example.com\",\n password=\"your_password\"\n)\n```\n\n## \ud83e\uddea Examples\n\n### Basic Lab Creation\n\n```python\nfrom pyHaasAPI import api\nfrom pyHaasAPI.model import CreateLabRequest, CloudMarket\n\n# Setup market and account\nmarket = CloudMarket(category=\"SPOT\", price_source=\"BINANCE\", primary=\"BTC\", secondary=\"USDT\")\naccount_id = \"your_account_id\"\nscript_id = \"your_script_id\"\n\n# Create lab with proper market assignment\nreq = CreateLabRequest.with_generated_name(\n script_id=script_id,\n account_id=account_id,\n market=market,\n exchange_code=\"BINANCE\",\n interval=1,\n default_price_data_style=\"CandleStick\"\n)\n\nlab = api.create_lab(auth_executor, req)\nprint(f\"Lab created with market: {lab.settings.market_tag}\")\n```\n\n### Running a Backtest\n\n```python\nfrom pyHaasAPI import lab\nfrom pyHaasAPI.domain import BacktestPeriod\n\n# Run a 30-day backtest\nperiod = BacktestPeriod(period_type=BacktestPeriod.Type.DAY, count=30)\nresults = lab.backtest(auth_executor, lab.lab_id, period)\n\nprint(f\"Backtest completed with {len(results.items)} configurations\")\n```\n\n### Bulk Lab Creation\n\n```python\nfrom pyHaasAPI.market_manager import MarketManager\n\n# Create labs for multiple trading pairs\nmarket_manager = MarketManager(auth_executor)\ntrading_pairs = ['BTC/USDT', 'ETH/USDT', 'SOL/USDT']\n\nfor pair in trading_pairs:\n validation = market_manager.validate_market_setup(\"BINANCE\", pair.split('/')[0], pair.split('/')[1])\n if validation[\"ready\"]:\n # Create lab using the working pattern\n req = CreateLabRequest.with_generated_name(\n script_id=script_id,\n account_id=validation[\"account\"].account_id,\n market=validation[\"market\"],\n exchange_code=\"BINANCE\",\n interval=1,\n default_price_data_style=\"CandleStick\"\n )\n lab = api.create_lab(auth_executor, req)\n print(f\"Created lab for {pair}: {lab.lab_id}\")\n```\n\n## \ud83d\udcca Market History Sync Utility\n\nBefore creating a lab or running a backtest, ensure your market is fully synced and has enough historical data:\n\n```python\nfrom pyHaasAPI import api\nsuccess = api.ensure_market_history_ready(executor, \"BINANCE_BTC_USDT_\", months=36)\nif success:\n print(\"Market is ready for lab creation or backtesting!\")\nelse:\n print(\"Failed to prepare market history.\")\n```\n\nUse this to automate and monitor history sync for any market. See [docs/api_reference.md](./docs/api_reference.md#market-history-sync-utility) for details.\n\n## \ud83d\udee0\ufe0f Development\n\n### Running Tests\n\n```bash\n# Run the working example\npython -m examples.lab_full_rundown\n\n# Run specific tests\npython -m pytest tests/\n```\n\n### Project Structure\n\n```\npyHaasAPI/\n\u251c\u2500\u2500 pyHaasAPI/ # Core library\n\u2502 \u251c\u2500\u2500 api.py # API client and functions\n\u2502 \u251c\u2500\u2500 lab.py # Lab management functions\n\u2502 \u251c\u2500\u2500 model.py # Data models and types\n\u2502 \u2514\u2500\u2500 ...\n\u251c\u2500\u2500 examples/ # Working examples\n\u2502 \u251c\u2500\u2500 lab_full_rundown.py # Complete workflow example\n\u2502 \u251c\u2500\u2500 bulk_create_labs_for_pairs.py # Bulk lab creation\n\u2502 \u2514\u2500\u2500 ...\n\u251c\u2500\u2500 docs/ # Documentation\n\u2502 \u251c\u2500\u2500 lab_management.md\n\u2502 \u251c\u2500\u2500 MARKET_ACCOUNT_ASSIGNMENT_FIX.md\n\u2502 \u2514\u2500\u2500 ...\n\u2514\u2500\u2500 tests/ # Test suite\n```\n\n## \ud83e\udd1d Contributing\n\n1. Fork the repository\n2. Create a feature branch\n3. Make your changes\n4. Add tests for new functionality\n5. Ensure all tests pass\n6. Submit a pull request\n\n## \ud83d\udcc4 License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## \ud83c\udd98 Support\n\n- **Documentation**: Check the [docs](./docs/) directory\n- **Examples**: See the [examples](./examples/) directory\n- **Issues**: Report bugs and feature requests on GitHub\n\n## \ud83d\udd04 Changelog\n\n### Latest Changes\n- \u2705 Fixed market and account assignment issues\n- \u2705 Enhanced lab creation with proper market tag formatting\n- \u2705 Improved parameter handling for both dict and object types\n- \u2705 Added comprehensive documentation and examples\n- \u2705 Fixed HTTP method and data format issues in API layer\n\nFor detailed information about recent fixes, see [CHANGES_SUMMARY.md](CHANGES_SUMMARY.md).\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Python library for HaasOnline API - Free for individual traders and research institutions. Commercial licensing available for hedge funds and financial institutions.",
"version": "0.1.1",
"project_urls": {
"Documentation": "https://github.com/iamcos/pyHaasAPI#readme",
"Homepage": "https://github.com/iamcos/pyHaasAPI",
"Issues": "https://github.com/iamcos/pyHaasAPI/issues",
"Repository": "https://github.com/iamcos/pyHaasAPI"
},
"split_keywords": [
"haasonline",
" trading",
" api",
" cryptocurrency",
" automation",
" backtesting"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d9263918464379aa599d1dc766d3c6b6356f4d29302853289b3bbef8d9e03c1f",
"md5": "51d8720feac8b5fecc1016562ed27974",
"sha256": "7327b44ad56244e7ebc10b22a87cce2f9f2a90d896120d03bf471498a14f39a6"
},
"downloads": -1,
"filename": "pyhaasapi-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "51d8720feac8b5fecc1016562ed27974",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.11",
"size": 52236,
"upload_time": "2025-07-21T06:01:38",
"upload_time_iso_8601": "2025-07-21T06:01:38.952864Z",
"url": "https://files.pythonhosted.org/packages/d9/26/3918464379aa599d1dc766d3c6b6356f4d29302853289b3bbef8d9e03c1f/pyhaasapi-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "65b02578b268b0a289f2fd1163a4a8a322379d28dd10d8990cfa255aeb41f57e",
"md5": "3fd21daee476853c0596b48314fd0d6a",
"sha256": "da023ae14d18240c01543db0a20ca933cf9abb6e0622b0b8f266ac30b7dbbb52"
},
"downloads": -1,
"filename": "pyhaasapi-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "3fd21daee476853c0596b48314fd0d6a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.11",
"size": 111347,
"upload_time": "2025-07-21T06:01:40",
"upload_time_iso_8601": "2025-07-21T06:01:40.380816Z",
"url": "https://files.pythonhosted.org/packages/65/b0/2578b268b0a289f2fd1163a4a8a322379d28dd10d8990cfa255aeb41f57e/pyhaasapi-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-21 06:01:40",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "iamcos",
"github_project": "pyHaasAPI",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "requests",
"specs": [
[
">=",
"2.31.0"
]
]
},
{
"name": "pydantic",
"specs": [
[
">=",
"2.0.0"
]
]
},
{
"name": "loguru",
"specs": [
[
">=",
"0.7.0"
]
]
},
{
"name": "python-dotenv",
"specs": [
[
">=",
"1.0.0"
]
]
},
{
"name": "pytest",
"specs": [
[
">=",
"8.0.0"
]
]
}
],
"lcname": "pyhaasapi"
}