示例#1
0
        /// <summary>
        /// Расчёт объёма сделки в валюте депозита с учётом наличия или отсутствия фиксированного объёма
        /// </summary>
        protected int CalculateVolume(string ticker, decimal?calculateLeverage = null, int?fixedVolm = null)
        {
            int?baseDealVolume = fixedVolm ?? FixedVolume;

            if (!fixedVolm.HasValue || fixedVolm.Value == 0)
            {
                calculateLeverage = calculateLeverage ?? Leverage;
                Account ac;
                robotContext.GetAccountInfo(robotContext.AccountInfo.ID, true, out ac);
                if (ac == null || ac.Equity <= 0)
                {
                    var errorStr = ac == null
                        ? string.Format("Счет {0} не найден", robotContext.AccountInfo.ID)
                        : string.Format("На счете {0} недостаточно средств ({1})",
                                        robotContext.AccountInfo.ID, ac.Equity.ToStringUniformMoneyFormat());

                    Logger.Info(errorStr);
                    return(0);
                }

                var    leverageVolume = ac.Equity * calculateLeverage.Value;
                string error;
                var    depoVolm = DalSpot.Instance.ConvertToTargetCurrency(ticker, true, ac.Currency,
                                                                           (double)leverageVolume, robotContext.QuotesStorage.ReceiveAllData(),
                                                                           out error) ?? 0M;

                if (depoVolm == 0)
                {
                    Logger.InfoFormat("Не удалось перевести средства в валюту депозита. " + error);
                    return(0);
                }

                baseDealVolume = (int)Math.Round(leverageVolume * leverageVolume / depoVolm);
            }

            var roundMinVolm  = RoundMinVolume;
            var roundVolmStep = RoundVolumeStep;

            if (roundMinVolm == 0 || roundVolmStep == 0)
            {
                var minStepLot = DalSpot.Instance.GetMinStepLot(ticker, robotContext.AccountInfo.Group);
                if (roundMinVolm == 0)
                {
                    roundMinVolm = minStepLot.b;
                }
                if (roundVolmStep == 0)
                {
                    roundVolmStep = minStepLot.a;
                }
            }

            var volume = MarketOrder.RoundDealVolume((int)baseDealVolume.Value, RoundType, roundMinVolm, roundVolmStep);

            if (volume == 0)
            {
                Logger.InfoFormat("{0}: OpenDeal({0} {1}) - объем в валюте депозита ({2:f2}) недостаточен", TypeName, ticker, volume);
            }

            return(volume);
        }