Merge remote-tracking branch 'origin/dev' into dev
This commit is contained in:
@@ -39,120 +39,183 @@ public class PositionManagerWorker : BaseWorker<PositionManagerWorker>
|
|||||||
protected override async Task Run(CancellationToken cancellationToken)
|
protected override async Task Run(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
await ManageNewPositions();
|
await ManageNewPositions();
|
||||||
await ManagePartillyFilledPositions();
|
await ManagePartiallyFilledPositions();
|
||||||
await ManageFilledPositions();
|
await ManageFilledPositions();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task ManagePartillyFilledPositions()
|
private async Task ManagePartiallyFilledPositions()
|
||||||
{
|
{
|
||||||
var positions = GetPositions(PositionStatus.PartiallyFilled);
|
var positions = GetPositions(PositionStatus.PartiallyFilled);
|
||||||
|
_logger.LogInformation("Processing {PartiallyFilledCount} partially filled positions", positions.Count());
|
||||||
_logger.LogInformation("Partilly filled positions count : {0} ", positions.Count());
|
|
||||||
|
|
||||||
foreach (var position in positions)
|
foreach (var position in positions)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Managing Partilly filled position: {0} - Date: {1} - Direction: {2} - Ticker: {3}",
|
using (_logger.BeginScope("Position {PositionId} ({Ticker})", position.Identifier, position.Ticker))
|
||||||
position.Identifier, position.Date, position.OriginDirection, position.Ticker);
|
|
||||||
|
|
||||||
var account = await _accountService.GetAccount(position.AccountName, false, false);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
if (position.StopLoss.Status == TradeStatus.PendingOpen)
|
try
|
||||||
{
|
{
|
||||||
var stopLoss = await _exchangeService.OpenStopLoss(account, position.Ticker,
|
// Lock position for processing
|
||||||
position.OriginDirection,
|
position.Status = PositionStatus.Updating;
|
||||||
position.StopLoss.Price, position.StopLoss.Quantity, false, DateTime.UtcNow);
|
_tradingService.UpdatePosition(position);
|
||||||
|
|
||||||
if (stopLoss != null & (stopLoss.Status == TradeStatus.Requested))
|
_logger.LogDebug("Processing risk orders for {Direction} position opened at {OpenDate}",
|
||||||
{
|
position.OriginDirection, position.Date.ToString("o"));
|
||||||
position.StopLoss = stopLoss;
|
|
||||||
_logger.LogInformation("|_ SL is requested");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new Exception("Stop loss not requested");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_logger.LogInformation($"|_ SL is already handle. Current status = {position.StopLoss.Status}");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (position.TakeProfit1.Status == TradeStatus.PendingOpen)
|
var account = await _accountService.GetAccount(position.AccountName, false, false);
|
||||||
{
|
var success = true;
|
||||||
var takeProfit1 = await _exchangeService.OpenTakeProfit(account, position.Ticker,
|
|
||||||
position.OriginDirection, position.TakeProfit1.Price, position.TakeProfit1.Quantity, false,
|
|
||||||
DateTime.UtcNow);
|
|
||||||
|
|
||||||
if (takeProfit1 != null & (takeProfit1.Status == TradeStatus.Requested))
|
// Process and update trades
|
||||||
{
|
var updatedSl = await ProcessTrade(account, position.StopLoss, "SL", async () =>
|
||||||
position.TakeProfit1 = takeProfit1;
|
await _exchangeService.OpenStopLoss(account, position.Ticker, position.OriginDirection,
|
||||||
_logger.LogInformation("|_ TP is requested");
|
position.StopLoss.Price, position.StopLoss.Quantity, false, DateTime.UtcNow));
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new Exception("Take Profit 1 not requested");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_logger.LogInformation($"|_ TP is already handle. Current status = {position.TakeProfit1.Status}");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (position.TakeProfit2 != null &&
|
if (updatedSl != null)
|
||||||
position.TakeProfit2.Status == TradeStatus.PendingOpen)
|
{
|
||||||
{
|
position.StopLoss = updatedSl;
|
||||||
var takeProfit2 = await _exchangeService.OpenTakeProfit(account, position.Ticker,
|
success &= updatedSl.Status.IsActive();
|
||||||
position.OriginDirection, position.TakeProfit2.Price, position.TakeProfit2.Quantity, false,
|
}
|
||||||
DateTime.UtcNow);
|
|
||||||
|
|
||||||
if (takeProfit2 != null & (takeProfit2.Status == TradeStatus.Requested))
|
var updatedTp1 = await ProcessTrade(account, position.TakeProfit1, "TP1", async () =>
|
||||||
|
await _exchangeService.OpenTakeProfit(account, position.Ticker, position.OriginDirection,
|
||||||
|
position.TakeProfit1.Price, position.TakeProfit1.Quantity, false, DateTime.UtcNow));
|
||||||
|
|
||||||
|
if (updatedTp1 != null)
|
||||||
{
|
{
|
||||||
position.TakeProfit2 = takeProfit2;
|
position.TakeProfit1 = updatedTp1;
|
||||||
_logger.LogInformation("|_ TP2 is requested");
|
success &= updatedTp1.Status.IsActive();
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
Trade? updatedTp2 = null;
|
||||||
|
if (position.TakeProfit2 != null)
|
||||||
{
|
{
|
||||||
throw new Exception("Take Profit 2 not requested");
|
updatedTp2 = await ProcessTrade(account, position.TakeProfit2, "TP2", async () =>
|
||||||
|
await _exchangeService.OpenTakeProfit(account, position.Ticker, position.OriginDirection,
|
||||||
|
position.TakeProfit2.Price, position.TakeProfit2.Quantity, false, DateTime.UtcNow));
|
||||||
|
|
||||||
|
if (updatedTp2 != null)
|
||||||
|
{
|
||||||
|
position.TakeProfit2 = updatedTp2;
|
||||||
|
success &= updatedTp2.Status.IsActive() || updatedTp2.Status == TradeStatus.Cancelled;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update position status based on trade states
|
||||||
|
position.Status = success && AllTradesActive(position)
|
||||||
|
? PositionStatus.Filled
|
||||||
|
: PositionStatus.PartiallyFilled;
|
||||||
|
|
||||||
|
_logger.LogInformation("Final position status: {Status}", position.Status);
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("|_ TP2 is already handle or not required");
|
_logger.LogError(ex, "Position processing failed");
|
||||||
|
position.Status = PositionStatus.PartiallyFilled;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_tradingService.UpdatePosition(position);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.LogError($"|_ Cannot fully filled position because : {ex.Message}");
|
|
||||||
}
|
|
||||||
|
|
||||||
TradeStatus[] validStatus = [TradeStatus.Requested, TradeStatus.Filled];
|
|
||||||
if (validStatus.Contains(position.StopLoss.Status)
|
|
||||||
&& (validStatus.Contains(position.TakeProfit1.Status)))
|
|
||||||
{
|
|
||||||
position.Status = PositionStatus.Filled;
|
|
||||||
_logger.LogInformation($"|_ Position is now open and SL/TP are correctly requested");
|
|
||||||
}
|
|
||||||
|
|
||||||
_tradingService.UpdatePosition(position);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task<Trade?> ProcessTrade(Account account, Trade trade, string tradeType, Func<Task<Trade>> createTrade)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// 1. Check existing status on exchange
|
||||||
|
var exchangeTrade = await _exchangeService.GetTrade(account, trade.ExchangeOrderId, trade.Ticker);
|
||||||
|
if (exchangeTrade != null && exchangeTrade.Status.IsActive())
|
||||||
|
{
|
||||||
|
_logger.LogInformation("{TradeType} already exists on exchange - Status: {Status}",
|
||||||
|
tradeType, exchangeTrade.Status);
|
||||||
|
return exchangeTrade;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Only create new order if in pending state
|
||||||
|
if (trade.Status != TradeStatus.PendingOpen)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("{TradeType} creation skipped - Invalid status: {Status}",
|
||||||
|
tradeType, trade.Status);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Create new order
|
||||||
|
var newTrade = await createTrade();
|
||||||
|
if (newTrade?.Status == TradeStatus.Requested)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("{TradeType} successfully created - ExchangeID: {ExchangeOrderId}",
|
||||||
|
tradeType, newTrade.ExchangeOrderId);
|
||||||
|
return newTrade;
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogError("{TradeType} creation failed - Null response or invalid status", tradeType);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "{TradeType} processing failed", tradeType);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool AllTradesActive(Position position)
|
||||||
|
{
|
||||||
|
return position.StopLoss.Status.IsActive() &&
|
||||||
|
position.TakeProfit1.Status.IsActive() &&
|
||||||
|
(position.TakeProfit2?.Status.IsActive() ?? true);
|
||||||
|
}
|
||||||
private async Task ManageFilledPositions()
|
private async Task ManageFilledPositions()
|
||||||
{
|
{
|
||||||
var positions = GetPositions(PositionStatus.Filled);
|
var positions = GetPositions(PositionStatus.Filled);
|
||||||
|
_logger.LogInformation("Monitoring {FilledPositionCount} filled positions", positions.Count());
|
||||||
_logger.LogInformation("Filled positions count : {0} ", positions.Count());
|
|
||||||
|
|
||||||
foreach (var position in positions)
|
foreach (var position in positions)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Managing filled position: {0} - Date: {1} - Direction: {2} - Ticker: {3}",
|
using (_logger.BeginScope("Position {PositionId} ({Ticker})", position.Identifier, position.Ticker))
|
||||||
position.Identifier, position.Date, position.OriginDirection, position.Ticker);
|
{
|
||||||
var account = await GetAccount(position.AccountName);
|
try
|
||||||
|
{
|
||||||
|
// Acquire processing lock
|
||||||
|
_logger.LogDebug("Acquiring position lock");
|
||||||
|
position.Status = PositionStatus.Updating;
|
||||||
|
_tradingService.UpdatePosition(position);
|
||||||
|
|
||||||
var updatedPosition = await _tradingService.ManagePosition(account, position);
|
_logger.LogInformation("Managing filled position - Direction: {Direction}, Open Since: {OpenDate}",
|
||||||
_tradingService.UpdatePosition(updatedPosition);
|
position.OriginDirection, position.Date.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||||
|
|
||||||
|
var account = await GetAccount(position.AccountName);
|
||||||
|
|
||||||
|
// Perform position management
|
||||||
|
var updatedPosition = await _tradingService.ManagePosition(account, position);
|
||||||
|
|
||||||
|
// Log status changes if they occurred
|
||||||
|
if (updatedPosition.Status != position.Status)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Position status updated: {OldStatus} → {NewStatus}",
|
||||||
|
position.Status, updatedPosition.Status);
|
||||||
|
}
|
||||||
|
|
||||||
|
_tradingService.UpdatePosition(updatedPosition);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Failed to manage position - {ErrorMessage}", ex.Message);
|
||||||
|
|
||||||
|
// Reset status for retry
|
||||||
|
position.Status = PositionStatus.Filled;
|
||||||
|
_tradingService.UpdatePosition(position);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
// Ensure lock is always released
|
||||||
|
if (position.Status == PositionStatus.Updating)
|
||||||
|
{
|
||||||
|
position.Status = PositionStatus.Filled;
|
||||||
|
_tradingService.UpdatePosition(position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -191,6 +191,12 @@ public static class Enums
|
|||||||
Filled = 3,
|
Filled = 3,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static bool IsActive(this TradeStatus status) =>
|
||||||
|
status == TradeStatus.Requested ||
|
||||||
|
status == TradeStatus.Cancelled ||
|
||||||
|
status == TradeStatus.Filled;
|
||||||
|
|
||||||
|
|
||||||
public enum PositionStatus
|
public enum PositionStatus
|
||||||
{
|
{
|
||||||
New = 0,
|
New = 0,
|
||||||
|
|||||||
Reference in New Issue
Block a user