Skip to content
← All examples

Example 49

Footprint test

a TEST strategy, made to trade often -- to see positions open with their take-profit and their stop on the chart, and those be hit.

Category
Host: needs the terminal's data and engine
Permissions
orders.submit
Before you run it
a chart in hand on the live market or a replay, whose instrument the live feed serves (aggregated prints)
What it does
asks the terminal to run the strategy on that market; once running it places orders on the account the person chose.
How to run it
Add script on the Market Replay window or the title bar's script icon (replay or real time); Insert strategy in the Datasets window (backtest).
Language
>=0.1.2, <0.2.0
Host
>=0.1.9, <0.2.0
Needs
features >=0.1.4, <0.2.0trade >=0.1.0, <0.2.0

Source

// Purpose: a TEST strategy, made to trade often -- to see positions open with their take-profit and their stop on the chart, and those be hit.
// Not a strategy to trust with money: its edge was never measured, only how often it trades.
//
// It reads the footprint of the bar still forming, as far as a strategy can read one today: what has traded in the minute in progress
// (`forming_volume`) and its buying minus its selling (`forming_delta`), with the bar's high and low. The footprint BY PRICE LEVEL is not
// readable by a strategy yet.
//
//   ENTRY   in the minute in progress at least `min_volume` has traded, buyers are ahead of sellers by `min_delta`, and the price is at the
//           bar's high: it buys. The mirror of that -- sellers ahead, the price at the bar's low -- sells.
//   TARGET  `target` dollars in favour of the price the entry was decided at.
//   STOP    `stop` dollars against it.
//
// The target and the stop are ORDERS, left on the account with the entry (`Protect.Bracket`): once the entry fills they rest on the position it
// opened, listed and drawn like any order a person places, and the account fills them at their own price. When one is hit the other is withdrawn.
// The strategy has no rule that closes: it only opens, and each position carries its own way out.
//
// With the values below, on BTCUSDT, rules like these opened a position about every minute and a half over two hours of the recorded tape, and
// they closed in about half a minute -- two thirds at the target, one third at the stop.
// Category: host
// Permissions: orders.submit
// Preconditions: a chart in hand on the live market or a replay, whose instrument the live feed serves (aggregated prints)
// 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 (replay or real time); Insert strategy in the Datasets window (backtest).

lang ">=0.1.2, <0.2.0";
host "ttt" ">=0.1.9, <0.2.0";
requires "features" ">=0.1.4, <0.2.0";
requires "trade" ">=0.1.0, <0.2.0";

strategy footprint_test(min_delta: Decimal, min_volume: Decimal, target: Decimal, stop: Decimal, size: Quantity) version "1.1.0" {
    // The bar still forming: its extremes, what has traded in it, and who is ahead.
    let px = features.price(features.Source.AggTrades);
    let high = features.forming_high(features.Source.AggTrades);
    let low = features.forming_low(features.Source.AggTrades);
    let volume = features.forming_volume(features.Source.AggTrades);
    let delta = features.forming_delta(features.Source.AggTrades);

    rule enter_long when exposure_is(Exposure.Flat) && volume >= min_volume && delta >= min_delta && px >= high
        => order(Side.Buy, Intent.Open, OrderType.Market, Sizing.Fixed(size), Protect.Bracket(px + target, px - stop));
    rule enter_short when exposure_is(Exposure.Flat) && volume >= min_volume && delta + min_delta <= 0 && px <= low
        => order(Side.Sell, Intent.Open, OrderType.Market, Sizing.Fixed(size), Protect.Bracket(px - target, px + stop));
    flatten_on_stop;
}

fn main() -> Result<Unit, Error> {
    // One unit more bought than sold with two traded in the minute; twenty dollars to the target, twenty to the stop.
    let run = trade.run(footprint_test(1.0, 2.0, 20.0, 20.0, qty(0.01)))?;
    print(run.name + " on " + run.target + ": " + run.state);
    return Ok(());
}