コード例 #1
0
ファイル: Program.cs プロジェクト: lizhengquan1985/cat
        static void DoSellForEmpty(TradeItem tradeItem, decimal nowPrice)
        {
            string quote  = tradeItem.quote;
            string symbol = tradeItem.symbol;

            if (lastSellForEmptyDate > DateTime.Now.AddSeconds(-20))
            {
                // 如果20秒内做空过一单, 则不能再次做空
                return;
            }

            var sellSize = tradeItem.EmptySize;

            if (sellSize <= 0)
            {
                return;
            }

            var count = new SellInfoDao().GetNotBuyCount(quote, symbol);

            count = Math.Min(count, 60);

            sellSize = sellSize * (1 + count / (decimal)30);
            var sellPrice = nowPrice / (decimal)1.01;

            var okInstrument = InstrumentsUtils.GetOkInstruments(quote, symbol);

            sellPrice = decimal.Round(sellPrice, okInstrument.GetTickSizeNumber());
            sellSize  = decimal.Round(sellSize, okInstrument.GetSizeIncrementNumber());

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

            try
            {
                logger.Error($"");
                logger.Error($"1: 准备出售(空) {quote}-{symbol}, client_oid:{client_oid},  nowPrice:{nowPrice}, sellPrice:{sellPrice.ToString()}, sellSize:{sellSize.ToString()}");
                var tradeResult = OkApi.Sell(client_oid, symbol + "-" + quote, sellPrice.ToString(), sellSize.ToString());
                logger.Error($"2: 下单完成 {JsonConvert.SerializeObject(tradeResult)}");

                new SellInfoDao().CreateSellInfo(new SellInfo
                {
                    SellClientOid      = client_oid,
                    SellPrice          = sellPrice,
                    SellQuantity       = sellSize,
                    SellCreateAt       = DateTime.Now.ToString("yyyy-MM-dd"),
                    SellFilledNotional = (decimal)0,
                    SellStatus         = "prepare",
                    SellOrderId        = tradeResult.order_id,
                    SellResult         = tradeResult.result,

                    Quote    = quote,
                    Symbol   = symbol,
                    UserName = "******"
                });
                logger.Error($"3: 添加记录完成");
                logger.Error($"");

                lastSellForEmptyDate = DateTime.Now;
            }
            catch (Exception e)
            {
                logger.Error("做空出售异常 严重 --> " + e.Message, e);
                Thread.Sleep(1000 * 60 * 10);
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: lizhengquan1985/cat
        static void DoSellForMore(BuyInfo buyInfo, decimal nowPrice)
        {
            if (lastSellDate > DateTime.Now.AddMinutes(-1))
            {
                // 如果1分钟内购出售一单, 则不能再次出售
                return;
            }

            if (!string.IsNullOrEmpty(buyInfo.SellClientOid) && buyInfo.SellStatus != OrderStatus.cancelled)
            {
                return;
            }

            var percent   = 1 + ((nowPrice / buyInfo.BuyPrice) - 1) / 3;
            var sellSize  = buyInfo.BuyQuantity / percent;
            var sellPrice = nowPrice / (decimal)1.01; // 更低的价格出售, 是为了能够出售

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

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

            sellPrice = decimal.Round(sellPrice, okInstrument.GetTickSizeNumber());
            sellSize  = decimal.Round(sellSize, okInstrument.GetSizeIncrementNumber());

            if (buyInfo.Quote.ToLower() == "btc" && buyInfo.Symbol.ToLower() == "bch")
            {
                if (sellSize < (decimal)0.01)
                {
                    sellSize = (decimal)0.01;
                }
            }

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

            try
            {
                logger.Error($"");
                logger.Error($"{JsonConvert.SerializeObject(buyInfo)}");
                logger.Error($"");
                logger.Error($"1: 准备出售 {buyInfo.Quote}-{buyInfo.Symbol}, client_oid:{client_oid},  nowPrice:{nowPrice.ToString()}, sellPrice:{sellPrice.ToString()}, sellSize:{sellSize}");
                var sellResult = OkApi.Sell(client_oid, buyInfo.Symbol + "-" + buyInfo.Quote, sellPrice.ToString(), sellSize.ToString());
                logger.Error($"2: 下单完成 {JsonConvert.SerializeObject(sellResult)}");

                buyInfo.SellClientOid = client_oid;
                buyInfo.SellPrice     = sellPrice;
                buyInfo.SellQuantity  = sellSize;
                buyInfo.SellResult    = sellResult.result;
                buyInfo.SellOrderId   = sellResult.order_id;
                new BuyInfoDao().UpdateBuyInfoWhenSell(buyInfo);

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

                lastSellDate = DateTime.Now;
            }
            catch (Exception e)
            {
                logger.Error("出售异常 严重 --> " + e.Message, e);

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