public async Task <IEnumerable <Core.Model.AccountTrade> > GetAccountTradesAsync(Core.Model.User user, string symbol, DateTime startDate, DateTime endDate, long recWindow = 0, CancellationToken cancellationToken = default)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var binanceApi = new BinanceApi();

            using var apiUser = new BinanceApiUser(user.ApiKey, user.ApiSecret);
            var result = await binanceApi.GetAccountTradesAsync(apiUser, symbol, startDate, endDate, recWindow, cancellationToken).ConfigureAwait(false);

            var accountTrades = result.Select(at => NewAccountTrade(at)).ToList();

            return(accountTrades);
        }
Exemple #2
0
        public virtual async void Initialize(bool subscribeSymbolsAutomatically = true)
        {
            var apiKey    = Trader.Utility.SettingsManager.Manager.SettingsManager.Instance.Get(SettingName.API_KEY, string.Empty);
            var secretKey = Trader.Utility.SettingsManager.Manager.SettingsManager.Instance.Get(SettingName.SECRET_KEY, string.Empty);

            if (!string.IsNullOrEmpty(apiKey))
            {
                apiKey = EncryptionHelper.DecryptText(apiKey);
            }
            if (!string.IsNullOrEmpty(secretKey))
            {
                secretKey = EncryptionHelper.DecryptText(secretKey);
            }

            Symbol   = Trader.Utility.SettingsManager.Manager.SettingsManager.Instance.Get(SettingName.TRADING_SYMBOL, Defaults.TRADING_SYMBOL);
            Decimals = Trader.Utility.SettingsManager.Manager.SettingsManager.Instance.Get(SettingName.DECIMALS, Defaults.DECIMALS);

            var unitTypeStr = Trader.Utility.SettingsManager.Manager.SettingsManager.Instance.Get(SettingName.UNIT_TYPE, UnitType.Value.ToString());

            UnitType = (UnitType)System.Enum.Parse(typeof(UnitType), unitTypeStr, true);

            var orderTypeStr = Trader.Utility.SettingsManager.Manager.SettingsManager.Instance.Get(SettingName.ORDER_TYPE, Enum.OrderType.Limit.ToString());

            OrderType = (Enum.OrderType)System.Enum.Parse(typeof(Enum.OrderType), orderTypeStr, true);

            Api = new BinanceApi();

            if (subscribeSymbolsAutomatically)
            {
                SubscribeSymbols();
            }

            if (!string.IsNullOrEmpty(apiKey) && !string.IsNullOrEmpty(secretKey))
            {
                User = new BinanceApiUser(apiKey, secretKey);
            }

            // get last orders
            LogManager.Instance.ClearTradeLogs();

            if (User != null)
            {
                try
                {
                    var ordersSymbol = await Api.GetOrdersAsync(User, Symbol, -1, 30);

                    foreach (var order in ordersSymbol)
                    {
                        if (order.Status == OrderStatus.Filled)
                        {
                            if (order.Type == Binance.OrderType.Limit)
                            {
                                LogManager.Instance.AddLog(new TradeLog()
                                {
                                    Amount  = order.ExecutedQuantity,
                                    Date    = order.Time.ToLocalTime(),
                                    OrderId = order.Id,
                                    Price   = order.Price != 0 ? order.Price.RoundTo(6) : await TradingServiceHelper.GetPriceByDate(order.Symbol, order.Time),
                                    Side    = order.Side == OrderSide.Buy ? TradeSide.Buy : TradeSide.Sell,
                                    Symbol  = order.Symbol
                                }, false);
                            }
                            else
                            {
                                var price  = 0m;
                                var trades = await Api.GetAccountTradesAsync(User, Symbol, 200);

                                foreach (var trade in trades)
                                {
                                    if (trade.OrderId == order.Id)
                                    {
                                        price += trade.Price * trade.Quantity;
                                    }
                                }
                                price = price / order.ExecutedQuantity;
                                price = price.RoundTo(6);

                                LogManager.Instance.AddLog(new TradeLog()
                                {
                                    Amount  = order.ExecutedQuantity,
                                    Date    = order.Time.ToLocalTime(),
                                    OrderId = order.Id,
                                    Price   = price,
                                    Side    = order.Side == OrderSide.Buy ? TradeSide.Buy : TradeSide.Sell,
                                    Symbol  = order.Symbol
                                }, false);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message);
                }
            }
        }