private async Task <ExternalTrade> ExecuteLimitOrderAsync(string assetPairId, decimal volume, PositionType positionType) { ExternalTrade externalTrade = null; if (!_attempts.TryGetValue(assetPairId, out Tuple <int, int> attempt)) { attempt = new Tuple <int, int>(0, 0); } try { if (attempt.Item2 <= 0) { if (positionType == PositionType.Long) { externalTrade = await _externalExchangeService.ExecuteSellLimitOrderAsync(assetPairId, volume); } else { externalTrade = await _externalExchangeService.ExecuteBuyLimitOrderAsync(assetPairId, volume); } _attempts.Remove(assetPairId); } else { _attempts[assetPairId] = new Tuple <int, int>(attempt.Item1, attempt.Item2 - 1); _log.InfoWithDetails("Execution of hedge limit order is skipped", new { assetPairId, volume, positionType, attempt = attempt.Item1, iteration = attempt.Item2 }); } } catch (Exception exception) { _attempts[assetPairId] = new Tuple <int, int>(attempt.Item1 + 1, Math.Min(attempt.Item1 + 1, MaxIterations)); _log.WarningWithDetails("Can not close positions.", exception, new { assetPairId, volume, positionType, attempt = attempt.Item1 }); } return(externalTrade); }
private async Task <ExternalTrade> ExecuteLimitOrderAsync(string assetPairId, decimal volume, PositionType positionType) { var startedAt = DateTime.UtcNow; ExternalTrade externalTrade = null; if (!_attempts.TryGetValue(assetPairId, out Tuple <int, int> attempt)) { attempt = new Tuple <int, int>(0, 0); } try { if (attempt.Item2 <= 0) { if (positionType == PositionType.Long) { externalTrade = await _externalExchangeService.ExecuteSellLimitOrderAsync(assetPairId, volume); } else { externalTrade = await _externalExchangeService.ExecuteBuyLimitOrderAsync(assetPairId, volume); } _attempts.Remove(assetPairId); } else { _attempts[assetPairId] = new Tuple <int, int>(attempt.Item1, attempt.Item2 - 1); _log.InfoWithDetails("Execution of hedge limit order is skipped", new { assetPairId, volume, positionType, attempt = attempt.Item1, iteration = attempt.Item2 }); } } catch (Exception exception) { _attempts[assetPairId] = new Tuple <int, int>(attempt.Item1 + 1, Math.Min(attempt.Item1 + 1, MaxIterations)); _log.WarningWithDetails("Can not close positions.", exception, new { assetPairId, volume, positionType, attempt = attempt.Item1 }); } var finishedAt = DateTime.UtcNow; _log.Info("HedgeService.ExecuteLimitOrderAsync() completed.", new { AssetPairId = assetPairId, StartedAt = startedAt, FinishedAt = finishedAt, Latency = (finishedAt - startedAt).TotalMilliseconds }); return(externalTrade); }