← Todos los ejemplos
Ejemplo 54
Trend and fade books
two independent positions on one symbol -- a trend book and a fade book -- each with its own entries, its own scaling out and its own
- Categoría
- Host: necesita los datos y el motor de la terminal
- Permisos
- orders.submit
- Antes de ejecutarlo
- a market whose aggregated prints and closed bars the terminal has, or a dataset holding aggTrades and 1-minute klines.
- Qué hace
- asks the terminal to run the strategy on that market; once running it places orders on the account the person chose.
- Cómo ejecutarlo
- Add script on the Market Replay window or the title bar's script icon; Insert strategy in the Datasets window (backtest).
- Lenguaje
>=0.2.0, <0.3.0- Host
>=0.1.13, <0.2.0- Necesita
- features >=0.1.8, <0.2.0trade >=0.1.0, <0.2.0
Código
// Purpose: two independent positions on one symbol -- a trend book and a fade book -- each with its own entries, its own scaling out and its own // exits. A rule says which book it belongs to (`for trend`); the books never net against each other in the account's accounting, so the // trend can be long while the fade is short. // // TREND long while the price is over its 20-bar average; half off after 30 minutes; the rest out when it falls back under. // FADE short when the price is stretched more than `stretch` above the average; out after 20 minutes or back at the average. // // 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 and closed bars the terminal has, or a dataset holding aggTrades and 1-minute klines. // 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 trend_and_fade(period: Int, stretch: Decimal, size: Quantity) version "1.0.0" { position trend; position fade; let px = features.price(features.Source.AggTrades); let avg = features.sma(period); let above = px > avg; let stretched = px > avg * (1.0 + stretch); rule trend_in for trend when trend.exposure_is(Exposure.Flat) && above => order(Side.Buy, Intent.Open, OrderType.Market, Sizing.Fixed(size)); rule trend_half for trend when trend.exposure_is(Exposure.Open) && trend.minutes() > 30 => order(Side.Sell, Intent.Reduce, OrderType.Market, Sizing.PercentOfPosition(percent(50))); rule trend_out for trend when trend.exposure_is(Exposure.Open) && !above => order(Side.Sell, Intent.Close, OrderType.Market, Sizing.Fixed(size)); rule fade_in for fade when fade.exposure_is(Exposure.Flat) && stretched => order(Side.Sell, Intent.Open, OrderType.Market, Sizing.Fixed(size)); rule fade_out for fade when fade.exposure_is(Exposure.Open) && (fade.minutes() > 20 || px <= avg) => order(Side.Buy, Intent.Close, OrderType.Market, Sizing.Fixed(size)); flatten_on_stop; } fn main() -> Result<Unit, Error> { let run = trade.run(trend_and_fade(20, 0.004, qty(0.02)))?; print(run.name + " on " + run.target + ": " + run.state); return Ok(()); }