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); } }
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); } }
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); } }
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); } }