Strategy_concepts_examples.md 1.8 KB

Strategy Concepts Examples

1. Minimal strategy example

from strategy_sdk import Strategy

class MyStrategy(Strategy):
    CONFIG_SCHEMA = {
        "risk": {"type": "float", "default": 0.01},
        "window": {"type": "int", "default": 20}
    }

    def init(self):
        return {
            "prices": [],
            "position": 0
        }

    def on_tick(self, tick):
        self.state["prices"].append(tick["price"])

    def render(self):
        return {
            "widgets": [
                {"type": "line_chart", "data": self.state["prices"]}
            ]
        }

2. Base class contract

class Strategy:
    CONFIG_SCHEMA = {}

    def __init__(self, context, config):
        self.context = context
        self.config = config
        self.state = self.init()

    def init(self):
        return {}

    def on_tick(self, tick):
        pass

    def render(self):
        return {"widgets": []}

3. Config schema example

CONFIG_SCHEMA = {
    "risk": {
        "type": "float",
        "default": 0.01,
        "min": 0.0,
        "max": 1.0
    },
    "window": {
        "type": "int",
        "default": 20
    }
}

4. Context sketch

class StrategyContext:
    def get_price(self):
        raise NotImplementedError

    def place_order(self, side, amount):
        raise NotImplementedError

    def get_orders(self):
        raise NotImplementedError

    def place_order(self, side, amount, client_id=None):
        raise NotImplementedError

    def log(self, message):
        raise NotImplementedError

5. Lifecycle

Instantiate -> init() -> on_tick() -> render()

6. File structure

strategy_sdk/
  __init__.py
  base.py
  context.py
  config.py (optional)
  ui.py (optional)

strategies/
  my_strategy.py