Fix price impact

This commit is contained in:
2025-05-28 01:04:58 +07:00
parent f743436634
commit 4e49f03199
6 changed files with 45 additions and 31 deletions

View File

@@ -429,7 +429,7 @@ public class DataController : ControllerBase
return new UserStrategyDetailsViewModel
{
Name = strategy.Name,
StrategyName = strategy.Config.ScenarioName,
ScenarioName = strategy.Config.ScenarioName,
State = strategy.GetStatus() == BotStatus.Up.ToString() ? "RUNNING" :
strategy.GetStatus() == BotStatus.Down.ToString() ? "STOPPED" : "UNUSED",
PnL = pnl,

View File

@@ -12,11 +12,6 @@ namespace Managing.Api.Models.Responses
/// </summary>
public string Name { get; set; }
/// <summary>
/// Strategy identifier
/// </summary>
public string StrategyName { get; set; }
/// <summary>
/// Current state of the strategy (RUNNING, STOPPED, UNUSED)
/// </summary>
@@ -73,5 +68,6 @@ namespace Managing.Api.Models.Responses
public List<Position> Positions { get; set; } = new List<Position>();
public string Identifier { get; set; }
public string ScenarioName { get; set; }
}
}

View File

@@ -72,6 +72,11 @@ public static class WorkersBootstrap
return services;
}
private static IServiceCollection AddWorkers(this IServiceCollection services, IConfiguration configuration)
{
return services;
}
private static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
{
// Database

View File

@@ -6,7 +6,7 @@ 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 {getIncreasePositionAmounts} from "../../utils/trade/amounts.js";
import {getAcceptablePriceInfo, getOrderThresholdType} from "../../utils/prices.js";
import {getAcceptablePriceInfo, getDefaultAcceptablePriceImpactBps, getOrderThresholdType} from "../../utils/prices.js";
import type {GmxSdk} from "../..";
import {createSwapEstimator, getMarketsGraph} from "../../utils/swap/swapRouting.js";
@@ -164,13 +164,24 @@ export async function increaseOrderHelper(
if (params.stopLossPrice) {
const stopLossCollateralDeltaUsd = convertToUsd(increaseAmounts.collateralDeltaAmount, collateralToken.decimals, params.stopLossPrice);
// Use a higher acceptable price impact buffer for stop loss orders to ensure execution
const stopLossAcceptablePriceImpactBps = params.fixedAcceptablePriceImpactBps ??
getDefaultAcceptablePriceImpactBps({
isIncrease: false,
isLong: params.isLong,
indexPrice: params.stopLossPrice,
sizeDeltaUsd: increaseAmounts.sizeDeltaUsd,
priceImpactDeltaUsd: 0n, // We'll calculate this based on current market conditions
acceptablePriceImapctBuffer: params.acceptablePriceImpactBuffer ?? 100, // Default 1% buffer for SL orders
});
const acceptablePriceInfo = getAcceptablePriceInfo({
marketInfo,
isIncrease: false,
isLong: params.isLong,
indexPrice: params.stopLossPrice,
sizeDeltaUsd: increaseAmounts.sizeDeltaUsd,
maxNegativePriceImpactBps: marketInfo.maxPositionImpactFactorForLiquidations,
maxNegativePriceImpactBps: stopLossAcceptablePriceImpactBps,
});
stopLossDecreaseAmounts = {
@@ -241,13 +252,24 @@ export async function increaseOrderHelper(
if (params.takeProfitPrice) {
const takeProfitCollateralDeltaUsd = convertToUsd(increaseAmounts.collateralDeltaAmount, collateralToken.decimals, params.takeProfitPrice);
// Use a higher acceptable price impact buffer for take profit orders to ensure execution
const takeProfitAcceptablePriceImpactBps = params.fixedAcceptablePriceImpactBps ??
getDefaultAcceptablePriceImpactBps({
isIncrease: false,
isLong: params.isLong,
indexPrice: params.takeProfitPrice,
sizeDeltaUsd: increaseAmounts.sizeDeltaUsd,
priceImpactDeltaUsd: 0n, // We'll calculate this based on current market conditions
acceptablePriceImapctBuffer: params.acceptablePriceImpactBuffer ?? 100, // Default 1% buffer for TP orders
});
const acceptablePriceInfo = getAcceptablePriceInfo({
marketInfo,
isIncrease: false,
isLong: params.isLong,
indexPrice: params.takeProfitPrice,
sizeDeltaUsd: increaseAmounts.sizeDeltaUsd,
maxNegativePriceImpactBps: marketInfo.maxPositionImpactFactorForLiquidations,
maxNegativePriceImpactBps: takeProfitAcceptablePriceImpactBps,
});
takeProfitDecreaseAmounts = {

View File

@@ -212,12 +212,13 @@ export const openGmxPositionImpl = async (
marketAddress: marketInfo.marketTokenAddress,
payTokenAddress: collateralToken.address,
collateralTokenAddress: collateralToken.address,
allowedSlippageBps: 50, // 0.5% slippage
allowedSlippageBps: 100, // 0.5% slippage
leverage: leverageBps,
skipSimulation: true,
referralCodeForTxn: encodeReferralCode("kaigen_ai"),
stopLossPrice: stopLossPrice ? numberToBigint(stopLossPrice, 30) : undefined,
takeProfitPrice: takeProfitPrice ? numberToBigint(takeProfitPrice, 30) : undefined
takeProfitPrice: takeProfitPrice ? numberToBigint(takeProfitPrice, 30) : undefined,
acceptablePriceImpactBuffer: 150,
};
if (direction === TradeDirection.Long) {

View File

@@ -1132,7 +1132,7 @@ export class DataClient extends AuthorizedApiBase {
return Promise.resolve<PlatformSummaryViewModel>(null as any);
}
data_GetAgentBalances(agentName: string | null | undefined, startDate: Date | undefined, endDate: Date | null | undefined): Promise<AgentBalance[]> {
data_GetAgentBalances(agentName: string | null | undefined, startDate: Date | undefined, endDate: Date | null | undefined): Promise<AgentBalanceHistory> {
let url_ = this.baseUrl + "/Data/GetAgentBalances?";
if (agentName !== undefined && agentName !== null)
url_ += "agentName=" + encodeURIComponent("" + agentName) + "&";
@@ -1158,13 +1158,13 @@ export class DataClient extends AuthorizedApiBase {
});
}
protected processData_GetAgentBalances(response: Response): Promise<AgentBalance[]> {
protected processData_GetAgentBalances(response: Response): Promise<AgentBalanceHistory> {
const status = response.status;
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
if (status === 200) {
return response.text().then((_responseText) => {
let result200: any = null;
result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as AgentBalance[];
result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as AgentBalanceHistory;
return result200;
});
} else if (status !== 200 && status !== 204) {
@@ -1172,7 +1172,7 @@ export class DataClient extends AuthorizedApiBase {
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
});
}
return Promise.resolve<AgentBalance[]>(null as any);
return Promise.resolve<AgentBalanceHistory>(null as any);
}
data_GetBestAgents(startDate: Date | undefined, endDate: Date | null | undefined, page: number | undefined, pageSize: number | undefined): Promise<BestAgentsResponse> {
@@ -3212,7 +3212,6 @@ export interface StrategyPerformance {
export interface UserStrategyDetailsViewModel {
name?: string | null;
strategyName?: string | null;
state?: string | null;
pnL?: number;
roiPercentage?: number;
@@ -3225,6 +3224,7 @@ export interface UserStrategyDetailsViewModel {
losses?: number;
positions?: Position[] | null;
identifier?: string | null;
scenarioName?: string | null;
}
export interface PlatformSummaryViewModel {
@@ -3251,6 +3251,11 @@ export interface AgentSummaryViewModel {
volumeLast24h?: number;
}
export interface AgentBalanceHistory {
agentName?: string | null;
agentBalances?: AgentBalance[] | null;
}
export interface AgentBalance {
agentName?: string | null;
totalValue?: number;
@@ -3268,21 +3273,6 @@ export interface BestAgentsResponse {
totalPages?: number;
}
export interface AgentBalanceHistory {
agentName?: string | null;
totalValue?: number;
totalAccountUsdValue?: number;
botsAllocationUsdValue?: number;
pnL?: number;
time?: Date;
pnLHistory?: PnLPoint[] | null;
}
export interface PnLPoint {
time?: Date;
value?: number;
}
export enum RiskLevel {
Low = "Low",
Medium = "Medium",