Skip to main content

Trail

TrailBase is the abstract base for trailing stops. A trail is attached to an Entry and manages the stop loss dynamically after position entry. Two built-in implementations cover tick-based and bar-based trailing.

Namespace: MZpack.NT8.Algo Inheritance: TrailBase (abstract) Source: [INSTALL PATH]/API/Extensions/Trails/TrailBase.cs

TrailBase Properties

PropertyTypeDefaultDescription
CalculateCalculateOnEachTickWhen to evaluate: OnEachTick or OnBarClose
IsActivebooltrueEnable/disable the trail

Read-Only State

PropertyTypeDescription
IsTrailedboolTrue once trailing has begun
TrailingPricedoubleCurrent trailing stop price

TrailBase Methods

MethodDescription
OnExecution(Entry entry, MarketDataEventArgs e)Main loop: checks IsActive, calls IsBeginTrailing/IsTrailingStep, submits stop via entry.SubmitStopLoss()
IsBeginTrailing(MarketPosition position, Entry entry)Abstract — return true when trailing should activate
IsTrailingStep(MarketPosition position, Entry entry)Abstract — return true when the stop should move
GetTrailingPrice(MarketPosition position, Entry entry)Abstract — return the new trailing stop price
Reset()Reset IsTrailed, TrailingPrice, and internal state

Trail (Tick-Based)

Activates after a profit threshold, then moves the stop every N ticks at a fixed distance behind the current price.

Inheritance: Trail : TrailBase Source: [INSTALL PATH]/API/Extensions/Trails/Trail.cs

Properties

PropertyTypeDescription
TrailAfterTicksintTicks of profit before trailing starts
TrailDistanceTicksintDistance in ticks behind current price
TrailStepTicksintMinimum tick movement before stop is moved (1 = every tick)

Constructor

public Trail(int trailAfterTicks, int trailDistanceTicks, int trailStepTicks)

Behavior

  • Activation: PnL in ticks >= TrailAfterTicks
  • Step: price moved >= TrailStepTicks from last trailed price
  • Price (Long): RoundToTickSize(Bid - TicksToPrice(TrailDistanceTicks))
  • Price (Short): RoundToTickSize(Ask + TicksToPrice(TrailDistanceTicks))

BarHiLoTrail (Bar-Based)

Trails the stop to the high or low of a bar N bars ago, with an optional tick shift.

Inheritance: BarHiLoTrail : TrailBase Source: [INSTALL PATH]/API/Extensions/Trails/BarHiLoTrail.cs

Properties

PropertyTypeDefaultDescription
BarsAgoint1Reference bar (0 = current, 1 = previous)
ShiftTicksint0Tick shift from bar high/low

Behavior

  • Activation: BarsAgo <= BarsSinceEntryExecution(SignalName)
  • Step (Long): Low[BarsAgo] > TrailingPrice (bar low moved up)
  • Step (Short): High[BarsAgo] < TrailingPrice (bar high moved down)
  • Price (Long): Min(CurrentBid, RoundToTickSize(Low[BarsAgo] - ShiftTicks))
  • Price (Short): Max(CurrentAsk, RoundToTickSize(High[BarsAgo] + ShiftTicks))

See Also

  • Pipeline — execution order of components
  • Entry — order submission and protective orders
  • Exit — exit conditions