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 Spy
TELEGRAM__API_ID=123456
TELEGRAM__API_HASH=your_api_hash
TELEGRAM__SESSION=1BQA...          # from `telegram-spy auth`
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_ID = "550e8400-e29b-41d4-a716-446655440000"  # UUID from Django
bot = MyBot(bot_id=BOT_ID, name="My Bot")
config = ClientConfig.from_settings(bot_id=BOT_ID, 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

CLI

Command Description Args
telegram-spy auth Generate session string -i API_ID -h API_HASH
telegram-spy test Test session validity -i API_ID -h API_HASH -s SESSION
telegram-spy channels List your channels -i API_ID -h API_HASH -s SESSION
telegram-spy run Run spy → Redis reads from env
telegram-spy send Send test signal BOT__TOKEN env

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.3.0.tar.gz (97.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.3.0-py3-none-any.whl (97.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: stockapis_bot-0.3.0.tar.gz
  • Upload date:
  • Size: 97.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.3.0.tar.gz
Algorithm Hash digest
SHA256 e28cb6bffcf2dd6f822d338566b04614a11fd3c0ac7e64824a1d1db35e8ffc7e
MD5 2bf331cc81079bb2c7dfe56dcc3f8d51
BLAKE2b-256 dbf90e1e74035ea47e67d44d924f65fe2d466c4a58fd8e51129e9b77b4b0a98f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: stockapis_bot-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 97.3 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b8b24eaccb6f12f706d023b6340c4e668712e9d0437da1d32c3e72631bee24d8
MD5 7b84c7b9daaa29d990773d4a74ea04d9
BLAKE2b-256 ea908523d4b0b80cd32ea311a8a7a445fc9ce1f30b46a10ee89c49ff2490faa4

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