-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-trade.ts
More file actions
89 lines (76 loc) Β· 2.37 KB
/
basic-trade.ts
File metadata and controls
89 lines (76 loc) Β· 2.37 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
/**
* Basic Trade Example
*
* Demonstrates core trading operations:
* - Connect to cTrader
* - Check account state (balance, equity, free margin)
* - Find a symbol (EURUSD)
* - Place a buy order with stop loss and take profit
* - Modify the stop loss
* - Close the position
* - Disconnect
*
* Run: npx ts-node examples/basic-trade.ts
*/
import { connect } from "../src/index.js";
async function main() {
const ct = await connect();
try {
// Get current account state
const state = await ct.getState();
console.log("=== Account State ===");
console.log(`Balance: ${state.balance}`);
console.log(`Equity: ${state.equity}`);
console.log(`Free Margin: ${state.freeMargin}`);
console.log();
// Get available symbols
const symbols = await ct.getSymbols();
const eurusd = symbols.find((s) => s.symbolName === "EURUSD");
if (!eurusd) {
console.log("EURUSD not found in available symbols");
return;
}
console.log(`Found EURUSD: ID=${eurusd.symbolId}`);
console.log();
// Place a buy order with stop loss and take profit
console.log("=== Opening Position ===");
const position = await ct.buy("EURUSD", {
lots: 0.01,
sl: { pips: 50 },
tp: { pips: 100 },
});
console.log(`Position Opened:`);
console.log(` Position ID: ${position.positionId}`);
console.log(` Entry Price: ${position.price}`);
console.log(` Volume: ${position.tradeData.volume} units`);
console.log();
// Wait 2 seconds
console.log("Waiting 2 seconds before modifying...");
await new Promise((resolve) => setTimeout(resolve, 2000));
// Modify the stop loss
console.log("=== Modifying Position ===");
await ct.modify(position.positionId, {
sl: { pips: 30 },
});
console.log(`Stop Loss modified to 30 pips`);
console.log();
// Close the position
console.log("=== Closing Position ===");
const closeEvent = await ct.close(position.positionId);
console.log(`Position closed`);
console.log(` Execution type: ${closeEvent.executionType}`);
console.log();
// Get final state
const finalState = await ct.getState();
console.log("=== Final Account State ===");
console.log(`Balance: ${finalState.balance}`);
console.log(`Equity: ${finalState.equity}`);
console.log(`Unrealized P&L: ${finalState.unrealizedPnl}`);
} catch (error) {
console.error("Error:", error);
} finally {
ct.disconnect();
console.log("\nDisconnected");
}
}
main();