StockSharp - algorithmic trading platform

## 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

- MarketRuleHelperWhenChanged - the instrument change event rule.
- MarketRuleHelper.WhenNewTrades(Security, IConnector) - the rule when instrument has the new trade occurrence event.
- MarketRuleHelperWhenMarketDepthChanged - the rule when instrument has the order book change event.
- MarketRuleHelperWhenBestBidPriceMore - the rule for the event of the best bid increase above a specified level.
- MarketRuleHelperWhenBestBidPriceLess - the rule for the event of the best bid decrease below a specified level.
- MarketRuleHelperWhenBestAskPriceMore - the rule for the event of the best offer increase above a specified level.
- MarketRuleHelperWhenBestAskPriceLess - the rule for the event of the best offer decrease below a specified level.
- MarketRuleHelperWhenLastTradePriceMore - the rule for the event of the last trade price increase above a specified level.
- MarketRuleHelperWhenLastTradePriceLess - the rule for the event of the last trade price decrease below a specified level.

For the MarketDepth

- MarketRuleHelperWhenChanged - the order book change event rule.
- MarketRuleHelperWhenSpreadMore - the rule for the event of the book order spread size more then specified value.
- MarketRuleHelperWhenSpreadLess - the rule for the event of the book order spread size less then specified value.
- MarketRuleHelperWhenBestBidPriceMore - the rule for the event of the best bid increase above a specified level.
- MarketRuleHelperWhenBestBidPriceLess - the rule for the event of the best bid decrease below a specified level.
- MarketRuleHelperWhenBestAskPriceMore - the rule for the event of the best offer increase above a specified level.
- MarketRuleHelperWhenBestAskPriceLess - the rule for the event of the best offer decrease below a specified level.

For the Order

- MarketRuleHelperWhenRegistered - the rule for the event of the successful order registration on exchange.
- MarketRuleHelperWhenActivated - the rule for the event of the stop order activation.
- MarketRuleHelperWhenPartiallyMatched - the rule for the event of the partially matched order.
- MarketRuleHelperWhenRegisterFailed - the rule for the event of the failed order registration on exchange.
- MarketRuleHelperWhenCancelFailed - the rule for the event of the failed order cancel on exchange.
- MarketRuleHelperWhenCanceled - the rule for the event of the order cancel on exchange.
- MarketRuleHelperWhenMatched - the rule for the event of the fully matched order on exchange.
- MarketRuleHelperWhenChanged - the rule for the event of the order change.
- MarketRuleHelperWhenNewTrade - the rule for the event of the trade occurrence by the order.

For the Portfolio

- MarketRuleHelperWhenMoneyLess - the rule for the event of the money decrease in the portfolio below a specified level.
- MarketRuleHelperWhenMoneyMore - the rule for the event of the money increase in the portfolio above a specified level.

Для Position

- MarketRuleHelperWhenLess - the rule for the event of the position decrease below a specified level.
- MarketRuleHelperWhenMore - the rule for the event of the position increase above a specified level.
- MarketRuleHelperChanged - the rule for the event of the position change.

For the IPnLManager

- StrategyHelperWhenPnLLess - the rule for the event of the profit decrease below a specified level.
- StrategyHelperWhenPnLMore - the rule for the event of the profit increase above a specified level.
- StrategyHelperWhenPositionChanged - the rule for the event of the profit change.

For the Candle

- MarketRuleHelperWhenClosePriceMore - the rule for the event of the candle closing price increase above a specified level.
- MarketRuleHelperWhenClosePriceLess - the rule for the event of the candle closing price decrease below a specified level.
- MarketRuleHelperWhenTotalVolumeMore - the rule for the event of the candle total volume increase above a specified level.
- MarketRuleHelperWhenCurrentCandleTotalVolumeMore - the rule for the event of the current candle total volume increase above a specified level.
- MarketRuleHelperWhenCandlesStarted - the rule for the event of the new candles occurrence.
- MarketRuleHelperWhenCandlesChanged - the rule for the event of the candles change.
- MarketRuleHelperWhenCandlesFinished - the rule for the event of the candles end.
- MarketRuleHelperWhenChanged - the rule for the event of the candle change.
- MarketRuleHelperWhenFinished - the rule for the event of the candle finish.
- MarketRuleHelperWhenPartiallyFinished - the rule for the event of the candle partial finish.
- MarketRuleHelperWhenPartiallyFinishedCandles - the rule for the event of the candles partial finish.

For the Strategy

- StrategyHelper.WhenNewMyTrades(Strategy) - the rule for the event of the strategy new trades occurrence.
- StrategyHelperWhenPositionChanged - the rule for the event of the strategy position change.
- StrategyHelperWhenStarted - the rule for the event of the strategy work start.
- StrategyHelperWhenStopping - the rule for the event of the strategy work stopping.
- StrategyHelperWhenStopped - the rule for the event of the strategy work stopped.
- StrategyHelperWhenError - the rule for the event of the strategy error.
- StrategyHelperWhenWarning - the rule for the event of the strategy warning.

For the IConnector

- MarketRuleHelperWhenIntervalElapsed - the rule for the event of the IConnectorMarketTimeChanged change on value, which greater than or equal to the parameter.
- MarketRuleHelperWhenTimeCome - a rule that is activated upon the exact time occurrence.
- MarketRuleHelperWhenNewMyTrade - the rule for the event of the new trade occurrence.
- MarketRuleHelperWhenNewOrder - the rule for the event of the new order occurrence.

Similarly to the conditions there are predefined actions:

- StrategyHelperRegister - the action which registers the order.
- StrategyHelperReRegister - the action which reregisters the order.
- StrategyHelperCancel - the action which cancels the order.
- Protect - 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 MarketRuleTToken, TArg class, which will work with a prerequisite. Below is the WhenMoneyMore method implementation:

C#

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.PortfoliosChanged += OnPortfoliosChanged; } private void OnPortfoliosChanged(IEnumerable<Portfolio> portfolios) { if (portfolios.Contains(_portfolio) && _changed(_portfolio)) Activate(_portfolio); } protected override void DisposeManaged() { _connector.PortfoliosChanged -= OnPortfoliosChanged; 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 = "Portfolio {0} balance increased above {1}".Put(portfolio, finishMoney) }; }

The *PortfolioRule* rule subscribes to the
IConnectorPortfoliosChanged
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
MarketRuleTToken, TArgActivate method.