Click or drag to resize

Rules using

  • Creating a rule for the order registration condition:

    C#
    private void btnBuy_Click(object sender, RoutedEventArgs e)
    {
        var order = new Order
        {
            Portfolio = Portfolio.SelectedPortfolio,
            Price = _instr1.BestAsk.Price,
            Security = _instr1,
            Volume = 1,
            Direction = Sides.Buy,
        };
    
        order
            .WhenRegistered()
            .Do(() => trader.AddInfoLog("The order was successfully registered."))
            .Once()
            .Apply(this);
    
        // registering the order
        trader.RegisterOrder(order);
    }

    Now, when the event is triggered (the order will be registered on exchange), the action specified through the IMarketRuleDo method will be called.

    At the end of the rule creating the MarketRuleHelperApply method is called. As long as the method is not called for the rule - it is inactive (the handler in the IMarketRuleDo will not be called).

  • Rules creating within the strategy:

    C#
    class FirstStrategy : Strategy
    {
        ...
    
        protected override void OnStarting()
        {
            _series
                .WhenCandlesFinished()
                .Do(FinishCandle)
                .Apply(this);
    
            Security
                .WhenNewTrade()
                .Do(NewTrade)
                .Apply(this);
    
            base.OnStarting();
        }
    
        ...
    }
  • Unnecessary rules removing:

    The IMarketRule has the Token - this token is associated with the rule. For example, for the WhenCanceled rule the token will be the order.

    When the rule of the successful order cancel has worked, then it is better to remove all of the other rules related to this order:

    C#
    var order = this.CreateOrder(direction, (decimal) Security.GetCurrentPrice(direction), Volume);
    
    var ruleCanceled = order.WhenCanceled();
    
    ruleCanceled
        .Do(() => 
                {
                    this.AddInfoLog("The order was successfully cancelled.");
    
                    // removing all rules associated with the specified order
                    Rules.RemoveRulesByToken(ruleCanceled, ruleCanceled.Token);
                })
        .Once()
        .Apply(this);
    
    order
        .WhenRegistered()
        .Do(() => this.AddInfoLog("The order was successfully registered."))
        .Once()
        .Apply(this)
    
    order
        .WhenRegisterFailed()
        .Do(() => this.AddInfoLog("The order was not accepted by broker."))
        .Once()
        .Apply(this)
    
    order
        .WhenMatched()
        .Do(() => this.AddInfoLog("The order was fully matched."))
        .Once()
        .Apply(this);    
    
    // registering the order
    RegisterOrder(order);
  • Rules combination by the condition Or / And.

    When time is over OR the candle is closed:

    C#
    CandleSeries _series;
    TimeSpan _holdTimeToOpen = TimeSpan.FromMilliseconds(5000);
    
    ...
    
    Trader
        .WhenIntervalElapsed(_holdTimeToOpen)
        .Or(_series.WhenCandlesStarted())
        .Do(() => this.AddInfoLog("The candle is finished or time is over."))
        .Once()
        .Apply(this);

    Or such writing format:

    C#
    MarketRuleHelper
        .Or(new IMarketRule[] {Trader.WhenIntervalElapsed(_holdTimeToOpen), _series.WhenCandlesStarted()})
        .Do(() => this.AddInfoLog("The candle is finished or time is over."))
        .Once()
        .Apply(this);

    When the last trade price will be higher than 135000 AND lower than 140000:

    C#
    var priceMore = new Unit(135000m, UnitTypes.Limit);
    var priceLess = new Unit(140000m, UnitTypes.Limit);
    
    MarketRuleHelper
        .And(new IMarketRule[] {Security.WhenLastTradePriceMore(Connector, Connector, priceMore), Security.WhenLastTradePriceLess(Connector, Connector, priceLess)})
        .Do(() => /* some action */))
        .Apply(this);
    Note Note

    The handler in the IMarketRuleDo will be called after the last rule added through the And has worked.

  • Periodicity of the rule - Until:

    C#
    bool flag = false;
    
    ...
    
    Security
        .WhenNewTrade()
        .Do(() =>
                {
                    if(...) flag = true;
                })
        .Until(() => flag)            
        .Apply(this);