public override void HandleCore(PrivateMessageReceivedContext context)
        {
            MahuaApi.SendPrivateMessage(context.FromQq)
            .Text("Begin Get User Orders")
            .Done();

            var userAssets = _assetService.GetUserAssets();

            if (userAssets.Any())
            {
                var orderApi = _okexApiFactory.Create <IOrderApi>();
                var orders   = new List <Order>();
                var assets   = userAssets.OrderByDescending(x => x.Value.AsUsdt).ToArray();
                foreach (var kv in assets)
                {
                    if (kv.Key == "usdt")
                    {
                        continue;
                    }
                    var market         = $"{kv.Key}_usdt";
                    var getOrdersInput = new GetOrdersInput
                    {
                        Order_Id = "-1",
                        Symbol   = market,
                    }.WithSign();
                    var output = orderApi.GetOrders(getOrdersInput).GetAwaiter().GetResult();
                    if (output.Result)
                    {
                        orders.AddRange(output.Orders);
                    }
                    else
                    {
                        MahuaApi.SendPrivateMessage(context.FromQq)
                        .Text($"{market} No Orders")
                        .Done();
                    }
                }

                foreach (var order in orders.Where(x => x.Status == Order.OrderStatus.Doing || x.Status == Order.OrderStatus.Some))
                {
                    var dealPercetion = order.DealAmount / order.Amount * 100M;
                    var ticker        = _tickerProvider.GetTikcer(order.Symbol);
                    MahuaApi.SendPrivateMessage(context.FromQq)
                    .Text($"{order.Symbol} {order.Type:G}")
                    .Newline()
                    .Text($"Price:{order.Price:E6}")
                    .Newline()
                    .Text($"Amount:({dealPercetion:###.00}){order.DealAmount:e2}/{order.Amount:e2} ")
                    .Newline()
                    .Text($"USD:{order.DealAmount * ticker.Last:0000000.00}/{order.Amount * ticker.Last:0000000.00}")
                    .Done();
                }
            }
            else
            {
                MahuaApi.SendPrivateMessage(context.FromQq)
                .Text("No Orders")
                .Done();
            }
        }
        public Ticker GetTikcer(string symbol)
        {
            if (symbol == "usdt_usdt")
            {
                return(UsdtTicker);
            }
            var localCache = Cache.GetOrAdd(symbol, s => new LocalCache <Ticker>());
            var ticker     = localCache.Get(60 * 10, () =>
            {
                var tickerOutput = _okexApiFactory.Create <ITickerApi>().GetTicker(symbol).GetAwaiter().GetResult();
                return(tickerOutput.Ticker);
            });

            return(ticker);
        }
Ejemplo n.º 3
0
        public Dictionary <string, UserAsset> GetUserAssets()
        {
            var userInfoApi = _okexApiFactory.Create <IUserInfoApi>();
            var input       = new OkexInput().WithSign();
            var output      = userInfoApi.GetUserInfo(input).ConfigureAwait(false).GetAwaiter().GetResult();

            if (output.Result)
            {
                var userAssets = new Dictionary <string, UserAsset>();
                var funds      = output.Info.Funds;
                foreach (var kv in funds.Free.Where(x => x.Value != 0))
                {
                    userAssets[kv.Key] = new UserAsset
                    {
                        Currency = kv.Key,
                        Balance  = kv.Value,
                    };
                }

                foreach (var kv in funds.Freezed.Where(x => x.Value != 0))
                {
                    if (!userAssets.ContainsKey(kv.Key))
                    {
                        userAssets[kv.Key] = new UserAsset();
                    }
                    userAssets[kv.Key].Locked = kv.Value;
                }

                foreach (var kv in userAssets)
                {
                    var ticker    = _tickerProvider.GetTikcer($"{kv.Key}_usdt");
                    var userAsset = kv.Value;
                    userAssets[kv.Key].AsUsdt = ticker.Last * (userAsset.Balance + userAsset.Locked);
                }

                return(userAssets);
            }

            return(new Dictionary <string, UserAsset>());
        }
Ejemplo n.º 4
0
        public void ReportFinishedOrders()
        {
            var userAssets    = _userAssetService.GetUserAssets();
            var systemOptions = SystemOptions.Instance;
            var list          = new List <Order>();

            using (var session = MahuaRobotManager.Instance.CreateSession())
            {
                var api = session.MahuaApi;
                foreach (var kv in userAssets)
                {
                    if (kv.Key == "usdt")
                    {
                        continue;
                    }
                    var market = $"{kv.Key}_usdt";
                    var input  = new GetOrderHistoryInput
                    {
                        CurrentPage = 1,
                        PageLength  = 100,
                        Status      = (int)GetOrderHistoryInput.GetOrderHistoryInputStatus.Finished,
                        Symbol      = market
                    }.WithSign();
                    var orderHistory = _okexApiFactory.Create <IOrderApi>().GetOrderHistory(input).GetAwaiter().GetResult();
                    if (orderHistory.Result)
                    {
                        list.AddRange(orderHistory.Orders);
                    }
                    else
                    {
                        api.SendPrivateMessage(systemOptions.MasterQq, $"get {market} orders failed");
                    }
                }

                lock (Locker)
                {
                    if (!FinishedOrders.Any())
                    {
                        FinishedOrders.AddRange(list.Select(x => x.OrderId));
                    }
                    else
                    {
                        var newOrders = list.Where(x => !FinishedOrders.Contains(x.OrderId)).ToArray();
                        foreach (var order in newOrders)
                        {
                            var ticker = _tickerProvider.GetTikcer(order.Symbol);
                            switch (order.Status)
                            {
                            case Order.OrderStatus.Canceled:
                                var step = api.SendPrivateMessage(systemOptions.MasterQq)
                                           .Text($"{order.Symbol} {order.Type:G} CANCELED")
                                           .Newline()
                                           .Text($"Price:{order.Price:E6}")
                                           .Newline();
                                if (order.DealAmount != 0)
                                {
                                    step
                                    .Text($"Amount:{order.DealAmount:e2}/{order.Amount:e2} ")
                                    .Newline()
                                    .Text($"USD:{order.Amount * ticker.Last:0000000.00}")
                                    .Done();
                                }
                                else
                                {
                                    step.Done();
                                }
                                break;

                            case Order.OrderStatus.Finished:
                                api.SendPrivateMessage(systemOptions.MasterQq)
                                .Text($"{order.Symbol} {order.Type:G} FINISHED")
                                .Newline()
                                .Text($"Price:{order.Price:E6}")
                                .Newline()
                                .Text($"Amount:{order.DealAmount:e2}/{order.Amount:e2} ")
                                .Newline()
                                .Text($"USD:{order.Amount * ticker.Last:0000000.00}")
                                .Done();
                                break;

                            case Order.OrderStatus.Doing:
                            case Order.OrderStatus.Some:
                            case Order.OrderStatus.Canceling:
                                api.SendPrivateMessage(systemOptions.MasterQq)
                                .Text($"{order.Symbol} {order.Type:G} {order.Status:D}")
                                .Newline()
                                .Text($"Price:{order.Price:E6}")
                                .Newline()
                                .Text($"Amount:{order.DealAmount:e2}/{order.Amount:e2} ")
                                .Newline()
                                .Text($"USD:{order.DealAmount * ticker.Last:0000000.00}")
                                .Done();
                                break;

                            default:
                                throw new ArgumentOutOfRangeException();
                            }
                        }
                        FinishedOrders.AddRange(newOrders.Select(x => x.OrderId).ToArray());
                    }
                }
            }
        }