Skip to main content
Ultra Feature. Requires an Ultra subscription. Get started at api.mathematicalcompany.com

Triggers

Set conditions, get actions. Horizon’s trigger system evaluates rules each cycle and fires actions when conditions are met. Supports 8 condition types, 5 action types, cooldown periods, and hysteresis to prevent trigger storms. The evaluation logic is pure Rust (stateless); all mutable state lives in the Python closure.

Overview

8 Conditions

Above, below, cross above/below, percent change, absolute change, between, outside.

5 Actions

Alert, webhook, order, hedge, kill switch.

Cooldown & Hysteresis

Prevent trigger storms with configurable cooldown and hysteresis bands.

Oracle Server

hz.oracle() exposes feed prices as a JSON HTTP endpoint.

Core Functions

hz.evaluate_trigger

Evaluate a trigger rule against current market state. Pure function (no side effects).
import horizon as hz

rule = hz.create_trigger(
    name="btc_pump",
    condition=hz.TriggerCondition.Above,
    threshold=0.75,
    action=hz.TriggerActionType.Alert,
)
result = hz.evaluate_trigger(
    rule=rule,
    current_value=0.80,
    previous_value=0.70,
    last_fire_time=0.0,
    current_time=1000.0,
    armed=True,
)
if result.fired:
    print(f"Fired: {result.reason}")

hz.create_trigger

Create a trigger rule with full configuration.
rule = hz.create_trigger(
    name="volatility_spike",
    condition=hz.TriggerCondition.PercentChange,
    threshold=0.10,                     # 10% move
    action=hz.TriggerActionType.Webhook,
    threshold_upper=0.0,                # for Between/Outside conditions
    cooldown_secs=300.0,                # 5 minute cooldown
    hysteresis=0.02,                    # 2% hysteresis band
)
ParameterTypeDefaultDescription
namestrrequiredTrigger name
conditionTriggerConditionrequiredCondition type
thresholdfloatrequiredPrimary threshold
actionTriggerActionTyperequiredAction to take
threshold_upperfloat0.0Upper threshold (for Between/Outside)
cooldown_secsfloat60.0Minimum seconds between fires
hysteresisfloat0.0Hysteresis band to prevent flapping

Condition Types

ConditionFires when
Abovevalue > threshold
Belowvalue < threshold
CrossAboveprevious <= threshold AND current > threshold
CrossBelowprevious >= threshold AND current < threshold
PercentChangeabs(current - previous) / previous > threshold
AbsoluteChangeabs(current - previous) > threshold
Betweenthreshold <= value <= threshold_upper
Outsidevalue < threshold OR value > threshold_upper

Action Types

ActionDescription
AlertSend alert via AlertManager
WebhookFire HTTP POST to configured URL
OrderSubmit an order
HedgeTrigger a hedge trade
KillSwitchActivate kill switch

Pipeline Functions

hz.trigger

Evaluate multiple trigger rules each cycle.
triggers = hz.trigger(
    triggers=[
        hz.TriggerConfig(
            name="price_pump",
            feed_name="btc",
            condition="cross_above",
            threshold=0.75,
            action="alert",
            cooldown_secs=300,
        ),
        hz.TriggerConfig(
            name="price_dump",
            feed_name="btc",
            condition="cross_below",
            threshold=0.25,
            action="webhook",
            webhook_url="https://hooks.slack.com/...",
            cooldown_secs=60,
        ),
    ],
)
ParameterTypeDescription
triggerslist[TriggerConfig]Trigger configurations
alert_managerAnyOptional AlertManager for alert actions

hz.conditional_hedge

Automatically hedge when a condition is met.
hedge = hz.conditional_hedge(
    feed_name="btc",
    condition="below",
    threshold=0.3,
    hedge_size=100.0,
    cooldown_secs=300,
)

hz.oracle

Start a minimal HTTP server exposing live feed prices as JSON.
oracle = hz.oracle(port=8099)
# GET http://localhost:8099/ returns {"feeds": {"btc": 0.65, "eth": 0.42}}
ParameterTypeDefaultDescription
portint8099HTTP server port

Examples

Alert on Price Moves

import horizon as hz

hz.run(
    name="trigger_bot",
    feeds={
        "btc": hz.PolymarketBook("will-btc-hit-100k"),
        "fed": hz.PolymarketBook("will-fed-cut-rates"),
    },
    pipeline=[
        hz.trigger(triggers=[
            hz.TriggerConfig(
                name="btc_breakout", feed_name="btc",
                condition="cross_above", threshold=0.80,
                action="alert", cooldown_secs=600,
            ),
            hz.TriggerConfig(
                name="fed_crash", feed_name="fed",
                condition="percent_change", threshold=0.10,
                action="webhook",
                webhook_url="https://hooks.slack.com/services/...",
                cooldown_secs=120,
            ),
        ]),
        my_strategy,
    ],
)

Oracle for External Systems

import horizon as hz

hz.run(
    name="price_oracle",
    feeds={
        "btc": hz.PolymarketBook("will-btc-hit-100k"),
        "eth": hz.PolymarketBook("will-eth-hit-10k"),
    },
    pipeline=[hz.oracle(port=8099)],
)
# External systems can now GET http://localhost:8099/ for live prices