Fix collateral price + remove close orders

This commit is contained in:
2025-05-28 12:39:25 +07:00
parent 4e49f03199
commit a31dff3f22
3 changed files with 36 additions and 14 deletions

View File

@@ -193,7 +193,7 @@ public class TradingBot : Bot, ITradingBot
UpdateStrategiesValues(); UpdateStrategiesValues();
} }
await UpdateWalletBalances(); UpdateWalletBalances();
if (OptimizedCandles.Count % 100 == 0) // Log every 10th execution if (OptimizedCandles.Count % 100 == 0) // Log every 10th execution
{ {
Logger.LogInformation($"Candle date : {OptimizedCandles.Last().Date:u}"); Logger.LogInformation($"Candle date : {OptimizedCandles.Last().Date:u}");
@@ -289,28 +289,34 @@ public class TradingBot : Bot, ITradingBot
private async Task ManagePositions() private async Task ManagePositions()
{ {
// Update position // Update positions - iterate through positions instead of signals for better synchronization
foreach (var signal in Signals.Where(s => s.Status == SignalStatus.PositionOpen)) foreach (var position in Positions.Where(p => !p.IsFinished()))
{ {
var positionForSignal = Positions.FirstOrDefault(p => p.SignalIdentifier == signal.Identifier); var signalForPosition = Signals.FirstOrDefault(s => s.Identifier == position.SignalIdentifier);
if (positionForSignal == null) if (signalForPosition == null)
{ {
await LogInformation($"Cannot find position for signal {signal.Identifier}"); await LogWarning($"Cannot find signal for position {position.Identifier}");
} continue;
else
{
await UpdatePosition(signal, positionForSignal);
}
} }
// Open position for signal waiting for a position open // Ensure signal status is correctly set to PositionOpen if position is not finished
if (signalForPosition.Status != SignalStatus.PositionOpen)
{
await LogInformation($"Updating signal {signalForPosition.Identifier} status from {signalForPosition.Status} to PositionOpen");
SetSignalStatus(signalForPosition.Identifier, SignalStatus.PositionOpen);
}
await UpdatePosition(signalForPosition, position);
}
// Open position for signals waiting for a position open
foreach (var signal in Signals.Where(s => s.Status == SignalStatus.WaitingForPosition)) foreach (var signal in Signals.Where(s => s.Status == SignalStatus.WaitingForPosition))
{ {
Task.Run(() => OpenPosition(signal)).GetAwaiter().GetResult(); Task.Run(() => OpenPosition(signal)).GetAwaiter().GetResult();
} }
} }
private async Task UpdateWalletBalances() private void UpdateWalletBalances()
{ {
var lastCandle = OptimizedCandles.LastOrDefault(); var lastCandle = OptimizedCandles.LastOrDefault();
if (lastCandle == null) return; if (lastCandle == null) return;

View File

@@ -53,7 +53,7 @@ public class ClosePositionCommandHandler(
var closeRequestedOrders = var closeRequestedOrders =
isForPaperTrading || (await exchangeService.CancelOrder(account, request.Position.Ticker)); true; // TODO: For gmx no need to close orders since they are closed automatically
// Close market // Close market
var closedPosition = var closedPosition =

View File

@@ -4,7 +4,13 @@ import {TokenData, TokensData, TokensRatio} from "../../types/tokens.js";
import {DecreasePositionAmounts, FindSwapPath, SwapAmounts} from "../../types/trade.js"; import {DecreasePositionAmounts, FindSwapPath, SwapAmounts} from "../../types/trade.js";
import {getByKey} from "../../utils/objects.js"; import {getByKey} from "../../utils/objects.js";
import {createFindSwapPath, findAllSwapPaths, getWrappedAddress} from "../../utils/swap/swapPath.js"; import {createFindSwapPath, findAllSwapPaths, getWrappedAddress} from "../../utils/swap/swapPath.js";
import {convertToUsd, getIsUnwrap, getIsWrap, getTokensRatioByPrice} from "../../utils/tokens.js"; import {
convertToUsd,
getIsEquivalentTokens,
getIsUnwrap,
getIsWrap,
getTokensRatioByPrice
} from "../../utils/tokens.js";
import {getIncreasePositionAmounts} from "../../utils/trade/amounts.js"; import {getIncreasePositionAmounts} from "../../utils/trade/amounts.js";
import {getAcceptablePriceInfo, getDefaultAcceptablePriceImpactBps, getOrderThresholdType} from "../../utils/prices.js"; import {getAcceptablePriceInfo, getDefaultAcceptablePriceImpactBps, getOrderThresholdType} from "../../utils/prices.js";
@@ -184,6 +190,11 @@ export async function increaseOrderHelper(
maxNegativePriceImpactBps: stopLossAcceptablePriceImpactBps, maxNegativePriceImpactBps: stopLossAcceptablePriceImpactBps,
}); });
// Calculate collateral price based on GMX UI logic
const stopLossCollateralPrice = getIsEquivalentTokens(marketInfo.indexToken, collateralToken)
? params.stopLossPrice // Use trigger price if index token equals collateral token
: collateralToken.prices.minPrice; // Otherwise use collateral token's min price
stopLossDecreaseAmounts = { stopLossDecreaseAmounts = {
isFullClose: true, isFullClose: true,
sizeDeltaUsd: increaseAmounts.sizeDeltaUsd, sizeDeltaUsd: increaseAmounts.sizeDeltaUsd,
@@ -191,7 +202,7 @@ export async function increaseOrderHelper(
collateralDeltaUsd: stopLossCollateralDeltaUsd, collateralDeltaUsd: stopLossCollateralDeltaUsd,
collateralDeltaAmount: increaseAmounts.collateralDeltaAmount, collateralDeltaAmount: increaseAmounts.collateralDeltaAmount,
indexPrice: params.stopLossPrice, indexPrice: params.stopLossPrice,
collateralPrice: 0n, collateralPrice: stopLossCollateralPrice,
acceptablePrice: params.isLong ? 0n : ethers.MaxUint256, acceptablePrice: params.isLong ? 0n : ethers.MaxUint256,
acceptablePriceDeltaBps: acceptablePriceInfo.acceptablePriceDeltaBps, acceptablePriceDeltaBps: acceptablePriceInfo.acceptablePriceDeltaBps,
recommendedAcceptablePriceDeltaBps: 30n, recommendedAcceptablePriceDeltaBps: 30n,
@@ -272,6 +283,11 @@ export async function increaseOrderHelper(
maxNegativePriceImpactBps: takeProfitAcceptablePriceImpactBps, maxNegativePriceImpactBps: takeProfitAcceptablePriceImpactBps,
}); });
// Calculate collateral price based on GMX UI logic
const takeProfitCollateralPrice = getIsEquivalentTokens(marketInfo.indexToken, collateralToken)
? params.takeProfitPrice // Use trigger price if index token equals collateral token
: collateralToken.prices.minPrice; // Otherwise use collateral token's min price
takeProfitDecreaseAmounts = { takeProfitDecreaseAmounts = {
isFullClose: true, isFullClose: true,
sizeDeltaUsd: increaseAmounts.sizeDeltaUsd, sizeDeltaUsd: increaseAmounts.sizeDeltaUsd,
@@ -279,7 +295,7 @@ export async function increaseOrderHelper(
collateralDeltaUsd: takeProfitCollateralDeltaUsd, collateralDeltaUsd: takeProfitCollateralDeltaUsd,
collateralDeltaAmount: increaseAmounts.collateralDeltaAmount, collateralDeltaAmount: increaseAmounts.collateralDeltaAmount,
indexPrice: params.takeProfitPrice, // Keep original trigger price for indexPrice indexPrice: params.takeProfitPrice, // Keep original trigger price for indexPrice
collateralPrice: 0n, // Consider if this needs calculation collateralPrice: takeProfitCollateralPrice,
acceptablePrice: acceptablePriceInfo.acceptablePrice, acceptablePrice: acceptablePriceInfo.acceptablePrice,
acceptablePriceDeltaBps: acceptablePriceInfo.acceptablePriceDeltaBps, // Add 0.5% buffer to acceptable price impact acceptablePriceDeltaBps: acceptablePriceInfo.acceptablePriceDeltaBps, // Add 0.5% buffer to acceptable price impact
recommendedAcceptablePriceDeltaBps: 30n, // Set recommended buffer to 0.5% recommendedAcceptablePriceDeltaBps: 30n, // Set recommended buffer to 0.5%