More Related Content More from Platonov Sergey (20) Тененёв Анатолий, Boost.Asio в алгоритмической торговле2. Содержание
● Торговля на финансовых ранках
● Boost.Asio
● Алгоритмическая торговля
https://github.com/bytefury/trading_robot_2
https://github.com/bytefury/asio_examples
4. Операции и события
● add_order – добавить заявку
● cancel_order – отменить заявку
● move_order – переместить заявку
● on_change_order – изменение статуса заявки
● on_tick – новая сделка по инструменту
● on_stocks – стакан котировок инструмента
6. ● io_service ios;
● ios.post([]() { ... });
● ios.run(); ios.run_one();
● ios.poll(); ios.poll_one();
● ios.reset();
● io_service::work work(ios);
7. #include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;
namespace asio = boost::asio;
namespace pt = boost::posix_time;
namespace bs = boost::system;
int main() {
asio::io_service ios; // (1)
asio::deadline_timer timer(ios, pt::seconds(3)); // (2)
timer.async_wait([](bs::error_code){ cout << "Hello World!n"; }); // (3)
ios.run(); // (4)
return 0;
}
Пример 1
8. #include <iostream>
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;
namespace asio = boost::asio;
namespace pt = boost::posix_time;
namespace bs = boost::system;
int main() {
asio::io_service ios; // (1)
asio::spawn(ios, [&ios](asio::yield_context yield) { // (2)
asio::deadline_timer timer(ios, pt::seconds(3)); // (3)
timer.async_wait(yield); // (4)
cout << "Hello World!n";
});
ios.run(); // (5)
return 0;
}
Пример 2
9. Пример 3
int main() {
asio::io_service ios;
async_wait(ios, 1, [](bs::error_code, pt::ptime time) { // (1)
cout << "Hello World 1: " << time << "n";
});
asio::spawn(ios, [&ios](asio::yield_context yield) {
pt::ptime time = async_wait(ios, 3, yield); // (2)
cout << "Hello World 2: " << time << "n";
});
ios.run();
return 0;
}
10. Продолжение
template<typename Token>
typename boost::asio::async_result
<typename boost::asio::handler_type<Token,
void(bs::error_code, pt::ptime)>::type
>::type // в С++14 заменяем на auto
async_wait(asio::io_service& ios, int seconds, Token&& token) // (3)
{
using handler_type = typename asio::handler_type
<Token, void(bs::error_code, pt::ptime)>::type; // (4)
handler_type handler(std::forward<Token>(token)); // (5)
asio::async_result<handler_type> result(handler); // (6)
ios.post([&ios, handler, seconds]() mutable { // (7)
asio::spawn(ios, [&ios, handler, seconds](asio::yield_context yield) mutable { // (8)
asio::deadline_timer timer(ios, pt::seconds(seconds));
timer.async_wait(yield);
handler(bs::error_code(), pt::second_clock::local_time()); // (9)
});
});
return result.get(); // (10)
}
12. Продолжение
int main() {
unique_ptr<asio::io_service> ios1{new asio::io_service}; // (2)
unique_ptr<asio::io_service> ios2{new asio::io_service}; // (3)
asio::spawn(*ios1, [&ios1](asio::yield_context yield) {
ScopeGuard sg; // (4)
asio::deadline_timer timer(*ios1, pt::seconds(999999)); // (5)
timer.async_wait(yield);
cout << "Will it be printed?n"; // (6)
});
asio::spawn(*ios2, [&ios1, &ios2](asio::yield_context yield) {
asio::deadline_timer timer(*ios2, pt::seconds(3)); // (7)
timer.async_wait(yield);
ios1.reset(); // (8)
cout << "Coroutine finishn"; // (9)
});
while (true) { // (10)
if (!ios1 || !ios2) break; // (11)
ios1->poll(); // (12)
ios2->poll(); // (13)
}
cout << "Exitn"; // (14)
return 0;
}
14. Пример 5
int main() {
unique_ptr<asio::io_service> ios1{new asio::io_service};
unique_ptr<asio::io_service> ios2{new asio::io_service};
asio::spawn(*ios1, [&ios1](asio::yield_context yield) {
ScopeGuard sg;
async_wait(*ios1, 999999, yield); // (1)
cout << "Will it be printed?n";
});
asio::spawn(*ios2, [&ios1, &ios2](asio::yield_context yield) {
async_wait(*ios2, 3, yield); // (2)
ios1.reset();
cout << "Coroutine finishn";
});
while (true) {
if (!ios1 || !ios2) break;
ios1->poll();
ios2->poll();
}
cout << "Exitn";
return 0;
}
16. Пример 6
int main() {
unique_ptr<asio::io_service> ios1{new asio::io_service};
unique_ptr<asio::io_service> ios2{new asio::io_service};
asio::spawn(*ios1, [&ios1](asio::yield_context yield) {
ScopeGuard sg;
CustomTimer timer(*ios1, pt::seconds(999999)); // (1)
timer.async_wait(yield);
cout << "Will it be printed?n";
});
asio::spawn(*ios2, [&ios1, &ios2](asio::yield_context yield) {
CustomTimer timer(*ios2, pt::seconds(3)); // (2)
timer.async_wait(yield);
ios1.reset();
cout << "Coroutine finishn";
});
while (true) {
if (!ios1 || !ios2) break;
ios1->poll();
ios2->poll();
}
cout << "Exitn";
return 0;
}
17. class CustomTimer : public asio::basic_io_object<TimerService> { // (3)
public:
CustomTimer(asio::io_service& ios, const pt::time_duration& td) :
basic_io_object(ios) // (4)
{
auto& impl = get_implementation(); // (5)
impl.datetime = pt::second_clock::universal_time() + td; // (6)
}
template<typename Token>
auto async_wait(Token&& token) { // (7)
using handler_type = typename asio::handler_type
<Token, void(bs::error_code)>::type;
handler_type handler(std::forward<Token>(token));
asio::async_result<handler_type> result(handler);
get_service().async_wait(get_implementation(), std::move(handler)); // (8)
return result.get();
}
};
Продолжение
18. class TimerService : public asio::io_service::service { // (9)
public:
struct implementation_type { // (10)
pt::ptime datetime;
};
typedef implementation_type impl_type;
TimerService(asio::io_service& ios) : service(ios) {} // (11)
~TimerService() {}
static asio::io_service::id id; // (12)
void construct(impl_type&) {} // (13)
void destroy(impl_type&) {} // (14)
template<typename Handler>
void async_wait(impl_type& impl, Handler&& handler) { // (15)
unique_ptr<asio::deadline_timer> timer{new asio::deadline_timer(get_io_service(),
impl.datetime)}; // (16)
timer->async_wait([this, &impl, _handler=std::move(handler)](bs::error_code) mutable { // (17)
get_io_service().post([this, &impl, _handler]() mutable { // (18)
_handler(bs::error_code()); // (19)
m_timers.erase(&impl); // (20)
});
});
m_timers[&impl] = std::move(timer); // (21)
}
private:
unordered_map<void*, unique_ptr<asio::deadline_timer>> m_timers; // (22)
void shutdown_service() {} // (23)
};
asio::io_service::id TimerService::id;
19. Boost.Asio в алготорговле
class IMarket
{
public:
virtual ~IMarket() {}
virtual void add_order(const Order& order) = 0;
virtual void del_order(int64_t order_id) = 0;
virtual void move_order(int64_t order_id, int64_t new_order_id, double price, int volume) = 0;
virtual void req_securities() = 0;
virtual void ordlog_handler(const OrdLogHandler& handler) = 0;
virtual void order_handler(const OrderHandler& handler) = 0;
virtual void order_error_handler(const OrderErrorHandler& handler) = 0;
virtual void tick_handler(const TickHandler& handler) = 0;
virtual void stock_handler(const StockHandler& handler) = 0;
virtual void security_handler(const SecurityHandler& handler) = 0;
virtual std::vector<Quote> stocks(int64_t isin_id) = 0;
virtual pt::ptime datetime() = 0;
};
Бэктестер
while(true) {
backtester.process();
ios.poll;
}
Торговля
ios.run();