public static void SellCurrencyToUsdt(string currencyPair, ref TradeSignalOrder tradeSignalOrder) { lock (_syncRoot) { decimal percentToTrade = 1.00M; // always sell 100% bool isMoving = false; int attemptCount = 0; Dictionary <string, decimal> balances = PoloniexExchangeService.Instance.ReturnBalances(); decimal currencyBalance = balances[CurrencyUtility.GetCurrencyFromUsdtCurrencyPair(currencyPair)] * percentToTrade; long orderNumber = 0; while (true) { attemptCount++; Dictionary <string, Dictionary <string, decimal> > res = PoloniexExchangeService.Instance.ReturnTicker(); var latestUsdtBtcTicker = res[currencyPair]; // weighted average decimal usdtCurrencyLastPrice = latestUsdtBtcTicker[TickerResultKeys.last]; // 3 decimal usdtCurrencyHighestBid = latestUsdtBtcTicker[TickerResultKeys.highestBid]; // 2 decimal usdtCurrencyLowestAsk = latestUsdtBtcTicker[TickerResultKeys.lowestAsk]; // 1 /* TESTING CODE : BEGIN */ //decimal sellRate = (1.25M) * usdtBtcLastPrice; // TODO: FIX!!! //if (isMoving) //{ // sellRate = sellRate * 1.25M; //} /* TESTING CODE : END */ /* PRODUCTION CODE : BEGIN */ decimal sellRate = ((3M * usdtCurrencyLastPrice) + (2M * usdtCurrencyHighestBid) + (1M * usdtCurrencyLowestAsk)) / 6M;; /* PRODUCTION CODE : END */ decimal sellAmount = currencyBalance; if (!isMoving) { tradeSignalOrder.PlaceValueTradedAt = sellRate; // SELL Thread.Sleep(1000); // guarantee nonce (unix timestamp) var sellResult = PoloniexExchangeService.Instance.Sell(currencyPair, sellRate, sellAmount); orderNumber = sellResult.orderNumber; Logger.Write($"Order: Selling {currencyPair} to USDT; sellRate: {sellRate}, sellAmount: {sellAmount}, _numberOfTraders: {_numberOfTraders}, _numberOfHoldings: {_numberOfHoldings}", Logger.LogType.TransactionLog); isMoving = true; } else { tradeSignalOrder.MoveValueTradedAt = sellRate; // MOVE Thread.Sleep(1000); // guarantee nonce (unix timestamp) var moveResult = PoloniexExchangeService.Instance.MoveOrder(orderNumber, sellRate, sellAmount); orderNumber = moveResult.orderNumber; Logger.Write($"Order: Moving (attemptCount:{attemptCount}) {currencyPair} to USDT; sellRate: {sellRate}, sellAmount: {sellAmount}, _numberOfTraders: {_numberOfTraders}, _numberOfHoldings: {_numberOfHoldings}", Logger.LogType.TransactionLog); } Thread.Sleep(10 * 1000); // allow exchange to resolve order // Get open orders var openOrders = PoloniexExchangeService.Instance.ReturnOpenOrders(currencyPair); var originalBuyOrder = openOrders.SingleOrDefault(x => x[OpenOrderKeys.orderNumber] == orderNumber.ToString()); Logger.Write($"openOrders: {JsonConvert.SerializeObject(openOrders)}", Logger.LogType.TransactionLog); bool isTradeComplete = originalBuyOrder == null; if (isTradeComplete) { tradeSignalOrder.LastValueAtProcessing = sellRate; _numberOfHoldings--; break; } } } }