Show / Hide Table of Contents

Rule

For the IMarketRule the S# already has a number of predefined conditions and actions for the most common scenarios. The MarketRuleHelper class conditions lists grouped by trading objects are listed below:

For the Security

  • MarketRuleHelper.WhenChanged(StockSharp.BusinessEntities.Security security, StockSharp.BusinessEntities.IMarketDataProvider provider) - the instrument change event rule.
  • MarketRuleHelper.WhenNewTrade(StockSharp.BusinessEntities.Security security, StockSharp.BusinessEntities.IMarketDataProvider provider) - the rule when instrument has the new trade occurrence event.
  • MarketRuleHelper.WhenMarketDepthChanged(StockSharp.BusinessEntities.Security security, StockSharp.BusinessEntities.IMarketDataProvider provider) - the rule when instrument has the order book change event.
  • MarketRuleHelper.WhenBestBidPriceMore(StockSharp.BusinessEntities.Security security, StockSharp.BusinessEntities.IMarketDataProvider provider, StockSharp.Messages.Unit price) - the rule for the event of the best bid increase above a specified level.
  • MarketRuleHelper.WhenBestBidPriceLess(StockSharp.BusinessEntities.Security security, StockSharp.BusinessEntities.IMarketDataProvider provider, StockSharp.Messages.Unit price) - the rule for the event of the best bid decrease below a specified level.
  • MarketRuleHelper.WhenBestAskPriceMore(StockSharp.BusinessEntities.Security security, StockSharp.BusinessEntities.IMarketDataProvider provider, StockSharp.Messages.Unit price) - the rule for the event of the best offer increase above a specified level.
  • MarketRuleHelper.WhenBestAskPriceLess(StockSharp.BusinessEntities.Security security, StockSharp.BusinessEntities.IMarketDataProvider provider, StockSharp.Messages.Unit price) - the rule for the event of the best offer decrease below a specified level.
  • MarketRuleHelper.WhenLastTradePriceMore(StockSharp.BusinessEntities.Security security, StockSharp.BusinessEntities.IMarketDataProvider provider, StockSharp.Messages.Unit price) - the rule for the event of the last trade price increase above a specified level.
  • MarketRuleHelper.WhenLastTradePriceLess(StockSharp.BusinessEntities.Security security, StockSharp.BusinessEntities.IMarketDataProvider provider, StockSharp.Messages.Unit price) - the rule for the event of the last trade price decrease below a specified level.

For the MarketDepth

  • MarketRuleHelper.WhenChanged(StockSharp.BusinessEntities.MarketDepth depth, StockSharp.BusinessEntities.IMarketDataProvider provider) - the order book change event rule.
  • MarketRuleHelper.WhenSpreadMore(StockSharp.BusinessEntities.MarketDepth depth, StockSharp.Messages.Unit price, StockSharp.BusinessEntities.IMarketDataProvider provider) - the rule for the event of the book order spread size more then specified value.
  • MarketRuleHelper.WhenSpreadLess(StockSharp.BusinessEntities.MarketDepth depth, StockSharp.Messages.Unit price, StockSharp.BusinessEntities.IMarketDataProvider provider) - the rule for the event of the book order spread size less then specified value.
  • MarketRuleHelper.WhenBestBidPriceMore(StockSharp.BusinessEntities.MarketDepth depth, StockSharp.Messages.Unit price, StockSharp.BusinessEntities.IMarketDataProvider provider) - the rule for the event of the best bid increase above a specified level.
  • MarketRuleHelper.WhenBestBidPriceLess(StockSharp.BusinessEntities.MarketDepth depth, StockSharp.Messages.Unit price, StockSharp.BusinessEntities.IMarketDataProvider provider) - the rule for the event of the best bid decrease below a specified level.
  • MarketRuleHelper.WhenBestAskPriceMore(StockSharp.BusinessEntities.MarketDepth depth, StockSharp.Messages.Unit price, StockSharp.BusinessEntities.IMarketDataProvider provider) - the rule for the event of the best offer increase above a specified level.
  • MarketRuleHelper.WhenBestAskPriceLess(StockSharp.BusinessEntities.MarketDepth depth, StockSharp.Messages.Unit price, StockSharp.BusinessEntities.IMarketDataProvider provider) - the rule for the event of the best offer decrease below a specified level.

For the Order

  • MarketRuleHelper.WhenRegistered(StockSharp.BusinessEntities.Order order, StockSharp.BusinessEntities.ITransactionProvider provider) - the rule for the event of the successful order registration on exchange.
  • MarketRuleHelper.WhenPartiallyMatched(StockSharp.BusinessEntities.Order order, StockSharp.BusinessEntities.ITransactionProvider provider) - the rule for the event of the partially matched order.
  • MarketRuleHelper.WhenRegisterFailed(StockSharp.BusinessEntities.Order order, StockSharp.BusinessEntities.ITransactionProvider provider) - the rule for the event of the failed order registration on exchange.
  • MarketRuleHelper.WhenCancelFailed(StockSharp.BusinessEntities.Order order, StockSharp.BusinessEntities.ITransactionProvider provider) - the rule for the event of the failed order cancel on exchange.
  • MarketRuleHelper.WhenCanceled(StockSharp.BusinessEntities.Order order, StockSharp.BusinessEntities.ITransactionProvider provider) - the rule for the event of the order cancel on exchange.
  • MarketRuleHelper.WhenMatched(StockSharp.BusinessEntities.Order order, StockSharp.BusinessEntities.ITransactionProvider provider) - the rule for the event of the fully matched order on exchange.
  • MarketRuleHelper.WhenChanged(StockSharp.BusinessEntities.Order order, StockSharp.BusinessEntities.ITransactionProvider provider) - the rule for the event of the order change.
  • MarketRuleHelper.WhenNewTrade(StockSharp.BusinessEntities.Order order, StockSharp.BusinessEntities.ITransactionProvider provider) - the rule for the event of the trade occurrence by the order.

For the Portfolio

  • MarketRuleHelper.WhenMoneyLess(StockSharp.BusinessEntities.Portfolio portfolio, StockSharp.BusinessEntities.IPortfolioProvider provider, StockSharp.Messages.Unit money) - the rule for the event of the money decrease in the portfolio below a specified level.
  • MarketRuleHelper.WhenMoneyMore(StockSharp.BusinessEntities.Portfolio portfolio, StockSharp.BusinessEntities.IPortfolioProvider provider, StockSharp.Messages.Unit money) - the rule for the event of the money increase in the portfolio above a specified level.

For Position

  • MarketRuleHelper.WhenLess(StockSharp.BusinessEntities.Position position, StockSharp.BusinessEntities.IPositionProvider provider, StockSharp.Messages.Unit value) - the rule for the event of the position decrease below a specified level.
  • MarketRuleHelper.WhenMore(StockSharp.BusinessEntities.Position position, StockSharp.BusinessEntities.IPositionProvider provider, StockSharp.Messages.Unit value) - the rule for the event of the position increase above a specified level.
  • MarketRuleHelper.Changed(StockSharp.BusinessEntities.Position position, StockSharp.BusinessEntities.IPositionProvider provider) - the rule for the event of the position change.

For the IPnLManager

  • StrategyHelper.WhenPnLLess(StockSharp.Algo.Strategies.Strategy strategy, StockSharp.Messages.Unit value) - the rule for the event of the profit decrease below a specified level.
  • StrategyHelper.WhenPnLMore(StockSharp.Algo.Strategies.Strategy strategy, StockSharp.Messages.Unit value) - the rule for the event of the profit increase above a specified level.
  • StrategyHelper.WhenPositionChanged(StockSharp.Algo.Strategies.Strategy strategy) - the rule for the event of the profit change.

For the Candle

  • MarketRuleHelper.WhenClosePriceMore(StockSharp.Algo.Candles.ICandleManager candleManager, StockSharp.Algo.Candles.Candle candle, StockSharp.Messages.Unit price) - the rule for the event of the candle closing price increase above a specified level.
  • MarketRuleHelper.WhenClosePriceLess(StockSharp.Algo.Candles.ICandleManager candleManager, StockSharp.Algo.Candles.Candle candle, StockSharp.Messages.Unit price) - the rule for the event of the candle closing price decrease below a specified level.
  • MarketRuleHelper.WhenTotalVolumeMore(StockSharp.Algo.Candles.ICandleManager candleManager, StockSharp.Algo.Candles.Candle candle, StockSharp.Messages.Unit volume) - the rule for the event of the candle total volume increase above a specified level.
  • MarketRuleHelper.WhenCurrentCandleTotalVolumeMore(StockSharp.Algo.Candles.ICandleManager candleManager, StockSharp.Algo.Candles.CandleSeries series, StockSharp.Messages.Unit volume) - the rule for the event of the current candle total volume increase above a specified level.
  • MarketRuleHelper.WhenCandlesStarted(StockSharp.Algo.Candles.ICandleManager candleManager, StockSharp.Algo.Candles.CandleSeries series) - the rule for the event of the new candles occurrence.
  • MarketRuleHelper.WhenCandlesChanged(StockSharp.Algo.Candles.ICandleManager candleManager, StockSharp.Algo.Candles.CandleSeries series) - the rule for the event of the candles change.
  • MarketRuleHelper.WhenCandlesFinished(StockSharp.Algo.Candles.ICandleManager candleManager, StockSharp.Algo.Candles.CandleSeries series) - the rule for the event of the candles end.
  • MarketRuleHelper.WhenChanged(StockSharp.Algo.Candles.ICandleManager candleManager, StockSharp.Algo.Candles.Candle candle) - the rule for the event of the candle change.
  • MarketRuleHelper.WhenFinished(StockSharp.Algo.Candles.ICandleManager candleManager, StockSharp.Algo.Candles.Candle candle) - the rule for the event of the candle finish.
  • MarketRuleHelper.WhenPartiallyFinished(StockSharp.Algo.Candles.ICandleManager candleManager, StockSharp.Algo.Candles.Candle candle, StockSharp.BusinessEntities.IConnector connector, System.Decimal percent) - the rule for the event of the candle partial finish.
  • MarketRuleHelper.WhenPartiallyFinishedCandles(StockSharp.Algo.Candles.ICandleManager candleManager, StockSharp.Algo.Candles.CandleSeries series, StockSharp.BusinessEntities.IConnector connector, System.Decimal percent) - the rule for the event of the candles partial finish.

For the Strategy

  • StrategyHelper.WhenNewMyTrade(StockSharp.Algo.Strategies.Strategy strategy) - the rule for the event of the strategy new trades occurrence.
  • StrategyHelper.WhenPositionChanged(StockSharp.Algo.Strategies.Strategy strategy) - the rule for the event of the strategy position change.
  • StrategyHelper.WhenStarted(StockSharp.Algo.Strategies.Strategy strategy) - the rule for the event of the strategy work start.
  • StrategyHelper.WhenStopping(StockSharp.Algo.Strategies.Strategy strategy) - the rule for the event of the strategy work stopping.
  • StrategyHelper.WhenStopped(StockSharp.Algo.Strategies.Strategy strategy) - the rule for the event of the strategy work stopped.
  • StrategyHelper.WhenError(StockSharp.Algo.Strategies.Strategy strategy, System.Boolean processChildStrategyErrors) - the rule for the event of the strategy error.
  • StrategyHelper.WhenWarning(StockSharp.Algo.Strategies.Strategy strategy) - the rule for the event of the strategy warning.

For the IConnector

  • MarketRuleHelper.WhenIntervalElapsed(StockSharp.BusinessEntities.IConnector connector, System.TimeSpan interval) - the rule for the event of the IConnector.MarketTimeChanged change on value, which greater than or equal to the parameter.
  • MarketRuleHelper.WhenTimeCome(StockSharp.BusinessEntities.IConnector connector, System.DateTimeOffset[] times) - a rule that is activated upon the exact time occurrence.
  • MarketRuleHelper.WhenNewMyTrade(StockSharp.BusinessEntities.ITransactionProvider provider) - the rule for the event of the new trade occurrence.
  • MarketRuleHelper.WhenNewOrder(StockSharp.BusinessEntities.ITransactionProvider provider) - the rule for the event of the new order occurrence.

Similarly to the conditions there are predefined actions:

  • StrategyHelper.Register(StockSharp.Algo.IMarketRule rule, StockSharp.BusinessEntities.Order order) - the action which registers the order.
  • StrategyHelper.ReRegister(StockSharp.Algo.IMarketRule rule, StockSharp.BusinessEntities.Order oldOrder, StockSharp.BusinessEntities.Order newOrder) - the action which reregisters the order.
  • StrategyHelper.Cancel(StockSharp.Algo.IMarketRule rule, StockSharp.BusinessEntities.Order order) - the action which cancels the order.
  • Extensions.Protect(StockSharp.Algo.MarketRule<StockSharp.BusinessEntities.Order,StockSharp.BusinessEntities.MyTrade> rule, StockSharp.Messages.Unit takePriceDelta, StockSharp.Messages.Unit stopPriceDelta) - the action which protects trades simultaneously with TakeProfitStrategy and StopLossStrategy strategies.

If you need to create your own unique rule (on any event, which is not provided as standard), you must create the derived MarketRule<TToken,TArg> class, which will work with a prerequisite. Below is the MarketRuleHelper.WhenMoneyMore(StockSharp.BusinessEntities.Portfolio portfolio, StockSharp.BusinessEntities.IPortfolioProvider provider, StockSharp.Messages.Unit money) method implementation:


private sealed class PortfolioRule : MarketRule<Portfolio, Portfolio>
{
    private readonly Func<Portfolio, bool> _changed;
    private readonly Portfolio _portfolio;
    private readonly IConnector _connector;
    public PortfolioRule(Portfolio portfolio, IConnector connector, Func<Portfolio, bool> changed) : base(portfolio)
    {
        if (portfolio == null)
            throw new ArgumentNullException("portfolio");
        if (changed == null)
            throw new ArgumentNullException("changed");
        _changed = changed;
        _portfolio = portfolio;
        _connector = connector;
        _connector.PortfolioChanged += OnPortfolioChanged;
    }
    private void OnPortfolioChanged(Portfolio portfolio)
    {
        if ((portfolio==_portfolio) && _changed(_portfolio))
            Activate(_portfolio);
    }
    protected override void DisposeManaged()
    {
        _connector.PortfolioChanged -= OnPortfolioChanged;
        base.DisposeManaged();
    }
}

public static MarketRule<Portfolio, Portfolio> WhenMoneyMore(this Portfolio portfolio, Unit money)
{
    if (portfolio == null)
        throw new ArgumentNullException("portfolio");
    if (money == null)
        throw new ArgumentNullException("money");
    var finishMoney = money.Type == UnitTypes.Limit ? money : portfolio.CurrentValue + money;
    return new PortfolioRule(portfolio, pf => pf.CurrentValue > finishMoney)
    {
        Name = "Money increase of portfolio {0} above {1}".Put(portfolio, finishMoney)
    };
}

The PortfolioRule rule subscribes to the IPortfolioProvider.PortfolioChanged event, and as soon as it is called, then the condition is checked to exceed the current level of money in the portfolio above a specified limit. If the condition returns true, then the rule is activated through the MarketRule<TToken,TArg>.Activate method.

  • Improve this Doc
☀
☾
In This Article
Back to top
Copyright © StockSharp.
☀
☾