Example #1
0
        protected override void OnStarted()
        {
            if (SecurityWithSignalToClose == null)
            {
                DoStrategyPreparation(new Security[] { }, new Security[] { Security }, new Portfolio[] { Portfolio });
            }
            else
            {
                DoStrategyPreparation(new Security[] { }, new Security[] { Security, SecurityWithSignalToClose }, new Portfolio[] { Portfolio });
            }


            if (Volume <= 0 || _priceToClose <= 0)
            {
                throw new ArgumentException(
                          $"Volume: {Volume} or price to close: {_priceToClose} cannot be below zero");
            }
            ;

            this.WhenPositionChanged()
            .Do(() =>
            {
                if (Math.Abs(Position) >= Volume)
                {
                    PrimaryStopping();
                }
            })
            .Apply(this);

            //не проверяем время, т.к. правило выполняется Once()
            TimingController.SetTimingUnnecessary();

            if (SecurityWithSignalToClose == null)
            {
                var md = GetMarketDepth(Security);

                Security.WhenMarketDepthChanged(Connector)
                .Do(() =>
                {
                    var mqs = new MarketQuoterStrategy(_strategyOrderSide, Volume, _priceToClose);

                    mqs.WhenStopped()
                    .Do(this.Stop)
                    .Once()
                    .Apply(this);

                    MarkStrategyLikeChild(mqs);
                    ChildStrategies.Add(mqs);
                })
                .Once()
                .Apply(this);
            }
            else
            {
                PrimaryStrategy mqs = null;

                var md = GetMarketDepth(SecurityWithSignalToClose);

                var mqsStartRule = SecurityWithSignalToClose
                                   .WhenMarketDepthChanged(Connector)
                                   .Do(() =>
                {
                    if (!IsTradingTime())
                    {
                        return;
                    }

                    //котировки специально развернуты неверно - как только была сделка на графике (ударили в аск или налили в бид) - закрываемся
                    if (_securityDesirableDirection == PriceDirection.Up && md.BestAsk.Price >= _priceToClose ||
                        _securityDesirableDirection == PriceDirection.Down && md.BestBid.Price <= _priceToClose)
                    {
                        // пока делаем по любой цене, как только сработает условие
                        mqs = new MarketQuoterStrategy(_strategyOrderSide, Volume, Security.GetMarketPrice(_strategyOrderSide));

                        mqs.WhenStopped()
                        .Do(this.Stop)
                        .Once()
                        .Apply(this);

                        MarkStrategyLikeChild(mqs);
                        ChildStrategies.Add(mqs);
                    }
                })
                                   .Until(() => mqs != null)
                                   .Apply(this);

                this.WhenStopping()
                .Do(() => { /*NOP*/ })
                .Apply(this)
                .Exclusive(mqsStartRule);
            }

            base.OnStarted();
        }
Example #2
0
 protected void MarkStrategyLikeChild(PrimaryStrategy child)
 {
     child._isCorrectChild = true;
     child.Log            += message => this.AddWarningLog($"CHILD MESSAGE (parent: {this.Name} child: {child.Name}): {message.Message}");
 }