public void TradingSignal_IsTimeInThreshold() { var signal = new TradingSignal(null, "", OrderCommand.Create, TradeType.Buy, 100m, 100m, DateTime.UtcNow.AddMinutes(-5)); Assert.True(signal.IsTimeInThreshold(TimeSpan.FromMinutes(6))); Assert.False(signal.IsTimeInThreshold(TimeSpan.FromMinutes(4))); var signalInFuture = new TradingSignal(null, "", OrderCommand.Create, TradeType.Buy, 100m, 100m, DateTime.UtcNow.AddMinutes(5)); Assert.True(signalInFuture.IsTimeInThreshold(TimeSpan.FromMinutes(6))); Assert.False(signalInFuture.IsTimeInThreshold(TimeSpan.FromMinutes(4))); }
private async Task HandleCreation(TradingSignal signal, TranslatedSignalTableEntity translatedSignal, Exchange exchange) { try { if (!signal.IsTimeInThreshold(tradingSignalsThreshold)) { translatedSignal.Failure("The signal is too old"); await logger.WriteInfoAsync(nameof(TradingSignalsHandler), nameof(HandleCreation), signal.ToString(), "Skipping old signal"); return; } var executedTrade = await exchange.AddOrderAndWaitExecution(signal, translatedSignal, apiTimeout); bool orderAdded = executedTrade.ExecutionStatus == OrderExecutionStatus.New || executedTrade.ExecutionStatus == OrderExecutionStatus.Pending; bool orderFilled = executedTrade.ExecutionStatus == OrderExecutionStatus.Fill || executedTrade.ExecutionStatus == OrderExecutionStatus.PartialFill; if (orderAdded || orderFilled) { await logger.WriteInfoAsync(nameof(TradingSignalsHandler), nameof(HandleCreation), signal.ToString(), "Created new order"); } else { await logger.WriteWarningAsync(nameof(TradingSignalsHandler), nameof(HandleCreation), signal.ToString(), $"Added order is in unexpected status: {executedTrade}"); translatedSignal.Failure($"Added order is in unexpected status: {executedTrade}"); } await _acknowledHandler.Handle(CreateAcknowledgement(exchange, orderAdded, signal, translatedSignal)); if (orderFilled) { await _tradeHandler.Handle(executedTrade); } } catch (ApiException e) { await logger.WriteInfoAsync(nameof(TradingSignalsHandler), nameof(HandleCreation), signal.ToString(), e.Message); translatedSignal.Failure(e); await _acknowledHandler.Handle(CreateAcknowledgement(exchange, false, signal, translatedSignal, e)); } catch (Exception e) { await logger.WriteErrorAsync(nameof(TradingSignalsHandler), nameof(HandleCreation), signal.ToString(), e); translatedSignal.Failure(e); await _acknowledHandler.Handle(CreateAcknowledgement(exchange, false, signal, translatedSignal, e)); } }