Skip to main content

Pattern

Pattern is the top-level container in the strategy framework. It holds two decision trees — SignalsTree for signal evaluation and FiltersTree for post-validation gating — and drives the evaluation loop on each market event. An Algo.Strategy can have multiple patterns of different types.

Namespace: MZpack.NT8.Algo Inheritance: Pattern : StrategyItem Source: [INSTALL PATH]/API/Pattern.cs

Pattern Types

Each pattern serves a distinct role in position lifecycle:

PatternTypeDescription
EntryOpens a new position when flat
ExitCloses the current position on signal
ReversalCloses the current position and opens the opposite direction
ScaleInAdds contracts to an existing position
ScaleOutReduces contracts from an existing position

A typical strategy has at least an Entry pattern. Exit, Reversal, ScaleIn, and ScaleOut patterns are optional and evaluated independently.

Properties

PropertyTypeDefaultDescription
TypePatternTypeEntryPattern type
AllowedDirectionSignalDirectionAnyConstrain pattern to Long, Short, or Any
IsShortCircuitANDEvaluationboolIf true, AND signals are validated in sequence (left to right)
SignalsSignalsTreeSignals decision tree
FiltersFiltersTreeFilters decision tree (evaluated after signals validate)

Read-Only State

PropertyTypeDescription
DirectionSignalDirectionResulting direction after evaluation (None, Long, Short)
StrategyStrategyParent Algo.Strategy
ViewItemsObservableCollection\<Node>View items for visual constructor

Constructor

public Pattern(Strategy strategy, Logic rootLogic, Range signalsRange,
Logic filtersLogic, Range filtersRange, bool isShortCircuitANDEvaluation)

Creates both SignalsTree and FiltersTree with the specified root logic and range constraints.

Simplified overload (no filters):

public Pattern(Strategy strategy, Logic rootLogic, Range signalsRange,
bool isShortCircuitANDEvaluation)

Evaluation

On every market event, the pattern evaluates in two phases:

Pattern.Evaluate()
├── [1] SignalsTree.OnMarketEvent()
│ └── Root LogicalNode evaluates all child signals
│ → Direction: Long | Short | None

└── [2] FiltersTree.OnMarketEvent() ← only if signals validated
└── Root LogicalNode evaluates all child filters
→ Pass (Any) or Block (None)

If SignalsTree returns None, the pattern short-circuits — FiltersTree is not evaluated. If FiltersTree blocks, the pattern direction resets to None.

Short-Circuit AND Evaluation

When IsShortCircuitANDEvaluation = true, the AND root node validates signals in sequence. Each signal must validate before the next is evaluated. This is useful when signals have dependencies or when you want to avoid unnecessary computation.

Patterns Collection

Patterns manages all pattern instances within an Algo.Strategy.

Inheritance: Patterns : StrategyItem

MethodDescription
Get(PatternType type)Get pattern by type
Set(Pattern pattern, PatternType type)Set pattern for a type
Initialize(Strategy strategy, bool isInConstructor)Initialize all patterns
Reset(List\<MarketDataSource> sources, bool resetView, bool isSessionBreak)Reset all patterns
CheckSyntax()Validate all pattern trees

Example: Entry + Reversal Patterns

OnCreateAlgoStrategy = () =>
{
var strategy = new Strategy("MyStrategy", this);

// Entry pattern: open position on signal A AND signal B
var entryPattern = new Pattern(strategy,
Logic.And, new Range { Bars = 5 },
isShortCircuitANDEvaluation: true);
entryPattern.Signals.Root.Add(signalA);
entryPattern.Signals.Root.Add(signalB);

// Reversal pattern: reverse position on signal C
var reversalPattern = new Pattern(strategy,
Logic.And, new Range(),
isShortCircuitANDEvaluation: false);
reversalPattern.Type = PatternType.Reversal;
reversalPattern.Signals.Root.Add(signalC);

var entry = new Entry(strategy)
{
StopLossTicks = 20,
ProfitTargetTicks = 40
};

strategy.Initialize(entryPattern, null,
new Entry[] { entry }, attempts: 1);
strategy.Patterns.Set(reversalPattern, PatternType.Reversal);

return strategy;
};

See Also