示例#1
0
        /// <summary>
        /// 1. 计算每个购入价位,再最近一段时间的徘徊值,如果偏低,则出售百分比偏高
        /// 2. 计算是当前bi种是否再所有里面是最低的,最更偏高。
        /// 3. 耐性和毅力是最好结果的来源
        /// </summary>
        /// <param name="accountId"></param>
        /// <param name="coin"></param>
        /// <param name="account"></param>
        /// <param name="flexPointList"></param>
        public static void BusinessRunAccountForSell(string accountId, string coin, AccountData account, List <FlexPoint> flexPointList)
        {
            if (flexPointList.Count == 0 || !flexPointList[0].isHigh)
            {
                return;
            }

            var needSellList = new CoinDao().ListBuySuccessAndNoSellRecord(accountId, coin);

            // 查询数据库中已经下单数据,如果有,则比较之后的最高值,如果有,则出售
            foreach (var item in needSellList)
            {
                // 分析是否 大于
                //decimal itemNowOpen = 0;
                //decimal higher = new CoinAnalyze().AnalyzeNeedSell(item.BuyOrderPrice, item.BuyDate, coin, "usdt", out itemNowOpen);
                AnaylyzeData anaylyzeData = new CoinAnalyze().GetAnaylyzeData(coin, "usdt");

                //if (CheckCanSell(item.BuyOrderPrice, higher, itemNowOpen))
                if (CheckCanSellByAnaylyzeData(anaylyzeData, item))
                {
                    decimal sellQuantity = item.BuyTotalQuantity * (decimal)0.99;
                    sellQuantity = decimal.Round(sellQuantity, getSellPrecisionNumber(coin));
                    if (coin == "btc" && sellQuantity >= item.BuyTotalQuantity)
                    {
                        sellQuantity = sellQuantity - (decimal)0.0001;
                    }
                    if (sellQuantity < (decimal)0.0001)
                    {
                        sellQuantity = (decimal)0.0001;
                    }
                    // 出售
                    decimal sellOrderPrice = decimal.Round(anaylyzeData.NowPrice * (decimal)0.985, getPrecisionNumber(coin));
                    if (sellOrderPrice < item.BuyOrderPrice * (decimal)1.025)
                    {
                        logger.Error("-----------------算法有误--------------");
                        Console.WriteLine("-----------------算法有误--------------");
                        return;
                    }
                    ResponseOrder order = new AccountOrder().NewOrderSell(accountId, sellQuantity, sellOrderPrice, null, coin, "usdt");
                    if (order.status != "error")
                    {
                        new CoinDao().ChangeDataWhenSell(item.Id, sellQuantity, sellOrderPrice, JsonConvert.SerializeObject(order), JsonConvert.SerializeObject(flexPointList), order.data);
                        // 下单成功马上去查一次
                        QuerySellDetailAndUpdate(order.data);
                    }
                    logger.Error($"出售结果 coin{coin} accountId:{accountId}  出售数量{sellQuantity} sellOrderPrice:{sellOrderPrice}");
                    logger.Error($"出售拐点 {JsonConvert.SerializeObject(flexPointList)}");
                    logger.Error($"出售单子 {JsonConvert.SerializeObject(item)}");
                    logger.Error($"出售结果 {JsonConvert.SerializeObject(order)}");
                }
            }
        }
示例#2
0
        public static void Balance()
        {
            var res = new AccountOrder().Accounts();

            Console.WriteLine(res);
            Console.WriteLine(res.data.Count);
            while (true)
            {
                Console.WriteLine("请输入 id:");
                var id = Console.ReadLine();
                var b  = new AccountOrder().AccountBalance(id);
                b.data.list = b.data.list.Where(it => it.balance > 0).ToList();
                var usdt = b.data.list.Find(it => it.currency == "usdt");
                Console.WriteLine(JsonConvert.SerializeObject(b));
                Console.WriteLine(JsonConvert.SerializeObject(usdt));
            }

            //new CoinDao().InsertLog(new BuyRecord()
            //{
            //     BuyCoin ="ltc",
            //     BuyPrice = new decimal(1.1),
            //      BuyDate = DateTime.Now,
            //       HasSell = false,
            //});

            //var list = new CoinDao().ListNoSellRecord("ltc");
            //Console.WriteLine(list.Count);
            //new CoinDao().SetHasSell(1);

            //while (true)
            //{
            //    Console.WriteLine("请输入:");
            //    var coin = Console.ReadLine();
            //    ResponseOrder order = new AccountOrder().NewOrderBuy(AccountConfig.mainAccountId, 1, (decimal)0.01, null, coin, "usdt");
            //}

            //while (true)
            //{
            //    Console.WriteLine("请输入:");
            //    var coin = Console.ReadLine();

            //    decimal lastLow;
            //    decimal nowOpen;
            //    var flexPointList = new CoinAnalyze().Analyze(coin, "usdt", out lastLow, out nowOpen);
            //    foreach (var flexPoint in flexPointList)
            //    {
            //        Console.WriteLine($"{flexPoint.isHigh}, {flexPoint.open}, {Utils.GetDateById(flexPoint.id)}");
            //    }
            //}
        }
示例#3
0
        public static void Order()
        {
            while (true)
            {
                Console.WriteLine("请输入 orderid:");
                var    orderId    = Console.ReadLine();
                string orderQuery = "";
                var    b          = new AccountOrder().QueryOrder(orderId, out orderQuery);
                Console.WriteLine(JsonConvert.SerializeObject(b));

                string orderDetail = "";
                var    detail      = new AccountOrder().QueryDetail(orderId, out orderDetail);
                Console.WriteLine(detail);
            }
        }
示例#4
0
        private static void QuerySellDetailAndUpdate(string orderId)
        {
            string orderQuery = "";
            var    queryOrder = new AccountOrder().QueryOrder(orderId, out orderQuery);

            if (queryOrder.status == "ok" && queryOrder.data.state == "filled")
            {
                string  orderDetail = "";
                var     detail      = new AccountOrder().QueryDetail(orderId, out orderDetail);
                decimal minPrice    = 99999999;
                foreach (var item in detail.data)
                {
                    if (minPrice > item.price)
                    {
                        minPrice = item.price;
                    }
                }
                // 完成
                new CoinDao().UpdateTradeRecordSellSuccess(orderId, minPrice, orderQuery);
            }
        }
示例#5
0
        public static AccountBalanceItem GetBlance(string accountId, string coin)
        {
            if (lastGetDate == null)
            {
                lastGetDate = new Dictionary <string, DateTime>();
            }
            if (usdtDict == null)
            {
                usdtDict = new Dictionary <string, AccountBalanceItem>();
            }

            if (lastGetDate.ContainsKey(coin))
            {
                if (lastGetDate[coin] < DateTime.Now.AddMinutes(-10))
                {
                    // 每隔n分钟刷新一次余额0缓存
                    if (usdtDict.ContainsKey(coin))
                    {
                        usdtDict.Remove(coin);
                    }
                }
            }

            if (usdtDict.ContainsKey(coin))
            {
                // 已经有了,不需要再次获取
                return(usdtDict[coin]);
            }

            var accountInfo = new AccountOrder().AccountBalance(accountId);
            var usdt        = accountInfo.data.list.Find(it => it.currency == "usdt" && it.type == "trade");

            usdtDict.Add(coin, usdt);
            if (lastGetDate.ContainsKey(coin))
            {
                lastGetDate.Remove(coin);
            }
            lastGetDate.Add(coin, DateTime.Now);
            return(usdtDict[coin]);
        }
示例#6
0
        private static void QueryDetailAndUpdate(string orderId)
        {
            string orderQuery = "";
            var    queryOrder = new AccountOrder().QueryOrder(orderId, out orderQuery);

            if (queryOrder.status == "ok" && queryOrder.data.state == "filled")
            {
                string  orderDetail = "";
                var     detail      = new AccountOrder().QueryDetail(orderId, out orderDetail);
                decimal maxPrice    = 0;
                foreach (var item in detail.data)
                {
                    if (maxPrice < item.price)
                    {
                        maxPrice = item.price;
                    }
                }
                if (detail.status == "ok")
                {
                    new CoinDao().UpdateTradeRecordBuySuccess(orderId, maxPrice, orderQuery);
                }
            }
        }
示例#7
0
        public static void BusinessRunAccountForBuy(string accountId, string coin, AccountData account, decimal nowOpen, List <FlexPoint> flexPointList)
        {
            var usdtBalance = GetBlance(accountId, coin);

            var noSellCount = new CoinDao().GetNoSellRecordCount(account.id, coin);
            // 平均推荐购买金额
            var avgBuyAmount = GetAvgBuyAmount(usdtBalance.balance, noSellCount);

            Console.WriteLine($"杠杆------->{coin.PadLeft(7, ' ')}   推荐额度:{decimal.Round(avgBuyAmount, 6).ToString().PadLeft(10)}     未售出数量 {noSellCount}");
            if (!flexPointList[0].isHigh && avgBuyAmount >= 1)
            {
                AnaylyzeData anaylyzeData = new CoinAnalyze().GetAnaylyzeData(coin, "usdt");
                // 最后一次是高位,
                // 一定要小于5天内最高位的5%
                // 不追高????
                if (noSellCount <= 0 && CheckCanBuy(nowOpen, flexPointList[0].open) && anaylyzeData.NowPrice * (decimal)1.05 < anaylyzeData.FiveHighestPrice)
                {
                    // 可以考虑
                    decimal buyQuantity = avgBuyAmount / nowOpen;
                    buyQuantity = decimal.Round(buyQuantity, GetBuyQuantityPrecisionNumber(coin));
                    decimal       orderPrice = decimal.Round(nowOpen * (decimal)1.008, getPrecisionNumber(coin));
                    ResponseOrder order      = new AccountOrder().NewOrderBuy(accountId, buyQuantity, orderPrice, null, coin, "usdt");
                    if (order.status != "error")
                    {
                        new CoinDao().CreateTradeRecord(new TradeRecord()
                        {
                            Coin             = coin,
                            UserName         = AccountConfig.userName,
                            BuyTotalQuantity = buyQuantity,
                            BuyOrderPrice    = orderPrice,
                            BuyDate          = DateTime.Now,
                            HasSell          = false,
                            BuyOrderResult   = JsonConvert.SerializeObject(order),
                            BuyAnalyze       = JsonConvert.SerializeObject(flexPointList),
                            AccountId        = accountId,
                            BuySuccess       = false,
                            BuyTradePrice    = 0,
                            BuyOrderId       = order.data,
                            BuyOrderQuery    = "",
                            SellAnalyze      = "",
                            SellOrderId      = "",
                            SellOrderQuery   = "",
                            SellOrderResult  = ""
                        });
                        // 下单成功马上去查一次
                        QueryDetailAndUpdate(order.data);
                    }
                    logger.Error($"下单数据 coin:{coin} accountId:{accountId}  购买数量{buyQuantity} nowOpen{nowOpen},orderPrice:{orderPrice}");
                    logger.Error($"下单拐点 {JsonConvert.SerializeObject(flexPointList)}");
                    logger.Error($"下单结果 {JsonConvert.SerializeObject(order)}");
                }

                if (noSellCount > 0)
                {
                    // 获取最小的那个, 如果有,
                    decimal  minBuyPrice = 9999;
                    DateTime nearBuyDate = DateTime.MinValue;
                    var      noSellList  = new CoinDao().ListNoSellRecord(accountId, coin);
                    if (noSellList.Count == 0)
                    {
                        return;
                    }
                    foreach (var item in noSellList)
                    {
                        if (item.BuyOrderPrice < minBuyPrice)
                        {
                            minBuyPrice = item.BuyOrderPrice;
                        }
                        if (item.BuyDate > nearBuyDate)
                        {
                            nearBuyDate = item.BuyDate;
                        }
                    }

                    // 再少于1%, 并且最近购买时间要至少相差30分钟   控制下单的次数
                    decimal pecent = noSellCount >= 15 ? (decimal)1.05 : (decimal)1.05;
                    if (nearBuyDate < DateTime.Now.AddMinutes(-60 * 4) || nowOpen * pecent < minBuyPrice)
                    {
                        decimal buyQuantity = avgBuyAmount / nowOpen;
                        buyQuantity = decimal.Round(buyQuantity, GetBuyQuantityPrecisionNumber(coin));
                        decimal       buyPrice = decimal.Round(nowOpen * (decimal)1.005, getPrecisionNumber(coin));
                        ResponseOrder order    = new AccountOrder().NewOrderBuy(accountId, buyQuantity, buyPrice, null, coin, "usdt");
                        if (order.status != "error")
                        {
                            new CoinDao().CreateTradeRecord(new TradeRecord()
                            {
                                Coin             = coin,
                                UserName         = AccountConfig.userName,
                                BuyTotalQuantity = buyQuantity,
                                BuyOrderPrice    = buyPrice,
                                BuyDate          = DateTime.Now,
                                HasSell          = false,
                                BuyOrderResult   = JsonConvert.SerializeObject(order),
                                BuyAnalyze       = JsonConvert.SerializeObject(flexPointList),
                                AccountId        = accountId,
                                BuySuccess       = false,
                                BuyTradePrice    = 0,
                                BuyOrderId       = order.data,
                                BuyOrderQuery    = "",
                                SellAnalyze      = "",
                                SellOrderId      = "",
                                SellOrderQuery   = "",
                                SellOrderResult  = ""
                            });
                            // 下单成功马上去查一次
                            QueryDetailAndUpdate(order.data);
                        }
                        logger.Error($"下单数据 coin:{coin} accountId:{accountId}  购买数量:{buyQuantity} buyPrice:{buyPrice} nowOpen:{nowOpen},minBuyPrice:{minBuyPrice}");
                        logger.Error($"下单拐点{JsonConvert.SerializeObject(flexPointList)}");
                        logger.Error($"下单结果{JsonConvert.SerializeObject(order)}");
                    }
                }
            }
        }