예제 #1
0
        /// <summary>
        /// 计算多单收割的数量
        /// </summary>
        /// <param name="buyQuantity"></param>
        /// <param name="buyTradePrice"></param>
        /// <param name="nowPrice"></param>
        /// <param name="symbol"></param>
        /// <returns></returns>
        public static decimal CalcSellQuantityForMoreShouge(decimal buyQuantity, decimal buyTradePrice, decimal nowPrice, CommonSymbol symbol)
        {
            if (nowPrice <= buyTradePrice * (decimal)1.03)
            {
                throw new Exception("收割多价格不合理");
            }

            // 作用是,价格越高,则收割的量越多
            var position = (decimal)0.66;

            // 作用是,价格越高,则收割的量越多
            decimal sellQuantity = buyQuantity * buyTradePrice / nowPrice;

            sellQuantity = sellQuantity + (buyQuantity - sellQuantity) * position;
            sellQuantity = decimal.Round(sellQuantity, symbol.AmountPrecision);

            var newSellQuantity = sellQuantity;

            if (newSellQuantity == buyQuantity)
            {
                if (symbol.AmountPrecision == 4)
                {
                    newSellQuantity -= (decimal)0.0001;
                }
                else if (symbol.AmountPrecision == 3)
                {
                    newSellQuantity -= (decimal)0.001;
                }
                else if (symbol.AmountPrecision == 2)
                {
                    newSellQuantity -= (decimal)0.01;
                }
                else if (symbol.AmountPrecision == 1)
                {
                    newSellQuantity -= (decimal)0.1;
                }
                else if (symbol.AmountPrecision == 0)
                {
                    newSellQuantity -= (decimal)1;
                }
            }

            // 是否满足最小出售数量
            if (!CoinUtils.IsBiggerThenLeast(symbol.BaseCurrency, symbol.QuoteCurrency, sellQuantity))
            {
                newSellQuantity = CoinUtils.GetLeast(symbol.BaseCurrency, symbol.QuoteCurrency);
            }

            if (buyQuantity > newSellQuantity && buyQuantity * buyTradePrice < newSellQuantity * nowPrice)
            {
                return(newSellQuantity);
            }

            throw new Exception($"计算sellquantity不合理, buyQuantity:{buyQuantity},newSellQuantity:{newSellQuantity}, 没有赚头");
        }