basana

repository·develop·Indexed 21 days ago

https://github.com/gbeced/basana

An asynchronous, event-driven Python framework (v1.11) for algorithmic trading, specifically optimized for cryptocurrencies. It supports backtesting and live trading via CCXT integration with exchanges like Binance and Bitstamp. The framework includes tools for historical data download, order book mirroring, and a comprehensive backtesting suite featuring simulated exchange behavior, fee strategies, lending schemes, and liquidity modeling to account for price slippage.

Tokens
8.5K
Snippets
29
Records
84
Agent score
74%

What's inside basana

  1. Overview of Basana framework

    develop

    Basana is an async and event-driven Python framework designed for algorithmic trading, specifically optimized for cryptocurrencies.

    The framework is composed of three primary architectural components:

    1. The Core: Contains fundamental abstractions including events, event sources, and the event dispatcher.
    2. Backtesting Exchange: A built-in component used to validate trading strategies in a simulated environment before deploying real capital.
    3. External Integrations: Provides support for live trading on exchanges like Binance and Bitstamp, as well as over 100 other cryptocurrency exchanges via the CCXT library.

    Note on Technical Indicators: Basana does not include built-in technical indicators. While the official examples use TALIpp (which is well-suited for event-driven and real-time applications), you can integrate any technical analysis library of your choice.

  2. Use basana.external.binance.spot for Binance Spot trading

    develop
    The basana.external.binance.spot module provides interfaces for interacting with the Binance Spot trading API. It includes classes for managing accounts, checking balances, and handling various order types (Standard, OCO, Open, Created, Canceled).
  3. Use the Binance Order Book Diff module

    develop

    The basana.external.binance.order_book_diff module provides tools for handling order book difference events from Binance. It is composed of three primary classes:

    1. OrderBookDiff: Represents the state of the order book differences.
    2. OrderBookDiffEvent: Represents a specific event containing order book updates.
    3. Entry: Represents an individual entry within the order book.
  4. Use the basana.external.binance.isolated_margin module

    develop
    The basana.external.binance.isolated_margin module provides interfaces for interacting with Binance Isolated Margin accounts. It primarily exposes the Account and IsolatedBalance classes to manage and query balances within specific isolated margin pairs.
  5. Use basana.external.ccxt.order_book for CCXT order book data

    develop
    The basana.external.ccxt.order_book module provides abstractions for handling order book data retrieved via the CCXT library. It defines structures for individual order book entries, the full order book state, and events representing partial updates to an order book.
  6. Use the Binance Order Book mirror in Basana

    develop

    The basana.external.binance.order_book module provides tools to mirror the Binance order book. It consists of three primary classes:

    1. PartialOrderBook: Represents the state of the order book at a specific point in time.
    2. PartialOrderBookEvent: Represents an update or event occurring within the order book.
    3. Entry: Represents an individual price level (an order or a collection of orders) within the book.

    These classes allow you to interact with real-time or historical Binance order book data within the Basana framework.

  7. Use the Bitstamp order book module

    develop

    The basana.external.bitstamp.order_book module provides tools to interface with Bitstamp's order book data. It consists of three primary classes:

    • OrderBook: The main interface for managing and accessing order book data.
    • OrderBookEvent: Represents an event or update occurring within the order book.
    • Entry: Represents an individual price level or entry within the order book.
  8. Use basana.external.binance.margin for Binance Margin trading

    develop
    The basana.external.binance.margin module provides interfaces for interacting with the Binance Margin trading API. It includes data models for managing balances, orders, and order information specific to margin accounts.
  9. How liquidity strategies work in Basana backtesting

    develop

    In Basana, liquidity strategies are used by the backtesting exchange to model market impact. They determine two critical factors for order execution:

    1. Volume Consumption: How much of a bar's total volume can be consumed by a single order.
    2. Price Slippage: The degree to which an order's size affects the execution price.

    You can choose between different implementations depending on whether you want to assume infinite liquidity or model price impact based on volume share.

  10. How strategies and position managers work together

    develop

    Basana's architecture typically separates the logic of decision-making from the logic of execution using two main components:

    1. Strategy: Implements the rules defining when to enter or exit a position. Strategies process market data (like Bar/OHLC events) and generate trading signals when market conditions meet specific criteria.
    2. Position Manager: Responsible for executing trades in response to the signals generated by the strategy. While the provided examples use market orders for simplicity, production implementations typically use limit orders.

    In a typical workflow:

    • The exchange (or a data source) generates Bar (OHLC) events.
    • The Strategy receives these bars, updates technical indicators, and pushes a trading signal if conditions are met.
    • The Position Manager receives the signal and submits a buy or sell order to the exchange.
  11. Manage Binance Margin orders with OrderInfo and OpenOrder

    develop

    The module provides several classes to track the lifecycle of margin orders:

    • OrderInfo: General information about a margin order.
    • OpenOrder: Represents an order that is currently active and unfilled in the margin market.
    • CreatedOrder: Represents a newly created margin order.
    • CanceledOrder: Represents a margin order that has been canceled.
  12. How the Basana event-driven architecture works

    develop

    Basana is built on an event-driven architecture composed of four primary components:

    1. Events: Data objects representing occurrences such as a new bar, a new trade, or an order book update.
    2. Event sources: Components (like a websocket) that push new messages when specific updates occur.
    3. Event handlers: Logic connected to specific event sources that are invoked when those sources generate new events.
    4. Event dispatcher: The core engine responsible for running the event loop and invoking event handlers in the correct order as events arrive from various sources.

    Users implement trading logic by creating a TradingSignalSource, which defines the rules for entering or exiting trades based on specific conditions.