예제 #1
0
        public static void AccountBalanceDetail()
        {
            while (true)
            {
                Console.WriteLine("请输入 accountname:");
                var name = Console.ReadLine();
                AccountConfig.init(name);

                // 获取主账户的财富值
                var accountBalance = new AccountOrder().AccountBalance(AccountConfig.mainAccountId);
                //foreach (var item in accountBalance.data.list)
                //{
                //    Console.WriteLine($"{item.currency} -- {item.balance}");
                //}

                // 统计被套牢的数据
                Dictionary <string, decimal> coins = new Dictionary <string, decimal>();

                var noselllist = new CoinDao().ListAllNoSellRecord(AccountConfig.mainAccountId);
                foreach (var item in noselllist)
                {
                    if (coins.ContainsKey(item.Coin))
                    {
                        coins[item.Coin] += item.BuyTotalQuantity;
                    }
                    else
                    {
                        coins.Add(item.Coin, item.BuyTotalQuantity);
                    }
                }

                var noselllist2 = new CoinDao().ListNoSellRecordFromOther();
                foreach (var item in noselllist2)
                {
                    if (coins.ContainsKey(item.BuyCoin))
                    {
                        coins[item.BuyCoin] += item.BuyAmount;
                    }
                    else
                    {
                        coins.Add(item.BuyCoin, item.BuyAmount);
                    }
                }
                foreach (var item in accountBalance.data.list)
                {
                    if (item.balance == 0)
                    {
                        continue;
                    }
                    decimal tl = 0;
                    if (coins.ContainsKey(item.currency))
                    {
                        tl = coins[item.currency];
                    }
                    Console.WriteLine($"{item.currency} -- {item.balance} --》{tl}");
                }
            }
        }
예제 #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 bool CheckBalance()
        {
            i++;
            if (usdt == null)
            {
                var accountId   = AccountConfig.mainAccountId;
                var accountInfo = new AccountOrder().AccountBalance(accountId);
                usdt = accountInfo.data.list.Find(it => it.currency == "usdt");
            }

            if (usdt.balance < 10 && i % 100 == 0)
            {
                Console.WriteLine($"--------------------- 余额{usdt.balance}----------------------------");
            }

            if (usdt.balance < 6)
            {
                Console.WriteLine("---------------------余额小于6,无法交易----------------------------");
                return(false);
            }
            return(true);
        }
예제 #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 decimal GetRecommendBuyAmount(string coin)
        {
            if (noSellCount < 0)
            {
                noSellCount = new CoinDao().GetAllNoSellRecordCount();
            }

            if (usdt == null)
            {
                var accountId   = AccountConfig.mainAccountId;
                var accountInfo = new AccountOrder().AccountBalance(accountId);
                usdt = accountInfo.data.list.Find(it => it.currency == "usdt");
            }

            var calcPencert = getCalcPencent(new CoinAnalyze().CalcPercent(coin));

            decimal recommend = 0;

            if (noSellCount < 80)
            {
                recommend = (usdt.balance / 200) / calcPencert;///  0.8,  1,  1.2,  1.5;
            }
            else
            {
                recommend = (usdt.balance / 160) / calcPencert;///  0.8,  1,  1.2,  1.5;
            }
            return(Math.Min(recommend, AccountConfig.userName == "lzq" ? (decimal)16.5 : (decimal)7.5));

            //if (noSellCount > 80)
            //{
            //    return usdt.balance / 30;
            //}

            //// 让每个承受8轮
            //return usdt.balance / (100 - noSellCount);
        }
예제 #8
0
        public static void BusinessRun(string coin)
        {
            var           accountId = AccountConfig.mainAccountId;
            ResponseKline res       = new AnaylyzeApi().kline(coin + "usdt", "1min", 1000);
            // 获取最近行情
            decimal lastLow;
            decimal nowOpen;
            // 分析是否下跌, 下跌超过一定数据,可以考虑
            decimal flexPercent   = (decimal)1.04;
            var     flexPointList = new CoinAnalyze().Analyze(res, out lastLow, out nowOpen, flexPercent);

            if (flexPointList == null || flexPointList.Count <= 1)
            {
                flexPercent   = (decimal)1.03;
                flexPointList = new CoinAnalyze().Analyze(res, out lastLow, out nowOpen, flexPercent);
            }
            int celuo = 0; //默认 下面一截都是在平稳时候的策略
            var celue2FlextPointList = new List <FlexPoint>();

            if (flexPointList == null || flexPointList.Count <= 1)
            {
                flexPercent          = (decimal)1.02;
                celue2FlextPointList = new CoinAnalyze().Analyze(res, out lastLow, out nowOpen, flexPercent);
                celuo = 1;
                if (flexPointList == null || flexPointList.Count < 1)
                {
                    flexPercent          = (decimal)1.015;
                    celue2FlextPointList = new CoinAnalyze().Analyze(res, out lastLow, out nowOpen, flexPercent);
                }
            }
            if (flexPointList.Count == 0 && celue2FlextPointList.Count == 0)
            {
                logger.Error($"--------------> 分析结果数量为0 {coin}");
                return;
            }

            decimal recommendAmount = GetRecommendBuyAmount(coin);

            Console.Write($"spot--------> 开始 {coin}  推荐额度:{decimal.Round(recommendAmount, 2)} ");

            try
            {
                // 查询出结果还没好的数据, 去搜索一下
                var noSetBuySuccess = new CoinDao().ListNotSetBuySuccess(accountId, coin);
                foreach (var item in noSetBuySuccess)
                {
                    QueryDetailAndUpdate(item.BuyOrderId);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
            }

            try
            {
                // 查询出结果还没好的数据, 去搜索一下
                var noSetSellSuccess = new CoinDao().ListHasSellNotSetSellSuccess(accountId, coin);
                foreach (var item in noSetSellSuccess)
                {
                    Console.WriteLine("----------> " + JsonConvert.SerializeObject(item));
                    QuerySellDetailAndUpdate(item.SellOrderId);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
            }

            if (CheckBalance() && recommendAmount > (decimal)0.3 && !IsQuickRise(coin, res))
            {
                if (celuo > 0 && celue2FlextPointList.Count > 0 && !celue2FlextPointList[0].isHigh && recommendAmount > (decimal)1.3)
                {
                    var celue1count = new CoinDao().GetNoSellRecordCountForCelue1(accountId, coin);
                    if (celue1count <= 0 && new CoinAnalyze().CheckCalcMaxhuoluo(coin, "usdt", "5min"))
                    {
                        // 说明可以购入策略1的方案
                        recommendAmount = recommendAmount / 4;
                        decimal buyQuantity = recommendAmount / nowOpen;
                        buyQuantity = decimal.Round(buyQuantity, GetBuyQuantityPrecisionNumber(coin));
                        decimal       orderPrice = decimal.Round(nowOpen * (decimal)1.005, getPrecisionNumber(coin));
                        ResponseOrder order      = new AccountOrder().NewOrderBuy(accountId, buyQuantity, orderPrice, null, coin, "usdt");
                        if (order.status != "error")
                        {
                            new CoinDao().CreateSpotRecord(new SpotRecord()
                            {
                                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  = "",
                                Celuo            = celuo
                            });
                            ClearData();
                            // 下单成功马上去查一次
                            QueryDetailAndUpdate(order.data);
                        }
                        else
                        {
                            logger.Error($"下单结果(策略1) coin{coin} accountId:{accountId}  购买数量{buyQuantity} nowOpen{nowOpen} {JsonConvert.SerializeObject(order)}");
                            logger.Error($"下单结果(策略1) 分析 {JsonConvert.SerializeObject(flexPointList)}");
                        }
                    }
                }
                if (flexPointList.Count > 0 && !flexPointList[0].isHigh)
                {
                    var noSellCount = new CoinDao().GetNoSellRecordCount(accountId, coin);
                    // 最后一次是高位, 没有交易记录, 则判断是否少于最近的6%
                    if (noSellCount <= 0 && CheckCanBuy(nowOpen, flexPointList[0].open, celuo) && new CoinAnalyze().CheckCalcMaxhuoluo(coin, "usdt", "5min"))
                    {
                        // 可以考虑
                        decimal buyQuantity = recommendAmount / nowOpen;
                        buyQuantity = decimal.Round(buyQuantity, GetBuyQuantityPrecisionNumber(coin));
                        decimal       orderPrice = decimal.Round(nowOpen * (decimal)1.005, getPrecisionNumber(coin));
                        ResponseOrder order      = new AccountOrder().NewOrderBuy(accountId, buyQuantity, orderPrice, null, coin, "usdt");
                        if (order.status != "error")
                        {
                            new CoinDao().CreateSpotRecord(new SpotRecord()
                            {
                                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  = "",
                                Celuo            = celuo
                            });
                            ClearData();
                            // 下单成功马上去查一次
                            QueryDetailAndUpdate(order.data);
                        }
                        else
                        {
                            logger.Error($"下单结果 coin{coin} accountId:{accountId}  购买数量{buyQuantity} nowOpen{nowOpen} {JsonConvert.SerializeObject(order)}");
                            logger.Error($"下单结果 分析 {JsonConvert.SerializeObject(flexPointList)}");
                        }
                    }

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

                        // 再少于5%,
                        var     per    = new CoinAnalyze().CalcPercent(coin);
                        decimal pecent = getCalcPencent222(per, flexPointList.Count);//noSellCount >= 15 ? (decimal)1.03 : (decimal)1.025;
                        if (nowOpen * pecent < minBuyPrice)
                        {
                            decimal buyQuantity = recommendAmount / nowOpen;
                            buyQuantity = decimal.Round(buyQuantity, GetBuyQuantityPrecisionNumber(coin));
                            decimal       orderPrice = decimal.Round(nowOpen * (decimal)1.005, getPrecisionNumber(coin));
                            ResponseOrder order      = new AccountOrder().NewOrderBuy(accountId, buyQuantity, orderPrice, null, coin, "usdt");
                            if (order.status != "error")
                            {
                                new CoinDao().CreateSpotRecord(new SpotRecord()
                                {
                                    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  = ""
                                });
                                ClearData();
                                // 下单成功马上去查一次
                                QueryDetailAndUpdate(order.data);
                            }
                            else
                            {
                                logger.Error($"下单结果 coin{coin} accountId:{accountId}  购买数量{buyQuantity} nowOpen{nowOpen} {JsonConvert.SerializeObject(order)}");
                                logger.Error($"下单结果 分析 {JsonConvert.SerializeObject(flexPointList)}");
                            }
                        }
                    }
                }
            }

            //ResponseKline res = new AnaylyzeApi().kline(coin + "usdt", "1min", 1000);
            // 查询数据库中已经下单数据,如果有,则比较之后的最高值,如果有,则出售

            {
                var        needSellList = new CoinDao().ListBuySuccessAndNoSellRecord(accountId, coin, 0);
                SpotRecord last         = null;
                foreach (var item in needSellList)
                {
                    if (last == null || item.BuyDate > last.BuyDate)
                    {
                        last = item;
                    }
                }

                foreach (var item in needSellList)
                {
                    // 分析是否 大于
                    decimal itemNowOpen = 0;
                    decimal higher      = new CoinAnalyze().AnalyzeNeedSell(item.BuyOrderPrice, item.BuyDate, coin, "usdt", out itemNowOpen, res);

                    decimal gaoyuPercentSell = (decimal)1.035;
                    if (needSellList.Count > 10)
                    {
                        gaoyuPercentSell = (decimal)1.050;
                    }
                    else if (needSellList.Count > 9)
                    {
                        gaoyuPercentSell = (decimal)1.048;
                    }
                    else if (needSellList.Count > 8)
                    {
                        gaoyuPercentSell = (decimal)1.046;
                    }
                    else if (needSellList.Count > 7)
                    {
                        gaoyuPercentSell = (decimal)1.044;
                    }
                    else if (needSellList.Count > 6)
                    {
                        gaoyuPercentSell = (decimal)1.042;
                    }
                    else if (needSellList.Count > 5)
                    {
                        gaoyuPercentSell = (decimal)1.04;
                    }

                    bool needHuitou = true;// 如果很久没有出售过,则要考虑不需要回头
                    if (flexPercent < (decimal)1.04)
                    {
                        gaoyuPercentSell = (decimal)1.035;
                        if (flexPointList.Count <= 2 && last.BuyDate < DateTime.Now.AddDays(-1))
                        {
                            // 1天都没有交易. 并且波动比较小. 则不需要回头
                            needHuitou = false;
                        }
                    }

                    var canSell = CheckCanSell(item.BuyOrderPrice, higher, itemNowOpen, gaoyuPercentSell, needHuitou);

                    if (canSell)
                    {
                        decimal sellQuantity = item.BuyTotalQuantity * (decimal)0.99;
                        sellQuantity = decimal.Round(sellQuantity, getSellPrecisionNumber(coin));
                        if (coin == "xrp" && sellQuantity < 1)
                        {
                            sellQuantity = 1;
                        }
                        // 出售
                        decimal       sellPrice = decimal.Round(itemNowOpen * (decimal)0.985, getPrecisionNumber(coin));
                        ResponseOrder order     = new AccountOrder().NewOrderSell(accountId, sellQuantity, sellPrice, null, coin, "usdt");
                        if (order.status != "error")
                        {
                            new CoinDao().ChangeDataWhenSell(item.Id, sellQuantity, sellPrice, JsonConvert.SerializeObject(order), JsonConvert.SerializeObject(flexPointList), order.data);
                            // 下单成功马上去查一次
                            QuerySellDetailAndUpdate(order.data);
                        }
                        else
                        {
                            logger.Error($"出售结果 coin{coin} accountId:{accountId}  出售数量{sellQuantity} itemNowOpen{itemNowOpen} higher{higher} {JsonConvert.SerializeObject(order)}");
                            logger.Error($"出售结果 分析 {JsonConvert.SerializeObject(flexPointList)}");
                        }
                        ClearData();
                    }
                }
            }
            {
                var needSellList = new CoinDao().ListBuySuccessAndNoSellRecord(accountId, coin, 1);
                foreach (var item in needSellList)
                {
                    // 分析是否 大于
                    decimal itemNowOpen = 0;
                    decimal higher      = new CoinAnalyze().AnalyzeNeedSell(item.BuyOrderPrice, item.BuyDate, coin, "usdt", out itemNowOpen, res);

                    decimal gaoyuPercentSell = (decimal)1.016;
                    bool    needHuitou       = true;// 如果很久没有出售过,则要考虑不需要回头
                    gaoyuPercentSell = (decimal)1.016;

                    if (CheckCanSell(item.BuyOrderPrice, higher, itemNowOpen, gaoyuPercentSell, needHuitou))
                    {
                        decimal sellQuantity = item.BuyTotalQuantity * (decimal)0.99;
                        sellQuantity = decimal.Round(sellQuantity, getSellPrecisionNumber(coin));
                        if (coin == "xrp" && sellQuantity < 1)
                        {
                            sellQuantity = 1;
                        }
                        // 出售
                        decimal       sellPrice = decimal.Round(itemNowOpen * (decimal)0.985, getPrecisionNumber(coin));
                        ResponseOrder order     = new AccountOrder().NewOrderSell(accountId, sellQuantity, sellPrice, null, coin, "usdt");
                        if (order.status != "error")
                        {
                            new CoinDao().ChangeDataWhenSell(item.Id, sellQuantity, sellPrice, JsonConvert.SerializeObject(order), JsonConvert.SerializeObject(flexPointList), order.data);
                            // 下单成功马上去查一次
                            QuerySellDetailAndUpdate(order.data);
                        }
                        else
                        {
                            logger.Error($"出售结果 coin{coin} accountId:{accountId}  出售数量{sellQuantity} itemNowOpen{itemNowOpen} higher{higher} {JsonConvert.SerializeObject(order)}");
                            logger.Error($"出售结果 分析 {JsonConvert.SerializeObject(flexPointList)}");
                        }
                        ClearData();
                    }
                }
            }

            //try
            //{
            //    // 判断是否出售老的数据 old old old old old old old old old old old old old old old old old
            //    var oldSelllist = new OldCoinDao().ListNoSellRecord(coin);
            //    Console.WriteLine($"老的数据,需要出售 ${oldSelllist.Count}");
            //    foreach (var item in oldSelllist)
            //    {
            //        // 分析是否 大于
            //        decimal itemNowOpen = 0;
            //        decimal higher = new CoinAnalyze().AnalyzeNeedSell(item.BuyPrice, item.BuyDate, coin, "usdt", out itemNowOpen, res);

            //        decimal gaoyuPercentSell = (decimal)1.04;

            //        if (CheckCanSell(item.BuyPrice, higher, itemNowOpen, gaoyuPercentSell))
            //        {
            //            Console.WriteLine($"老的数据, 满足出售条件 需要出售 ${oldSelllist.Count} 购买价格{item.BuyPrice} 现在价位{itemNowOpen}");

            //            decimal sellQuantity = item.BuyAmount * (decimal)0.99;
            //            sellQuantity = decimal.Round(sellQuantity, getSellPrecisionNumber(coin));
            //            // 出售
            //            decimal sellPrice = decimal.Round(itemNowOpen * (decimal)0.985, getPrecisionNumber(coin));
            //            ResponseOrder order = new AccountOrder().NewOrderSell(accountId, sellQuantity, sellPrice, null, coin, "usdt");
            //            if (order.status != "error")
            //            {
            //                new OldCoinDao().SetHasSell(item.Id, sellQuantity, JsonConvert.SerializeObject(order), JsonConvert.SerializeObject(flexPointList));
            //            }

            //            logger.Error($"old出售结果 coin{coin} accountId:{accountId}  出售数量{sellQuantity}  购买价格{item.BuyPrice} itemNowOpen{itemNowOpen} higher{higher} {JsonConvert.SerializeObject(order)}");
            //            logger.Error($"old出售结果 分析 {JsonConvert.SerializeObject(flexPointList)}");

            //            ClearData();
            //        }
            //    }

            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine($"老的数据,需要出售 ${ex.Message}");
            //}
        }