public static void ShowDetail()
        {
            Console.WriteLine(" begin ShowDetail");
            var             btcPrice     = (decimal)0;
            var             ethPrice     = (decimal)0;
            var             okbPrice     = (decimal)0;
            List <CoinInfo> allCoinInfos = new List <CoinInfo>();

            try
            {
                allCoinInfos = new CoinInfoDao().ListCoinInfo();
                var btcKlines = OkApi.GetKLineDataAsync("btc-usdt");
                btcPrice = btcKlines[0].close;
                var ethKlines = OkApi.GetKLineDataAsync("eth-usdt");
                ethPrice = ethKlines[0].close;
                var okbKlines = OkApi.GetKLineDataAsync("okb-usdt");
                okbPrice = okbKlines[0].close;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            var accounts = OkApi.ListAccounts();

            foreach (var account in accounts)
            {
                try
                {
                    var klines   = OkApi.GetKLineDataAsync($"{account.currency}-usdt");
                    var nowPrice = klines[0].close;
                    if (account.balance * nowPrice > 50)
                    {
                        Console.WriteLine($"usdt {account.currency} --> {account.balance} --> usd{account.balance * nowPrice}  ");
                    }
                    continue;
                }
                catch (Exception ex)
                {
                    //Console.WriteLine(ex.Message);
                }

                try
                {
                    var klines   = OkApi.GetKLineDataAsync($"{account.currency}-btc");
                    var nowPrice = klines[0].close;
                    if (account.balance * nowPrice * btcPrice > 50)
                    {
                        Console.WriteLine($"btc {account.currency} --> {account.balance} --> usd{account.balance * nowPrice * btcPrice} ");
                    }
                    continue;
                }
                catch (Exception ex)
                {
                    //Console.WriteLine(ex.Message);
                }

                try
                {
                    var klines   = OkApi.GetKLineDataAsync($"{account.currency}-eth");
                    var nowPrice = klines[0].close;
                    if (account.balance * nowPrice * ethPrice > 50)
                    {
                        Console.WriteLine($"eth {account.currency} --> {account.balance} --> usd{account.balance * nowPrice * ethPrice}  ");
                    }
                    continue;
                }
                catch (Exception ex)
                {
                    //Console.WriteLine(ex.Message);
                }

                try
                {
                    var klines   = OkApi.GetKLineDataAsync($"{account.currency}-okb");
                    var nowPrice = klines[0].close;
                    if (account.balance * nowPrice * okbPrice > 50)
                    {
                        Console.WriteLine($"eth {account.currency} --> {account.balance} --> usd{account.balance * nowPrice * okbPrice}  ");
                    }
                    continue;
                }
                catch (Exception ex)
                {
                    //Console.WriteLine(ex.Message);
                }

                Thread.Sleep(300);
            }

            Console.WriteLine(" end ShowDetail");
        }
Example #2
0
        static void Main(string[] args)
        {
            Console.WriteLine(1);
            // 注册日志
            XmlConfigurator.Configure(new FileInfo("log4net.config"));
            Console.WriteLine(2);

            // 初始化币种
            InstrumentsUtils.InitAllCoins();
            Console.WriteLine(3);
            instruments = InstrumentsUtils.GetAll();

            //DayAvgUtils.CalcAvgPrice();

            // AccountDetailsUtils.ShowDetail();

            //logger.Error(JsonConvert.SerializeObject(InstrumentsUtils.GetOkInstruments()));

            // 判断设置是否合理
            //foreach (var item in instruments)
            //{
            //    // 获取行情,
            //    var klineDataList = OkApi.GetKLineDataAsync(item.symbol + "-" + item.quote, "604800");
            //    if (klineDataList.Count == 0)
            //    {
            //        logger.Error($"获取行情数据有误 {item.quote},{item.symbol}");
            //        continue;
            //    }
            //    var close = klineDataList[0].close;
            //    var max = klineDataList.Max(it => it.open);
            //    var min = klineDataList.Min(it => it.low);
            //    var avg = klineDataList.Sum(it => it.open) / klineDataList.Count;
            //    if (close > item.MaxBuyPrice)
            //    {
            //        logger.Error($"现在价格大于MaxBuyPrice --> close:{close}, {item.quote}-{item.symbol} -- MaxBuyPrice: {item.MaxBuyPrice}");
            //    }
            //    if (item.MaxBuyPrice > avg)
            //    {
            //        Console.WriteLine($"大于加权平均 --> avg:{avg}, {item.quote}-{item.symbol} -- MaxBuyPrice: {item.MaxBuyPrice}");
            //    }
            //    if (item.MaxBuyPrice > max)
            //    {
            //        Console.WriteLine($"超过历史最大值 --> max:{max},min:{min}, {item.quote}-{item.symbol} -- MaxBuyPrice: {item.MaxBuyPrice}");
            //    }
            //    else if (item.MaxBuyPrice > min + (max - min) * (decimal)0.7)
            //    {
            //        Console.WriteLine($"超过0.7 --> max:{max}, {item.quote}-{item.symbol}");
            //    }
            //    Thread.Sleep(200);
            //}
            var orderDetailIndex = 0L;

            while (true)
            {
                orderDetailIndex++;

                var now = DateTime.Now;

                var             btcPrice     = (decimal)0;
                var             ethPrice     = (decimal)0;
                var             okbPrice     = (decimal)0;
                List <CoinInfo> allCoinInfos = new List <CoinInfo>();
                try
                {
                    allCoinInfos = new CoinInfoDao().ListCoinInfo();
                    var btcKlines = OkApi.GetKLineDataAsync("btc-usdt");
                    btcPrice = btcKlines[0].close;
                    var ethKlines = OkApi.GetKLineDataAsync("eth-usdt");
                    ethPrice = ethKlines[0].close;
                    var okbKlines = OkApi.GetKLineDataAsync("okb-usdt");
                    okbPrice = okbKlines[0].close;
                }
                catch (Exception ex)
                {
                    logger.Error(ex.Message, ex);
                }
                // 获取所有ticks
                var tickers = OkApi.ListTickers();

                foreach (var item in instruments)
                {
                    // 查询订单结果
                    QueryOrderDetail(item.quote, item.symbol, (orderDetailIndex) % 10 != 0);

                    // 找到当前的ticker
                    var ticker       = tickers.Find(it => it.instrument_id.ToLower() == $"{item.symbol}-{item.quote}".ToLower());
                    var needContinue = true;
                    var oldData      = new BuyInfoDao().List5LowerBuyForBuy(item.quote, item.symbol);
                    if (oldData.Count == 0 || oldData[0].BuyPrice > ticker.last * (decimal)1.05)
                    {
                        needContinue = false;
                    }
                    if (needContinue)
                    {
                        var needSellForMoreList = new BuyInfoDao().ListNeedSellOrder(item.quote, item.symbol);
                        if (needSellForMoreList.Count > 0 && needSellForMoreList[0].BuyPrice * (decimal)1.08 < ticker.last)
                        {
                            needContinue = false;
                        }
                    }
                    if (needContinue)
                    {
                        // 读取数据库 看看以前的交易
                        var higherSell = new SellInfoDao().List5HigherSellForEmpty(item.quote, item.symbol);
                        if (higherSell.Count > 0 && higherSell[0].SellPrice * (decimal)1.075 < ticker.last)
                        {
                            needContinue = false;
                        }
                    }
                    if (needContinue)
                    {
                        var needBuyForEmptyList = new SellInfoDao().ListNeedBuyOrder(item.quote, item.symbol);
                        if (needBuyForEmptyList.Count > 0 && needBuyForEmptyList[0].SellPrice > ticker.last * (decimal)1.075)
                        {
                            needContinue = false;
                        }
                    }

                    if (needContinue && item.symbol.ToLower() != "egt")
                    {
                        continue;
                    }

                    // 查询订单结果
                    QueryOrderDetail(item.quote, item.symbol, false);

                    // 核实订单
                    CheckOrderUtils.Check(item);

                    // 获取行情,
                    var klineDataList = OkApi.GetKLineDataAsync(item.symbol + "-" + item.quote);
                    if (klineDataList == null || klineDataList.Count < 50)
                    {
                        logger.Error($"获取行情数据有误 {item.symbol}, quote:{item.quote}");
                        continue;
                    }

                    // 记录下价格
                    var findCoinfInfo = allCoinInfos.Find(it => it.Symbol == item.symbol);
                    if (findCoinfInfo != null)
                    {
                        var nowPrice = (decimal)0;;
                        if (item.quote.ToLower() == "btc")
                        {
                            nowPrice = klineDataList[0].close * btcPrice;
                        }
                        else if (item.quote.ToLower() == "eth")
                        {
                            nowPrice = klineDataList[0].close * ethPrice;
                        }
                        else if (item.quote.ToLower() == "okb")
                        {
                            nowPrice = klineDataList[0].close * okbPrice;
                        }
                        if (nowPrice > 0)
                        {
                            new CoinInfoDao().UpdateCoinInfo(item.symbol, nowPrice);
                        }
                    }

                    if (runCount < 3)
                    {
                        var totalAmount = klineDataList.Sum(it => it.volume * (it.open + it.close) / 2);
                        if (item.quote == "btc" && totalAmount < (decimal)0.01)
                        {
                            Console.WriteLine($"交易量太少, 不好办啊 {item.quote},{item.symbol}, totalAmount:{totalAmount}");
                        }

                        if (item.quote == "eth" && totalAmount < 1)
                        {
                            Console.WriteLine($"交易量太少, 不好办啊 {item.quote},{item.symbol}, totalAmount:{totalAmount}");
                        }
                    }

                    if (item.MaxBuyPrice <= 0)
                    {
                        Console.WriteLine($"MaxBuyPrice -->没有设置 {item.quote}-{item.symbol} --> {klineDataList.Min(it => it.low)}");
                    }
                    if (item.MaxBuyPrice > klineDataList[0].close * 4)
                    {
                        // 这里只是做粗略记录和控制
                        Console.WriteLine($"MaxBuyPrice xxxx -->设置的过大会接盘哦 {item.quote}-{item.symbol} --> {klineDataList.Min(it => it.low)}");
                    }

                    try
                    {
                        // 启动交易
                        RunTrade(klineDataList, item);
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex.Message, ex);
                    }

                    // 每走一遍, 休眠一下
                    Thread.Sleep(500);
                }

                Console.WriteLine($"-------------> 运行次数:{runCount++}, 花费时间{(DateTime.Now - now).TotalSeconds} ");
                Thread.Sleep(1000 * 5);
            }
        }