Skip to content
SPECTRE
Xcoming soon

Examples

Three small strategies. Each states how it loses money, because yours should too.

Moving-average trend

from spectre import Strategy, Context, Target

class SmaTrend(Strategy):
    """Long when the fast average is above the slow one, short when below.
    Loses money in rangebound markets where the averages cross often."""
    interval = "1h"

    def on_bar(self, ctx: Context) -> list[Target]:
        targets = []
        for coin in ctx.markets:
            closes = ctx.closes(coin, 120)
            if len(closes) < 120:
                continue
            fast = sum(closes[-20:]) / 20
            slow = sum(closes) / 120
            weight = 0.5 / len(ctx.markets)
            targets.append(Target(coin, weight if fast > slow else -weight))
        return targets

Z-score mean reversion

from statistics import mean, pstdev
from spectre import Strategy, Context, Target

class ZScoreRevert(Strategy):
    """Fade moves more than two standard deviations from the 48-bar mean.
    Loses money when a move keeps running."""
    interval = "15m"

    def on_bar(self, ctx: Context) -> list[Target]:
        targets = []
        for coin in ctx.markets:
            closes = ctx.closes(coin, 48)
            if len(closes) < 48:
                continue
            sd = pstdev(closes) or 1e-9
            z = (closes[-1] - mean(closes)) / sd
            if abs(z) > 2:
                targets.append(Target(coin, -0.3 if z > 0 else 0.3))
            elif abs(z) < 0.5:
                targets.append(Target(coin, 0.0))
        return targets

Funding carry

from spectre import Strategy, Context, Target

class FundingCarry(Strategy):
    """Lean toward the side that receives funding, capped at 20% per market.
    Earns slowly; loses money when price moves against the tilt."""
    interval = "1h"

    def on_bar(self, ctx: Context) -> list[Target]:
        targets = []
        for coin in ctx.markets:
            rate = ctx.funding(coin)          # hourly rate, e.g. 0.0000125
            if rate is None:
                continue
            tilt = max(-0.2, min(0.2, -rate * 8000))
            targets.append(Target(coin, round(tilt, 3)))
        return targets

All three are in the SDK repository under examples/ with tests and manifests.