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

サンプル 56

Opening range breakout

the opening-range breakout. The first 30 minutes of the UTC day set a range; a stop order rests above it and another below,

カテゴリ
Host:ターミナルのデータとエンジンが必要
権限
orders.submit
実行する前に
a market whose aggregated prints the terminal has, from before the day's open, or a dataset holding aggTrades.
何をするか
asks the terminal to run the strategy on that market; once running it places orders on the account the person chose.
実行方法
Add script on the Market Replay window or the title bar's script icon; Insert strategy in the Datasets window (backtest).
言語
>=0.2.0, <0.3.0
ホスト
>=0.1.13, <0.2.0
必要なもの
features >=0.1.8, <0.2.0trade >=0.1.0, <0.2.0

ソース

// Purpose: the opening-range breakout. The first 30 minutes of the UTC day set a range; a stop order rests above it and another below,
// and whichever price trades through first enters, with the other side of the range as its stop and the target one range away.
// The levels are made of the day's own prints and are unknown until the range is over -- and unknown, never invented, for a day the
// terminal did not see from its open.
//
//   RANGE    `opening_range_high` / `opening_range_low` of the first `minutes` minutes of the UTC day.
//   ENTRY    a buy stop at the high, a sell stop at the low (`OrderType.Stop`), once the range is over.
//   EXIT     the stop at the other side of the range, the target one range away; both ride on the stop order itself (`Protect.Bracket`)
//            and are attached to the position the moment its trigger fills.
//
// HONEST NOTE: written to the specification and checked that it compiles and hands over a strategy; its edge has not been measured.
// Category: host
// Permissions: orders.submit
// Preconditions: a market whose aggregated prints the terminal has, from before the day's open, or a dataset holding aggTrades.
// Side effects: asks the terminal to run the strategy on that market; once running it places orders on the account the person chose.
// Run: Add script on the Market Replay window or the title bar's script icon; Insert strategy in the Datasets window (backtest).

lang ">=0.2.0, <0.3.0";
host "ttt" ">=0.1.13, <0.2.0";
requires "features" ">=0.1.8, <0.2.0";
requires "trade" ">=0.1.0, <0.2.0";

strategy opening_range_breakout(minutes: Int, size: Quantity) version "1.0.0" {
    let px = features.price(features.Source.AggTrades);
    let high = features.opening_range_high(features.Source.AggTrades, minutes);
    let low = features.opening_range_low(features.Source.AggTrades, minutes);
    let range = high - low;
    rule break_up when exposure_is(Exposure.Flat) && px < high
        => order(Side.Buy, Intent.Open, OrderType.Stop(high), Sizing.Fixed(size), Protect.Bracket(high + range, low));
    rule break_down when exposure_is(Exposure.Flat) && px > low
        => order(Side.Sell, Intent.Open, OrderType.Stop(low), Sizing.Fixed(size), Protect.Bracket(low - range, high));
    flatten_on_stop;
}

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