Skip to content

18michal/binance_azure_sdk

Repository files navigation

Automated Cryptocurrency Trading SDK

A modular SDK for automating cryptocurrency trading using Binance API, CoinGecko and Azure services (Key Vault & SQL Database).

👉 It does not have a main.py file but instead provides usage examples in the examples/ folder.

Overview

This SDK provides:
✅ Secure retrieval of secrets (API keys) from Azure Key Vault.
✅ Real-time cryptocurrency price tracking using Binance API and CoinGecko API.
✅ Trading operations (buy/sell orders, wallet balance checks) using Binance API.
✅ Database storage for historical price data, trade history, and portfolio balances in Azure SQL.
✅ Flexible DCA (Dollar-Cost Averaging) strategy per user, based on configurable drop thresholds.
✅ Notification System – Email Alerts & Portfolio Reporting.
✅ Automatic retries & logging for robust execution.
✅ Unit tests for validating API & database interactions.

Strategy Layer – Rule-Based DCA Automation

The SDK project contains rule-based DCA (Dollar-Cost Averaging) crypto strategy with user-level configuration. The goal is to give each user control over their trading parameters and allow for low-cost, scheduled automation via Raspberry Pi.

⚠️ Note: The actual logic in dca_strategy.py is intentionally left minimal to give users flexibility in defining their own strategy logic. This keeps the SDK reusable and not opinionated.

📁 Folder Structure – Strategy

strategy/
├── dca_config_loader.py     # User strategy configuration file
├── dca_config_loader.py     # Loads and validates user strategy config
├── dca_strategy.py          # Core logic for running user-defined DCA strategy
└── src/
    ├── azure_sql.py         # Interacts with Azure SQL (e.g., price history, trade logs)
    ├── binance_trading.py   # Simplified Binance order execution helpers
    ├── dates.py             # Date range helpers (e.g., month start, today)
    └── setup.py             # Initializes Azure and Binance manager objects

Notification System – Email Alerts & Portfolio Reporting

To keep users informed and in control, this SDK includes a modular notification system via gmail.
It’s designed to send automatic alerts about low balances and portfolio performance.

📁 Folder Structure – Strategy

services/
└── notification/
    ├── base_notifier.py             # Base class for sending messages (gmail)
    ├── wallet_balance_notifier.py   # Sends alert if available funds drop below configured threshold
    └── portfolio_reporter.py        # Generates and sends portfolio summary to the user

Prerequisites

  1. Azure Setup To use this SDK, you need:

    • Azure Key Vault (for storing credentials).
    • Azure SQL Database (for storing trade & portfolio data).
    • Azure Active Directory (AAD) application with the following:
      • Client ID (AZURE_CLIENT_ID)
      • Tenant ID (AZURE_TENANT_ID)
      • Client Secret (AZURE_CLIENT_SECRET)

    Follow this tutorial to set up Azure Key Vault and retrieve these credentials.

  2. Binance API Setup

    • Create Binance API Keys: Guide
    • Create a Binance Subaccount: Guide
    • Binance Connector API Documentation: Read the Docs
  3. Configuration File (config.yaml) Before running the script, update the config.yaml file with your database and Binance trading settings:

    azure_database:
    driver: "{ODBC Driver 18 for SQL Server}"
    sql_database: "CryptoDB"
    
    binance:
    min_trade_amount: 15.0 # Fixed minimum trade amount in USDC
  4. Environment Variables (.env file) Before running the code localy, create a .env file in the project root and add:

    AZURE_CLIENT_ID="your-client-id"
    AZURE_TENANT_ID="your-tenant-id"
    AZURE_CLIENT_SECRET="your-client-secret"
    AZURE_VAULT_URL="https://your-keyvault-name.vault.azure.net"
    AZURE_VAULT_NAME="your-keyvault-name"
    
    # Binance Credentials (Stored in Azure Key Vault)
    BINANCE_API_KEY="BINANCE_API_KEY"
    BINANCE_API_SECRET="BINANCE_API_SECRET"
    
    # Azure SQL Database Credentials (Stored in Azure Key Vault)
    AZURE_SQL_SERVER="your-sql-server.database.windows.net"
    AZURE_SQL_USERNAME="your-username"
    AZURE_SQL_PASSWORD="your-password"

    👉 Important: Never share .env files or commit them to Git!

SDK Modules & Classes

  1. Modules

    Module Description
    services/azure_manager.py Handles secure retrieval of API keys from Azure Key Vault.
    services/market_manager.py Handles Binance API interactions (trades, balances, orders).
    services/crypto_market_fetcher.py Fetches data about top 100 Cryptocurrencies from CoinGecko API.
    services/src/helpers.py Configures logging system for debugging & tracking.
    services/src/market_manager_helper.py Helper functions for Binance trading logic.
  2. Class Azure Services - azure_manager.py

    • AzureKeyVaultManager Manages retrieval of secrets (API keys, database credentials) from Azure Key Vault:
      • Securely fetches API keys
      • Handles errors & logging
    • AzureDatabaseManager Manages connection to Azure SQL, storing & retrieving trade history, portfolio balances, and market data:
      • Inserts data
      • Fetches portfolio balances
      • Deletes old records
  3. Class Binance Trading - market_manager.py
    BinanceManager Handles all interactions with Binance API, including fetching wallet balances, market data, and placing trades:

    • Fetches real-time prices
    • Retrieves account type
    • Checks open orders
    • Places buy/sell orders
    • Cancels orders
    • Gets the yesterdays price for requested asset
  4. Class Market Data (CoinGecko API) - crypto_market_fetcher.py
    CoinGeckoMarketData Fetches real-time cryptocurrency market data from CoinGecko API:

    • Retrieves top 100 cryptocurrencies
    • Cleans and structures data
    • Handles API rate limits

Database Schema (Azure SQL)

Before using the SDK, create the following tables in Azure SQL Database:

  • Portfolio Balance Table (Portfolio_Balance)
  • Trade History Table (Trade_History)
  • Market Capitalization History Table (Market_Capitalization_History)
  • DCA Table Helper For Storing The Daily High Price For Each Asset (Daily_High_Price)

This schema is available to copy and use here: examples/databse_table_creation.sql
Conection to the database is based on the sql user and password.

CI/CD & Automation with Raspberry Pi

To make the trading system self-sufficient and cost-efficient, the SDK is integrated with GitHub Actions and a self-hosted Raspberry Pi runner for continuous deployment and daily execution.

This setup allows:

  • Automatic updates of your Raspberry Pi after each push to the main branch.
  • Daily and monthly automation of DCA strategy and notifications using cron.

Prerequisites

  1. Clone your project into your home directory on your Raspberry Pi

  2. Set up your self-hosted runner by following this tutorial

  3. Make the runner a service (so it survives reboots and runs in the background):

    # From inside the GitHub runner directory
    sudo ./svc.sh install
    sudo ./svc.sh start
    sudo ./svc.sh status  # To check if it's running

    This ensures your Raspberry Pi will automatically pull changes whenever a new commit lands on the main branch—no need to SSH in or run manual scripts.

  4. Automate daily/monthly tasks. To load the cron jobs:

crontab raspberry_cron.txt

Running Code & Tests tip

  1. To execute a code, use this example:
    python -m examples.azure_get_secret_key
  2. To execute unit tests, use this example:
    python -m pytest tests/test_azure_database_manager.py

Recommended Setup: Raspberry Pi for Cost Efficiency

Instead of using Azure Functions, Virtual Network and assign a static ip (which can be expensive), I recommend using a Raspberry Pi for scheduling tasks.

Support This Project – Sign Up for Binance Using My Referral

If you find this project useful and don’t have a Binance account yet, please support my work by using my referral link!
🔗 Binance Referral Link
🆔 Referral ID

About

A modular SDK for automating cryptocurrency trading using Binance API and Azure services.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages