Exemple #1
0
 /// <summary>
 /// If we have a trade pair of for instance ETH - BTC then to exhange x-ETH to y-BTC we're doing a sell order.
 /// In other words the from currency is the one that dictates.
 /// </summary>
 public ExchangeOrderAction(IExchangeTrader tradingIntegration, ExchangeTradePair tradePair, ExchangeOrderType orderType, decimal transactionAmountInFromCurrency)
 {
     this.TradePair              = tradePair;
     this.OrderType              = orderType;
     this.BaseCurrency           = this.TradePair.TradePair.ToCurrency; // The base currency is the to currency
     this.EstimatedCost          = this.TradePair.FeePercent / 100m * transactionAmountInFromCurrency;
     this.MaxExposureCost        = 0m;                                  // We set this to zero as order actions are instantaneous.
     this.EstimatedTimeToExecute = new TimeSpan(0, 0, 10);              // We arbitrarly set the estimated time to execute to 10seconds. Generally, buying or selling at market price will be executed immediately.
     this.TradingIntegration     = tradingIntegration;
     this.TransactionAmount      = transactionAmountInFromCurrency;
 }
Exemple #2
0
 public CurrencyTransfer(Currency currency, IExchangeTrader fromExchangeTrader, IExchangeTrader toExchangeTrader, decimal transferAmount)
 {
     this.FromTraderIntegration  = fromExchangeTrader;
     this.ToTraderIntegration    = toExchangeTrader;
     this.FromAccount            = fromExchangeTrader.Exchange.TradeAccounts.Where(x => x.Currency == currency).Single();
     this.ToAccount              = toExchangeTrader.Exchange.TradeAccounts.Where(x => x.Currency == currency).Single();
     this.EstimatedCost          = currency.AverageTransferFee;
     this.EstimatedTimeToExecute = new TimeSpan(0, currency.AverageTransferTimeMinutes, 0);
     this.BaseCurrency           = currency;
     this.TransactionAmount      = TransactionAmount;
     this.MaxExposureCost        = this.BaseCurrency.DailyVolatilityIndex / (24 * 60 * 100m) * transferAmount * currency.AverageTransferTimeMinutes; // At worse, this is the maximum amount one would expect to loose during the transfer of funds
 }
Exemple #3
0
        private static async Task SynchronizeAccounts(IExchangeTrader exchangeIntegration, RBBotContext dbContext)
        {
            Exchange exchangeModel = exchangeIntegration.Exchange;

            // Get the balances from the exchange integration
            var exchangebalances = (await exchangeIntegration.GetBalancesAsync()).ToDictionary(x => x.CurrencyCode, y => y);

            // Get the exchange's trading accounts.
            var existingAccounts = exchangeModel.TradeAccounts.ToDictionary(x => x.Currency.Code, y => y);

            // If the account exists already, then update.
            existingAccounts.Where(x => exchangebalances.Keys.Contains(x.Key)).ToList().ForEach(x =>
            {
                var exCurr     = exchangebalances[x.Key];
                var acc        = existingAccounts[x.Key];
                acc.Balance    = exCurr.Balance;
                acc.LastUpdate = exCurr.Timestamp;

                if (exCurr.ExchangeIdentifier != null)
                {
                    acc.ExchangeIdentifier = exCurr.ExchangeIdentifier;
                }
                if (exCurr.Address != null)
                {
                    acc.Address = exCurr.Address;
                }
            });

            // If the account doesn't exist, then create it.
            exchangebalances.Keys.Where(x => !existingAccounts.Keys.Contains(x)).ToList().ForEach(x =>
            {
                var b            = exchangebalances[x];
                TradeAccount acc = new TradeAccount()
                {
                    Address            = b.Address,
                    Balance            = b.Balance,
                    Exchange           = exchangeModel,
                    ExchangeIdentifier = b.ExchangeIdentifier,
                    LastUpdate         = b.Timestamp,
                    Currency           = dbContext.Currencies.Where(c => c.Code == b.CurrencyCode).Single()
                };

                dbContext.TradeAccounts.Add(acc);
            });
        }