Ir al contenido
← Todos los ejemplos

Ejemplo 55

Pyramid with state

a pyramid that remembers and that notices when an order is turned away. The strategy adds to a winning long up to `max_adds`

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: a pyramid that remembers and that notices when an order is turned away. The strategy adds to a winning long up to `max_adds`
// times, counts the adds in its own state variable, moves the stop to the average entry after the first add, and stops itself
// at the first refusal instead of carrying on blind (`halt_on_error`).
//
//   ENTRY    price breaks over the highest high of `lookback` bars (the window before the last closed bar).
//   ADD      each further `step` dollars in favour, one more entry, while adds < max_adds (`var adds`, changed with `set`).
//   STOP     under the entry by `risk`; after an add the stop sits at the average entry (`entry_price`).
//   ERRORS   a refused or rejected entry or add ends the run (`halt_on_error`).
//
// 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 pyramid(lookback: Int, step: Decimal, risk: Decimal, max_adds: Int, size: Quantity) version "1.0.0" {
    var adds = 0;
    let px = features.price(features.Source.AggTrades);
    let high = features.highest_high(lookback, 1);
    let entry = features.entry_price();
    let next_add = entry + step * (adds + 1);
    let counted = adds + 1;
    let none = 0;
    rule breakout when exposure_is(Exposure.Flat) && px > high
        => [order(Side.Buy, Intent.Open, OrderType.Market, Sizing.Fixed(size), Protect.StopLoss(px - risk)), set(adds, none)];
    rule add when exposure_is(Exposure.Open) && adds < max_adds && px >= next_add
        => [order(Side.Buy, Intent.Add, OrderType.Market, Sizing.Fixed(size)), set(adds, counted)];
    rule stop_to_average when exposure_is(Exposure.Open) && adds >= 1 && features.stop_price() < entry
        => protect(Protect.StopLoss(entry));
    halt_on_error;
    flatten_on_stop;
}

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