Skip to main content

Risk Management

Demonstrates the RiskManagement class with daily loss, profit, and trade count limits. Trades up/down bars on bar close using a simple UpDownBarSignal.

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

What It Covers

  • RiskManagement object with daily loss, profit, and trades limits
  • Algo.Strategy with OppositePatternAction.None
  • UpDownBarSignal (OnBarClose) -- Long for up-bars, Short for down-bars
  • Single market entry with stop loss and profit target

Strategy Setup

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

strategy.RiskManagement = new MZpack.NT8.Algo.RiskManagement(strategy)
{
Currency = Currency.UsDollar,
EntryName = ENTRY_NAME,
DailyLossLimitEnable = Common_DailyLossLimit_Enable,
DailyLossLimit = Common_DailyLossLimit,
DailyProfitLimitEnable = Common_DailyProfitLimit_Enable,
DailyProfitLimit = Common_DailyProfitLimit,
DailyTradesLimitEnable = Common_DailyTradesLimit_Enable,
DailyTradesLimit = Common_DailyTradesLimit
};

return strategy;
}

Signal

The UpDownBarSignal generates Long entries for up-bars and Short entries 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

Risk Management

PropertyDefaultDescription
Common_DailyLossLimit_EnabletrueEnable daily loss limit
Common_DailyLossLimit300Daily loss limit (USD)
Common_DailyProfitLimit_EnablefalseEnable daily profit limit
Common_DailyProfitLimit3000Daily profit limit (USD)
Common_DailyTradesLimit_EnablefalseEnable daily trades limit
Common_DailyTradesLimit5Max trades per day

Position

PropertyDefaultDescription
Position_DirectionAnyAllowed direction
Position_Quantity2Contracts
Position_StopLoss6Stop loss ticks
Position_ProfitTarget12Profit target ticks

See Also