本文へ移動
← すべてのサンプル

サンプル 73

Notify on cross

a strategy that tells and never trades. On every closed five-minute bar a handler compares the last price with its three-bar average and, when the price has gone to the

カテゴリ
Host:ターミナルのデータとエンジンが必要
権限
orders.submit
実行する前に
a chart in hand on the live market or a replay, whose instrument the live feed serves (aggregated prints and closed bars); a hook on `strategy_signal` to hear it
何をするか
asks the terminal to run the strategy on that market (handing a strategy over asks for orders.submit, though this one never places an order); it places no order. Each notice is one `strategy_signal` (the hooks' limits apply: webhooks 1 a second and 20 a minute).
実行方法
Add script on the Market Replay window or the title bar's script icon; then Script Hooks > Hooks > Add webhook > event "A strategy gives a signal".
言語
>=0.2.0, <0.3.0
ホスト
>=0.1.24, <0.2.0
必要なもの
features >=0.1.3, <0.2.0handlers >=0.1.2, <0.2.0trade >=0.1.0, <0.2.0

ソース

// Purpose: a strategy that tells and never trades. On every closed five-minute bar a handler compares the last price with its three-bar average and, when the price has gone to the
// other side since the bar before, says so with `Act.Notify`: no order, only a notice. Put on a market it gives a live signal -- the event `strategy_signal`, named `above-average` or
// `below-average` -- so a hook attached to that event (a webhook to Discord or Telegram, or a control script such as 72_on_level_cross.tzc) reaches you the moment it happens.
// A backtest has nobody to tell and ignores the notices; run it live, or on a replay, to see them.
// Category: host
// Permissions: orders.submit
// Preconditions: a chart in hand on the live market or a replay, whose instrument the live feed serves (aggregated prints and closed bars); a hook on `strategy_signal` to hear it
// Side effects: asks the terminal to run the strategy on that market (handing a strategy over asks for orders.submit, though this one never places an order); it places no order. Each notice is one `strategy_signal` (the hooks' limits apply: webhooks 1 a second and 20 a minute).
// Run: Add script on the Market Replay window or the title bar's script icon; then Script Hooks > Hooks > Add webhook > event "A strategy gives a signal".

lang ">=0.2.0, <0.3.0";
host "ttt" ">=0.1.24, <0.2.0";
requires "features" ">=0.1.3, <0.2.0";
requires "handlers" ">=0.1.2, <0.2.0";
requires "trade" ">=0.1.0, <0.2.0";

struct Memory {
    above: Bool,
    known: Bool,
}

struct Step {
    state: Memory,
    actions: List<handlers.Act>,
}

fn begin() -> Memory {
    return Memory { above: false, known: false };
}

fn on_bar(memory: Memory, px: Float, average: Float) -> Step {
    let above = px > average;
    if memory.known && above != memory.above {
        let name = if above { "above-average" } else { "below-average" };
        let text = "the price " + to_string(px) + " is " + (if above { "over" } else { "under" }) + " its average " + to_string(average);
        return Step { state: Memory { above: above, known: true }, actions: [handlers.Act.Notify(handlers.Notice { name: name, text: text })] };
    }
    return Step { state: Memory { above: above, known: true }, actions: [] };
}

strategy crossing_notice(period: Int) version "1.0.0" {
    let px = features.price(features.Source.AggTrades);
    let average = features.sma(period);
    state = begin();
    handler bar(5) = on_bar(px, average);
}

fn main() -> Result<Unit, Error> {
    let run = trade.run(crossing_notice(3))?;
    print(run.name + " on " + run.target + ": " + run.state);
    return Ok(());
}