본문으로 건너뛰기
← 모든 예제

예제 58

Scale out breakout

a trade managed with resting exits. A breakout enters on a stop order that carries its own bracket; half of the position is taken at a

분류
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: a trade managed with resting exits. A breakout enters on a stop order that carries its own bracket; half of the position is taken at a
// limit above (post-only, withdrawn if it has not filled in 20 minutes), and what remains leaves through a stop-limit if price falls back under the
// range. Each exit is an order of the account, linked to the position it leaves: it never trades more than the position holds and goes if the
// position is closed first. While one rests, the book reads `Closing`, so the rule that waits for `Exposure.Open` does not stack a second exit on it.
//
//   ENTRY    a buy stop at the range high (`OrderType.Stop`), with a target and a stop on the position it opens (`Protect.Bracket`).
//   SCALE    half taken at a post-only limit one range above the entry, expiring in 20 minutes (`OrderOption.PostOnly`, `OrderOption.Expire`).
//   EXIT     the rest through a stop-limit under the range high (`OrderType.StopLimit`).
//
// HONEST NOTE: written to the specification and checked that it compiles and runs on a dataset; 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 scale_out_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;
    let entry = features.entry_price();
    rule break_up when exposure_is(Exposure.Flat) && px < high
        => order(Side.Buy, Intent.Open, OrderType.Stop(high), Sizing.Fixed(size), Protect.Bracket(high + 2 * range, low));
    rule take_half when exposure_is(Exposure.Open) && px > entry
        => order(Side.Sell, Intent.Reduce, OrderType.Limit(entry + range), Sizing.PercentOfPosition(percent(50)), OrderOption.PostOnly, OrderOption.Expire(20));
    rule fall_back when exposure_is(Exposure.Open) && px < high
        => order(Side.Sell, Intent.Close, OrderType.StopLimit(high - range / 4, high - range / 2), Sizing.Fixed(size));
    flatten_on_stop;
}

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