forked from victorycodedev/metaapi-cloud-php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc_example.php
More file actions
111 lines (93 loc) · 2.8 KB
/
rpc_example.php
File metadata and controls
111 lines (93 loc) · 2.8 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
<?php
require_once __DIR__ . '/../../vendor/autoload.php';
use Oyi77\MetaapiCloudPhpSdk\MetaApi\MetaApi;
/**
* MetaApi RPC Connection Example
*
* Demonstrates how to:
* - Connect to MetaApi via RPC (request/response pattern)
* - Query account information, positions, orders
* - Execute trades with various order types
* - Calculate margin requirements
*/
$token = getenv('TOKEN') ?: '<put your token here>';
$accountId = getenv('ACCOUNT_ID') ?: '<put your account ID here>';
try {
// Create MetaApi instance
$metaApi = new MetaApi($token, [
'region' => 'new-york',
'domain' => 'agiliumtrade.agiliumtrade.ai',
]);
// Get RPC connection for account
$connection = $metaApi->rpcConnection($accountId);
// Connect to websocket
echo "Connecting to MetaApi RPC...\n";
$connection->connect(10.0);
// Wait for synchronization
echo "Waiting for synchronization...\n";
$connection->waitSynchronized(300);
echo "Synchronized!\n\n";
// Get account information
echo "Account Information:\n";
$accountInfo = $connection->getAccountInformation();
print_r($accountInfo);
echo "\n";
// Get open positions
echo "Open Positions:\n";
$positions = $connection->getPositions();
print_r($positions);
echo "\n";
// Get pending orders
echo "Pending Orders:\n";
$orders = $connection->getOrders();
print_r($orders);
echo "\n";
// Get server time
echo "Server Time:\n";
$serverTime = $connection->getServerTime();
print_r($serverTime);
echo "\n";
// Calculate margin for a trade
echo "Calculating margin for 0.1 lot GBPUSD buy...\n";
$marginInfo = $connection->calculateMargin([
'symbol' => 'GBPUSD',
'type' => 'ORDER_TYPE_BUY',
'volume' => 0.1,
'openPrice' => 1.25,
]);
print_r($marginInfo);
echo "\n";
// Example: Place a limit buy order with trailing stop loss and expiration
/*
echo "Placing limit buy order...\n";
$result = $connection->createLimitBuyOrder(
'GBPUSD',
0.01,
1.2400,
1.2300,
1.2600,
[
'comment' => 'PHP SDK Example',
'clientId' => 'example_' . time(),
'trailingStopLoss' => [
'distance' => [
'distance' => 50,
'units' => 'RELATIVE_POINTS',
],
],
'expiration' => [
'type' => 'ORDER_TIME_SPECIFIED',
'time' => (new DateTime('+1 day'))->format('c'),
],
]
);
print_r($result);
echo "\n";
*/
// Close connection
$connection->close();
echo "Connection closed.\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
echo $e->getTraceAsString() . "\n";
}