Ir al contenido
← Todos los ejemplos

Ejemplo 65

Sized by risk

a strategy that does not size its own trades. It says which way, where its stop is, and `Sizing.ByRisk`: the quantity is left to the

Categoría
Host: necesita los datos y el motor de la terminal
Permisos
orders.submit
Antes de ejecutarlo
an account whose risk policy defines `fn size` (example 64), a market whose aggregated prints the terminal has.
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, with a risk script that sizes bound to that account.
Lenguaje
>=0.2.0, <0.3.0
Host
>=0.1.17, <0.2.0
Necesita
features >=0.1.8, <0.2.0trade >=0.1.0, <0.2.0

Código

// Purpose: a strategy that does not size its own trades. It says which way, where its stop is, and `Sizing.ByRisk`: the quantity is left to the
// risk policy bound to the account (example 64 risks a share of the account between the entry and the stop). If the policy declines the
// signal -- no stop, a price it cannot trust, a portfolio that is already loaded -- there is no order and the strategy waits for the next one.
// An account whose policy does not size refuses the entry; an account with no risk policy at all refuses it too: a size nobody decided is never guessed.
//
//   ENTRY   the price crosses above its average with the book flat: a market buy, stopped `stop_pct` under the entry.
//   EXIT    the price crosses back under: a close takes whatever the position holds, so its size is only a formality; the stop rides on the entry.
//
// 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: an account whose risk policy defines `fn size` (example 64), a market whose aggregated prints the terminal has.
// 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, with a risk script that sizes bound to that account.

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

strategy trend_sized_by_risk(window: Int, stop_pct: Decimal) version "1.0.0" {
    let px = features.price(features.Source.AggTrades);
    let average = features.sma(window);
    let stop = px * (1 - stop_pct / 100);
    rule enter when cross_above(px, average) && exposure_is(Exposure.Flat)
        => order(Side.Buy, Intent.Open, OrderType.Market, Sizing.ByRisk, Protect.StopLoss(stop));
    rule exit when cross_below(px, average) && exposure_is(Exposure.Open)
        => order(Side.Sell, Intent.Close, OrderType.Market, Sizing.Fixed(qty(1)));
    flatten_on_stop;
}

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