/// <summary> /// Checks our current running trades against the strategy. /// If the strategy tells us to sell we need to do so. /// </summary> /// <returns></returns> private async Task CheckActiveTradesAgainstStrategy() { // Check our active trades for a sell signal from the strategy foreach (var trade in _activeTrades.Where(x => (x.OpenOrderId == null || x.SellType == SellType.Immediate) && x.IsOpen)) { var signal = await GetStrategySignal(trade.Market); // If the strategy is telling us to sell we need to do so. if (signal != null && signal.TradeAdvice == TradeAdvice.Sell) { if ((trade.IsSelling && trade.SellType == SellType.Immediate)) { // If an immediate order is placed it needs to be cancelled first. await _api.CancelOrder(trade.OpenOrderId, trade.Market); } // Create a sell order for our strategy. var ticker = await _api.GetTicker(trade.Market); var orderId = await _api.Sell(trade.Market, trade.Quantity, ticker.Bid); trade.CloseRate = ticker.Bid; trade.OpenOrderId = orderId; trade.SellOrderId = orderId; trade.SellType = SellType.Strategy; trade.IsSelling = true; _orderBatch.Add(TableOperation.Replace(trade)); await SendNotification($"Sell order placed for {trade.Market} at {trade.CloseRate:0.00000000} (Strategy sell)."); } } }
/// <summary> /// Checks our current running trades against the strategy. /// If the strategy tells us to sell we need to do so. /// </summary> /// <returns></returns> private async Task CheckActiveTradesAgainstStrategy() { // Check our active trades for a sell signal from the strategy foreach (var trade in _activeTrades.Where(x => (x.OpenOrderId == null || x.SellType == SellType.Immediate) && x.IsOpen)) { var signal = await GetStrategySignal(trade.Market); // If the strategy is telling us to sell we need to do so. if (signal != null && signal.TradeAdvice == TradeAdvice.Sell) { // If the trade is an immediate order, we leave it alone. if ((trade.IsSelling && trade.SellType == SellType.Immediate)) { return; } // Create a sell order for our strategy. var ticker = await _api.GetTicker(trade.Market); var orderId = await _api.Sell(trade.Market, trade.Quantity, ticker.Bid); trade.CloseRate = ticker.Bid; trade.OpenOrderId = orderId; trade.SellOrderId = orderId; trade.SellType = SellType.Strategy; trade.IsSelling = true; await _dataStore.SaveTradeAsync(trade); } } }
/// <summary> /// Executes a sell for the given trade and updated the entity. /// </summary> /// <param name="trade"></param> /// <param name="currentRateBid">Rate to sell for</param> /// <param name="balance">Amount to sell for</param> /// <returns>Current profit as percentage</returns> private async Task <double> ExecuteSellOrder(Trade trade, double currentRateBid, double balance) { // Calculate our profit. var investment = (trade.StakeAmount * (1 - Constants.TransactionFeePercentage)); var sales = (trade.Quantity * currentRateBid) - (trade.Quantity * currentRateBid * Constants.TransactionFeePercentage); var profit = 100 * ((sales - investment) / investment); // Sell the thing. var orderId = await _api.Sell(trade.Market, balance, currentRateBid); trade.CloseRate = currentRateBid; trade.CloseProfitPercentage = profit; trade.CloseProfit = sales - investment; trade.CloseDate = DateTime.UtcNow; trade.OpenOrderId = orderId; trade.SellOrderId = orderId; return(profit); }