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

View File

@@ -53,7 +53,7 @@ public class ClosePositionCommandHandler(
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
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 {getByKey} from "../../utils/objects.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 {getAcceptablePriceInfo, getDefaultAcceptablePriceImpactBps, getOrderThresholdType} from "../../utils/prices.js";
@@ -184,6 +190,11 @@ export async function increaseOrderHelper(
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 = {
isFullClose: true,
sizeDeltaUsd: increaseAmounts.sizeDeltaUsd,
@@ -191,7 +202,7 @@ export async function increaseOrderHelper(
collateralDeltaUsd: stopLossCollateralDeltaUsd,
collateralDeltaAmount: increaseAmounts.collateralDeltaAmount,
indexPrice: params.stopLossPrice,
collateralPrice: 0n,
collateralPrice: stopLossCollateralPrice,
acceptablePrice: params.isLong ? 0n : ethers.MaxUint256,
acceptablePriceDeltaBps: acceptablePriceInfo.acceptablePriceDeltaBps,
recommendedAcceptablePriceDeltaBps: 30n,
@@ -272,6 +283,11 @@ export async function increaseOrderHelper(
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 = {
isFullClose: true,
sizeDeltaUsd: increaseAmounts.sizeDeltaUsd,
@@ -279,7 +295,7 @@ export async function increaseOrderHelper(
collateralDeltaUsd: takeProfitCollateralDeltaUsd,
collateralDeltaAmount: increaseAmounts.collateralDeltaAmount,
indexPrice: params.takeProfitPrice, // Keep original trigger price for indexPrice
collateralPrice: 0n, // Consider if this needs calculation
collateralPrice: takeProfitCollateralPrice,
acceptablePrice: acceptablePriceInfo.acceptablePrice,
acceptablePriceDeltaBps: acceptablePriceInfo.acceptablePriceDeltaBps, // Add 0.5% buffer to acceptable price impact
recommendedAcceptablePriceDeltaBps: 30n, // Set recommended buffer to 0.5%