From 21eb3460617eb54a257d208589fc508066624ead Mon Sep 17 00:00:00 2001 From: TZGyn Date: Fri, 14 Feb 2025 01:30:51 +0800 Subject: [PATCH] add full amount order if has active order + add custom decimal points for certain coins --- src/routes/webhook/tradingview/+server.ts | 37 ++++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/routes/webhook/tradingview/+server.ts b/src/routes/webhook/tradingview/+server.ts index 9146ed6..b729c1e 100644 --- a/src/routes/webhook/tradingview/+server.ts +++ b/src/routes/webhook/tradingview/+server.ts @@ -57,6 +57,29 @@ export const POST = async ({ locals, request }) => { demoTrading: true, }) + const orders = await client.getActiveOrders({ + category: 'linear', + symbol, + }) + + const amount = orders.result.list.reduce( + (acc, curr) => acc + Number(curr.qty), + 0, + ) + + if (amount > 0) { + const order = await client.submitOrder({ + category: 'linear', + symbol, + side, + orderType: 'Market', + qty: amount.toString(), + takeProfit, + stopLoss, + }) + return new Response() + } + if (form.data.type === 'Flat') { qty = form.data.qty takeProfit = form.data.takeProfit @@ -77,19 +100,18 @@ export const POST = async ({ locals, request }) => { Number(form.data.qtyPercent)) / 100 / Number(wallet.result.list[0].totalAvailableBalance) - ).toFixed(2) + ).toFixed(getDecimalPoint(symbol.slice(0, -4))) takeProfit = ( Number(form.data.entryPrice) * (1 + Number(form.data.takeProfitPercent) / 100) - ).toFixed(2) + ).toFixed(1) stopLoss = ( Number(form.data.entryPrice) * (1 - Number(form.data.stopLossPercent) / 100) - ).toFixed(2) + ).toFixed(1) } console.log({ qty, takeProfit, stopLoss }) - const order = await client.submitOrder({ category: 'linear', symbol, @@ -99,9 +121,16 @@ export const POST = async ({ locals, request }) => { takeProfit, stopLoss, }) + return new Response() } catch (error) { console.log('Error', error) } finally { return new Response() } } + +const getDecimalPoint = (symbol: string) => { + if (symbol === 'BTC') return 3 + if (symbol === 'ETH') return 2 + return 1 +}