forked from victorycodedev/metaapi-cloud-php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistorical_data_example.php
More file actions
104 lines (87 loc) · 2.87 KB
/
historical_data_example.php
File metadata and controls
104 lines (87 loc) · 2.87 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
<?php
require_once __DIR__ . '/../../vendor/autoload.php';
use Oyi77\MetaapiCloudPhpSdk\AccountApi;
/**
* Historical Market Data Example
*
* Demonstrates how to:
* - Retrieve historical candles
* - Retrieve historical ticks
*
* Note: Historical market data is only available on G1 and MT4 G2 instances.
*/
$token = getenv('TOKEN') ?: '<put your token here>';
$accountId = getenv('ACCOUNT_ID') ?: '<put your account ID here>';
$symbol = getenv('SYMBOL') ?: 'EURUSD';
try {
// Create AccountApi instance
$accountApi = new AccountApi($token);
// Get historical candles
echo "Fetching historical candles for {$symbol}...\n";
$candles = $accountApi->getHistoricalCandles(
$accountId,
$symbol,
'1h', // 1-hour timeframe
'2024-01-01 00:00:00.000', // Start time
100 // Limit to 100 candles
);
if (is_array($candles)) {
echo "Retrieved " . count($candles) . " candles\n";
if (count($candles) > 0) {
echo "First candle: " . json_encode($candles[0]) . "\n";
echo "Last candle: " . json_encode($candles[count($candles) - 1]) . "\n";
}
} else {
echo "Candles response: {$candles}\n";
}
echo "\n";
// Get recent candles without start time (returns latest)
echo "Fetching latest candles for {$symbol}...\n";
$recentCandles = $accountApi->getHistoricalCandles(
$accountId,
$symbol,
'15m', // 15-minute timeframe
null, // No start time - get latest
10 // Limit to 10 candles
);
if (is_array($recentCandles)) {
echo "Retrieved " . count($recentCandles) . " recent candles\n";
}
echo "\n";
// Get historical ticks
echo "Fetching historical ticks for {$symbol}...\n";
$ticks = $accountApi->getHistoricalTicks(
$accountId,
$symbol,
'2024-01-01 00:00:00.000', // Start time
0, // Offset
50 // Limit to 50 ticks
);
if (is_array($ticks)) {
echo "Retrieved " . count($ticks) . " ticks\n";
if (count($ticks) > 0) {
echo "First tick: " . json_encode($ticks[0]) . "\n";
echo "Last tick: " . json_encode($ticks[count($ticks) - 1]) . "\n";
}
} else {
echo "Ticks response: {$ticks}\n";
}
echo "\n";
// Get recent ticks without start time (returns latest)
echo "Fetching latest ticks for {$symbol}...\n";
$recentTicks = $accountApi->getHistoricalTicks(
$accountId,
$symbol,
null, // No start time - get latest
null, // No offset
20 // Limit to 20 ticks
);
if (is_array($recentTicks)) {
echo "Retrieved " . count($recentTicks) . " recent ticks\n";
}
echo "\n";
echo "Historical data retrieval complete!\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
echo $e->getTraceAsString() . "\n";
}