Function trailingTake

  • Adjusts the trailing take-profit distance for an active pending signal.

    CRITICAL: Always calculates from ORIGINAL TP, not from current trailing TP. This prevents error accumulation on repeated calls. Larger percentShift ABSORBS smaller one (updates only towards more conservative TP).

    Updates the take-profit distance by a percentage adjustment relative to the ORIGINAL TP distance. Negative percentShift brings TP closer to entry (more conservative). Positive percentShift moves TP further from entry (more aggressive).

    Absorption behavior:

    • First call: sets trailing TP unconditionally
    • Subsequent calls: updates only if new TP is MORE CONSERVATIVE (closer to entry)
    • For LONG: only accepts LOWER TP (never moves up, closer to entry wins)
    • For SHORT: only accepts HIGHER TP (never moves down, closer to entry wins)

    Automatically detects backtest/live mode from execution context.

    Parameters

    • symbol: string

      Trading pair symbol

    • percentShift: number

      Percentage adjustment to ORIGINAL TP distance (-100 to 100)

    • currentPrice: number

      Current market price to check for intrusion

    Returns Promise<boolean>

    Promise - true if trailing TP was set/updated, false if rejected (absorption/intrusion/conflict)

    import { trailingTake } from "backtest-kit";

    // LONG: entry=100, originalTP=110, distance=10%, currentPrice=102

    // First call: bring TP closer by 3%
    const success1 = await trailingTake("BTCUSDT", -3, 102);
    // success1 = true, newDistance = 10% - 3% = 7%, newTP = 107

    // Second call: try to move TP further (less conservative)
    const success2 = await trailingTake("BTCUSDT", 2, 102);
    // success2 = false (SKIPPED: newTP=112 > 107, less conservative, larger % absorbs smaller)

    // Third call: even more conservative
    const success3 = await trailingTake("BTCUSDT", -5, 102);
    // success3 = true (ACCEPTED: newDistance = 10% - 5% = 5%, newTP = 105 < 107, more conservative)