Fix a bit the spot trading
This commit is contained in:
@@ -29,32 +29,11 @@ public class CloseSpotPositionCommandHandler(
|
||||
? TradeDirection.Short
|
||||
: TradeDirection.Long;
|
||||
|
||||
// For spot trading, determine swap direction for closing
|
||||
// Long position: Swap Token -> USDC (sell token for USDC)
|
||||
// Short position: Swap USDC -> Token (buy token with USDC)
|
||||
Ticker fromTicker;
|
||||
Ticker toTicker;
|
||||
double swapAmount;
|
||||
|
||||
if (request.Position.OriginDirection == TradeDirection.Long)
|
||||
{
|
||||
fromTicker = request.Position.Ticker;
|
||||
toTicker = Ticker.USDC;
|
||||
swapAmount = (double)request.Position.Open.Quantity;
|
||||
}
|
||||
else
|
||||
{
|
||||
fromTicker = Ticker.USDC;
|
||||
toTicker = request.Position.Ticker;
|
||||
// For short, we need to calculate how much USDC to swap back
|
||||
// This should be the original amount + profit/loss
|
||||
var originalAmount = request.Position.Open.Price * request.Position.Open.Quantity;
|
||||
swapAmount = (double)originalAmount;
|
||||
}
|
||||
|
||||
// For backtest/paper trading, simulate the swap without calling the exchange
|
||||
SwapInfos swapResult;
|
||||
if (request.Position.TradingType == TradingType.BacktestSpot)
|
||||
var isForBacktest = request.Position.TradingType == TradingType.BacktestSpot;
|
||||
|
||||
if (isForBacktest)
|
||||
{
|
||||
// Simulate successful swap for backtest
|
||||
swapResult = new SwapInfos
|
||||
@@ -71,9 +50,9 @@ public class CloseSpotPositionCommandHandler(
|
||||
swapResult = await tradingService.SwapGmxTokensAsync(
|
||||
request.Position.User,
|
||||
account.Name,
|
||||
fromTicker,
|
||||
toTicker,
|
||||
swapAmount,
|
||||
request.Position.Ticker,
|
||||
Ticker.USDC,
|
||||
(double)request.Position.Open.Quantity,
|
||||
"market",
|
||||
null,
|
||||
0.5);
|
||||
@@ -81,7 +60,8 @@ public class CloseSpotPositionCommandHandler(
|
||||
|
||||
if (!swapResult.Success)
|
||||
{
|
||||
throw new InvalidOperationException($"Failed to close spot position: {swapResult.Error ?? swapResult.Message}");
|
||||
throw new InvalidOperationException(
|
||||
$"Failed to close spot position: {swapResult.Error ?? swapResult.Message}");
|
||||
}
|
||||
|
||||
// Build the closing trade directly for backtest (no exchange call needed)
|
||||
@@ -107,7 +87,10 @@ public class CloseSpotPositionCommandHandler(
|
||||
request.Position.AddUiFees(closingUiFees);
|
||||
request.Position.AddGasFees(Constants.GMX.Config.GasFeePerTransaction);
|
||||
|
||||
// For backtest, skip database update
|
||||
if (!isForBacktest)
|
||||
{
|
||||
await tradingService.UpdatePositionAsync(request.Position);
|
||||
}
|
||||
|
||||
return request.Position;
|
||||
}
|
||||
@@ -120,5 +103,4 @@ public class CloseSpotPositionCommandHandler(
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user