Skip to main content

Trading Times

Demonstrates the TradingTimes feature with two configurable time windows. Trades up/down bars on bar close using a simple UpDownBarSignal.

Source: [INSTALL PATH]/API/Samples/TradingTimes.cs Class: TradingTimes : MZpackStrategyBase

What It Covers

  • TradingTime objects with Begin/End time windows
  • Strategy.TradingTimes list for restricting when the strategy can trade
  • SessionBreak control — disabled when time schedule is active
  • UpDownBarSignal (OnBarClose) — Long for up-bars, Short for down-bars
  • Time string parsing with error handling via NTMessageBox

Strategy Setup

protected MZpack.NT8.Algo.Strategy CreateAlgoStrategy()
{
MZpack.NT8.Algo.Strategy strategy = new MZpack.NT8.Algo.Strategy(
@"Trading Times", this)
{
OppositePatternAction = OppositePatternAction.None,
LogLevel = LogLevel, LogTarget = LogTarget, LogTime = LogTime
};

// Trading times
if (Time1_Enable)
strategy.TradingTimes.Add(new TradingTime()
{ Begin = TryParseDateTime(Time1_Begin), End = TryParseDateTime(Time1_End) });
if (Time2_Enable)
strategy.TradingTimes.Add(new TradingTime()
{ Begin = TryParseDateTime(Time2_Begin), End = TryParseDateTime(Time2_End) });

// No session break when time schedule is enabled
strategy.SessionBreak = !(Time1_Enable || Time2_Enable);

return strategy;
}

Time parsing helper:

DateTime TryParseDateTime(string s)
{
if (!DateTime.TryParse(s, out DateTime time))
NTMessageBox.Show("Error in date/time format: '" + s + "'",
Name, System.Windows.MessageBoxImage.None);
return time;
}

Entry and Signal

Entry[] entries = new Entry[EntriesPerDirection];
entries[0] = new Entry(Strategy)
{
EntryMethod = EntryMethod.Market,
Quantity = Position_Quantity,
SignalName = ENTRY_NAME,
StopLossTicks = Position_StopLoss,
ProfitTargetTicks = Position_ProfitTarget,
};

Pattern pattern = new Pattern(Strategy, Logic.And, null, true);
pattern.Signals.Root.AddChild(new UpDownBarSignal(Strategy));
Strategy.Initialize(pattern, entries, 3);

The UpDownBarSignal evaluates on bar close — Long for up-bars, Short for down-bars:

class UpDownBarSignal : Signal
{
public UpDownBarSignal(MZpack.NT8.Algo.Strategy strategy)
: base(strategy, MarketDataSource.Level1, SignalCalculate.OnBarClose, true) { }

public override void OnCalculate(MarketDataEventArgs e, int barIdx,
SignalDirection allowed)
{
SignalDirection direction = SignalDirection.None;

if (Signal.IsLongAllowed(allowed)
&& Strategy.MZpackStrategy.Open[1] > Strategy.MZpackStrategy.Close[1])
direction = Signal.ResolveDirection(SignalDirection.Short, allowed);
else if (Signal.IsLongAllowed(allowed)
&& Strategy.MZpackStrategy.Open[1] < Strategy.MZpackStrategy.Close[1])
direction = Signal.ResolveDirection(SignalDirection.Long, allowed);

if (Signal.IsDetermined(direction))
{
Direction = direction;
ChartRange = new ChartRange() { MinBarIdx = barIdx, MaxBarIdx = barIdx };
Time = e.Time;
EntryPrice = e.Price;
}
}
}

Configurable Properties

Trading Time

PropertyDefaultDescription
Time1_EnabletrueEnable first time window
Time1_Begin"08:30:00"First window start
Time1_End"15:15:00"First window end
Time2_EnablefalseEnable second time window
Time2_Begin"15:30:00"Second window start
Time2_End"17:00:00"Second window end

Position

PropertyDefaultDescription
Position_DirectionAnyAllowed direction
Position_Quantity2Contracts
Position_StopLoss6Stop loss ticks
Position_ProfitTarget12Profit target ticks

See Also