Skip to content

Stops and targets

Attach protection when requesting a position. Percentage arguments are fractions: 0.02 means 2%.

def on_tick():
    if len(close) < 20:
        return
    if position.is_flat and close[0] > mean(close, 20):
        long(10, stop=0.02, target=0.04)

For a long position, the stop is below entry and the target above it. For a short, the directions reverse. trail=0.02 follows favourable movement; hold=5 is a five-bar time stop.

Price checks

Registered percentage protection checks the bar's high/low range rather than close alone. A trailing anchor is checked from its prior value before it updates with the current bar. Actual execution prices can differ from the level because of gaps, costs and slippage.

For an explicit close-price check, call check_stop(0.05). It requests a close at a 5% loss from that owner's confirmed average entry price: 95 for a long from 100, or 105 for a short. It returns False when your strategy/group is flat, including when another strategy holds the symbol. A held position missing its confirmed owner raises ATOM-PROTECTION-STATE. Use this action when you want to check protection yourself on each tick.

Change an existing level

def on_tick():
    if position.is_long:
        position.set_stop(0.01)

set_stop, set_target and set_trail require an open position and replace that protection. Do not call them while flat.

Plan risk separately from allocation

long(risk_pct=0.01, stop=0.05) sizes from a 1% planned loss budget and a 5% stop distance. A stop cannot guarantee the realised loss.

For exact signatures, see orders and position state.

Translate fractions into price levels

Entry at 100 stop=0.02 target=0.04
Long 98 104
Short 102 96

The quantity determines the money impact. Ten units moving two price units represents 20 before costs. This differs from risking 2% of portfolio equity.

Argument Unit and role
stop=0.02 Fractional distance from entry.
target=0.04 Fractional distance from entry in the favourable direction.
trail=0.02 Fractional distance from the favourable trailing anchor.
trail_amount=2.0 A fixed price distance from that anchor.
stop_price=98 Absolute price level.
target_price=104 Absolute price level.
hold=5 Time stop measured in bars.

Do not combine stop with stop_price, target with target_price, or trail with trail_amount. risk_pct sizing requires a fixed percentage stop; an absolute stop argument is not a substitute for that sizing contract.

Update protection deliberately

position.set_stop(0.01) replaces the registered stop with a 1% distance from the original entry, not 1% below the current price. set_target also refers to entry; set_trail uses the favourable peak/trough. These require an open position and an execution environment with protection registration support.

If both a stop and target fall within one historical bar, the bar alone cannot reveal the intrabar path. Interpret the result according to the run's protection/execution rules. A runtime-watched level is not proof of a broker-native resting order.

Protection belongs to the current holding. Fully closing it clears its stop, target, trailing registration and hold timer. For example, close a hold=5 entry after two bars, then open a new entry without hold: the earlier timer must not close the new holding three bars later. Give the new entry its own hold value when you want a new time stop.