private void DoHedge(decimal currentDelta, decimal deltaStep)
        {
            QuoterStrategy mqs = null;

            var hedgeSize = (currentDelta / deltaStep).PrepareSizeToTrade();

            if (currentDelta > 0)
            {
                hedgeSize = hedgeSize.ShrinkSizeToTrade(Sides.Sell, _futuresPosition,
                                                        MinFuturesPositionVal);

                if (hedgeSize <= 0)
                {
                    return;
                }

                mqs = new MarketQuoterStrategy(Sides.Sell, hedgeSize, Security.GetMarketPrice(Sides.Sell));
                _futuresPosition -= hedgeSize;
            }

            if (currentDelta < 0)
            {
                hedgeSize = hedgeSize.ShrinkSizeToTrade(Sides.Buy, _futuresPosition,
                                                        MaxFuturesPositionVal);

                if (hedgeSize <= 0)
                {
                    return;
                }

                mqs = new MarketQuoterStrategy(Sides.Buy, hedgeSize, Security.GetMarketPrice(Sides.Buy));
                _futuresPosition += hedgeSize;
            }

            MarkStrategyLikeChild(mqs);
            ChildStrategies.Add(mqs);
        }
Exemplo n.º 2
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();
        }