Skip to content

Instantly share code, notes, and snippets.

@miaodonghan
Last active November 1, 2022 03:49
Show Gist options
  • Save miaodonghan/b9f224d159bab7d4db6f446bafe0a467 to your computer and use it in GitHub Desktop.
Save miaodonghan/b9f224d159bab7d4db6f446bafe0a467 to your computer and use it in GitHub Desktop.

High-level design of a trading bot

Overview

flowchart LR;
    UI[UI Component - WPF/Web/etc] --> B;
    B([Trading Bot APIs]) -- list --> SR[[Strategy Registry]];
    SR --x SI[Strategy Implementations...];
    T[Tasks] --> E;
    SI o--o E[Executors Pool];
    B -- create, list, activate, deactivate --> C[[Accounts Configs / Strategy params]];
    C --> T;
    E --> L[[Logging]];
    C --x L;
Loading

APIs

TBD(donghan): API spec and protocol.

Stategy Registry

  • strategies.list

Account Config

  • accountConfigs.list
  • accountConfigs.create
  • accountConfig.activate
  • accountConfig.deactivate

SQLite Storage

AccountConfigs

  • config id
  • account credentials
  • current_strategy
  • strategy_params {}

AccountOperations (Logging, it is important to keep track of all trades that were placed)

  • operation id
  • config id
  • action (BUY/SELL)
  • operation params {}

Strategy Implementations

All strategies implement the follwing interface.

interface IStrategy {
    // Name of the stragey
    string name();

    // Strategy details
    string description();
    

    // execution logic, no blocking code is allowed.
    async Task Run();
}

For example, a strategy that does nothing.

class NoopStrategy : IStrategy {
    // Name of the stragey
    string name() {
      return "no-op";   
    }

    // Strategy details
    string description() {
        return "i do nothing";
    }
    

    // execution logic, no blocking code is allowed.
    async Task Run() {
        // Async method never holds an idle thread. This code will not impact the overall throughput.
        await Task.Delay(60000);
        System.out.println("no harm is done");
    }
}

Strategy Registry

A singleton class that contains all strategies

   public sealed class  StrategyRegistry 
   {
   
        SortedList strategies = new SortedList() {
            {StrategyA.name(), StrategyA.class},
        };
        private StrategyRegistry() { }

        private static StrategyRegistry _instance;

        public static Singleton GetInstance()
        {
            if (_instance == null)
            {
                _instance = new Singleton();
            }
            return _instance;
        }
    }

Instantiation using reflection

IStrategy strategy = (IStrategy) Activator.CreateInstance(type);
await strategy.Run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment