|
|
|
|
@ -95,6 +95,8 @@ export const POST = async ({ locals, request }) => {
|
|
|
|
|
|
|
|
|
|
const { key, secret, symbol, hedge } = form.data
|
|
|
|
|
|
|
|
|
|
let makeOrder = true
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
let qty
|
|
|
|
|
let takeProfit
|
|
|
|
|
@ -190,10 +192,7 @@ export const POST = async ({ locals, request }) => {
|
|
|
|
|
).toFixed(decimalLength)
|
|
|
|
|
}
|
|
|
|
|
positionIdx = form.data.side === 'Buy' ? 1 : 2
|
|
|
|
|
} else if (
|
|
|
|
|
form.data.type === 'Close Position' ||
|
|
|
|
|
form.data.type === 'Close Position Compound'
|
|
|
|
|
) {
|
|
|
|
|
} else if (form.data.type === 'Close Position') {
|
|
|
|
|
const position = await client.getPositionInfo({
|
|
|
|
|
category: 'linear',
|
|
|
|
|
symbol: symbol,
|
|
|
|
|
@ -236,6 +235,54 @@ export const POST = async ({ locals, request }) => {
|
|
|
|
|
: ('Buy' as const)
|
|
|
|
|
qty = position.result.list[0].size
|
|
|
|
|
}
|
|
|
|
|
} else if (form.data.type === 'Close Position Compound') {
|
|
|
|
|
const position = await client.getPositionInfo({
|
|
|
|
|
category: 'linear',
|
|
|
|
|
symbol: symbol,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
console.log('closing')
|
|
|
|
|
console.log(position.result.list)
|
|
|
|
|
if (hedge === 'true') {
|
|
|
|
|
const closeSide = form.data.side
|
|
|
|
|
if (closeSide === undefined) {
|
|
|
|
|
makeOrder = false
|
|
|
|
|
} else {
|
|
|
|
|
if (position.result.list.length <= 0) {
|
|
|
|
|
makeOrder = false
|
|
|
|
|
} else {
|
|
|
|
|
const order = position.result.list.find(
|
|
|
|
|
(result) => result.side === closeSide,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if (order) {
|
|
|
|
|
side =
|
|
|
|
|
order.side === 'Buy'
|
|
|
|
|
? ('Sell' as const)
|
|
|
|
|
: ('Buy' as const)
|
|
|
|
|
qty = order.size
|
|
|
|
|
|
|
|
|
|
positionIdx = order.side === 'Buy' ? 1 : 2
|
|
|
|
|
} else {
|
|
|
|
|
makeOrder = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (position.result.list.length <= 0) {
|
|
|
|
|
makeOrder = false
|
|
|
|
|
} else {
|
|
|
|
|
if (position.result.list[0].side === 'None') {
|
|
|
|
|
makeOrder = false
|
|
|
|
|
} else {
|
|
|
|
|
side =
|
|
|
|
|
position.result.list[0].side === 'Buy'
|
|
|
|
|
? ('Sell' as const)
|
|
|
|
|
: ('Buy' as const)
|
|
|
|
|
qty = position.result.list[0].size
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
form.data.type
|
|
|
|
|
const current_stack = await db.query.current_stack.findFirst()
|
|
|
|
|
@ -256,19 +303,39 @@ export const POST = async ({ locals, request }) => {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log({ qty, takeProfit, stopLoss })
|
|
|
|
|
const order = await client.submitOrder({
|
|
|
|
|
category: 'linear',
|
|
|
|
|
symbol,
|
|
|
|
|
side,
|
|
|
|
|
orderType: 'Market',
|
|
|
|
|
qty,
|
|
|
|
|
takeProfit: takeProfit,
|
|
|
|
|
stopLoss: stopLoss,
|
|
|
|
|
isLeverage: isLeverage ? 1 : 0,
|
|
|
|
|
positionIdx: hedge !== undefined ? positionIdx : undefined,
|
|
|
|
|
})
|
|
|
|
|
if (makeOrder) {
|
|
|
|
|
const order = await client.submitOrder({
|
|
|
|
|
category: 'linear',
|
|
|
|
|
symbol,
|
|
|
|
|
side: side!,
|
|
|
|
|
orderType: 'Market',
|
|
|
|
|
qty: qty!,
|
|
|
|
|
takeProfit: takeProfit,
|
|
|
|
|
stopLoss: stopLoss,
|
|
|
|
|
isLeverage: isLeverage ? 1 : 0,
|
|
|
|
|
positionIdx: hedge !== undefined ? positionIdx : undefined,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
console.log('Order:', order)
|
|
|
|
|
console.log('Order:', order)
|
|
|
|
|
|
|
|
|
|
await db.insert(bybit_logs).values({
|
|
|
|
|
status: order.retCode === 0 ? 'success' : 'failed',
|
|
|
|
|
request: form.data,
|
|
|
|
|
payload: {
|
|
|
|
|
category: 'linear',
|
|
|
|
|
symbol,
|
|
|
|
|
side,
|
|
|
|
|
orderType: 'Market',
|
|
|
|
|
qty,
|
|
|
|
|
takeProfit: side === 'Buy' ? takeProfit : stopLoss,
|
|
|
|
|
stopLoss: side === 'Buy' ? stopLoss : takeProfit,
|
|
|
|
|
isLeverage: isLeverage ? 1 : 0,
|
|
|
|
|
positionIdx: hedge !== undefined ? positionIdx : undefined,
|
|
|
|
|
},
|
|
|
|
|
response: order,
|
|
|
|
|
createdAt: Date.now(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (form.data.type === 'Close Position Compound') {
|
|
|
|
|
console.log('Close Position, checking for compound...')
|
|
|
|
|
@ -333,23 +400,6 @@ export const POST = async ({ locals, request }) => {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await db.insert(bybit_logs).values({
|
|
|
|
|
status: order.retCode === 0 ? 'success' : 'failed',
|
|
|
|
|
request: form.data,
|
|
|
|
|
payload: {
|
|
|
|
|
category: 'linear',
|
|
|
|
|
symbol,
|
|
|
|
|
side,
|
|
|
|
|
orderType: 'Market',
|
|
|
|
|
qty,
|
|
|
|
|
takeProfit: side === 'Buy' ? takeProfit : stopLoss,
|
|
|
|
|
stopLoss: side === 'Buy' ? stopLoss : takeProfit,
|
|
|
|
|
isLeverage: isLeverage ? 1 : 0,
|
|
|
|
|
positionIdx: hedge !== undefined ? positionIdx : undefined,
|
|
|
|
|
},
|
|
|
|
|
response: order,
|
|
|
|
|
createdAt: Date.now(),
|
|
|
|
|
})
|
|
|
|
|
return new Response()
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log('Error', error)
|
|
|
|
|
|