#!/usr/bin/env ts-node /** * Place fresh TP1 + SL orders for existing position * Does NOT close or modify the position itself */ import { initializeDriftService } from '../lib/drift/client' import { placeExitOrders } from '../lib/drift/orders' import { getPythClient } from '../lib/pyth/client' async function placeExitOrdersForPosition() { try { console.log('šŸ”§ Initializing services...') // Initialize Drift const driftService = await initializeDriftService() const driftClient = driftService.getDriftClient() // Get current position const position = await driftService.getPosition(0) // SOL-PERP market index if (!position || position.baseAssetAmount.toString() === '0') { console.error('āŒ No open SOL-PERP position found') process.exit(1) } console.log(`āœ… Found position: ${position.baseAssetAmount.toString()} base units`) // Get current price const pythClient = getPythClient() const currentPrice = await pythClient.getPrice('SOL-PERP') console.log(`šŸ’° Current SOL price: $${currentPrice}`) // Position details from database const entryPrice = 131.7993991266376 const positionSizeUSD = 903.1306122 const tp1Price = 132.8537943196507 const stopLossPrice = 129.822408139738 const tp1SizePercent = 60 // Close 60% at TP1 console.log(`\nšŸ“Š Position Configuration:`) console.log(` Entry: $${entryPrice}`) console.log(` Size: $${positionSizeUSD} (${(positionSizeUSD / currentPrice).toFixed(2)} SOL)`) console.log(` TP1: $${tp1Price} (+${((tp1Price / entryPrice - 1) * 100).toFixed(2)}%)`) console.log(` SL: $${stopLossPrice} (${((stopLossPrice / entryPrice - 1) * 100).toFixed(2)}%)`) console.log(` TP1 will close: ${tp1SizePercent}%`) console.log(` Runner: ${100 - tp1SizePercent}% (software-monitored, no on-chain TP2)`) // Place orders console.log(`\nšŸ”Ø Placing exit orders...`) const result = await placeExitOrders({ symbol: 'SOL-PERP', direction: 'long', entryPrice: entryPrice, positionSizeUSD: positionSizeUSD, tp1Price: tp1Price, tp1SizePercent: tp1SizePercent, tp2Price: 134.171788310917, // Not used (tp2SizePercent = 0) tp2SizePercent: 0, // No TP2 order (runner only) stopLossPrice: stopLossPrice, useDualStops: false, // Single SL only }) if (result.success) { console.log(`\nāœ… Orders placed successfully!`) console.log(` Signatures:`, result.signatures) console.log(`\nšŸ“ Update database with these signatures:`) console.log(` tp1OrderTx: ${result.signatures[0]}`) console.log(` slOrderTx: ${result.signatures[1]}`) console.log(` tp2OrderTx: NULL (runner-only, no on-chain order)`) } else { console.error(`\nāŒ Failed to place orders:`, result.error) process.exit(1) } } catch (error) { console.error('āŒ Error:', error) process.exit(1) } } placeExitOrdersForPosition()