52 lines
2.0 KiB
TypeScript
52 lines
2.0 KiB
TypeScript
import * as dotenv from 'dotenv';
|
|
import path from 'path';
|
|
import { AlpacaConnector } from '../src/connectors/alpaca.js';
|
|
import { ExecutionManager } from '../src/services/executionManager.js';
|
|
import { SignalDirection } from '../src/strategies/rules/types.js';
|
|
|
|
async function testExitFlow() {
|
|
console.log('--- Alpaca Exit Flow Test Start ---');
|
|
dotenv.config({ path: path.resolve(process.cwd(), '.env') });
|
|
|
|
const connector = new AlpacaConnector();
|
|
const manager = new ExecutionManager(connector);
|
|
|
|
const symbol = 'BTC/USD';
|
|
const qty = 0.001; // Tiny amount for testing
|
|
|
|
try {
|
|
// 1. Place a fresh BUY order to ensure we have something to exit
|
|
console.log(`Step 1: Placing initial BUY order for ${symbol}...`);
|
|
const buyOrder = await connector.placeOrder(symbol, 'buy', qty, 'market');
|
|
|
|
if (!buyOrder || !buyOrder.id) {
|
|
throw new Error('Failed to place initial buy order');
|
|
}
|
|
console.log(`✅ Buy order placed: ${buyOrder.id}`);
|
|
|
|
// 2. Mock the ExecutionManager state so it thinks it's in a trade
|
|
console.log('Step 2: Syncing local ExecutionManager state...');
|
|
// We simulate the post-buy state
|
|
(manager as any).activeTraders.set(symbol, {
|
|
side: SignalDirection.BUY,
|
|
entryPrice: 95000, // Dummy price
|
|
size: qty,
|
|
stopLoss: 94000,
|
|
takeProfit: 97000
|
|
});
|
|
|
|
// 3. Trigger the EXIT logic (simulating a TP/SL hit)
|
|
console.log('Step 3: Triggering automated EXIT logic (executeExit)...');
|
|
// We pass the current dummy price to simulate the exit
|
|
await manager.executeExit(symbol, 97000, 'Test Take Profit Hit');
|
|
|
|
console.log('✅ EXIT logic completed successfully.');
|
|
} catch (error: any) {
|
|
console.log(`❌ FAILED: ${error.message || error}`);
|
|
}
|
|
|
|
console.log('--- Alpaca Exit Flow Test End ---');
|
|
}
|
|
|
|
testExitFlow().catch(console.error);
|