Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions LICENCE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2014 OANDA Corporation

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CXX = g++
CXXFLAGS = -Wall -MMD -lPocoNet -lPocoFoundation -lPocoNetSSL

EXEC = streaming
OBJECTS = streaming.o
DEPENDS = ${OBJECTS:.o=.d}

${EXEC}: ${OBJECTS}
${CXX} ${OBJECTS} ${CXXFLAGS} -o ${EXEC}

-include ${DEPENDS}

.PHONY : clean

clean :
rm -rf ${OBJECTS} ${EXEC} ${DEPENDS}
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
cpp-api-streaming
=================

A demo app in C++ for streaming rates using OANDA open api and libcurl
A demo app in C++ for streaming rates using OANDA REST api and the [POCO C++ library](http://pocoproject.org/download/index.html).
A JSON parser such as [libjson](http://sourceforge.net/projects/libjson/) can be used decode the JSON response.

### Setup

Clone this repo to the location of your choice

Make sure you have libcurl installed

apt-get install libcurl4-gnutls-dev

Update the following information in main() in streaming.cpp:

domain
accountId
access_token (Authorization)

Compile the file through g++ compiler. Link libcurl using the -lcurl flag.
Compile the file through g++ compiler. Link the poco networking component using the following -l flags.

`g++ streaming.cpp -lPocoNet -lPocoFoundation -lPocoNetSSL -o streaming`

g++ streaming.cpp -lcurl -o streaming
Alternatively, use the provided Makefile by running `make`.

Run the output executable file

./streaming

### Sameple Output

{"instrument":"EUR_USD","time":"2014-03-07T20:52:17.453618Z","bid":1.3867,"ask":1.38682}
{"tick":{"instrument":"EUR_USD","time":"2014-03-07T20:52:17.453618Z","bid":1.3867,"ask":1.38682}}
{"heartbeat":{"time":"2014-03-07T20:52:29.452784Z"}}
{"instrument":"EUR_USD","time":"2014-03-07T20:52:31.430114Z","bid":1.38671,"ask":1.38685}
{"instrument":"EUR_USD","time":"2014-03-07T20:52:31.478110Z","bid":1.38673,"ask":1.38687}
{"tick":{"instrument":"EUR_USD","time":"2014-03-07T20:52:31.430114Z","bid":1.38671,"ask":1.38685}}
{"tick":{"instrument":"EUR_USD","time":"2014-03-07T20:52:31.478110Z","bid":1.38673,"ask":1.38687}}

### More Information

Expand Down
113 changes: 70 additions & 43 deletions streaming.cpp
Original file line number Diff line number Diff line change
@@ -1,59 +1,86 @@
/*
This is a demo app in C++ for streaming feature using OANDA open api and libcurl
*/

#include <curl/curl.h>
#include <iostream>
#include <iterator>
#include <string>
#include <sstream>

int main()
{
CURL *connection;
CURLcode code;
#include <Poco/Net/HTTPSClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/SSLManager.h>
#include <Poco/StreamCopier.h>
#include <Poco/Path.h>
#include <Poco/URI.h>
#include <Poco/Exception.h>

connection = curl_easy_init();
if (connection)
{
// Please update the following parameters with proper value
// [:domain] : use either fxpractice.oanda.com or fxtrade.oanda.com
// [:instruments] : feel free to subscribe more instruments (at most 10 instruments)
// [:accountId] : your account ID
code = curl_easy_setopt(connection, CURLOPT_URL, "https://<domain>/v1/quote?instruments=EUR_USD&accountId=<your account ID>");

if (code != CURLE_OK)
{
std::cout << "Failed to set URL" << std::endl;
}
using namespace Poco;
using namespace Poco::Net;
using namespace std;

struct curl_slist *headers = NULL;
// Edit these global variables with your own information

// Headers
// [:access_token] : "Authorization: Bearer <your access token>"
headers = curl_slist_append(headers, "Authorization: Bearer <your access token>");
std::string account_id = "1234567";
std::string access_token = "ACCESS-TOKEN";
std::string instruments = "EUR_USD,USD_CAD,EUR_JPY";
std::string domain = "https://stream-fxpractice.oanda.com";

code = curl_easy_setopt(connection, CURLOPT_HTTPHEADER, headers);
/*****************************
The domain variable should be:

if (code != CURLE_OK)
{
std::cout << "Failed to set headers" << std::endl;
}
For Sandbox -> https://stream-sandbox.oanda.com
For fxPractice -> https://stream-fxpractice.oanda.com
For fxTrade -> https://stream-fxtrade.oanda.com
******************************/

code = curl_easy_setopt(connection, CURLOPT_SSL_VERIFYPEER, 0);
void handleStream(streambuf* stream_buffer)
{
std::istreambuf_iterator<char> eos; // end-of-range iterator
std::istreambuf_iterator<char> iit (stream_buffer); // stream iterator
string str;

if (code != CURLE_OK)
{
std::cout << "Failed to disable 'verify the peer'" << std::endl;
while (iit!=eos) {
ostringstream oss;
while (*iit != '\n') {
oss << *iit++;
}

//print the tick
cout << oss.str() << endl;

*iit++;
}
}

code = curl_easy_perform(connection);
int main ()
{
try {
const Context::Ptr context = new Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_NONE, 9, false, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");

if (code != CURLE_OK)
{
std::cout << "Failed to perform connection" << std::endl;
}
// prepare session
URI uri(domain + std::string("/v1/prices?accountId=") + account_id + std::string("&instruments=") + instruments);

HTTPSClientSession session(uri.getHost(), uri.getPort(), context);
session.setKeepAlive(true);

// prepare path
string path(uri.getPathAndQuery());
if (path.empty()) path = "/";

// send request
HTTPRequest req(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
req.set("Authorization", std::string("Bearer ") + access_token);
session.sendRequest(req);

curl_easy_cleanup(connection);
// get response
HTTPResponse res;
istream& rs = session.receiveResponse(res);

curl_slist_free_all(headers);
// handle response
ostringstream out_string_stream;
handleStream(rs.rdbuf());
}
catch (const Exception &e)
{
cerr << e.displayText() << endl;
}
return 0;
}