Exemplo n.º 1
0
        static void DoBuyForEmpty(SellInfo sellInfo, decimal nowPrice)
        {
            if (lastBuyForEmptyDate > DateTime.Now.AddSeconds(-20))
            {
                // 如果20秒内购购买一单, 则不能再次购买
                return;
            }

            if (!string.IsNullOrEmpty(sellInfo.BuyClientOid))
            {
                return;
            }

            var percent  = 1 + ((sellInfo.SellPrice / nowPrice) - 1) / 3;
            var buySize  = sellInfo.SellQuantity * percent;
            var buyPrice = nowPrice * (decimal)1.01; // 更高的价格购入, 是为了能够购入

            var okInstrument = InstrumentsUtils.GetOkInstruments(sellInfo.Quote, sellInfo.Symbol);

            if (okInstrument == null)
            {
                logger.Error($"出售时候发现 不存在的交易对 {sellInfo.Quote},{sellInfo.Symbol}");
                return;
            }

            buyPrice = decimal.Round(buyPrice, okInstrument.GetTickSizeNumber());
            buySize  = decimal.Round(buySize, okInstrument.GetSizeIncrementNumber());

            var client_oid = "buy" + DateTime.Now.Ticks;

            try
            {
                logger.Error($"");
                logger.Error($"{JsonConvert.SerializeObject(sellInfo)}");
                logger.Error($"");
                logger.Error($"1: 准备购买(空) {sellInfo.Quote}-{sellInfo.Symbol}, client_oid:{client_oid},  nowPrice:{nowPrice.ToString()}, buyPrice:{buyPrice.ToString()}, buySize:{buySize}");
                var sellResult = OkApi.Buy(client_oid, sellInfo.Symbol + "-" + sellInfo.Quote, buyPrice.ToString(), buySize.ToString());
                logger.Error($"2: 下单完成 {JsonConvert.SerializeObject(sellResult)}");

                sellInfo.BuyClientOid = client_oid;
                sellInfo.BuyPrice     = buyPrice;
                sellInfo.BuyQuantity  = buySize;
                sellInfo.BuyResult    = sellResult.result;
                sellInfo.BuyOrderId   = sellResult.order_id;
                new SellInfoDao().UpdateSellInfoWhenBuy(sellInfo);

                logger.Error($"3: 添加记录完成");
                logger.Error($"");

                lastBuyForEmptyDate = DateTime.Now;
            }
            catch (Exception e)
            {
                logger.Error("购买异常(空) 严重 --> " + e.Message, e);

                Thread.Sleep(1000 * 60 * 10);
            }
        }
Exemplo n.º 2
0
        static void DoBuyForMore(string quote, string symbol, decimal nowPrice)
        {
            if (lastBuyDate > DateTime.Now.AddMinutes(-1))
            {
                // 如果1分钟内购买过一单, 则不能再次购买
                return;
            }

            var buyAmount = (decimal)0.00;

            if (quote.ToLower() == "eth")
            {
                // 获取没有出售的数量
                var count = new BuyInfoDao().GetNotSellCount(quote, symbol);
                if (count > 60)
                {
                    count = 60;
                }

                buyAmount = (decimal)0.01;
                buyAmount = buyAmount * ((decimal)1 + count / (decimal)40);
                Console.WriteLine($"已购买数量:{symbol} -> {count}, {buyAmount}");
            }
            else if (quote.ToLower() == "btc")
            {
                var count = new BuyInfoDao().GetNotSellCount(quote, symbol);
                if (count > 60)
                {
                    count = 60;
                }
                buyAmount = (decimal)0.00028;
                buyAmount = buyAmount * ((decimal)1 + count / (decimal)40);
                Console.WriteLine($"已购买数量:{symbol} -> {count}, {buyAmount}");
            }
            else if (quote.ToLower() == "okb")
            {
                var count = new BuyInfoDao().GetNotSellCount(quote, symbol);
                if (count > 60)
                {
                    count = 60;
                }
                buyAmount = (decimal)1.8;
                buyAmount = buyAmount * ((decimal)1 + count / (decimal)20);
                Console.WriteLine($"已购买数量:{symbol} -> {count}, {buyAmount}");
            }
            else if (quote.ToLower() == "usdt")
            {
                buyAmount = (decimal)2;
            }
            else
            {
                logger.Error("000000000000000");
                return;
            }

            var buySize      = buyAmount / nowPrice;
            var buyPrice     = nowPrice * (decimal)1.01;
            var okInstrument = InstrumentsUtils.GetOkInstruments(quote, symbol);

            if (okInstrument == null)
            {
                logger.Error($"不存在的交易对 {quote},{symbol}");
                return;
            }

            buyPrice = decimal.Round(buyPrice, okInstrument.GetTickSizeNumber());
            buySize  = decimal.Round(buySize, okInstrument.GetSizeIncrementNumber());

            if (buySize < okInstrument.min_size)
            {
                logger.Error($"购买最小额度有问题 ----> buySize: {buySize}, min_size:{okInstrument.min_size}");
                if (buySize * (decimal)1.4 > okInstrument.min_size)
                {
                    buySize = buySize * (decimal)1.4;
                }
            }

            if (quote.ToLower() == "btc" && symbol.ToLower() == "bch")
            {
                buySize = (decimal)0.0108;
            }

            var client_oid = "buy" + DateTime.Now.Ticks;

            try
            {
                logger.Error($"");
                logger.Error($"1: 准备购买 {quote}-{symbol}, client_oid:{client_oid},  nowPrice:{nowPrice}, buyPrice:{buyPrice.ToString()}, buySize:{buySize.ToString()}");
                var tradeResult = OkApi.Buy(client_oid, symbol + "-" + quote, buyPrice.ToString(), buySize.ToString());
                logger.Error($"2: 下单完成 {JsonConvert.SerializeObject(tradeResult)}");

                new BuyInfoDao().CreateBuyInfo(new BuyInfo
                {
                    BuyClientOid      = client_oid,
                    BuyPrice          = buyPrice,
                    BuyQuantity       = buySize,
                    BuyCreateAt       = DateTime.Now.ToString("yyyy-MM-dd"),
                    BuyFilledNotional = (decimal)0,
                    BuyStatus         = "prepare",

                    Quote    = quote,
                    Symbol   = symbol,
                    UserName = "******",

                    BuyOrderId = tradeResult.order_id,
                    BuyResult  = tradeResult.result
                });
                logger.Error($"3: 添加记录完成");
                logger.Error($"");
                lastBuyDate = DateTime.Now;
            }
            catch (Exception e)
            {
                logger.Error("购买异常 严重 --> " + e.Message, e);

                Thread.Sleep(1000 * 60 * 60);
            }
        }