diff --git a/LICENCE.md b/LICENCE.md new file mode 100644 index 0000000..59275a7 --- /dev/null +++ b/LICENCE.md @@ -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. \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1ba3688 --- /dev/null +++ b/Makefile @@ -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} diff --git a/README.md b/README.md index 7c1f0ed..0fabcc0 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,23 @@ 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 @@ -27,10 +25,10 @@ Run the output executable file ### 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 diff --git a/streaming.cpp b/streaming.cpp index 38f7352..e021fb3 100644 --- a/streaming.cpp +++ b/streaming.cpp @@ -1,59 +1,86 @@ -/* - This is a demo app in C++ for streaming feature using OANDA open api and libcurl -*/ - -#include #include +#include +#include +#include -int main() -{ - CURL *connection; - CURLcode code; +#include +#include +#include +#include +#include +#include +#include +#include - 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:///v1/quote?instruments=EUR_USD&accountId="); - - 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 " - headers = curl_slist_append(headers, "Authorization: Bearer "); +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 eos; // end-of-range iterator + std::istreambuf_iterator 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; } +