Skip to content

Commit 59ed59a

Browse files
committed
изменен код получения потока котировок, нулевой тик теперь передает данные предыдущего бара
1 parent f8ce7ec commit 59ed59a

File tree

7 files changed

+181
-18
lines changed

7 files changed

+181
-18
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ int main() {
110110

111111
Также в конструктор класса *MtBridge* была добавлена возможность вызывать лямбду-функцию для обработки получения событий (получение нового тика или бара исторических данных).
112112

113+
**Тик 0-вой секунды передает данные предыдущего бара, а не нового, это может быть полезно для торговли по цене закрытия**
114+
115+
Пример программы:
116+
113117
```
114118
#include <iostream>
115119
#include <mt-bridge.hpp>

code-blocks/example/example.cbp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
<Add directory="../../../boost_1_71_0/boost_all/include/boost-1_71" />
1818
<Add directory="../../../boost_1_71_0/boost_all/lib" />
1919
<Add directory="../../include" />
20+
<Add directory="../../lib/xtime_cpp/src" />
2021
</Compiler>
2122
<Linker>
2223
<Add option="-s" />
2324
<Add library="ws2_32" />
2425
<Add library="wsock32" />
2526
<Add directory="../../../boost_1_71_0/boost_all/include/boost-1_71" />
2627
<Add directory="../../include" />
28+
<Add directory="../../lib/xtime_cpp/src" />
2729
</Linker>
2830
</Target>
2931
</Build>
@@ -32,6 +34,8 @@
3234
<Add option="-fexceptions" />
3335
</Compiler>
3436
<Unit filename="../../include/mt-bridge.hpp" />
37+
<Unit filename="../../lib/xtime_cpp/src/xtime.cpp" />
38+
<Unit filename="../../lib/xtime_cpp/src/xtime.hpp" />
3539
<Unit filename="main.cpp" />
3640
<Extensions>
3741
<code_completion />

code-blocks/example/main.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <iostream>
22
#include <mt-bridge.hpp>
3+
#include <xtime.hpp>
34

45
int main() {
56
const uint32_t port = 5555;
@@ -33,13 +34,13 @@ int main() {
3334
/* get historical data to initialize your indicators */
3435
std::cout << iMT.get_symbol_list()[symbol_index] << std::endl;
3536
std::vector<mt_bridge::MtCandle> candles = iMT.get_candles(symbol_index);
36-
for(size_t i = 0; i < candles.size(); ++i) {
37+
for(size_t i = candles.size()/2; i < candles.size(); ++i) {
3738
std::cout << "candle, o: " << candles[i].open
3839
<< " h: " << candles[i].high
3940
<< " l: " << candles[i].low
4041
<< " c: " << candles[i].close
4142
<< " v: " << candles[i].volume
42-
<< " t: " << candles[i].timestamp
43+
<< " t: " << xtime::get_str_date_time(candles[i].timestamp)
4344
<< std::endl;
4445
}
4546

@@ -58,8 +59,21 @@ int main() {
5859
<< " ask: " << iMT.get_ask(symbol_index)
5960
<< " c: " << candle.close
6061
<< " v: " << candle.volume
61-
<< " t: " << candle.timestamp
62-
<< " s: " << iMT.get_server_timestamp()
62+
<< " t: " << xtime::get_str_date_time(candle.timestamp)
63+
<< " s: " << xtime::get_str_date_time(iMT.get_server_timestamp())
64+
<< std::endl;
65+
mt_bridge::MtCandle candle2 = iMT.get_timestamp_candle(symbol_index, xtime::get_timestamp());
66+
std::cout
67+
<< iMT.get_symbol_list()[symbol_index]
68+
<< " candle2,"
69+
//<< " o: " << candle.open
70+
//<< " h: " << candle.high
71+
//<< " l: " << candle.low
72+
<< " ask: " << iMT.get_ask(symbol_index)
73+
<< " c: " << candle2.close
74+
<< " v: " << candle2.volume
75+
<< " t: " << xtime::get_str_date_time(candle2.timestamp)
76+
<< " s: " << xtime::get_str_date_time(iMT.get_server_timestamp())
6377
<< std::endl;
6478
}
6579
return 0;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
2+
<CodeBlocks_project_file>
3+
<FileVersion major="1" minor="6" />
4+
<Project>
5+
<Option title="example_callback-2" />
6+
<Option pch_mode="2" />
7+
<Option compiler="mingw_64_7_3_0" />
8+
<Build>
9+
<Target title="Release">
10+
<Option output="example_callback-2" prefix_auto="1" extension_auto="1" />
11+
<Option object_output="obj/Release/" />
12+
<Option type="1" />
13+
<Option compiler="mingw_64_7_3_0" />
14+
<Compiler>
15+
<Add option="-O2" />
16+
<Add option="-std=c++11" />
17+
<Add directory="../../../boost_1_71_0/boost_all/include/boost-1_71" />
18+
<Add directory="../../../boost_1_71_0/boost_all/lib" />
19+
<Add directory="../../include" />
20+
<Add directory="../../lib/xtime_cpp/src" />
21+
</Compiler>
22+
<Linker>
23+
<Add option="-s" />
24+
<Add library="ws2_32" />
25+
<Add library="wsock32" />
26+
<Add directory="../../../boost_1_71_0/boost_all/include/boost-1_71" />
27+
<Add directory="../../include" />
28+
<Add directory="../../lib/xtime_cpp/src" />
29+
</Linker>
30+
</Target>
31+
</Build>
32+
<Compiler>
33+
<Add option="-Wall" />
34+
<Add option="-fexceptions" />
35+
</Compiler>
36+
<Unit filename="../../include/mt-bridge.hpp" />
37+
<Unit filename="../../lib/xtime_cpp/src/xtime.cpp" />
38+
<Unit filename="../../lib/xtime_cpp/src/xtime.hpp" />
39+
<Unit filename="main.cpp" />
40+
<Extensions>
41+
<code_completion />
42+
<envvars />
43+
<debugger />
44+
<lib_finder disable_auto="1" />
45+
</Extensions>
46+
</Project>
47+
</CodeBlocks_project_file>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <iostream>
2+
#include <mt-bridge.hpp>
3+
#include <xtime.hpp>
4+
5+
int main() {
6+
const uint32_t port = 5555;
7+
mt_bridge::MtBridge iMT(
8+
port,
9+
100,
10+
[&](const std::map<std::string, mt_bridge::MtCandle> &candles,
11+
const mt_bridge::MtBridge::EventType event,
12+
const uint64_t timestamp) {
13+
mt_bridge::MtCandle candle = mt_bridge::MtBridge::get_candle("AUDCAD", candles);
14+
switch(event) {
15+
case mt_bridge::MtBridge::EventType::HISTORICAL_DATA_RECEIVED:
16+
std::cout << "history bar: " << xtime::get_str_date_time(timestamp) << " minute day: " << ((timestamp / 60) % 1440) << std::endl;
17+
if(mt_bridge::MtBridge::check_candle(candle)) {
18+
std::cout << "AUDCAD, close: " << candle.close
19+
<< " volume: " << candle.volume
20+
<< " t: " << xtime::get_str_date_time(candle.timestamp) << std::endl;
21+
} else {
22+
std::cout << "AUDCAD, error"<< std::endl;
23+
}
24+
break;
25+
case mt_bridge::MtBridge::EventType::NEW_TICK:
26+
if(mt_bridge::MtBridge::check_candle(candle)) {
27+
std::cout << "AUDCAD (t1), close: " << candle.close
28+
<< " volume: " << candle.volume
29+
<< " t: " << xtime::get_str_date_time(timestamp) << "\r";
30+
} else {
31+
std::cout << "AUDCAD, error (t1), close: " << candle.close
32+
<< " volume: " << candle.volume
33+
<< " t: " << xtime::get_str_date_time(timestamp) << "\r";
34+
}
35+
if(xtime::get_second_minute(timestamp) == 0) std::cout << std::endl;
36+
return 0;
37+
break;
38+
};
39+
});
40+
41+
if(!iMT.wait()) {
42+
std::cout << "no connection" << std::endl;
43+
return 0;
44+
}
45+
std::cout << "connection established" << std::endl;
46+
47+
#if(0)
48+
/* list all symbols */
49+
std::vector<std::string> symbol_list = iMT.get_symbol_list();
50+
std::for_each(symbol_list.begin(), symbol_list.end(), [&](std::string &symbol) {
51+
static int n = 0;
52+
std::cout
53+
<< "symbol["
54+
<< std::to_string(n++)
55+
<< "]: "
56+
<< symbol
57+
<< std::endl;
58+
});
59+
std::cout << "mt-bridge version: " << iMT.get_mt_bridge_version() << std::endl;
60+
#endif
61+
62+
/* stop */
63+
while(true) {
64+
std::this_thread::yield();
65+
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
66+
}
67+
return 0;
68+
}

code-blocks/example_callback/main.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ int main() {
66
const uint32_t port = 5555;
77
mt_bridge::MtBridge iMT(
88
port,
9-
10,
9+
100,
1010
[&](const std::map<std::string, mt_bridge::MtCandle> &candles,
1111
const mt_bridge::MtBridge::EventType event,
1212
const uint64_t timestamp) {
13-
mt_bridge::MtCandle candle = mt_bridge::MtBridge::get_candle("EURUSD", candles);
13+
mt_bridge::MtCandle candle = mt_bridge::MtBridge::get_candle("AUDCAD", candles);
14+
15+
std::cout << "callback: " << xtime::get_str_date_time(timestamp) << std::endl;
1416
std::cout << "utc: " << xtime::get_str_date_time() << std::endl;
1517
std::cout << "server utc: " << xtime::get_str_date_time(iMT.get_server_ftimestamp()) << std::endl;
1618
std::cout << "server raw: " << xtime::get_str_date_time(iMT.get_raw_server_timestamp()) << std::endl;
@@ -20,35 +22,50 @@ int main() {
2022
case mt_bridge::MtBridge::EventType::HISTORICAL_DATA_RECEIVED:
2123
std::cout << "history bar: " << xtime::get_str_date_time(timestamp) << " minute day: " << ((timestamp / 60) % 1440) << std::endl;
2224
if(mt_bridge::MtBridge::check_candle(candle)) {
23-
std::cout << "EURUSD, close: " << candle.close
25+
std::cout << "AUDCAD, close: " << candle.close
2426
<< " volume: " << candle.volume
2527
<< " t: " << xtime::get_str_date_time(candle.timestamp) << std::endl;
2628
} else {
27-
std::cout << "EURUSD, error"<< std::endl;
29+
std::cout << "AUDCAD, error"<< std::endl;
2830
}
2931
break;
3032
case mt_bridge::MtBridge::EventType::NEW_TICK:
3133
std::cout << "new tick: " << xtime::get_str_date_time(timestamp) << std::endl;
3234
if(mt_bridge::MtBridge::check_candle(candle)) {
33-
std::cout << "EURUSD, close: " << candle.close
35+
std::cout << "AUDCAD (t1), close: " << candle.close
3436
<< " volume: " << candle.volume
3537
<< " t: " << xtime::get_str_date_time(candle.timestamp) << std::endl;
3638
} else {
37-
std::cout << "EURUSD, error"<< std::endl;
39+
std::cout << "AUDCAD, error (t1), close: " << candle.close
40+
<< " volume: " << candle.volume
41+
<< " t: " << xtime::get_str_date_time(candle.timestamp) << std::endl;
3842
}
3943
break;
4044
};
4145

4246
/* просто проверяем */
4347
std::map<std::string, mt_bridge::MtCandle> candles_2 = iMT.get_candles(timestamp);
44-
mt_bridge::MtCandle candle_2 = mt_bridge::MtBridge::get_candle("EURUSD", candles_2);
48+
mt_bridge::MtCandle candle_2 = mt_bridge::MtBridge::get_candle("AUDCAD", candles_2);
49+
mt_bridge::MtCandle candle_3 = iMT.get_timestamp_candle("AUDCAD", xtime::get_timestamp());
50+
4551
std::cout << "new candle: " << xtime::get_str_date_time(timestamp) << std::endl;
4652
if(mt_bridge::MtBridge::check_candle(candle_2)) {
47-
std::cout << "EURUSD, close: " << candle_2.close
53+
std::cout << "AUDCAD (2), close: " << candle_2.close
4854
<< " volume: " << candle_2.volume
4955
<< " t: " << xtime::get_str_date_time(candle_2.timestamp) << std::endl;
5056
} else {
51-
std::cout << "EURUSD, error (candle_2)"<< std::endl;
57+
std::cout << "AUDCAD, error (2), close: " << candle_2.close
58+
<< " volume: " << candle_2.volume
59+
<< " t: " << xtime::get_str_date_time(candle_2.timestamp) << std::endl;
60+
}
61+
if(mt_bridge::MtBridge::check_candle(candle_3)) {
62+
std::cout << "AUDCAD (3), close: " << candle_3.close
63+
<< " volume: " << candle_3.volume
64+
<< " t: " << xtime::get_str_date_time(candle_2.timestamp) << std::endl;
65+
} else {
66+
std::cout << "AUDCAD, error (3), close: " << candle_3.close
67+
<< " volume: " << candle_3.volume
68+
<< " t: " << xtime::get_str_date_time(candle_3.timestamp) << std::endl;
5269
}
5370
});
5471

include/mt-bridge.hpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ namespace mt_bridge {
267267

268268
public:
269269

270+
/** \brief Получить смещение метки времени
271+
* \return смещение метки времени
272+
*/
270273
inline double get_offset_timestamp() {
271274
return offset_timestamp;
272275
}
@@ -367,6 +370,8 @@ namespace mt_bridge {
367370

368371
/* читаем количество символов */
369372
num_symbol = connection->read_uint32();
373+
if(num_symbol == 0)
374+
throw("Error! Invalid list of currency pairs!");
370375

371376
/* инициализируем массивы тиковых данных */
372377
{
@@ -400,7 +405,6 @@ namespace mt_bridge {
400405
for(uint32_t s = 0; s < num_symbol; ++s) {
401406
const double bid = connection->read_double();
402407
const double ask = connection->read_double();
403-
404408
const double open = connection->read_double();
405409
const double high = connection->read_double();
406410
const double low = connection->read_double();
@@ -473,11 +477,11 @@ namespace mt_bridge {
473477
}
474478
} // while
475479
} catch (std::exception& e) {
476-
std::cerr << "error: " << e.what() << std::endl;
480+
std::cerr << "mt-bridge server error: " << e.what() << std::endl;
477481
is_mt_connected = false;
478482
is_error = true;
479483
} catch (...) {
480-
std::cerr << "error" << std::endl;
484+
std::cerr << "mt-bridge server error" << std::endl;
481485
is_mt_connected = false;
482486
is_error = true;
483487
}
@@ -535,11 +539,16 @@ namespace mt_bridge {
535539
std::map<std::string, CANDLE_TYPE> candles;
536540
{
537541
std::lock_guard<std::mutex> lock(symbol_list_mutex);
542+
const uint64_t second = timestamp % SECONDS_IN_MINUTE;
538543
for(uint32_t symbol_index = 0;
539544
symbol_index < num_symbol;
540545
++symbol_index) {
541546
std::string symbol_name(symbol_list[symbol_index]);
542-
candles[symbol_name] = get_timestamp_candle(symbol_index, timestamp);
547+
if(second == 0) {
548+
candles[symbol_name] = get_timestamp_candle(symbol_index, timestamp - 1);
549+
} else {
550+
candles[symbol_name] = get_timestamp_candle(symbol_index, timestamp);
551+
}
543552
}
544553
}
545554

@@ -763,7 +772,6 @@ namespace mt_bridge {
763772
const size_t array_candles_size =
764773
array_candles[symbol_index].size();
765774
if(array_candles_size == 0) return CANDLE_TYPE();
766-
size_t index = array_candles_size - 1;
767775
/* особый случай, бар еще не успел сформироваться */
768776
if(array_candles[symbol_index].back().timestamp == (first_timestamp - SECONDS_IN_MINUTE)) {
769777
double price = 0;
@@ -779,6 +787,7 @@ namespace mt_bridge {
779787
return CANDLE_TYPE(price, price, price, price,
780788
0, first_timestamp);
781789
}
790+
int64_t index = array_candles_size - 1;
782791
while(true) {
783792
if(array_candles[symbol_index][index].timestamp == first_timestamp) {
784793
return array_candles[symbol_index][index];

0 commit comments

Comments
 (0)