Example #1
0
        /// <summary>
        /// 0~1
        /// </summary>
        /// <param name="symbolName"></param>
        /// <param name="nowPrice"></param>
        /// <returns></returns>
        public static decimal GetLadderPosition(string symbolName, string quoteCurrency, decimal nowPrice, decimal defaultPosition = (decimal)0.5)
        {
            try
            {
                var control = new DogControlDao().GetDogControl(symbolName, quoteCurrency);
                if (control == null || control.HistoryMin == 0 || control.HistoryMax == 0 || control.HistoryMax < control.HistoryMin)
                {
                    return(defaultPosition);
                }

                if (nowPrice > control.HistoryMax)
                {
                    return((decimal)1);
                }

                if (nowPrice < control.HistoryMin)
                {
                    return((decimal)0);
                }

                var percent = (nowPrice - control.HistoryMin) / (control.HistoryMax - control.HistoryMin);
                return(percent);
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
                return(defaultPosition);
            }
        }
Example #2
0
        public static bool ControlCanSell(string symbolName, string quoteCurrency, List <HistoryKline> historyKlines, decimal nowPrice)
        {
            var control = new DogControlDao().GetDogControl(symbolName, quoteCurrency);

            if (control == null || control.HistoryMin <= 0 || control.EmptyPrice <= 0)
            {
                // 未管控的不能操作
                return(false);
            }

            if (nowPrice <= control.EmptyPrice ||
                (quoteCurrency == "usdt" && nowPrice < control.HistoryMin * 2) ||
                (quoteCurrency == "btc" && nowPrice < control.HistoryMin * (decimal)1.5) ||
                (quoteCurrency == "eth" && nowPrice < control.HistoryMin * (decimal)1.5) ||
                (quoteCurrency == "ht" && nowPrice < control.HistoryMin * (decimal)1.5))
            {
                return(false);
            }

            var maxPrice = historyKlines.Max(it => it.Close);
            var minPrice = historyKlines.Min(it => it.Close);

            if (nowPrice > minPrice * 2 && nowPrice > (control.HistoryMax - control.HistoryMin) * (decimal)0.3 + control.HistoryMin)
            {
                // 涨了1倍的,也可以空
                return(true);
            }

            if (nowPrice <= (control.HistoryMax - control.HistoryMin) * (decimal)0.2 + control.HistoryMin)
            {
                return(false);
            }

            return(true);
        }
Example #3
0
        public static void InitAsync()
        {
            try
            {
                {
                    long count = new DogControlDao().GetCount("usdt").Result;
                    coinCount["usdt"] = Math.Max(coinCount["usdt"], (int)count);
                }

                {
                    long count = new DogControlDao().GetCount("btc").Result;
                    coinCount["btc"] = Math.Max(coinCount["btc"], (int)count);
                }

                {
                    long count = new DogControlDao().GetCount("eth").Result;
                    coinCount["eth"] = Math.Max(coinCount["eth"], (int)count);
                }

                {
                    long count = new DogControlDao().GetCount("ht").Result;
                    coinCount["ht"] = Math.Max(coinCount["ht"], (int)count);
                }
                Console.WriteLine(JsonConvert.SerializeObject(coinCount));
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
            }
        }
Example #4
0
        public static bool ControlCanBuy(string symbolName, string quoteCurrency, decimal nowPrice)
        {
            var control = new DogControlDao().GetDogControl(symbolName, quoteCurrency);

            if (control == null || control.HistoryMin <= 0 || nowPrice > control.MaxInputPrice)
            {
                return(false);
            }
            return(true);
        }
Example #5
0
        public static int GetRecommendDivideForEmpty(string symbolName, string quoteCurrency, decimal nowPrice, decimal shouyiQuantity, int divide = 35)
        {
            try
            {
                var max     = 80;
                var min     = 25;
                var control = new DogControlDao().GetDogControl(symbolName, quoteCurrency);
                if (control == null || control.HistoryMax <= control.HistoryMin || control.HistoryMin <= 0 || control.HistoryMax <= 0)
                {
                    return(max);
                }

                // 防止价格波动后的, 分隔过合理.
                if (control.HistoryMax < control.HistoryMin * (decimal)2)
                {
                    control.HistoryMax = control.HistoryMax * (decimal)1.5;
                    control.HistoryMin = control.HistoryMin * (decimal)0.8;
                }
                if (control.HistoryMax <= control.HistoryMin * (decimal)3)
                {
                    control.HistoryMax = control.HistoryMax * (decimal)1.2;
                    control.HistoryMin = control.HistoryMin * (decimal)1;
                }

                if (nowPrice < control.HistoryMin)
                {
                    return(max);
                }
                if (nowPrice > control.HistoryMax)
                {
                    return(min);
                }

                var percent = (control.HistoryMax * (decimal)1.2 - nowPrice) / (control.HistoryMax * (decimal)1.2 - control.HistoryMin * (decimal)1.2);
                divide = min + Convert.ToInt32(percent * (max - min));
                if (divide > max)
                {
                    divide = max;
                }
                if (divide < min)
                {
                    divide = min;
                }
                return(divide);
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
                return(divide);
            }
        }
Example #6
0
        public static int GetRecommendDivideForMore(string symbolName, string quoteCurrency, decimal nowPrice)
        {
            int divide = coinCount[quoteCurrency] * 25;

            try
            {
                var control = new DogControlDao().GetDogControl(symbolName, quoteCurrency);
                if (control == null || control.HistoryMax <= control.HistoryMin || control.HistoryMin <= 0 || control.HistoryMax <= 0)
                {
                    return(divide);
                }
                var max = coinCount[quoteCurrency] * 38;
                var min = Math.Max(coinCount[quoteCurrency] * 10, 100);

                // 防止价格波动后的, 分隔过合理. 下
                if (control.HistoryMax < control.HistoryMin * (decimal)2)
                {
                    control.HistoryMax = control.HistoryMax * (decimal)1.2;
                    control.HistoryMin = control.HistoryMin * (decimal)0.4;
                }
                if (control.HistoryMax <= control.HistoryMin * (decimal)3)
                {
                    control.HistoryMax = control.HistoryMax * (decimal)1;
                    control.HistoryMin = control.HistoryMin * (decimal)0.8;
                }

                if (nowPrice >= control.HistoryMax)
                {
                    return(max);
                }

                if (nowPrice <= control.HistoryMin)
                {
                    return(min);
                }

                var percent = (control.HistoryMax - nowPrice) / (control.HistoryMax - control.HistoryMin);
                divide = max - Convert.ToInt32(percent * (max - min));
                divide = Math.Min(divide, max);
                divide = Math.Max(divide, min);

                //logger.Error($"分隔数据:{symbolName} {quoteCurrency} --- divide: {divide} ---- nowPrice: {nowPrice}");

                return(divide);
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
                return(divide);
            }
        }
        public async Task <object> DoEmpty(string userName, string symbolName, string quoteCurrency)
        {
            // 立马空单
            var symbols    = CoinUtils.GetAllCommonSymbols("usdt");
            var symbol     = symbols.Find(it => it.BaseCurrency == symbolName);
            var dao        = new KlineDao();
            var lastKlines = dao.List24HourKline(symbol.QuoteCurrency, symbol.BaseCurrency);

            if (Utils.GetDateById(lastKlines[0].Id) < DateTime.Now.AddMinutes(-3))
            {
                // 数据是3分钟前的数据, 不合理.
                return("没有拿到最近3分钟的数据");
            }
            // 大于今天最小值30%才行 or 大于24小时60%  并且大于历史最小的15%
            var control = new DogControlDao().GetDogControl(symbolName, quoteCurrency);

            if (control == null)
            {
                return("没有管控");
            }
            var nowPrice = lastKlines[0].Close;

            if (nowPrice < control.HistoryMin && nowPrice < control.HistoryMin + (control.HistoryMax - control.HistoryMin) * (decimal)0.12)
            {
                return("要大于区间12%");
            }

            var min24    = lastKlines.Min(it => it.Close);
            var minToday = lastKlines.Where(it => Utils.GetDateById(it.Id) >= DateTime.Now.Date).Min(it => it.Close);

            if (nowPrice > min24 * (decimal)1.60 || nowPrice > minToday * (decimal)1.30)
            {
                CoinTrade.DoEmpty(symbol, userName, AccountConfigUtils.GetAccountConfig(userName).MainAccountId);
                return(new { nowPrice, min24, minToday, DoEmpty = true });
            }
            return(new { nowPrice, min24, minToday });
        }
Example #8
0
        public static bool Run(int index, CommonSymbol symbol, List <Ticker> tickers)
        {
            // 先获取最近的数据, 看看是否靠近购入,卖出
            var minDogMoreBuy   = new DogMoreBuyDao().GetSmallestDogMoreBuy(symbol.QuoteCurrency, symbol.BaseCurrency);
            var maxDogEmptySell = new DogEmptySellDao().GetBiggestDogEmptySell(symbol.QuoteCurrency, symbol.BaseCurrency);
            var findTicker      = tickers.Find(it => it.symbol == symbol.BaseCurrency + symbol.QuoteCurrency);

            if (findTicker == null)
            {
                //logger.Error($"{symbol.QuoteCurrency}, {symbol.BaseCurrency}");
                return(false);
            }

            if (findTicker.open <= 0 || findTicker.close <= 0 || findTicker.high <= 0 || findTicker.low <= 0)
            {
                logger.Error($"数据不对 : {JsonConvert.SerializeObject(findTicker)}");
                return(false);
            }

            var control = new DogControlDao().GetDogControl(symbol.BaseCurrency, symbol.QuoteCurrency);

            if (control == null)
            {
                return(false);
            }

            if (control.HistoryMin >= findTicker.close || control.HistoryMax <= findTicker.close)
            {
                // 初始化一下
                RefreshHistoryMaxMinAsync(control.SymbolName, control.QuoteCurrency);
            }

            new DogNowPriceDao().CreateDogNowPrice(new DogNowPrice
            {
                NowPrice      = findTicker.close,
                NowTime       = Utils.GetIdByDate(DateTime.Now),
                QuoteCurrency = symbol.QuoteCurrency,
                SymbolName    = symbol.BaseCurrency,
                TodayMaxPrice = findTicker.high,
                TodayMinPrice = findTicker.low,
                NearMaxPrice  = findTicker.high
            });

            var maySell = false;
            var mayBuy  = false;

            if ((
                    control.EmptyPrice < findTicker.close && (
                        maxDogEmptySell == null ||
                        findTicker.close / maxDogEmptySell.SellOrderPrice > (decimal)1.082)
                    )

                || (maxDogEmptySell != null &&
                    maxDogEmptySell.SellOrderPrice / findTicker.close > (decimal)1.085))
            {
                maySell = true;
            }
            if (
                (control.MaxInputPrice > findTicker.close && (
                     minDogMoreBuy == null ||
                     minDogMoreBuy.BuyOrderPrice / findTicker.close > (decimal)1.062))

                || (minDogMoreBuy != null && findTicker.close / minDogMoreBuy.BuyOrderPrice > (decimal)1.09))
            {
                mayBuy = true;
                if (symbol.QuoteCurrency == "btc" && nobtcbalanceTime > DateTime.Now.AddMinutes(-5) && (
                        minDogMoreBuy == null || minDogMoreBuy.BuyOrderPrice / findTicker.close > (decimal)1.06
                        ))
                {
                    mayBuy = false;
                }
            }

            if (symbol.BaseCurrency == "xmx")
            {
                Console.WriteLine($"{maySell}, {mayBuy}");
            }
            if (!mayBuy && !maySell)
            {
                return(false);
            }

            AnalyzeResult analyzeResult = AnalyzeResult.GetAnalyzeResult(symbol);

            if (analyzeResult == null)
            {
                return(false);
            }

            try
            {
                // 计算是否适合购买
                RunBuy(symbol, analyzeResult);
            }
            catch (Exception ex)
            {
                logger.Error($"---> 购买异常: {JsonConvert.SerializeObject(symbol)}" + ex.Message, ex);
            }

            try
            {
                // 计算是否适合出售
                RunSell(symbol, analyzeResult, findTicker);

                RunCount++;
            }
            catch (Exception ex)
            {
                logger.Error($"---> 出售异常: {JsonConvert.SerializeObject(symbol)}" + ex.Message, ex);
            }

            return(true);
        }
Example #9
0
        public async Task <object> listMoreBuyIsNotFinished(string userName, string symbolName, string quoteCurrency, string sort = "lastbuy")
        {
            try
            {
                var list    = new List <DogMoreBuyDTO>();
                var symbols = CoinUtils.GetAllCommonSymbols(quoteCurrency);
                var tickers = PlatformApi.GetInstance("xx").GetTickers();
                Dictionary <string, decimal> closeDic = new Dictionary <string, decimal>();
                Dictionary <string, decimal> todayDic = new Dictionary <string, decimal>();
                var findTickers = tickers.FindAll(it => it.symbol.EndsWith(quoteCurrency));
                foreach (var item in findTickers)
                {
                    var itemSymbolName = item.symbol.Substring(0, item.symbol.Length - quoteCurrency.Length);
                    closeDic.Add(itemSymbolName, item.close);

                    if (item.close > 0)
                    {
                        todayDic.Add(itemSymbolName + "+", item.high / item.close);
                    }

                    if (item.low > 0)
                    {
                        todayDic.Add(itemSymbolName, item.high / item.low);
                        todayDic.Add(itemSymbolName + "-", item.close / item.low);
                    }
                }

                var dogcontrol    = new DogControlDao().ListAllDogControl();
                var maxInputPrice = new Dictionary <string, decimal>();
                var emptyPrice    = new Dictionary <string, decimal>();
                foreach (var item in dogcontrol)
                {
                    if (item.QuoteCurrency != quoteCurrency)
                    {
                        continue;
                    }
                    maxInputPrice.Add(item.SymbolName, item.MaxInputPrice);
                    emptyPrice.Add(item.SymbolName, item.EmptyPrice);
                }

                if (string.IsNullOrEmpty(symbolName))
                {
                    list = new DogMoreBuyDao().listEveryMinPriceMoreBuyIsNotFinished(userName, quoteCurrency);
                    var countSymbol = new DogMoreBuyDao().CountSymbol(userName, quoteCurrency);
                    foreach (var item in list)
                    {
                        item.Count = countSymbol.Find(it => it.SymbolName == item.SymbolName)?.Count ?? 0;
                    }
                    list = list.Where(it => it.SymbolName != "btc" && it.SymbolName != "ven" && it.SymbolName != "hsr").ToList();

                    if (sort != "lastbuy")
                    {
                        list.Sort((a, b) =>
                        {
                            if (!closeDic.ContainsKey(a.SymbolName) || !closeDic.ContainsKey(b.SymbolName))
                            {
                                return(1);
                            }
                            var aTradePrice = a.BuyTradePrice;
                            if (aTradePrice <= 0)
                            {
                                aTradePrice = a.BuyOrderPrice;
                            }
                            var bTradePrice = b.BuyTradePrice;
                            if (bTradePrice <= 0)
                            {
                                bTradePrice = b.BuyOrderPrice;
                            }
                            var ap = closeDic[a.SymbolName] / aTradePrice;
                            var bp = closeDic[b.SymbolName] / bTradePrice;
                            if (sort == "more")
                            {
                                return(ap > bp ? 1 : -1);
                            }
                            else
                            {
                                return(ap > bp ? -1 : 1);
                            }
                        });
                    }
                    else
                    {
                        list.Sort((a, b) =>
                        {
                            return((b.BuyDate.Ticks > a.BuyDate.Ticks || (b.BuyDate.Ticks == a.BuyDate.Ticks && string.Compare(b.SymbolName, a.SymbolName) > 0)) ? 1 : -1);
                        });
                    }
                }
                else
                {
                    list = new DogMoreBuyDao().listMoreBuyIsNotFinished(userName, symbolName, quoteCurrency);
                }

                var noBuy = symbols.Select(it => it.BaseCurrency).ToList();
                noBuy.RemoveAll(it => list.Find(item => item.SymbolName == it) != null);

                return(new { list, closeDic, todayDic, noBuy, maxInputPrice, emptyPrice });
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
                throw ex;
            }
        }