-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-orders.js
More file actions
228 lines (195 loc) · 8.41 KB
/
test-orders.js
File metadata and controls
228 lines (195 loc) · 8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
const TopstepXClient = require('./src/api/topstepx-client');
require('dotenv').config();
async function testOrders() {
const client = new TopstepXClient();
console.log('=== TopstepX Order Testing ===\n');
try {
// Step 1: Authenticate
console.log('Step 1: Authenticating...');
await client.authenticate();
console.log('✓ Authentication successful\n');
// Step 2: Get active accounts
console.log('Step 2: Fetching active accounts...');
const accountsResponse = await client.getAccounts(true);
if (!accountsResponse.accounts || accountsResponse.accounts.length === 0) {
console.error('❌ No active accounts found. Cannot test orders.');
process.exit(1);
}
const tradableAccounts = accountsResponse.accounts.filter(acc => acc.canTrade);
if (tradableAccounts.length === 0) {
console.error('❌ No tradable accounts found.');
process.exit(1);
}
// Find practice account (should have "PRAC" in the name)
const practiceAccount = tradableAccounts.find(acc =>
acc.name && acc.name.includes('PRAC')
);
if (!practiceAccount) {
console.error('❌ No practice account found (looking for "PRAC" in account name).');
console.log('Available accounts:', tradableAccounts.map(a => `${a.name} (ID: ${a.id})`).join(', '));
process.exit(1);
}
const testAccount = practiceAccount;
console.log(`✓ Using PRACTICE account: ${testAccount.name} (ID: ${testAccount.id})`);
console.log(` Balance: $${testAccount.balance?.toLocaleString() || 'N/A'}`);
console.log(` Simulated: ${testAccount.simulated ? 'Yes ✓' : 'No'}\n`);
// Step 3: Get current contracts
console.log('Step 3: Fetching MNQ and MES contracts...');
const contracts = await client.getCurrentFuturesContracts(['MNQ', 'MES']);
if (!contracts['MNQ']) {
console.error('❌ MNQ contract not found. Cannot test orders.');
process.exit(1);
}
if (!contracts['MES']) {
console.error('❌ MES contract not found. Cannot test orders.');
process.exit(1);
}
const mnqContract = contracts['MNQ'];
const mesContract = contracts['MES'];
console.log(`✓ MNQ Contract: ${mnqContract.id}`);
console.log(` Tick Size: ${mnqContract.tickSize}`);
console.log(` Tick Value: $${mnqContract.tickValue}`);
console.log(`✓ MES Contract: ${mesContract.id}`);
console.log(` Tick Size: ${mesContract.tickSize}`);
console.log(` Tick Value: $${mesContract.tickValue}\n`);
// Step 4: Get current prices
console.log('Step 4: Fetching current prices...');
const now = new Date();
const oneMinuteAgo = new Date(now.getTime() - 60 * 1000);
const mnqData = await client.getHistoricalData(
mnqContract.id,
oneMinuteAgo.toISOString(),
now.toISOString(),
2, // Minute
1, // 1 minute
1, // 1 bar
true // Include partial bar
);
const mesData = await client.getHistoricalData(
mesContract.id,
oneMinuteAgo.toISOString(),
now.toISOString(),
2, // Minute
1, // 1 minute
1, // 1 bar
true // Include partial bar
);
if (!mnqData || mnqData.length === 0) {
console.error('❌ Could not fetch current MNQ price data.');
process.exit(1);
}
if (!mesData || mesData.length === 0) {
console.error('❌ Could not fetch current MES price data.');
process.exit(1);
}
const mnqPrice = mnqData[0].c;
const mesPrice = mesData[0].c;
console.log(`✓ Current MNQ Price: ${mnqPrice}`);
console.log(`✓ Current MES Price: ${mesPrice}\n`);
// ============================================
// ORDER 1: MNQ LONG with 50 Tick SL and 25 Tick TP (0.5 RR)
// ============================================
console.log('=' .repeat(80));
console.log('ORDER 1: MNQ LONG (Market Buy)');
console.log('=' .repeat(80));
const mnqStopLossTicks = -50; // Negative for long positions (price goes down)
const mnqTakeProfitTicks = 25; // Positive for long positions (price goes up)
const mnqLongOrder = {
accountId: testAccount.id,
contractId: mnqContract.id,
type: 2, // 2 = Market order
side: 0, // 0 = Bid (buy), 1 = Ask (sell)
size: 1,
stopLossBracket: {
ticks: mnqStopLossTicks,
type: 4 // 4 = Stop (for stop loss on long positions)
},
takeProfitBracket: {
ticks: mnqTakeProfitTicks,
type: 1 // 1 = Limit (for take profit)
}
};
console.log('\nOrder Details:');
console.log('─'.repeat(80));
console.log(` Account: ${testAccount.name} (Practice)`);
console.log(` Contract: ${mnqContract.id}`);
console.log(` Type: Market Buy (type: 2, side: 0)`);
console.log(` Size: 1 contract`);
console.log(` Current Price: ${mnqPrice}`);
console.log(`\n Stop Loss Bracket:`);
console.log(` - ${mnqStopLossTicks} ticks from entry (Stop order)`);
console.log(` - Risk: $${(Math.abs(mnqStopLossTicks) * mnqContract.tickValue).toFixed(2)}`);
console.log(`\n Take Profit Bracket:`);
console.log(` - ${mnqTakeProfitTicks} ticks from entry (Limit order)`);
console.log(` - Reward: $${(mnqTakeProfitTicks * mnqContract.tickValue).toFixed(2)}`);
console.log(`\n Risk/Reward Ratio: 1:${(mnqTakeProfitTicks / Math.abs(mnqStopLossTicks)).toFixed(2)}`);
console.log('─'.repeat(80));
console.log('\nOrder Structure:');
console.log(JSON.stringify(mnqLongOrder, null, 2));
console.log('\nPlacing MNQ LONG order...');
const mnqOrderResult = await client.placeOrder(mnqLongOrder);
console.log('\n✓ MNQ Order Response:');
console.log(JSON.stringify(mnqOrderResult, null, 2));
// ============================================
// ORDER 2: MES SHORT with 50 Tick SL and 25 Tick TP (0.5 RR)
// ============================================
console.log('\n' + '='.repeat(80));
console.log('ORDER 2: MES SHORT (Market Sell)');
console.log('='.repeat(80));
const mesStopLossTicks = 50; // Positive for short positions (price goes up)
const mesTakeProfitTicks = -25; // Negative for short positions (price goes down)
const mesShortOrder = {
accountId: testAccount.id,
contractId: mesContract.id,
type: 2, // 2 = Market order
side: 1, // 0 = Bid (buy), 1 = Ask (sell)
size: 1,
stopLossBracket: {
ticks: mesStopLossTicks,
type: 4 // 4 = Stop (for stop loss on short positions)
},
takeProfitBracket: {
ticks: mesTakeProfitTicks,
type: 1 // 1 = Limit (for take profit)
}
};
console.log('\nOrder Details:');
console.log('─'.repeat(80));
console.log(` Account: ${testAccount.name} (Practice)`);
console.log(` Contract: ${mesContract.id}`);
console.log(` Type: Market Sell (type: 2, side: 1)`);
console.log(` Size: 1 contract`);
console.log(` Current Price: ${mesPrice}`);
console.log(`\n Stop Loss Bracket:`);
console.log(` - ${mesStopLossTicks} ticks from entry (Stop order)`);
console.log(` - Risk: $${(Math.abs(mesStopLossTicks) * mesContract.tickValue).toFixed(2)}`);
console.log(`\n Take Profit Bracket:`);
console.log(` - ${mesTakeProfitTicks} ticks from entry (Limit order)`);
console.log(` - Reward: $${(Math.abs(mesTakeProfitTicks) * mesContract.tickValue).toFixed(2)}`);
console.log(`\n Risk/Reward Ratio: 1:${(Math.abs(mesTakeProfitTicks) / Math.abs(mesStopLossTicks)).toFixed(2)}`);
console.log('─'.repeat(80));
console.log('\nOrder Structure:');
console.log(JSON.stringify(mesShortOrder, null, 2));
console.log('\nPlacing MES SHORT order...');
const mesOrderResult = await client.placeOrder(mesShortOrder);
console.log('\n✓ MES Order Response:');
console.log(JSON.stringify(mesOrderResult, null, 2));
console.log('\n' + '='.repeat(80));
console.log('TEST COMPLETE - BOTH ORDERS PLACED');
console.log('='.repeat(80));
console.log('\n✓ MNQ LONG order placed successfully');
console.log('✓ MES SHORT order placed successfully');
console.log('\nCheck your TopstepX platform to verify the order status\n');
} catch (error) {
console.error('\n❌ Error:', error.message);
if (error.response) {
console.error('Response status:', error.response.status);
console.error('Response data:', JSON.stringify(error.response.data, null, 2));
}
process.exit(1);
} finally {
client.disconnect();
}
}
// Run the test
testOrders();