Skip to content

Commit f60a151

Browse files
committed
trades as async
1 parent 1e1fc42 commit f60a151

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

src/controller/trades_controller.ts

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,54 @@ export class TradesController extends BaseController {
1212
}
1313

1414
private async getTradesData() {
15-
const openOrders: any[] = [];
16-
const profilePositions: any[] = [];
17-
18-
// Fetch open orders and swap/futures positions from all profiles
19-
const profiles = this.profileService.getProfiles();
20-
for (const profile of profiles) {
21-
if (!profile.apiKey || !profile.secret) {
22-
continue;
23-
}
24-
25-
// Fetch open orders
26-
try {
27-
const orders = await this.profileService.fetchOpenOrders(profile.id);
28-
29-
orders.forEach((order: OrderInfo) => {
30-
openOrders.push({
15+
// Fetch open orders and swap/futures positions from all profiles in parallel
16+
const profiles = this.profileService.getProfiles().filter(p => p.apiKey && p.secret);
17+
18+
// Create all fetch promises
19+
const fetchPromises = profiles.flatMap(profile => [
20+
this.profileService
21+
.fetchOpenOrders(profile.id)
22+
.then(orders =>
23+
orders.map((order: OrderInfo) => ({
3124
profileId: profile.id,
3225
profileName: profile.name,
3326
exchange: profile.exchange,
3427
order
35-
});
36-
});
37-
} catch (e) {
38-
console.log(`Failed to fetch orders for profile ${profile.name}: ${String(e)}`);
39-
}
40-
41-
// Fetch swap/futures positions
42-
try {
43-
const ppos = await this.profileService.fetchOpenPositions(profile.id);
44-
45-
ppos.forEach((position: PositionInfo) => {
46-
profilePositions.push({
28+
}))
29+
)
30+
.catch(e => {
31+
console.log(`Failed to fetch orders for profile ${profile.name}: ${String(e)}`);
32+
return [];
33+
}),
34+
this.profileService
35+
.fetchOpenPositions(profile.id)
36+
.then(positions =>
37+
positions.map((position: PositionInfo) => ({
4738
profileId: profile.id,
4839
profileName: profile.name,
4940
exchange: profile.exchange,
5041
position
51-
});
52-
});
53-
} catch (e) {
54-
console.log(`Failed to fetch positions for profile ${profile.name}: ${String(e)}`);
42+
}))
43+
)
44+
.catch(e => {
45+
console.log(`Failed to fetch positions for profile ${profile.name}: ${String(e)}`);
46+
return [];
47+
})
48+
]);
49+
50+
// Execute all requests in parallel
51+
const results = await Promise.all(fetchPromises);
52+
53+
// Separate orders and positions from results (alternating in array)
54+
const openOrders: any[] = [];
55+
const profilePositions: any[] = [];
56+
results.forEach((result, index) => {
57+
if (index % 2 === 0) {
58+
openOrders.push(...result);
59+
} else {
60+
profilePositions.push(...result);
5561
}
56-
}
62+
});
5763

5864
// Sort by timestamp descending
5965
openOrders.sort((a, b) => (b.order.timestamp || 0) - (a.order.timestamp || 0));
@@ -85,11 +91,7 @@ export class TradesController extends BaseController {
8591
const { symbol, type } = req.body;
8692

8793
try {
88-
await this.profileService.closePosition(
89-
profileId,
90-
decodeURIComponent(symbol),
91-
type as 'limit' | 'market'
92-
);
94+
await this.profileService.closePosition(profileId, decodeURIComponent(symbol), type as 'limit' | 'market');
9395
} catch (e) {
9496
console.log(`Failed to close position ${symbol} for profile ${profileId}: ${String(e)}`);
9597
}

0 commit comments

Comments
 (0)