コード例 #1
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);
            }
        }
コード例 #2
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);
                }
            }
        }
コード例 #3
0
        public static void BusinessRun(string coin)
        {
            var           accountId = "";
            ResponseKline res       = new AnaylyzeApi().kline(coin + "usdt", "1min", 1440);
            // 获取最近行情
            decimal lastLow;
            decimal nowOpen;
            // 分析是否下跌, 下跌超过一定数据,可以考虑
            var flexPointList = new CoinAnalyze().Analyze(res, out lastLow, out nowOpen);

            if (flexPointList.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 (!flexPointList[0].isHigh && CheckBalance() && recommendAmount > (decimal)0.3 && !IsQuickRise(res))
            {
                var noSellCount = new CoinDao().GetNoSellRecordCount(accountId, coin);
                // 最后一次是高位, 没有交易记录, 则判断是否少于最近的6%
                if (noSellCount <= 0 && CheckCanBuy(nowOpen, flexPointList[0].open) && 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 OkexRecord()
                        {
                            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)}");
                    }
                }

                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);//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 OkexRecord()
                            {
                                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)}");
                        }
                    }
                }
            }

            // 查询数据库中已经下单数据,如果有,则比较之后的最高值,如果有,则出售
            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);

                decimal gaoyuPercentSell = (decimal)1.03;
                if (needSellList.Count > 10)
                {
                    gaoyuPercentSell = (decimal)1.045;
                }
                else if (needSellList.Count > 9)
                {
                    gaoyuPercentSell = (decimal)1.042;
                }
                else if (needSellList.Count > 8)
                {
                    gaoyuPercentSell = (decimal)1.04;
                }
                else if (needSellList.Count > 7)
                {
                    gaoyuPercentSell = (decimal)1.038;
                }
                else if (needSellList.Count > 6)
                {
                    gaoyuPercentSell = (decimal)1.035;
                }
                else if (needSellList.Count > 5)
                {
                    gaoyuPercentSell = (decimal)1.032;
                }

                if (CheckCanSell(item.BuyOrderPrice, higher, itemNowOpen, gaoyuPercentSell))
                {
                    decimal sellQuantity = item.BuyTotalQuantity * (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 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();
                }
            }
        }