Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions csharp/TraderBot/TradingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -826,8 +826,17 @@ private async Task<PostOrderResponse> PlaceSellOrder(long amount, decimal price)
// {
// throw new InvalidOperationException($"Not enough amount to sell {amount} assets. Available amount: {securityPosition.Balance}");
// }
var response = await InvestApi.Orders.PostOrderAsync(sellOrderRequest).ResponseAsync;
using var call = InvestApi.Orders.PostOrderAsync(sellOrderRequest);
var headers = await call.ResponseHeadersAsync;
var response = await call.ResponseAsync;

var trackingId = headers.GetValue("x-tracking-id");
Logger.LogInformation($"Sell order placed: {response}");
if (!string.IsNullOrEmpty(trackingId))
{
Logger.LogInformation($"Tinkoff request tracking ID: {trackingId}");
}

return response;
}

Expand All @@ -847,21 +856,39 @@ private async Task<PostOrderResponse> PlaceBuyOrder(long amount, decimal price)
// {
// throw new InvalidOperationException($"Not enough money to buy {CurrentInstrument.Figi} asset.");
// }
var response = await InvestApi.Orders.PostOrderAsync(buyOrderRequest).ResponseAsync;
using var call = InvestApi.Orders.PostOrderAsync(buyOrderRequest);
var headers = await call.ResponseHeadersAsync;
var response = await call.ResponseAsync;

var trackingId = headers.GetValue("x-tracking-id");
var total = amount * price;
SetCashBalance(CashBalanceFree - total, CashBalanceLocked + total);
Logger.LogInformation($"Buy order placed: {response}");
if (!string.IsNullOrEmpty(trackingId))
{
Logger.LogInformation($"Tinkoff request tracking ID: {trackingId}");
}

return response;
}

private async Task<CancelOrderResponse> CancelOrder(string orderId)
{
var response = await InvestApi.Orders.CancelOrderAsync(new CancelOrderRequest
using var call = InvestApi.Orders.CancelOrderAsync(new CancelOrderRequest
{
AccountId = CurrentAccount.Id,
OrderId = orderId,
});
var headers = await call.ResponseHeadersAsync;
var response = await call.ResponseAsync;

var trackingId = headers.GetValue("x-tracking-id");
Logger.LogInformation($"Order cancelled: {response}");
if (!string.IsNullOrEmpty(trackingId))
{
Logger.LogInformation($"Tinkoff request tracking ID: {trackingId}");
}

return response;
}

Expand Down
45 changes: 45 additions & 0 deletions examples/TrackingIdExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using Grpc.Core;
using Microsoft.Extensions.Logging;

namespace TraderBot.Examples
{
/// <summary>
/// This example demonstrates how the tracking ID logging would work
/// when the Tinkoff API returns the x-tracking-id header
/// </summary>
public class TrackingIdExample
{
private readonly ILogger<TrackingIdExample> _logger;

public TrackingIdExample(ILogger<TrackingIdExample> logger)
{
_logger = logger;
}

/// <summary>
/// Example of how tracking ID is extracted and logged
/// This simulates what happens in the actual PlaceBuyOrder/PlaceSellOrder/CancelOrder methods
/// </summary>
public void SimulateTrackingIdLogging()
{
// Simulate a response with x-tracking-id header
var metadata = new Metadata
{
{ "x-tracking-id", "550e8400-e29b-41d4-a716-446655440000" }
};

// Extract tracking ID (as done in the actual implementation)
var trackingId = metadata.GetValue("x-tracking-id");

// Log the tracking ID (as done in the actual implementation)
_logger.LogInformation($"Buy order placed: [order response details]");
if (!string.IsNullOrEmpty(trackingId))
{
_logger.LogInformation($"Tinkoff request tracking ID: {trackingId}");
}

Console.WriteLine($"Tracking ID logged: {trackingId}");
}
}
}
Loading