본문으로 건너뛰기

이익 실현 및 손실 제한 SDK

TPSL(Take Profit Stop Loss) SDK는 특정 가격 조건이 충족될 때 자동으로 실행되는 conditional 주문을 관리하기 위한 함수를 제공한다.

TPSL 함수

DeepBook Margin SDK는 conditional 주문을 관리하기 위한 다음 함수를 제공한다.

addConditionalOrder

가격 조건이 충족될 때 실행되는 conditional 주문을 추가하려면 addConditionalOrder를 사용한다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • marginManagerKey: margin manager를 식별하는 String이다.
  • conditionalOrderId: 이 conditional 주문에 대한 고유 ID를 나타내는 Number이다.
  • triggerBelowPrice: 가격이 trigger price 아래로 떨어질 때 trigger할지 여부를 나타내는 Boolean이다.
  • triggerPrice: 주문이 trigger되는 price를 나타내는 Number이다.
  • pendingOrder: 주문 세부 정보를 포함하는 Object이다(PendingLimitOrderParams 또는 PendingMarketOrderParams 중 하나).

cancelConditionalOrder

특정 conditional 주문을 취소하려면 cancelConditionalOrder를 사용한다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • marginManagerKey: margin manager를 식별하는 String이다.
  • conditionalOrderId: 취소할 conditional 주문의 ID를 나타내는 String이다.

cancelAllConditionalOrders

margin manager에 대한 모든 conditional 주문을 취소하려면 cancelAllConditionalOrders를 사용한다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • marginManagerKey: margin manager를 식별하는 String이다.

executeConditionalOrders

trigger된 conditional 주문을 실행하려면 executeConditionalOrders를 사용한다. 이는 누구나 호출할 수 있는 permissionless 함수가다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • managerAddress: 실행할 주문을 가진 margin manager의 주소를 나타내는 String이다.
  • poolKey: DeepBook 풀을 식별하는 String이다.
  • maxOrdersToExecute: 이 호출에서 실행할 최대 주문 수를 나타내는 Number이다.

helper 함수

이 helper 함수들은 conditional 주문에 대한 condition과 pending 주문을 생성한다.

newCondition

conditional 주문을 위한 trigger condition을 생성하려면 newCondition을 사용한다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • poolKey: 풀을 식별하는 String이다.
  • triggerBelowPrice: 가격이 trigger price 아래로 떨어질 때 trigger할지 여부를 나타내는 Boolean이다.
  • triggerPrice: trigger되는 price를 나타내는 Number이다.

newPendingLimitOrder

conditional 주문에서 사용할 pending 지정가 주문을 생성하려면 newPendingLimitOrder를 사용한다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • poolKey: 풀을 식별하는 String이다.
  • params: 다음을 포함하는 PendingLimitOrderParams 객체이다:
    • clientOrderId: 클라이언트 tracking을 위한 Number이다.
    • orderType: optional 주문 type이다(기본값: NO_RESTRICTION).
    • selfMatchingOption: optional self-matching option이다(기본값: SELF_MATCHING_ALLOWED).
    • price: limit price를 나타내는 Number이다.
    • quantity: 주문 quantity를 나타내는 Number이다.
    • isBid: buy 주문인지 여부를 나타내는 Boolean이다.
    • payWithDeep: fee payment를 위한 optional boolean이다(기본값: true).
    • expireTimestamp: optional expiration 타임스탬프이다.

newPendingMarketOrder

conditional 주문에서 사용할 pending 시장가 주문을 생성하려면 newPendingMarketOrder를 사용한다. 이 호출은 Transaction 객체를 받는 함수를 반환한다.

매개변수

  • poolKey: 풀을 식별하는 String이다.
  • params: 다음을 포함하는 PendingMarketOrderParams 객체이다:
    • clientOrderId: 클라이언트 tracking을 위한 Number이다.
    • selfMatchingOption: optional self-matching option이다(기본값: SELF_MATCHING_ALLOWED).
    • quantity: 주문 quantity를 나타내는 Number이다.
    • isBid: buy 주문인지 여부를 나타내는 Boolean이다.
    • payWithDeep: fee payment를 위한 optional boolean이다(기본값: true).

read-only 함수

conditionalOrderIds

margin manager에 대한 모든 conditional 주문 ID를 쿼리한다.

conditionalOrder

ID로 특정 conditional 주문을 쿼리한다.

lowestTriggerAbovePrice, highestTriggerBelowPrice

conditional 주문의 trigger price를 쿼리한다. lowestTriggerAbovePrice는 trigger-above 주문 중 가장 낮은 trigger price를 반환하며, 없으면 max_u64를 반환한다. highestTriggerBelowPrice는 trigger-below 주문 중 가장 높은 trigger price를 반환하며, 없으면 0을 반환한다.

예시

stop loss 주문 설정

// Example: Create a stop loss order that sells when price drops below 2.0
const setStopLoss = (tx: Transaction) => {
const managerKey = 'MARGIN_MANAGER_1';
traderClient.marginTPSL.addConditionalOrder({
marginManagerKey: managerKey,
conditionalOrderId: 1,
triggerBelowPrice: true, // Trigger when price falls below
triggerPrice: 2.0,
pendingOrder: {
clientOrderId: 100,
quantity: 50,
isBid: false, // Sell order
payWithDeep: true,
},
})(tx);
};

take profit 주문 설정

// Example: Create a take profit order that sells when price rises above 5.0
const setTakeProfit = (tx: Transaction) => {
const managerKey = 'MARGIN_MANAGER_1';
traderClient.marginTPSL.addConditionalOrder({
marginManagerKey: managerKey,
conditionalOrderId: 2,
triggerBelowPrice: false, // Trigger when price rises above
triggerPrice: 5.0,
pendingOrder: {
clientOrderId: 101,
price: 5.0, // Limit order at 5.0
quantity: 50,
isBid: false, // Sell order
payWithDeep: true,
},
})(tx);
};

trigger된 주문 실행(keeper)

// Example: Execute conditional orders as a keeper
const executeOrders = (tx: Transaction) => {
const managerAddress = '0x...'; // Address of margin manager
// Execute up to 10 triggered orders
traderClient.marginTPSL.executeConditionalOrders(managerAddress, 'SUI_USDC', 10)(tx);
};