Skip to main content

Trading Bot SDK for StockAPIs - Build custom trading bots with ease

Project description

stockapis-bot

Python SDK for building trading bots with StockAPIs platform.

Features

  • gRPC streaming for real-time signals from Django
  • Binance Futures testnet/live trading
  • Redis pub/sub for event handling
  • Telegram channel signal extraction

Install

pip install stockapis-bot

Environment

# Binance TESTNET
BINANCE__API_KEY=your_testnet_api_key
BINANCE__API_SECRET=your_testnet_api_secret
BINANCE__TESTNET=true

# Django gRPC
GRPC__HOST=localhost
GRPC__PORT=50051
GRPC__API_KEY=dev-key

# Redis
REDIS__HOST=localhost
REDIS__PORT=6379
REDIS__CHANNEL=trading_signals

# Telegram (for spy)
TELEGRAM__API_ID=123456
TELEGRAM__API_HASH=your_api_hash
TELEGRAM__CHANNELS=channel1,channel2

Quick Start

from stockapis_bot import BotClient, ClientConfig, TradingBot, TradingSignal, SignalDecision

class MyBot(TradingBot):

    async def on_signal(self, signal: TradingSignal) -> SignalDecision:
        """Main handler - called for each signal."""
        if signal.confidence < 0.7:
            return SignalDecision.skip("Low confidence")

        # Calculate quantity from USDT amount
        qty = await self.calculate_quantity(signal.symbol, usdt_amount=100, leverage=10)

        if signal.is_buy_signal:
            return SignalDecision.buy(qty, "High confidence buy")
        elif signal.is_sell_signal:
            return SignalDecision.sell(qty, "High confidence sell")

        return SignalDecision.skip("Unknown signal")

    # Lifecycle hooks
    async def on_start(self) -> None:
        print(f"Bot {self.name} started")

    async def on_stop(self, reason: str) -> None:
        print(f"Bot stopped: {reason}")

    async def on_config_update(self, config) -> None:
        print(f"Config updated: active={config.active}")

# Run
bot = MyBot(bot_id="my-bot", name="My Bot")
config = ClientConfig.from_settings(bot_id="my-bot", bot_name="My Bot")
client = BotClient.create(bot, config)
await client.run()

Bot Methods

# Trading
await self.buy(symbol, quantity)
await self.sell(symbol, quantity, reduce_only=True)

# Market data
ticker = await self.get_ticker("BTCUSDT")  # ticker.price
balance = await self.get_balance()          # balance.available_usdt

# Utils
qty = await self.calculate_quantity(symbol, usdt_amount=100, leverage=10)
await self.set_leverage(symbol, leverage=20)

# State
self.is_running    # bool
self.is_active     # bool (running + config.active)
self.config        # BotConfig from Django

Commands

# Telegram Spy
make auth       # Generate session
make run        # Start spy → Redis

# Trading Bots
make bot        # gRPC Bot → Binance
make redis-bot  # Redis Bot → Binance

License

MIT

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

stockapis_bot-0.1.0.tar.gz (71.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stockapis_bot-0.1.0-py3-none-any.whl (96.5 kB view details)

Uploaded Python 3

File details

Details for the file stockapis_bot-0.1.0.tar.gz.

File metadata

  • Download URL: stockapis_bot-0.1.0.tar.gz
  • Upload date:
  • Size: 71.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for stockapis_bot-0.1.0.tar.gz
Algorithm Hash digest
SHA256 637946a69e466fd5e1315f01d5e9281e13ba2a6700d278ecfdc6d70c1c656e87
MD5 97ae0bfb74b22e0654d24043e718e014
BLAKE2b-256 d5e8e1a3ea659ac9bd410d68c5c2e3a6c79896382a5e69c6ec02ade565765d57

See more details on using hashes here.

File details

Details for the file stockapis_bot-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: stockapis_bot-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 96.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for stockapis_bot-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 319333cc0ec29fd4f00f0fe5b4a4f7682dbfd8df74c821612e18ca7ff5a77fcb
MD5 7bcfea6487fb9cc8263ad56b29e82d1e
BLAKE2b-256 11368d0b52aa77bc26b6f9acd8e60713a67f2466291567af5f5cdf06d39c4ce6

See more details on using hashes here.

Supported by

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