forked from github/codespaces-blank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStopOrderScheduler.cpp
More file actions
47 lines (44 loc) · 1.6 KB
/
StopOrderScheduler.cpp
File metadata and controls
47 lines (44 loc) · 1.6 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
#include "StopOrderScheduler.hpp"
#include <iostream>
#include <chrono>
using namespace std;
void StopOrderScheduler::addStopOrder(OrderPointer order) {
lock_guard<mutex> lock(mtx_);
pendingStopOrders_[order->orderId] = order;
cout << "[StopOrderScheduler] Added stop order -> ID=" << order->orderId << "\n";
}
void StopOrderScheduler::run()
{
while(running_)
{
{
lock_guard<mutex> lock(mtx_);
for(auto it=pendingStopOrders_.begin();it!=pendingStopOrders_.end();)
{
OrderPointer order = it->second;
// check the trigger condition for stop order
if(order->side=="buy"&&orderBook_.getBestAsk()>=order->stopPrice)
{
cout << "Activating stop order" << order->orderId << "buy as Market Order\n";
order->orderType=OrderType::Market;
orderBook_.processOrder(order);
it=pendingStopOrders_.erase(it);
}
else if(order->side=="sell"&&orderBook_.getBestBid()<=order->stopPrice)
{
cout << "Activating stop order" << order->orderId << "sell as Market Order\n";
order->orderType=OrderType::Market;
orderBook_.processOrder(order);
it=pendingStopOrders_.erase(it);
}
else ++it;
}
}
this_thread::sleep_for(chrono::milliseconds(100));
}
}
void StopOrderScheduler::stop()
{
running_ = false;
cout << "[StopOrderScheduler] Stopping scheduler...\n";
}