//下单
        protected override async Task <ExchangeOrderResult> OnPlaceOrderAsync(ExchangeOrderRequest order)
        {
            /*
             * {
             *  "status": 0,
             *  "data": "9d17a03b852e48c0b3920c7412867623"
             * }*/
            Dictionary <string, object> payload = new Dictionary <string, object>();

            RequestMethod = "POST";
            payload.Add("amount", order.Amount.ToString());
            payload.Add("method", "POST");
            payload.Add("price", SelfMath.ToFixed(order.Price, 4).ToString());
            payload.Add("side", order.IsBuy ? "buy" : "sell");
            payload.Add("symbol", NormalizeSymbol(order.Symbol));
            payload.Add("type", order.OrderType.ToString().ToLower());
            JToken obj = await MakeJsonRequestAsync <JToken>($"orders", BaseUrl, payload, "POST");

            ExchangeOrderResult result = new ExchangeOrderResult()
            {
                Amount  = order.Amount,
                Price   = order.Price,
                IsBuy   = order.IsBuy,
                OrderId = obj.ToStringInvariant(),
                Symbol  = order.Symbol
            };

            result.AveragePrice = result.Price;
            result.Result       = ExchangeAPIOrderResult.Pending;
            return(result);
        }
Esempio n. 2
0
        private Task DoBuy(bool isBuy, Input input)
        {
            try
            {
                var marketid       = $"{input.TargetCurrency}{input.BaseCurrency}";
                var baseCurrency   = input.BaseCurrency;
                var targetCurrency = input.TargetCurrency;
                //var initalPrice = input.InitalPrice;
                var depth   = input.ExchangeApi.GetOrderBook(marketid, 10);
                var txPrice =
                    input.InitalPrice == 0 ?
                    (isBuy ? depth.Asks.OrderBy(a => a.Value.Price).FirstOrDefault() : depth.Bids.OrderByDescending(a => a.Value.Price).FirstOrDefault()).Value.Price
                    : input.InitalPrice;

                //获取账户信息,确定目前账户存在多少钱和多少币
                var account = input.ExchangeApi.GetAmountsAvailableToTrade();
                var order   = new ExchangeOrderRequest();
                if (isBuy)
                {
                    //可买的比特币量
                    var amountTx = account.TryGetValueOrDefault(baseCurrency, 0) / txPrice;
                    if (amountTx < 0.001m)
                    {
                        return(Task.CompletedTask);
                    }
                    order.Amount = SelfMath.ToFixed(amountTx * decimal.Parse(input.Hold) / 100, int.Parse(input.Precision));
                    order.IsBuy  = isBuy;
                    order.Price  = txPrice;
                    order.Symbol = marketid;
                    input.ExchangeApi.PlaceOrder(order);
                    input.InitalPrice = order.Price;
                    input.Amount      = order.Amount.ToString();
                    input.Status      = InputStatus.Doing;
                }
                else
                {
                    var rangePriceMin = input.InitalPrice * (1 + decimal.Parse(input.GainPointMin) / 100);
                    //var rangePriceMax = holdPrice * (1 + decimal.Parse(TxtRangeMax.Text.Trim()));
                    var lossPrice = input.InitalPrice * (1 - decimal.Parse(input.LossPoint) / 100);
                    input.SellPrice = txPrice;
                    if ((txPrice >= rangePriceMin) || (txPrice <= lossPrice))
                    {
                        order.Amount = SelfMath.ToFixed(decimal.Parse(input.Amount) * (1 - 2.5m / 1000), int.Parse(input.Precision));
                        order.IsBuy  = isBuy;
                        order.Price  = txPrice;
                        order.Symbol = marketid;
                        input.ExchangeApi.PlaceOrder(order);
                        input.Status = InputStatus.Done;
                    }
                }
            }
            catch (Exception e)
            {
                using (FileStream fs = new FileStream("log.txt", FileMode.Append))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.WriteLine(e.Message.ToString() + "/r/n");
                    }
                }
            }
            return(Task.CompletedTask);
        }