private static async Task WithdrawTransactions(IDbService dbService, Dictionary <string, IExchange> exchanges, DbTransaction transaction, IOrder baseOrder, IOrder counterOrder, int pId) { IExchange baseExchange = exchanges[transaction.BaseExchange]; IExchange counterExchange = exchanges[transaction.CounterExchange]; if (transaction.Type == "basebuy") {//Withdraw Market Currency from Base var counterAddress = await counterExchange.GetDepositAddress(transaction.MarketCurrency); var tx = await baseExchange.Withdraw(transaction.MarketCurrency, baseOrder.Quantity, counterAddress.Address); //Withdraw Base Currency from Counter var baseAddress = await baseExchange.GetDepositAddress(transaction.BaseCurrency); var tx2 = await counterExchange.Withdraw(transaction.BaseCurrency, baseOrder.CostProceeds + transaction.CounterBaseWithdrawalFee, baseAddress.Address); decimal commission = counterOrder.CostProceeds - (baseOrder.CostProceeds + transaction.CounterBaseWithdrawalFee); dbService.UpdateTransactionWithdrawalUuid(transaction.Id, tx.Uuid, tx2.Uuid, commission); } else {//Withdraw Market Currency from Counter var baseAddress = await baseExchange.GetDepositAddress(transaction.MarketCurrency); var tx2 = await counterExchange.Withdraw(transaction.MarketCurrency, counterOrder.Quantity, baseAddress.Address); //Withdraw Base Currency from Base var counterAddress = await counterExchange.GetDepositAddress(transaction.BaseCurrency); var tx = await baseExchange.Withdraw(transaction.BaseCurrency, counterOrder.CostProceeds + transaction.BaseBaseWithdrawalFee, counterAddress.Address); decimal commission = baseOrder.CostProceeds - (counterOrder.CostProceeds + transaction.BaseBaseWithdrawalFee); dbService.UpdateTransactionWithdrawalUuid(transaction.Id, tx.Uuid, tx2.Uuid, commission); } }
public void BalanceCurrency(string currency) { var baseBalance = baseBalances[currency]; var counterBalance = counterBalances[currency]; decimal targetBalance = baseBalance.Available + counterBalance.Available / 2; decimal diff = 2 * (baseBalance.Available - counterBalance.Available) / (baseBalance.Available + counterBalance.Available); Console.WriteLine("{0}: {1} Balance {2}, {3} Balance {4}, Diff {5}", currency, baseExchange.Name, baseBalance.Available, counterExchange.Name, counterBalance.Available, diff); if (activeWithdrawals.ContainsKey(currency) && Math.Abs(diff) < 0.5m) { string wId; activeWithdrawals.Remove(currency, out wId); logger.Trace("Completed Withdrawal {0}", currency); } //If diff < 0 Base needs more funds, depost into base if (!activeWithdrawals.ContainsKey(currency) && Math.Abs(diff) > 1.5m && diff < 0) { var address = baseExchange.GetDepositAddress(currency).Result; decimal amount = targetBalance - baseBalance.Available; var result = counterExchange.Withdraw(address.Currency, amount, address.Address, address.Tag).Result; logger.Trace("Withdrawal {0}", currency); activeWithdrawals.TryAdd(currency, result.Uuid); } if (!activeWithdrawals.ContainsKey(currency) && Math.Abs(diff) > 1.5m && diff > 0) { var address = counterExchange.GetDepositAddress(currency).Result; decimal amount = targetBalance - baseBalance.Available; var result = baseExchange.Withdraw(address.Currency, amount, address.Address, address.Tag).Result; logger.Trace("Withdrawal {0}", currency); activeWithdrawals.TryAdd(currency, result.Uuid); } completedPairs.TryAdd(currency, true); }