예제 #1
0
        public void UpdateBalances()
        {
            if (_ex.Currency != CurrencyName.USD)
            {
                CurrencyRate = _ex.DbGateway.GetCurrencyRate(_ex.Currency);
            }

            var account = _ex.DbGateway.GetLastBalances(_ex.GetExchangeName());

            UsdBalance  = account.UsdBalance;
            BtcBalance  = account.BtcBalance;
            BchBalance  = account.BchBalance.HasValue ? account.BchBalance.Value : 0;
            LtcBalance  = account.LtcBalance.HasValue ? account.LtcBalance.Value : 0;
            EthBalance  = account.EthBalance.HasValue ? account.EthBalance.Value : 0;
            XrpBalance  = account.XrpBalance.HasValue ? account.XrpBalance.Value : 0;
            DashBalance = account.DashBalance.HasValue ? account.DashBalance.Value : 0;

            Utils.ThrowIf(UsdBalance < 0, "[{0}] USD balance {1}", _ex.GetExchangeName(), UsdBalance);
            Utils.ThrowIf(BtcBalance < 0, "[{0}] BTC balance {1}", _ex.GetExchangeName(), BtcBalance);
            Utils.ThrowIf(BchBalance < 0, "[{0}] BCH balance {1}", _ex.GetExchangeName(), BchBalance);
            Utils.ThrowIf(LtcBalance < 0, "[{0}] LTC balance {1}", _ex.GetExchangeName(), LtcBalance);
            Utils.ThrowIf(EthBalance < 0, "[{0}] ETH balance {1}", _ex.GetExchangeName(), EthBalance);
            Utils.ThrowIf(XrpBalance < 0, "[{0}] XRP balance {1}", _ex.GetExchangeName(), XrpBalance);
            Utils.ThrowIf(DashBalance < 0, "[{0}] DASH balance {1}", _ex.GetExchangeName(), DashBalance);
        }
예제 #2
0
        public void HandlingOrder(Action <IExchange> createOrder,
                                  IExchange exchange,
                                  AutoResetEvent evt,
                                  Instrument ins,
                                  ExchangePricesEventArgs args)
        {
            var type = ins.GetOrderType(args);
            var ex   = exchange.GetExchangeName();

            if (SignAsRemoved(ex, type, args))
            {
                Lock(exchange, () =>
                {
                    SaveOrder(ex, type);
                    if (!args.IsOrderDeleted)
                    {
                        SaveOrder(ex, type, args.OrderId);
                        createOrder(exchange);

                        if (ins.ContainsOrders(exchange))
                        {
                            evt.Set();
                        }
                    }
                });
            }
        }
예제 #3
0
        public void ExecuteOrdersAsync(IExchange ex1, IExchange ex2, MatchingData data)
        {
            string        order1 = null;
            string        order2 = null;
            Task <string> task1  = null;
            Task <string> task2  = null;

            if (FastExchanges.ContainsKey(ex1.GetExchangeName()))
            {
                task1 = Task <string> .Run(() => PlaceOrder(ex1, data.AskPrice1, data.Amount, OrderSide.BID, data));

                order1 = task1.Result;
            }
            else
            {
                order1 = PlaceOrder(ex1, data.AskPrice1, data.Amount, OrderSide.BID, data);
            }

            if (FastExchanges.ContainsKey(ex2.GetExchangeName()))
            {
                task2 = Task <string> .Run(() => PlaceOrder(ex2, data.BidPrice2, data.Amount, OrderSide.ASK, data));

                order2 = task2.Result;
            }
            else
            {
                order2 = PlaceOrder(ex2, data.BidPrice2, data.Amount, OrderSide.ASK, data);
            }

            if (order1 == null && order2 == null)
            {
                data.TransactionState = TradingState.Fail12;
                return;
            }

            if (order1 == null)
            {
                data.TransactionState = TradingState.Fail1;
            }
            else
            {
                data.Order1 = order1;
            }

            if (order2 == null)
            {
                data.TransactionState = TradingState.Fail2;
            }
            else
            {
                data.Order2 = order2;
            }

            ex1.RemoveOrders();
            ex2.RemoveOrders();
        }
예제 #4
0
        protected bool GetBestPrices(Dictionary <ExchangeName, IExchange> exchanges,
                                     Instrument ins1,
                                     Instrument ins2,
                                     out double maxBid,
                                     out double minAsk,
                                     out IExchange ex1,
                                     out IExchange ex2)
        {
            maxBid = 0; minAsk = Double.MaxValue;
            ex1    = null; ex2 = null;

            foreach (var ex in exchanges.Values)
            {
                lock (ex)
                {
                    if (ex.TradingState != TradingState.Ok)
                    {
                        continue;
                    }

                    var price = ins1.GetBidPrice(ex);
                    if (price != 0 && price > maxBid)
                    {
                        maxBid = price;
                        ex2    = ex;
                    }
                    price = ins2.GetAskPrice(ex);
                    if (price != 0 && price < minAsk)
                    {
                        minAsk = price;
                        ex1    = ex;
                    }
                }
            }

            if (maxBid == 0)
            {
                return(false);
            }

            if (minAsk == Double.MaxValue)
            {
                return(false);
            }

            if (ex1.GetExchangeName() == ex2.GetExchangeName())
            {
                return(false);
            }

            return(true);
        }
예제 #5
0
 private void Lock(IExchange exchange, Action action)
 {
     if (RestExchanges(exchange.GetExchangeName()))
     {
         Monitor.Enter(exchange);
     }
     else
     {
         if (!Monitor.TryEnter(exchange, 10))
         {
             return;
         }
     }
     action();
     Monitor.Exit(exchange);
 }
예제 #6
0
        public bool TryExecuteOrders(IExchange exchange1, IExchange exchange2)
        {
            ExchangeName ex1 = exchange1.GetExchangeName();

            if (WebsocketExchanges(ex1))
            {
                if (_removedOrders[ex1][0].Length == 0)
                {
                    return(false);
                }
            }
            ExchangeName ex2 = exchange2.GetExchangeName();

            if (WebsocketExchanges(ex2))
            {
                if (_removedOrders[ex2][1].Length == 0)
                {
                    return(false);
                }
            }
            return(true);
        }
예제 #7
0
 public bool CanUpdateBalances(IExchange ex)
 {
     return(_dbRepo.CanUpdateBalances(ex.GetExchangeName()));
 }
예제 #8
0
 public void SetExchange2(IExchange ex2)
 {
     CurrencyRate2 = ((BaseExchange)ex2).CurrencyRate;
     Exchange2     = ex2.GetExchangeName();
 }
예제 #9
0
 public void SetExchange1(IExchange ex1)
 {
     CurrencyRate1 = ((BaseExchange)ex1).CurrencyRate;
     Exchange1     = ex1.GetExchangeName();
 }