Update position saving and update
This commit is contained in:
@@ -406,6 +406,32 @@ public class TradingBotBase : ITradingBot
|
|||||||
internalPosition.Open.SetStatus(TradeStatus.Filled);
|
internalPosition.Open.SetStatus(TradeStatus.Filled);
|
||||||
positionForSignal.Open.SetStatus(TradeStatus.Filled);
|
positionForSignal.Open.SetStatus(TradeStatus.Filled);
|
||||||
|
|
||||||
|
// Update Open trade ExchangeOrderId if broker position has one
|
||||||
|
if (brokerPosition.Open?.ExchangeOrderId != null && internalPosition.Open != null)
|
||||||
|
{
|
||||||
|
internalPosition.Open.SetExchangeOrderId(brokerPosition.Open.ExchangeOrderId);
|
||||||
|
positionForSignal.Open.SetExchangeOrderId(brokerPosition.Open.ExchangeOrderId);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update Stop Loss and Take Profit trades with correct ExchangeOrderId from broker
|
||||||
|
if (brokerPosition.StopLoss != null && internalPosition.StopLoss != null)
|
||||||
|
{
|
||||||
|
internalPosition.StopLoss.SetExchangeOrderId(brokerPosition.StopLoss.ExchangeOrderId);
|
||||||
|
positionForSignal.StopLoss.SetExchangeOrderId(brokerPosition.StopLoss.ExchangeOrderId);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (brokerPosition.TakeProfit1 != null && internalPosition.TakeProfit1 != null)
|
||||||
|
{
|
||||||
|
internalPosition.TakeProfit1.SetExchangeOrderId(brokerPosition.TakeProfit1.ExchangeOrderId);
|
||||||
|
positionForSignal.TakeProfit1.SetExchangeOrderId(brokerPosition.TakeProfit1.ExchangeOrderId);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (brokerPosition.TakeProfit2 != null && internalPosition.TakeProfit2 != null)
|
||||||
|
{
|
||||||
|
internalPosition.TakeProfit2.SetExchangeOrderId(brokerPosition.TakeProfit2.ExchangeOrderId);
|
||||||
|
positionForSignal.TakeProfit2.SetExchangeOrderId(brokerPosition.TakeProfit2.ExchangeOrderId);
|
||||||
|
}
|
||||||
|
|
||||||
await UpdatePositionDatabase(internalPosition);
|
await UpdatePositionDatabase(internalPosition);
|
||||||
|
|
||||||
if (previousPositionStatus != PositionStatus.Filled &&
|
if (previousPositionStatus != PositionStatus.Filled &&
|
||||||
@@ -503,12 +529,22 @@ public class TradingBotBase : ITradingBot
|
|||||||
if (internalPosition.Open != null)
|
if (internalPosition.Open != null)
|
||||||
{
|
{
|
||||||
internalPosition.Open.SetStatus(TradeStatus.Filled);
|
internalPosition.Open.SetStatus(TradeStatus.Filled);
|
||||||
|
// Update Open trade ExchangeOrderId if broker position has one
|
||||||
|
if (brokerPosition.Open?.ExchangeOrderId != null)
|
||||||
|
{
|
||||||
|
internalPosition.Open.SetExchangeOrderId(brokerPosition.Open.ExchangeOrderId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Also update the position in the bot's positions dictionary
|
// Also update the position in the bot's positions dictionary
|
||||||
if (positionForSignal.Open != null)
|
if (positionForSignal.Open != null)
|
||||||
{
|
{
|
||||||
positionForSignal.Open.SetStatus(TradeStatus.Filled);
|
positionForSignal.Open.SetStatus(TradeStatus.Filled);
|
||||||
|
// Update Open trade ExchangeOrderId if broker position has one
|
||||||
|
if (brokerPosition.Open?.ExchangeOrderId != null)
|
||||||
|
{
|
||||||
|
positionForSignal.Open.SetExchangeOrderId(brokerPosition.Open.ExchangeOrderId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await SetPositionStatus(signal.Identifier, PositionStatus.Filled);
|
await SetPositionStatus(signal.Identifier, PositionStatus.Filled);
|
||||||
|
|||||||
@@ -689,8 +689,8 @@ public class EvmManager : IEvmManager
|
|||||||
quantity,
|
quantity,
|
||||||
price,
|
price,
|
||||||
leverage ?? 1.0m,
|
leverage ?? 1.0m,
|
||||||
account.Key,
|
"", // ExchangeOrderId should be empty for requested trades
|
||||||
""
|
"" // Empty message for now
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
@@ -750,7 +750,7 @@ public class EvmManager : IEvmManager
|
|||||||
quantity,
|
quantity,
|
||||||
price,
|
price,
|
||||||
leverage ?? 1,
|
leverage ?? 1,
|
||||||
response.Hash,
|
response.Hash, // This should be the actual transaction hash/order ID
|
||||||
""
|
""
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -819,7 +819,7 @@ public class EvmManager : IEvmManager
|
|||||||
if (position == null)
|
if (position == null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
// TODO: Map the position object to a Trade object
|
// Map the position object to a Trade object
|
||||||
var trade = new Trade(
|
var trade = new Trade(
|
||||||
position.Date,
|
position.Date,
|
||||||
MiscExtensions.ParseEnum<TradeDirection>(position.Direction),
|
MiscExtensions.ParseEnum<TradeDirection>(position.Direction),
|
||||||
@@ -829,8 +829,8 @@ public class EvmManager : IEvmManager
|
|||||||
(decimal)position.Quantity,
|
(decimal)position.Quantity,
|
||||||
(decimal)position.Price,
|
(decimal)position.Price,
|
||||||
(decimal?)position.Leverage,
|
(decimal?)position.Leverage,
|
||||||
account.Key,
|
position.ExchangeOrderId, // Use the actual exchange order ID from the position
|
||||||
position.ExchangeOrderId
|
"" // Empty message for now
|
||||||
);
|
);
|
||||||
|
|
||||||
return trade;
|
return trade;
|
||||||
|
|||||||
@@ -171,9 +171,11 @@ internal static class GmxV2Mappers
|
|||||||
PositionInitiator.User,
|
PositionInitiator.User,
|
||||||
gmxPosition.Date,
|
gmxPosition.Date,
|
||||||
new User());
|
new User());
|
||||||
|
// For the Open trade, use the Open trade's ExchangeOrderId if available, otherwise use a new GUID
|
||||||
|
var openExchangeOrderId = gmxPosition.Open?.ExchangeOrderId ?? Guid.NewGuid().ToString();
|
||||||
position.Open = new Trade(position.Date, direction, TradeStatus.Filled, TradeType.Market, ticker,
|
position.Open = new Trade(position.Date, direction, TradeStatus.Filled, TradeType.Market, ticker,
|
||||||
(decimal)gmxPosition.Quantity, (decimal)gmxPosition.Price, (decimal)gmxPosition.Leverage,
|
(decimal)gmxPosition.Quantity, (decimal)gmxPosition.Price, (decimal)gmxPosition.Leverage,
|
||||||
gmxPosition.Open.ExchangeOrderId, "");
|
openExchangeOrderId, "");
|
||||||
|
|
||||||
if (gmxPosition.TakeProfit1 != null)
|
if (gmxPosition.TakeProfit1 != null)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user