mirror of
https://github.com/discountry/ritmex-bot.git
synced 2026-09-11 09:18:08 +00:00
refactor: extract shared order handler factory, deduplicate 8 order.ts files
Created src/exchanges/order-handlers.ts with createOrderHandlers() factory. All 8 exchange order.ts files now use config-driven delegation instead of duplicated ~80-line implementations. Shared applyCommonFields() logic consolidated into the factory module.
This commit is contained in:
@@ -1,101 +1,17 @@
|
|||||||
import type { Order, CreateOrderParams } from "../types";
|
import { createOrderHandlers } from "../order-handlers";
|
||||||
import type {
|
|
||||||
BaseOrderIntent,
|
|
||||||
ClosePositionIntent,
|
|
||||||
LimitOrderIntent,
|
|
||||||
MarketOrderIntent,
|
|
||||||
StopOrderIntent,
|
|
||||||
TrailingStopOrderIntent,
|
|
||||||
} from "../order-schema";
|
|
||||||
import { toStringBoolean } from "../order-schema";
|
|
||||||
|
|
||||||
function applyCommonFields(params: CreateOrderParams, intent: BaseOrderIntent): CreateOrderParams {
|
const handlers = createOrderHandlers({
|
||||||
if (params.quantity === undefined) {
|
exchangeName: "Aster",
|
||||||
params.quantity = intent.quantity;
|
defaultLimitTimeInForce: "GTX",
|
||||||
}
|
supportsTrailingStop: true,
|
||||||
if (params.timeInForce === undefined && intent.timeInForce) {
|
supportsTriggerType: true,
|
||||||
params.timeInForce = intent.timeInForce;
|
});
|
||||||
}
|
|
||||||
if (intent.reduceOnly !== undefined) {
|
|
||||||
params.reduceOnly = toStringBoolean(intent.reduceOnly);
|
|
||||||
}
|
|
||||||
if (intent.closePosition !== undefined) {
|
|
||||||
params.closePosition = toStringBoolean(intent.closePosition);
|
|
||||||
}
|
|
||||||
return params;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createLimitOrder(intent: LimitOrderIntent): Promise<Order> {
|
export const {
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
createLimitOrder,
|
||||||
{
|
createMarketOrder,
|
||||||
symbol: intent.symbol,
|
createStopOrder,
|
||||||
side: intent.side,
|
createTrailingStopOrder,
|
||||||
type: "LIMIT",
|
createClosePositionOrder,
|
||||||
quantity: intent.quantity,
|
} = handlers;
|
||||||
price: intent.price,
|
|
||||||
timeInForce: intent.timeInForce ?? "GTX",
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createMarketOrder(intent: MarketOrderIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createStopOrder(intent: StopOrderIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "STOP_MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
stopPrice: intent.stopPrice,
|
|
||||||
timeInForce: intent.timeInForce ?? "GTC",
|
|
||||||
triggerType: intent.triggerType,
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createTrailingStopOrder(intent: TrailingStopOrderIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "TRAILING_STOP_MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
activationPrice: intent.activationPrice,
|
|
||||||
callbackRate: intent.callbackRate,
|
|
||||||
timeInForce: intent.timeInForce ?? "GTC",
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createClosePositionOrder(intent: ClosePositionIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
reduceOnly: "true",
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,84 +1,16 @@
|
|||||||
import type { Order, CreateOrderParams } from "../types";
|
import { createOrderHandlers } from "../order-handlers";
|
||||||
import type {
|
|
||||||
BaseOrderIntent,
|
|
||||||
ClosePositionIntent,
|
|
||||||
LimitOrderIntent,
|
|
||||||
MarketOrderIntent,
|
|
||||||
StopOrderIntent,
|
|
||||||
TrailingStopOrderIntent,
|
|
||||||
} from "../order-schema";
|
|
||||||
import { toStringBoolean } from "../order-schema";
|
|
||||||
|
|
||||||
function applyCommonFields(params: CreateOrderParams, intent: BaseOrderIntent): CreateOrderParams {
|
const handlers = createOrderHandlers({
|
||||||
if (params.quantity === undefined) {
|
exchangeName: "Backpack",
|
||||||
params.quantity = intent.quantity;
|
defaultLimitTimeInForce: "GTX",
|
||||||
}
|
supportsTrailingStop: false,
|
||||||
if (params.timeInForce === undefined && intent.timeInForce) {
|
supportsTriggerType: false,
|
||||||
params.timeInForce = intent.timeInForce;
|
});
|
||||||
}
|
|
||||||
if (intent.reduceOnly !== undefined) {
|
|
||||||
params.reduceOnly = toStringBoolean(intent.reduceOnly);
|
|
||||||
}
|
|
||||||
return params;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createLimitOrder(intent: LimitOrderIntent): Promise<Order> {
|
export const {
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
createLimitOrder,
|
||||||
{
|
createMarketOrder,
|
||||||
symbol: intent.symbol,
|
createStopOrder,
|
||||||
side: intent.side,
|
createTrailingStopOrder,
|
||||||
type: "LIMIT",
|
createClosePositionOrder,
|
||||||
quantity: intent.quantity,
|
} = handlers;
|
||||||
price: intent.price,
|
|
||||||
timeInForce: intent.timeInForce ?? "GTX",
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createMarketOrder(intent: MarketOrderIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createStopOrder(intent: StopOrderIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "STOP_MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
stopPrice: intent.stopPrice,
|
|
||||||
timeInForce: intent.timeInForce ?? "GTC",
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createTrailingStopOrder(_intent: TrailingStopOrderIntent): Promise<Order> {
|
|
||||||
throw new Error("Backpack exchange does not support trailing stop orders");
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createClosePositionOrder(intent: ClosePositionIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
reduceOnly: "true",
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,100 +1,16 @@
|
|||||||
import type { Order, CreateOrderParams } from "../types";
|
import { createOrderHandlers } from "../order-handlers";
|
||||||
import type {
|
|
||||||
BaseOrderIntent,
|
|
||||||
ClosePositionIntent,
|
|
||||||
LimitOrderIntent,
|
|
||||||
MarketOrderIntent,
|
|
||||||
StopOrderIntent,
|
|
||||||
TrailingStopOrderIntent,
|
|
||||||
} from "../order-schema";
|
|
||||||
import { toStringBoolean } from "../order-schema";
|
|
||||||
|
|
||||||
function applyCommonFields(params: CreateOrderParams, intent: BaseOrderIntent): CreateOrderParams {
|
const handlers = createOrderHandlers({
|
||||||
if (params.quantity === undefined) {
|
exchangeName: "Binance",
|
||||||
params.quantity = intent.quantity;
|
defaultLimitTimeInForce: "GTX",
|
||||||
}
|
supportsTrailingStop: true,
|
||||||
if (params.timeInForce === undefined && intent.timeInForce) {
|
supportsTriggerType: true,
|
||||||
params.timeInForce = intent.timeInForce;
|
});
|
||||||
}
|
|
||||||
if (intent.reduceOnly !== undefined) {
|
|
||||||
params.reduceOnly = toStringBoolean(intent.reduceOnly);
|
|
||||||
}
|
|
||||||
if (intent.closePosition !== undefined) {
|
|
||||||
params.closePosition = toStringBoolean(intent.closePosition);
|
|
||||||
}
|
|
||||||
return params;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createLimitOrder(intent: LimitOrderIntent): Promise<Order> {
|
export const {
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
createLimitOrder,
|
||||||
{
|
createMarketOrder,
|
||||||
symbol: intent.symbol,
|
createStopOrder,
|
||||||
side: intent.side,
|
createTrailingStopOrder,
|
||||||
type: "LIMIT",
|
createClosePositionOrder,
|
||||||
quantity: intent.quantity,
|
} = handlers;
|
||||||
price: intent.price,
|
|
||||||
timeInForce: intent.timeInForce ?? "GTX",
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createMarketOrder(intent: MarketOrderIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createStopOrder(intent: StopOrderIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "STOP_MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
stopPrice: intent.stopPrice,
|
|
||||||
timeInForce: intent.timeInForce ?? "GTC",
|
|
||||||
triggerType: intent.triggerType,
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createTrailingStopOrder(intent: TrailingStopOrderIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "TRAILING_STOP_MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
activationPrice: intent.activationPrice,
|
|
||||||
callbackRate: intent.callbackRate,
|
|
||||||
timeInForce: intent.timeInForce ?? "GTC",
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createClosePositionOrder(intent: ClosePositionIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
reduceOnly: "true",
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|||||||
+18
-89
@@ -1,92 +1,21 @@
|
|||||||
import type { Order, CreateOrderParams } from "../types";
|
import { createOrderHandlers } from "../order-handlers";
|
||||||
import type {
|
|
||||||
BaseOrderIntent,
|
|
||||||
ClosePositionIntent,
|
|
||||||
LimitOrderIntent,
|
|
||||||
MarketOrderIntent,
|
|
||||||
StopOrderIntent,
|
|
||||||
TrailingStopOrderIntent,
|
|
||||||
} from "../order-schema";
|
|
||||||
import { toStringBoolean } from "../order-schema";
|
|
||||||
|
|
||||||
function applyCommonFields(params: CreateOrderParams, intent: BaseOrderIntent): CreateOrderParams {
|
const handlers = createOrderHandlers({
|
||||||
if (params.quantity === undefined) {
|
exchangeName: "GRVT",
|
||||||
params.quantity = intent.quantity;
|
defaultLimitTimeInForce: "GTX",
|
||||||
}
|
supportsTrailingStop: false,
|
||||||
if (params.timeInForce === undefined && intent.timeInForce) {
|
supportsTriggerType: true,
|
||||||
params.timeInForce = intent.timeInForce;
|
defaultStopTriggerType: "STOP_LOSS",
|
||||||
}
|
stopDefaultReduceOnly: true,
|
||||||
if (intent.reduceOnly !== undefined) {
|
stopDefaultClosePosition: true,
|
||||||
params.reduceOnly = toStringBoolean(intent.reduceOnly);
|
closeDefaultClosePosition: true,
|
||||||
}
|
});
|
||||||
if (intent.closePosition !== undefined) {
|
|
||||||
params.closePosition = toStringBoolean(intent.closePosition);
|
|
||||||
}
|
|
||||||
return params;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createLimitOrder(intent: LimitOrderIntent): Promise<Order> {
|
export const {
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
createLimitOrder,
|
||||||
{
|
createMarketOrder,
|
||||||
symbol: intent.symbol,
|
createStopOrder,
|
||||||
side: intent.side,
|
createTrailingStopOrder,
|
||||||
type: "LIMIT",
|
createClosePositionOrder,
|
||||||
quantity: intent.quantity,
|
} = handlers;
|
||||||
price: intent.price,
|
|
||||||
timeInForce: intent.timeInForce ?? "GTX",
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createMarketOrder(intent: MarketOrderIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createStopOrder(intent: StopOrderIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "STOP_MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
stopPrice: intent.stopPrice,
|
|
||||||
timeInForce: intent.timeInForce ?? "GTC",
|
|
||||||
triggerType: intent.triggerType ?? "STOP_LOSS",
|
|
||||||
closePosition: toStringBoolean(intent.closePosition ?? true),
|
|
||||||
reduceOnly: toStringBoolean(intent.reduceOnly ?? true),
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createTrailingStopOrder(_intent: TrailingStopOrderIntent): Promise<Order> {
|
|
||||||
throw new Error("GRVT exchange does not support trailing stop orders");
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createClosePositionOrder(intent: ClosePositionIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
reduceOnly: "true",
|
|
||||||
closePosition: toStringBoolean(intent.closePosition ?? true),
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,93 +1,22 @@
|
|||||||
import type { Order, CreateOrderParams } from "../types";
|
import { createOrderHandlers } from "../order-handlers";
|
||||||
import type {
|
|
||||||
BaseOrderIntent,
|
|
||||||
ClosePositionIntent,
|
|
||||||
LimitOrderIntent,
|
|
||||||
MarketOrderIntent,
|
|
||||||
StopOrderIntent,
|
|
||||||
TrailingStopOrderIntent,
|
|
||||||
} from "../order-schema";
|
|
||||||
import { toStringBoolean } from "../order-schema";
|
|
||||||
|
|
||||||
function applyCommonFields(params: CreateOrderParams, intent: BaseOrderIntent): CreateOrderParams {
|
const handlers = createOrderHandlers({
|
||||||
if (params.quantity === undefined) {
|
exchangeName: "Lighter",
|
||||||
params.quantity = intent.quantity;
|
defaultLimitTimeInForce: "GTC",
|
||||||
}
|
defaultMarketTimeInForce: "IOC",
|
||||||
if (params.timeInForce === undefined && intent.timeInForce) {
|
defaultCloseTimeInForce: "IOC",
|
||||||
params.timeInForce = intent.timeInForce;
|
supportsTrailingStop: false,
|
||||||
}
|
supportsTriggerType: false,
|
||||||
if (intent.reduceOnly !== undefined) {
|
stopDefaultReduceOnly: true,
|
||||||
params.reduceOnly = toStringBoolean(intent.reduceOnly);
|
stopDefaultClosePosition: true,
|
||||||
}
|
closeDefaultClosePosition: true,
|
||||||
if (intent.closePosition !== undefined) {
|
});
|
||||||
params.closePosition = toStringBoolean(intent.closePosition);
|
|
||||||
}
|
|
||||||
return params;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createLimitOrder(intent: LimitOrderIntent): Promise<Order> {
|
export const {
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
createLimitOrder,
|
||||||
{
|
createMarketOrder,
|
||||||
symbol: intent.symbol,
|
createStopOrder,
|
||||||
side: intent.side,
|
createTrailingStopOrder,
|
||||||
type: "LIMIT",
|
createClosePositionOrder,
|
||||||
quantity: intent.quantity,
|
} = handlers;
|
||||||
price: intent.price,
|
|
||||||
timeInForce: intent.timeInForce ?? "GTC",
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createMarketOrder(intent: MarketOrderIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
timeInForce: intent.timeInForce ?? "IOC",
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createStopOrder(intent: StopOrderIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "STOP_MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
stopPrice: intent.stopPrice,
|
|
||||||
timeInForce: intent.timeInForce ?? "GTC",
|
|
||||||
reduceOnly: toStringBoolean(intent.reduceOnly ?? true),
|
|
||||||
closePosition: toStringBoolean(intent.closePosition ?? true),
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createTrailingStopOrder(_intent: TrailingStopOrderIntent): Promise<Order> {
|
|
||||||
throw new Error("Lighter exchange does not support trailing stop orders");
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createClosePositionOrder(intent: ClosePositionIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
reduceOnly: "true",
|
|
||||||
closePosition: toStringBoolean(intent.closePosition ?? true),
|
|
||||||
timeInForce: intent.timeInForce ?? "IOC",
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
+19
-90
@@ -1,93 +1,22 @@
|
|||||||
import type { Order, CreateOrderParams } from "../types";
|
import { createOrderHandlers } from "../order-handlers";
|
||||||
import type {
|
|
||||||
BaseOrderIntent,
|
|
||||||
ClosePositionIntent,
|
|
||||||
LimitOrderIntent,
|
|
||||||
MarketOrderIntent,
|
|
||||||
StopOrderIntent,
|
|
||||||
TrailingStopOrderIntent,
|
|
||||||
} from "../order-schema";
|
|
||||||
import { toStringBoolean } from "../order-schema";
|
|
||||||
|
|
||||||
function applyCommonFields(params: CreateOrderParams, intent: BaseOrderIntent): CreateOrderParams {
|
const handlers = createOrderHandlers({
|
||||||
if (params.quantity === undefined) {
|
exchangeName: "Nado",
|
||||||
params.quantity = intent.quantity;
|
defaultLimitTimeInForce: "GTC",
|
||||||
}
|
defaultMarketTimeInForce: "IOC",
|
||||||
if (params.timeInForce === undefined && intent.timeInForce) {
|
defaultCloseTimeInForce: "IOC",
|
||||||
params.timeInForce = intent.timeInForce;
|
supportsTrailingStop: false,
|
||||||
}
|
supportsTriggerType: false,
|
||||||
if (intent.reduceOnly !== undefined) {
|
stopDefaultReduceOnly: true,
|
||||||
params.reduceOnly = toStringBoolean(intent.reduceOnly);
|
stopDefaultClosePosition: true,
|
||||||
}
|
closeDefaultClosePosition: true,
|
||||||
if (intent.closePosition !== undefined) {
|
});
|
||||||
params.closePosition = toStringBoolean(intent.closePosition);
|
|
||||||
}
|
|
||||||
return params;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createLimitOrder(intent: LimitOrderIntent): Promise<Order> {
|
export const {
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
createLimitOrder,
|
||||||
{
|
createMarketOrder,
|
||||||
symbol: intent.symbol,
|
createStopOrder,
|
||||||
side: intent.side,
|
createTrailingStopOrder,
|
||||||
type: "LIMIT",
|
createClosePositionOrder,
|
||||||
quantity: intent.quantity,
|
} = handlers;
|
||||||
price: intent.price,
|
|
||||||
timeInForce: intent.timeInForce ?? "GTC",
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createMarketOrder(intent: MarketOrderIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
timeInForce: intent.timeInForce ?? "IOC",
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createStopOrder(intent: StopOrderIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "STOP_MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
stopPrice: intent.stopPrice,
|
|
||||||
timeInForce: intent.timeInForce ?? "GTC",
|
|
||||||
reduceOnly: toStringBoolean(intent.reduceOnly ?? true),
|
|
||||||
closePosition: toStringBoolean(intent.closePosition ?? true),
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createTrailingStopOrder(_intent: TrailingStopOrderIntent): Promise<Order> {
|
|
||||||
throw new Error("Nado exchange does not support trailing stop orders");
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createClosePositionOrder(intent: ClosePositionIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
reduceOnly: "true",
|
|
||||||
closePosition: toStringBoolean(intent.closePosition ?? true),
|
|
||||||
timeInForce: intent.timeInForce ?? "IOC",
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,144 @@
|
|||||||
|
import type { Order, CreateOrderParams, TimeInForce } from "./types";
|
||||||
|
import type {
|
||||||
|
BaseOrderIntent,
|
||||||
|
ClosePositionIntent,
|
||||||
|
LimitOrderIntent,
|
||||||
|
MarketOrderIntent,
|
||||||
|
StopOrderIntent,
|
||||||
|
TrailingStopOrderIntent,
|
||||||
|
} from "./order-schema";
|
||||||
|
import { toStringBoolean } from "./order-schema";
|
||||||
|
|
||||||
|
export interface OrderHandlerConfig {
|
||||||
|
exchangeName: string;
|
||||||
|
defaultLimitTimeInForce: TimeInForce | "GTX";
|
||||||
|
defaultMarketTimeInForce?: TimeInForce;
|
||||||
|
defaultCloseTimeInForce?: TimeInForce;
|
||||||
|
defaultStopTimeInForce?: TimeInForce;
|
||||||
|
defaultStopTriggerType?: "UNSPECIFIED" | "TAKE_PROFIT" | "STOP_LOSS";
|
||||||
|
supportsTrailingStop: boolean;
|
||||||
|
supportsTriggerType: boolean;
|
||||||
|
stopDefaultReduceOnly?: boolean;
|
||||||
|
stopDefaultClosePosition?: boolean;
|
||||||
|
closeDefaultClosePosition?: boolean;
|
||||||
|
stopPriceAsPrice?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface OrderHandlers {
|
||||||
|
createLimitOrder(intent: LimitOrderIntent): Promise<Order>;
|
||||||
|
createMarketOrder(intent: MarketOrderIntent): Promise<Order>;
|
||||||
|
createStopOrder(intent: StopOrderIntent): Promise<Order>;
|
||||||
|
createTrailingStopOrder(intent: TrailingStopOrderIntent): Promise<Order>;
|
||||||
|
createClosePositionOrder(intent: ClosePositionIntent): Promise<Order>;
|
||||||
|
}
|
||||||
|
|
||||||
|
function applyCommonFields(params: CreateOrderParams, intent: BaseOrderIntent): CreateOrderParams {
|
||||||
|
if (params.quantity === undefined) {
|
||||||
|
params.quantity = intent.quantity;
|
||||||
|
}
|
||||||
|
if (params.timeInForce === undefined && intent.timeInForce) {
|
||||||
|
params.timeInForce = intent.timeInForce;
|
||||||
|
}
|
||||||
|
if (intent.reduceOnly !== undefined) {
|
||||||
|
params.reduceOnly = toStringBoolean(intent.reduceOnly);
|
||||||
|
}
|
||||||
|
if (intent.closePosition !== undefined) {
|
||||||
|
params.closePosition = toStringBoolean(intent.closePosition);
|
||||||
|
}
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createOrderHandlers(config: OrderHandlerConfig): OrderHandlers {
|
||||||
|
return {
|
||||||
|
async createLimitOrder(intent: LimitOrderIntent): Promise<Order> {
|
||||||
|
const params: CreateOrderParams = applyCommonFields(
|
||||||
|
{
|
||||||
|
symbol: intent.symbol,
|
||||||
|
side: intent.side,
|
||||||
|
type: "LIMIT",
|
||||||
|
quantity: intent.quantity,
|
||||||
|
price: intent.price,
|
||||||
|
timeInForce: intent.timeInForce ?? config.defaultLimitTimeInForce,
|
||||||
|
slPrice: intent.slPrice,
|
||||||
|
tpPrice: intent.tpPrice,
|
||||||
|
},
|
||||||
|
intent,
|
||||||
|
);
|
||||||
|
return intent.adapter.createOrder(params);
|
||||||
|
},
|
||||||
|
|
||||||
|
async createMarketOrder(intent: MarketOrderIntent): Promise<Order> {
|
||||||
|
const params: CreateOrderParams = applyCommonFields(
|
||||||
|
{
|
||||||
|
symbol: intent.symbol,
|
||||||
|
side: intent.side,
|
||||||
|
type: "MARKET",
|
||||||
|
quantity: intent.quantity,
|
||||||
|
timeInForce: intent.timeInForce ?? config.defaultMarketTimeInForce,
|
||||||
|
},
|
||||||
|
intent,
|
||||||
|
);
|
||||||
|
return intent.adapter.createOrder(params);
|
||||||
|
},
|
||||||
|
|
||||||
|
async createStopOrder(intent: StopOrderIntent): Promise<Order> {
|
||||||
|
const params: CreateOrderParams = applyCommonFields(
|
||||||
|
{
|
||||||
|
symbol: intent.symbol,
|
||||||
|
side: intent.side,
|
||||||
|
type: "STOP_MARKET",
|
||||||
|
quantity: intent.quantity,
|
||||||
|
stopPrice: intent.stopPrice,
|
||||||
|
timeInForce: intent.timeInForce ?? (config.defaultStopTimeInForce ?? "GTC"),
|
||||||
|
triggerType: config.supportsTriggerType ? (intent.triggerType ?? config.defaultStopTriggerType) : undefined,
|
||||||
|
price: config.stopPriceAsPrice ? intent.stopPrice : undefined,
|
||||||
|
},
|
||||||
|
intent,
|
||||||
|
);
|
||||||
|
if (config.stopDefaultReduceOnly && intent.reduceOnly === undefined) {
|
||||||
|
params.reduceOnly = "true";
|
||||||
|
}
|
||||||
|
if (config.stopDefaultClosePosition && intent.closePosition === undefined) {
|
||||||
|
params.closePosition = "true";
|
||||||
|
}
|
||||||
|
return intent.adapter.createOrder(params);
|
||||||
|
},
|
||||||
|
|
||||||
|
async createTrailingStopOrder(intent: TrailingStopOrderIntent): Promise<Order> {
|
||||||
|
if (!config.supportsTrailingStop) {
|
||||||
|
throw new Error(`${config.exchangeName} does not support trailing stop orders`);
|
||||||
|
}
|
||||||
|
const params: CreateOrderParams = applyCommonFields(
|
||||||
|
{
|
||||||
|
symbol: intent.symbol,
|
||||||
|
side: intent.side,
|
||||||
|
type: "TRAILING_STOP_MARKET",
|
||||||
|
quantity: intent.quantity,
|
||||||
|
activationPrice: intent.activationPrice,
|
||||||
|
callbackRate: intent.callbackRate,
|
||||||
|
timeInForce: intent.timeInForce ?? "GTC",
|
||||||
|
},
|
||||||
|
intent,
|
||||||
|
);
|
||||||
|
return intent.adapter.createOrder(params);
|
||||||
|
},
|
||||||
|
|
||||||
|
async createClosePositionOrder(intent: ClosePositionIntent): Promise<Order> {
|
||||||
|
const params: CreateOrderParams = applyCommonFields(
|
||||||
|
{
|
||||||
|
symbol: intent.symbol,
|
||||||
|
side: intent.side,
|
||||||
|
type: "MARKET",
|
||||||
|
quantity: intent.quantity,
|
||||||
|
reduceOnly: "true",
|
||||||
|
timeInForce: intent.timeInForce ?? config.defaultCloseTimeInForce,
|
||||||
|
},
|
||||||
|
intent,
|
||||||
|
);
|
||||||
|
if (config.closeDefaultClosePosition && intent.closePosition === undefined) {
|
||||||
|
params.closePosition = "true";
|
||||||
|
}
|
||||||
|
return intent.adapter.createOrder(params);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,93 +1,20 @@
|
|||||||
import type { Order, CreateOrderParams } from "../types";
|
import { createOrderHandlers } from "../order-handlers";
|
||||||
import type {
|
|
||||||
BaseOrderIntent,
|
|
||||||
ClosePositionIntent,
|
|
||||||
LimitOrderIntent,
|
|
||||||
MarketOrderIntent,
|
|
||||||
StopOrderIntent,
|
|
||||||
TrailingStopOrderIntent,
|
|
||||||
} from "../order-schema";
|
|
||||||
import { toStringBoolean } from "../order-schema";
|
|
||||||
|
|
||||||
function applyCommonFields(params: CreateOrderParams, intent: BaseOrderIntent): CreateOrderParams {
|
const handlers = createOrderHandlers({
|
||||||
if (params.quantity === undefined) {
|
exchangeName: "Paradex",
|
||||||
params.quantity = intent.quantity;
|
defaultLimitTimeInForce: "GTC",
|
||||||
}
|
supportsTrailingStop: false,
|
||||||
if (params.timeInForce === undefined && intent.timeInForce) {
|
supportsTriggerType: false,
|
||||||
params.timeInForce = intent.timeInForce;
|
stopDefaultReduceOnly: true,
|
||||||
}
|
stopDefaultClosePosition: true,
|
||||||
if (intent.reduceOnly !== undefined) {
|
stopPriceAsPrice: true,
|
||||||
params.reduceOnly = toStringBoolean(intent.reduceOnly);
|
closeDefaultClosePosition: true,
|
||||||
}
|
});
|
||||||
if (intent.closePosition !== undefined) {
|
|
||||||
params.closePosition = toStringBoolean(intent.closePosition);
|
|
||||||
}
|
|
||||||
return params;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createLimitOrder(intent: LimitOrderIntent): Promise<Order> {
|
export const {
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
createLimitOrder,
|
||||||
{
|
createMarketOrder,
|
||||||
symbol: intent.symbol,
|
createStopOrder,
|
||||||
side: intent.side,
|
createTrailingStopOrder,
|
||||||
type: "LIMIT",
|
createClosePositionOrder,
|
||||||
quantity: intent.quantity,
|
} = handlers;
|
||||||
price: intent.price,
|
|
||||||
timeInForce: intent.timeInForce ?? "GTC",
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createMarketOrder(intent: MarketOrderIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
timeInForce: intent.timeInForce,
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createStopOrder(intent: StopOrderIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "STOP_MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
stopPrice: intent.stopPrice,
|
|
||||||
price: intent.stopPrice,
|
|
||||||
timeInForce: intent.timeInForce ?? "GTC",
|
|
||||||
reduceOnly: toStringBoolean(intent.reduceOnly ?? true),
|
|
||||||
closePosition: toStringBoolean(intent.closePosition ?? true),
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createTrailingStopOrder(_intent: TrailingStopOrderIntent): Promise<Order> {
|
|
||||||
throw new Error("Paradex exchange does not support trailing stop orders");
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createClosePositionOrder(intent: ClosePositionIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
reduceOnly: "true",
|
|
||||||
closePosition: toStringBoolean(intent.closePosition ?? true),
|
|
||||||
timeInForce: intent.timeInForce,
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,94 +1,21 @@
|
|||||||
import type { Order, CreateOrderParams } from "../types";
|
import { createOrderHandlers } from "../order-handlers";
|
||||||
import type {
|
|
||||||
BaseOrderIntent,
|
|
||||||
ClosePositionIntent,
|
|
||||||
LimitOrderIntent,
|
|
||||||
MarketOrderIntent,
|
|
||||||
StopOrderIntent,
|
|
||||||
TrailingStopOrderIntent,
|
|
||||||
} from "../order-schema";
|
|
||||||
import { toStringBoolean } from "../order-schema";
|
|
||||||
|
|
||||||
function applyCommonFields(params: CreateOrderParams, intent: BaseOrderIntent): CreateOrderParams {
|
const handlers = createOrderHandlers({
|
||||||
if (params.quantity === undefined) {
|
exchangeName: "StandX",
|
||||||
params.quantity = intent.quantity;
|
defaultLimitTimeInForce: "GTX",
|
||||||
}
|
defaultMarketTimeInForce: "IOC",
|
||||||
if (params.timeInForce === undefined && intent.timeInForce) {
|
defaultCloseTimeInForce: "IOC",
|
||||||
params.timeInForce = intent.timeInForce;
|
supportsTrailingStop: false,
|
||||||
}
|
supportsTriggerType: false,
|
||||||
if (intent.reduceOnly !== undefined) {
|
stopDefaultReduceOnly: true,
|
||||||
params.reduceOnly = toStringBoolean(intent.reduceOnly);
|
stopDefaultClosePosition: true,
|
||||||
}
|
closeDefaultClosePosition: true,
|
||||||
if (intent.closePosition !== undefined) {
|
});
|
||||||
params.closePosition = toStringBoolean(intent.closePosition);
|
|
||||||
}
|
|
||||||
return params;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createLimitOrder(intent: LimitOrderIntent): Promise<Order> {
|
export const {
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
createLimitOrder,
|
||||||
{
|
createMarketOrder,
|
||||||
symbol: intent.symbol,
|
createStopOrder,
|
||||||
side: intent.side,
|
createTrailingStopOrder,
|
||||||
type: "LIMIT",
|
createClosePositionOrder,
|
||||||
quantity: intent.quantity,
|
} = handlers;
|
||||||
price: intent.price,
|
|
||||||
timeInForce: intent.timeInForce ?? "GTX",
|
|
||||||
slPrice: intent.slPrice,
|
|
||||||
tpPrice: intent.tpPrice,
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createMarketOrder(intent: MarketOrderIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
timeInForce: intent.timeInForce ?? "IOC",
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createStopOrder(intent: StopOrderIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "STOP_MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
stopPrice: intent.stopPrice,
|
|
||||||
timeInForce: intent.timeInForce ?? "GTC",
|
|
||||||
reduceOnly: toStringBoolean(intent.reduceOnly ?? true),
|
|
||||||
closePosition: toStringBoolean(intent.closePosition ?? true),
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createTrailingStopOrder(_intent: TrailingStopOrderIntent): Promise<Order> {
|
|
||||||
throw new Error("StandX exchange does not support trailing stop orders");
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createClosePositionOrder(intent: ClosePositionIntent): Promise<Order> {
|
|
||||||
const params: CreateOrderParams = applyCommonFields(
|
|
||||||
{
|
|
||||||
symbol: intent.symbol,
|
|
||||||
side: intent.side,
|
|
||||||
type: "MARKET",
|
|
||||||
quantity: intent.quantity,
|
|
||||||
reduceOnly: "true",
|
|
||||||
closePosition: toStringBoolean(intent.closePosition ?? true),
|
|
||||||
timeInForce: intent.timeInForce ?? "IOC",
|
|
||||||
},
|
|
||||||
intent
|
|
||||||
);
|
|
||||||
return intent.adapter.createOrder(params);
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user