Beispiel #1
0
        /// <summary>
        /// Loads currency exchange orders from disk. This should only be called once or to reload orders.
        /// </summary>
        /// <param name="aCurrencyTicker"></param>
        /// <param name="aCurrencyID"></param>
        /// <returns></returns>
        public IEnumerable <UserTradeOrder> LoadOrders(ICurrencyIdentity aCurrency, IPandoraExchangeFactory lExchangeFactory, GetWalletIDDelegate aGetWalletIDFunction)
        {
            var lResult   = new List <UserTradeOrder>();
            var lDBOrders = FSaveManager.ReadOrders(aCurrency);

            if (lDBOrders != null && lDBOrders.Any())
            {
                int lErrorCounter;
                var lMarketCache = new Dictionary <int, IEnumerable <IExchangeMarket> >();
                foreach (var lDBOrder in lDBOrders)
                {
                    bool lOrdersLogs = FSaveManager.ReadOrderLogs(lDBOrder.InternalID, out List <OrderMessage> lMessages);
                    lMessages     = lMessages.OrderBy(lMessage => lMessage.Time).ToList();
                    lErrorCounter = 0;
                    for (int it2 = 0; it2 < lMessages.Count; it2++)
                    {
                        OrderMessage lIndividualMessage = lMessages[it2];

                        switch (lIndividualMessage.Level)
                        {
                        case OrderMessage.OrderMessageLevel.Error:
                            lErrorCounter += 60;
                            break;

                        case OrderMessage.OrderMessageLevel.StageChange:
                            lErrorCounter = 0;
                            break;

                        case OrderMessage.OrderMessageLevel.FatalError:
                            lErrorCounter = -1;
                            break;
                        }
                        if (lErrorCounter == -1)
                        {
                            break;
                        }
                    }

                    var lExchanger = lExchangeFactory.GetPandoraExchange((AvailableExchangesList)lDBOrder.ExchangeID);
                    if (!lMarketCache.TryGetValue(lDBOrder.ExchangeID, out IEnumerable <IExchangeMarket> lMarkets))
                    {
                        lMarkets = lExchanger.GetMarketCoins(aCurrency, aGetWalletIDFunction);
                        lMarketCache.Add(lDBOrder.ExchangeID, lMarkets);
                    }
                    var lUserOrder = new UserTradeOrder
                    {
                        InternalID   = lDBOrder.InternalID,
                        ID           = lDBOrder.ID,
                        Name         = lDBOrder.Name,
                        SentQuantity = lDBOrder.SentQuantity,
                        Status       = lDBOrder.Status,
                        CoinTxID     = lDBOrder.CoinTxID,
                        StopPrice    = lDBOrder.StopPrice,
                        OpenTime     = lDBOrder.OpenTime,
                        Rate         = lDBOrder.Rate,
                        ProfileID    = lDBOrder.ProfileID,
                        ErrorCounter = lErrorCounter,
                        Completed    = lDBOrder.Completed,
                        Cancelled    = lDBOrder.Cancelled,
                        Market       = lMarkets.FindByMarketID(lDBOrder.MarketID)
                    };
                    if (lUserOrder.Market == null)
                    {
                        Log.Write(LogLevel.Warning, $"Exchange: Unable to find market for order of id {lUserOrder.InternalID} of exchange {lExchanger.Name}");
                        continue;
                    }
                    lResult.Add(lUserOrder);
                    SetOrderInCache(lUserOrder, lDBOrder.ExchangeID);
                }
            }
            return(lResult);
        }