Example #1
0
        public bool CheckCanBuyForHuiDiao(DogEmptySell dogEmptySell)
        {
            // 是否回掉了,可以购买。 肯定要是最低点回掉

            // 找到最近24小时,并且是出售之后的价格数据
            var klines = HistoryKlines.FindAll(it => it.Id > dogEmptySell.Id);

            if (klines.Count == 0)
            {
                return(false);
            }

            // 判断是否有最小值,且小于nowPrice
            var min = klines.Min(it => it.Close);

            return(NowPrice > min * (decimal)1.005 && NowPrice * (decimal)1.04 < dogEmptySell.SellTradePrice);
        }
Example #2
0
 public void CreateDogEmptySell(DogEmptySell dogEmptySell)
 {
     try
     {
         using (var tx = Database.BeginTransaction())
         {
             Database.Insert(dogEmptySell);
             tx.Commit();
         }
     }
     catch (Exception ex)
     {
         logger.Error($"严重 --------危险----- CreateDogEmptySell----------{JsonConvert.SerializeObject(dogEmptySell)}");
         logger.Error(ex.Message, ex);
         Thread.Sleep(1000 * 60 * 60);
     }
 }
Example #3
0
        private static void SellWhenDoEmpty(string accountId, string userName, CommonSymbol symbol, decimal sellQuantity, decimal sellPrice, string sellMemo = "")
        {
            try
            {
                if ((symbol.AmountPrecision == 0 && sellQuantity < (decimal)20) ||
                    (symbol.AmountPrecision == 1 && sellQuantity < (decimal)2) ||
                    (symbol.AmountPrecision == 2 && sellQuantity < (decimal)0.2) ||
                    (symbol.AmountPrecision == 3 && sellQuantity < (decimal)0.02) ||
                    (symbol.AmountPrecision == 4 && sellQuantity < (decimal)0.002))
                {
                    LogNotBuy(symbol.BaseCurrency + "-empty-sell", $"sell userName:{userName} 出错,{symbol.BaseCurrency} 的精度为 {symbol.AmountPrecision}, 但是却要出售{sellQuantity}  ");
                    return;
                }

                OrderPlaceRequest req = new OrderPlaceRequest();
                req.account_id = accountId;
                req.amount     = sellQuantity.ToString();
                req.price      = sellPrice.ToString();
                req.source     = "api";
                req.symbol     = symbol.BaseCurrency + symbol.QuoteCurrency;;
                req.type       = "sell-limit";

                PlatformApi       api   = PlatformApi.GetInstance(userName);
                HBResponse <long> order = null;
                try
                {
                    logger.Error($"");
                    logger.Error($"1: 开始下单 -----------------------------{JsonConvert.SerializeObject(req)}");
                    order = api.OrderPlace(req);
                    logger.Error($"2: 下单结束 -----------------------------{JsonConvert.SerializeObject(req)}");

                    if (order.Status == "ok")
                    {
                        DogEmptySell dogEmptySell = new DogEmptySell()
                        {
                            AccountId             = accountId,
                            UserName              = userName,
                            SellOrderId           = order.Data,
                            SellOrderResult       = JsonConvert.SerializeObject(order),
                            SellDate              = DateTime.Now,
                            SellQuantity          = sellQuantity,
                            SellOrderPrice        = sellPrice,
                            SellState             = StateConst.Submitted,
                            SellTradePrice        = 0,
                            SymbolName            = symbol.BaseCurrency,
                            QuoteCurrency         = symbol.QuoteCurrency,
                            SellMemo              = sellMemo,
                            SellOrderDetail       = "",
                            SellOrderMatchResults = "",
                            IsFinished            = false
                        };
                        new DogEmptySellDao().CreateDogEmptySell(dogEmptySell);

                        // 下单成功马上去查一次
                        QueryEmptySellDetailAndUpdate(userName, order.Data);
                    }
                    logger.Error($"3: 入库结束 -----------------------------做空 {JsonConvert.SerializeObject(req)}下单出售结果:" + JsonConvert.SerializeObject(order));
                    logger.Error($"");
                }
                catch (Exception ex)
                {
                    logger.Error($"严重 ---------------  做空出错  --------------{JsonConvert.SerializeObject(req)}");
                    Thread.Sleep(1000 * 60 * 5);
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                logger.Error($"SellWhenDoEmpty  {ex.Message}", ex);
            }
        }
Example #4
0
        public static string ShouGeDogEmpty(DogEmptySell dogEmptySell, CommonSymbol symbol, AnalyzeResult analyzeResult, bool isShouge = false)
        {
            var nowPrice = analyzeResult.NowPrice;
            var ladderEmptyWhenShouGe = ladderEmptyBuyPercent;

            if (isShouge)
            {
                ladderEmptyWhenShouGe = (decimal)1.05;
            }
            if (nowPrice * ladderEmptyWhenShouGe > dogEmptySell.SellTradePrice)
            {
                Console.WriteLine("没有收益,不能收割");
                return("没有收益,不能收割");
            }

            if (!analyzeResult.CheckCanBuyForHuiDiao(dogEmptySell))
            {
                return("没有回调");
            }

            decimal buyQuantity = CommonHelper.CalcBuyQuantityForEmptyShouge(dogEmptySell.SellQuantity, dogEmptySell.SellTradePrice, nowPrice, symbol);

            if (buyQuantity <= dogEmptySell.SellQuantity || nowPrice * buyQuantity >= dogEmptySell.SellQuantity * dogEmptySell.SellTradePrice)
            {
                Console.WriteLine($"     {symbol.BaseCurrency}{symbol.QuoteCurrency}没有实现双向收益, 不能收割空单");
                return("没有实现双向收益, 不能收割空单");
            }
            decimal orderPrice = decimal.Round(nowPrice * (decimal)1.01, symbol.PricePrecision);

            OrderPlaceRequest req = new OrderPlaceRequest();

            req.account_id = dogEmptySell.AccountId;
            req.amount     = buyQuantity.ToString();
            req.price      = orderPrice.ToString();
            req.source     = "api";
            req.symbol     = symbol.BaseCurrency + symbol.QuoteCurrency;
            req.type       = "buy-limit";

            if (BuyLimitUtils.Record(dogEmptySell.UserName, symbol.BaseCurrency + symbol.QuoteCurrency))
            {
                logger.Error(" --------------------- 两个小时内购买次数太多,暂停一会 --------------------- ");
                Thread.Sleep(1000 * 5);
                return(" 两个小时内购买次数太多,暂停一会");
            }

            PlatformApi       api   = PlatformApi.GetInstance(dogEmptySell.UserName);
            HBResponse <long> order = null;

            try
            {
                logger.Error($"");
                logger.Error($"1: 开始下单 -----------------------------{JsonConvert.SerializeObject(req)}");
                order = api.OrderPlace(req);
                logger.Error($"2: 下单结束 -----------------------------{JsonConvert.SerializeObject(order)}");

                if (order.Status == "ok")
                {
                    new DogEmptyBuyDao().CreateDogEmptyBuy(new DogEmptyBuy()
                    {
                        SymbolName    = symbol.BaseCurrency,
                        QuoteCurrency = symbol.QuoteCurrency,
                        AccountId     = dogEmptySell.AccountId,
                        UserName      = dogEmptySell.UserName,
                        SellOrderId   = dogEmptySell.SellOrderId,

                        BuyQuantity          = buyQuantity,
                        BuyOrderPrice        = orderPrice,
                        BuyDate              = DateTime.Now,
                        BuyOrderResult       = JsonConvert.SerializeObject(order),
                        BuyState             = StateConst.PreSubmitted,
                        BuyTradePrice        = 0,
                        BuyOrderId           = order.Data,
                        BuyMemo              = "",
                        BuyOrderDetail       = "",
                        BuyOrderMatchResults = "",
                    });
                    // 下单成功马上去查一次
                    QueryEmptyBuyDetailAndUpdate(dogEmptySell.UserName, order.Data);
                }
                logger.Error($"3: 入库结束 ----------------------------- 空单收割");
                logger.Error($"");
                return("运行结束");
            }
            catch (Exception ex)
            {
                logger.Error($"严重严重 --------------- 空单收割出错");
                Thread.Sleep(1000 * 60 * 5);
                throw ex;
            }
        }