이익 실현 및 손실 제한 SDK
TPSL(Take Profit Stop Loss) SDK는 특정 가격 조건이 충족될 때 자동으로 실행되는 conditional order를 관리하기 위한 함수를 제공한다.
TPSL functions
DeepBook Margin SDK는 conditional order를 관리하기 위한 다음 함수를 제공한다.
addConditionalOrder
가격 조건이 충족될 때 실행되는 conditional order를 추가하려면 addConditionalOrder를 사용한다. 이 호출은 Transaction object를 받는 함수를 반환한다.
Parameters
marginManagerKey: margin manager를 식별하는 String이다.conditionalOrderId: 이 conditional order에 대한 고유 ID를 나타내는 Number이다.triggerBelowPrice: 가격이 trigger price 아래로 떨어질 때 trigger할지 여부를 나타내는 Boolean이다.triggerPrice: order가 trigger되는 price를 나타내는 Number이다.pendingOrder: order 세부 정보를 포함하는 Object이다(PendingLimitOrderParams또는PendingMarketOrderParams중 하나).
packages/deepbook-v3/src/transactions/marginTPSL.ts. You probably need to run `pnpm prebuild` and restart the site.cancelConditionalOrder
특정 conditional order를 취소하려면 cancelConditionalOrder를 사용한다. 이 호출은 Transaction object를 받는 함수를 반환한다.
Parameters
marginManagerKey: margin manager를 식별하는 String이다.conditionalOrderId: 취소할 conditional order의 ID를 나타내는 String이다.
packages/deepbook-v3/src/transactions/marginTPSL.ts. You probably need to run `pnpm prebuild` and restart the site.cancelAllConditionalOrders
margin manager에 대한 모든 conditional order를 취소하려면 cancelAllConditionalOrders를 사용한다. 이 호출은 Transaction object를 받는 함수를 반환한다.
Parameters
marginManagerKey: margin manager를 식별하는 String이다.
packages/deepbook-v3/src/transactions/marginTPSL.ts. You probably need to run `pnpm prebuild` and restart the site.executeConditionalOrders
trigger된 conditional order를 실행하려면 executeConditionalOrders를 사용한다. 이는 누구나 호출할 수 있는 permissionless 함수이다. 이 호출은 Transaction object를 받는 함수를 반환한다.
Parameters
marginManagerKey: margin manager를 식별하는 String이다.maxOrdersToExecute: 이 호출에서 실행할 최대 order 수를 나타내는 Number이다.
packages/deepbook-v3/src/transactions/marginTPSL.ts. You probably need to run `pnpm prebuild` and restart the site.Helper functions
이 helper 함수들은 conditional order에 대한 condition과 pending order를 생성한다.
newCondition
conditional order를 위한 trigger condition을 생성하려면 newCondition을 사용한다. 이 호출은 Transaction object를 받는 함수를 반환한다.
Parameters
poolKey: pool을 식별하는 String이다.triggerBelowPrice: 가격이 trigger price 아래로 떨어질 때 trigger할지 여부를 나타내는 Boolean이다.triggerPrice: trigger되는 price를 나타내는 Number이다.
packages/deepbook-v3/src/transactions/marginTPSL.ts. You probably need to run `pnpm prebuild` and restart the site.newPendingLimitOrder
conditional order에서 사용할 pending limit order를 생성하려면 newPendingLimitOrder를 사용한다. 이 호출은 Transaction object를 받는 함수를 반환한다.
Parameters
poolKey: pool을 식별하는 String이다.params: 다음을 포함하는PendingLimitOrderParamsobject이다:clientOrderId: client tracking을 위한 Number이다.orderType: optional order type이다(default:NO_RESTRICTION).selfMatchingOption: optional self-matching option이다(default:SELF_MATCHING_ALLOWED).price: limit price를 나타내는 Number이다.quantity: order quantity를 나타내는 Number이다.isBid: buy order인지 여부를 나타내는 Boolean이다.payWithDeep: fee payment를 위한 optional boolean이다(default:true).expireTimestamp: optional expiration timestamp이다.
packages/deepbook-v3/src/transactions/marginTPSL.ts. You probably need to run `pnpm prebuild` and restart the site.newPendingMarketOrder
conditional order에서 사용할 pending market order를 생성하려면 newPendingMarketOrder를 사용한다. 이 호출은 Transaction object를 받는 함수를 반환한다.
Parameters
poolKey: pool을 식별하는 String이다.params: 다음을 포함하는PendingMarketOrderParamsobject이 다:clientOrderId: client tracking을 위한 Number이다.selfMatchingOption: optional self-matching option이다(default:SELF_MATCHING_ALLOWED).quantity: order quantity를 나타내는 Number이다.isBid: buy order인지 여부를 나타내는 Boolean이다.payWithDeep: fee payment를 위한 optional boolean이다(default:true).
packages/deepbook-v3/src/transactions/marginTPSL.ts. You probably need to run `pnpm prebuild` and restart the site.Read-only functions
conditionalOrderIds
margin manager에 대한 모든 conditional order ID를 쿼리한다.
packages/deepbook-v3/src/transactions/marginTPSL.ts. You probably need to run `pnpm prebuild` and restart the site.conditionalOrder
ID로 특정 conditional order를 쿼리한다.
packages/deepbook-v3/src/transactions/marginTPSL.ts. You probably need to run `pnpm prebuild` and restart the site.triggerBelowOrders, triggerAboveOrders
trigger price로 정렬된 conditional order 목록을 쿼리한다.
packages/deepbook-v3/src/transactions/marginTPSL.ts. You probably need to run `pnpm prebuild` and restart the site.numConditionalOrders
margin manager에 대한 conditional order의 총 수를 쿼리한다.
packages/deepbook-v3/src/transactions/marginTPSL.ts. You probably need to run `pnpm prebuild` and restart the site.lowestTriggerAbovePrice, highestTriggerBelowPrice
conditional order의 trigger price를 쿼리한다.
packages/deepbook-v3/src/transactions/marginTPSL.ts. You probably need to run `pnpm prebuild` and restart the site.Examples
Set up a stop loss order
// 예시: 가격이 2.0 아래로 떨어지면 매도하는 stop loss order를 생성한다
const setStopLoss = (tx: Transaction) => {
const managerKey = 'MARGIN_MANAGER_1';
traderClient.marginTPSL.addConditionalOrder({
marginManagerKey: managerKey,
conditionalOrderId: 1,
triggerBelowPrice: true, // 가격이 아래로 내려가면 트리거한다
triggerPrice: 2.0,
pendingOrder: {
clientOrderId: 100,
quantity: 50,
isBid: false, // 매도 order
payWithDeep: true,
},
})(tx);
};
Set up a take profit order
// 예시: 가격이 5.0 위로 오르면 매도하는 take profit order를 생성한다
const setTakeProfit = (tx: Transaction) => {
const managerKey = 'MARGIN_MANAGER_1';
traderClient.marginTPSL.addConditionalOrder({
marginManagerKey: managerKey,
conditionalOrderId: 2,
triggerBelowPrice: false, // 가격이 위로 올라가면 트리거한다
triggerPrice: 5.0,
pendingOrder: {
clientOrderId: 101,
price: 5.0, // 5.0의 limit order
quantity: 50,
isBid: false, // 매도 order
payWithDeep: true,
},
})(tx);
};
Execute triggered orders (keeper)
// 예시: keeper로 conditional orders를 실행한다
const executeOrders = (tx: Transaction) => {
const managerKey = 'MARGIN_MANAGER_1';
// 최대 10개의 triggered order를 실행한다
traderClient.marginTPSL.executeConditionalOrders(managerKey, 10)(tx);
};