Strategy_concepts_2.md 2.4 KB

Strategy Configuration Ownership and Reload Semantics

1. Responsibility Split

Keep ownership clear:

  • database, stores config and identity
  • engine, loads config and creates strategy instances
  • strategy, reads config and maintains state
  • context, exposes capabilities only and binds instance identity (account_id, client_id) to execution calls

2. Context Should Not Own Config

The context is the strategy’s access boundary to the outside world. It should not become a secondary persistence layer.

If context loads config, it becomes harder to reason about, more coupled to storage, and less reusable.

Context = capabilities only.

3. Config Flow

Step 1, persisted record

record = {
    "id": "...",
    "strategy_type": "mean_rev",
    "config": {
        "risk": 0.01,
        "window": 20
    },
    "mode": "observe"
}

Step 2, engine loads the record

def load_instance(record):
    module = load_strategy(record["strategy_type"])
    context = StrategyContext(engine, record["id"], mode=record["mode"])

    instance = module.StrategyClass(
        context=context,
        config=record["config"]
    )

    running_instances[record["id"]] = instance

Step 3, strategy reads the config

risk = self.config["risk"]

That read should be conceptually read-only from the strategy’s point of view.

4. Reload Semantics

Config changes should normally mean a clean reload.

unload_instance(id)
load_instance(updated_record)

Benefits:

  • deterministic
  • easy to debug
  • no partially updated live object

Tradeoff:

  • runtime state resets

That tradeoff is acceptable and probably desirable for the first version.

5. Config vs State

Config is:

  • persistent
  • external
  • owned by the system

State is:

  • ephemeral
  • internal
  • owned by the strategy instance

Keep them separate.

6. Mode and Reload Safety

If a strategy is active, a config change should move it to a safe mode before reload, then optionally reactivate it afterward. That avoids accidental trading during transition.

7. Recommended Mental Model

Engine constructs the world
Strategy lives inside it
Context is the interface to reality
Config is part of the world, not the interface

8. Strong Default Recommendation

Use JSON config blobs in SQLite and reload on config change. That fits the current app shape well.