Exemplo n.º 1
0
        private void SpreadingProcess()
        {
            try
            {
                if (!IsTradingTime())
                {
                    return;
                }

                if (!_isEnterActivated && md.CheckIfSpreadExist())
                {
                    _isEnterActivated = true;

                    var sign = _sideForEnterToPosition == Sides.Buy ? 1 : -1;
                    var size = _lot.ShrinkSizeToTrade(_sideForEnterToPosition, CurrentPoisition,
                                                      LimitedFuturesValueAbs);
                    var step = md.BestPair.SpreadPrice.CheckIfValueNullThenZero() > _spread
                        ? Security.PriceStep.Value * sign
                        : 0;
                    var price = md.CalculateWorstLimitSpreadPrice(_sideForEnterToPosition, _spread,
                                                                  Security.PriceStep.Value);

                    if (size <= 0 || price <= 0)
                    {
                        _isEnterActivated = false;
                    }
                    else
                    {
                        _enterStrategy = new LimitQuoterStrategy(_sideForEnterToPosition, size, step, price)
                        {
                            IsLimitOrdersAlwaysRepresent = true
                        };

                        AssignEnterRulesAndStart();
                    }
                }

                if (!_isLeaverActivated && CurrentPoisition != 0)
                {
                    _isLeaverActivated = true;

                    var side = CurrentPoisition > 0 ? Sides.Sell : Sides.Buy;
                    var sign = side == Sides.Buy ? 1 : -1;
                    var size = CurrentPoisition.PrepareSizeToTrade();
                    var step = md.BestPair.SpreadPrice.CheckIfValueNullThenZero() > _spread
                        ? Security.PriceStep.Value * sign
                        : 0;
                    var price = CalculateExitPositionPrice();

                    if (price <= 0)
                    {
                        throw new ArgumentException("Impossible to continue work, exit price <=0");
                    }

                    _leaveStrategy = new LimitQuoterStrategy(side, size, step, price)
                    {
                        IsLimitOrdersAlwaysRepresent = true
                    };


                    AssignLeaveRulesAndStart();
                }
            }
            catch (Exception e1)
            {
                this.AddErrorLog($"exception: {e1.Message}");
                PrimaryStopping();
            }
        }