-
Notifications
You must be signed in to change notification settings - Fork 1
/
streaming.pl
executable file
·66 lines (56 loc) · 1.72 KB
/
streaming.pl
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
#!/usr/bin/perl
use strict;
use warnings;
use WWW::Curl::Easy;
# ------ Change the lines below ------
my $environment = "http://stream-sandbox.oanda.com";
my $accounts = "<accountId>";
my $accessToken = "<ACCESS-TOKEN>";
my $showHeartBeats = 1;
# ------------------------------------
# ------------------------------------
# The environment variable should be:
#
# For Sandbox -> http://stream-sandbox.oanda.com
# For fxPractice -> https://stream-fxpractice.oanda.com
# For fxTrade -> https://stream-fxtrade.oanda.com
# ------------------------------------
# read response and print transactions
sub callback {
my ($line) = @_;
if ($showHeartBeats==1)
{
print $line;
}
elsif ($line!~/heartbeat/)
{
print $line;
}
return length($line);
}
my $curl = WWW::Curl::Easy->new;
# don't print header
$curl->setopt(CURLOPT_HEADER,0);
if ($environment=~/fxpractice/ || $environment=~/fxtrade/)
{
# send access token in header for practice of trade environments
my @header = ('Authorization: Bearer '.$accessToken);
$curl->setopt(CURLOPT_HTTPHEADER, \@header);
}
# set url
$curl->setopt(CURLOPT_URL, $environment."/v1/events?accountIds=".$accounts);
# set callback function to print transactions
$curl->setopt(CURLOPT_WRITEFUNCTION,\&callback);
# Starts the actual request
my $retcode = $curl->perform;
# Looking at the results...
if ($retcode == 0) {
my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
if ($response_code != 200)
{
print "bad request, HTTP code: ".$response_code."\n";
}
} else {
# Error code, type of error, error message
print("An error happened: $retcode ".$curl->strerror($retcode)." ".$curl->errbuf."\n");
}