Skip to content

Commit 772a367

Browse files
authored
Merge pull request #442 from fasiondog/feature/factor_ma
Feature/factor ma
2 parents 98e4877 + f374943 commit 772a367

File tree

5 files changed

+68
-65
lines changed

5 files changed

+68
-65
lines changed

hikyuu/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import sys
3636
import os
3737
BASE_DIR = os.path.dirname(__file__)
38+
39+
3840
if sys.platform == 'win32':
3941
# add_dll_directory() 有时不生效
4042
os.add_dll_directory(os.path.join(os.path.dirname(__file__), 'cpp'))

hikyuu_cpp/hikyuu/indicator/IndicatorImp.cpp

Lines changed: 23 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ void IndicatorImp::setContext(const KData &k) {
280280
getAllSubNodes(nodes);
281281
if (ms_enable_increment_calculate) {
282282
for (const auto &node : nodes) {
283-
if (!node->m_need_calculate && !node->supportIncrementCalculate()) {
283+
if (!node->m_need_calculate && ((node->m_optype == LEAF || node->m_optype == OP) &&
284+
!node->supportIncrementCalculate())) {
284285
node->_clearBuffer();
285286
}
286287
}
@@ -877,13 +878,12 @@ bool IndicatorImp::increment_execute_leaf_or_op(const Indicator &ind) {
877878
return false;
878879
}
879880

880-
for (size_t r = 0; r < m_result_num; ++r) {
881-
if (m_pBuffer[r] == nullptr) {
882-
return false;
881+
if (copy_len > 0) {
882+
for (size_t r = 0; r < m_result_num; ++r) {
883+
m_pBuffer[r]->resize(total, Null<value_t>());
884+
auto *dst = this->data(r);
885+
memmove(dst, dst + copy_start_pos, sizeof(value_t) * (copy_len));
883886
}
884-
m_pBuffer[r]->resize(total, Null<value_t>());
885-
auto *dst = this->data(r);
886-
memmove(dst, dst + copy_start_pos, sizeof(value_t) * (copy_len));
887887
}
888888

889889
if (start_pos < m_discard) {
@@ -1054,35 +1054,13 @@ size_t IndicatorImp::increment_execute() {
10541054
return null_pos;
10551055
}
10561056

1057-
if (copy_start_pos < m_discard) {
1058-
size_t old_discard = m_discard;
1059-
m_discard = m_discard - copy_start_pos;
1060-
copy_start_pos = old_discard;
1061-
copy_len = m_old_context.size() - copy_start_pos;
1062-
for (size_t r = 0; r < m_result_num; ++r) {
1063-
if (m_pBuffer[r] == nullptr) {
1064-
return false;
1065-
}
1066-
m_pBuffer[r]->resize(total, Null<value_t>());
1067-
auto *dst = this->data(r);
1068-
memmove(dst + m_discard, dst + copy_start_pos, sizeof(value_t) * (copy_len));
1069-
}
1070-
} else {
1071-
for (size_t r = 0; r < m_result_num; ++r) {
1072-
if (m_pBuffer[r] == nullptr) {
1073-
return false;
1074-
}
1075-
m_pBuffer[r]->resize(total, Null<value_t>());
1076-
auto *dst = this->data(r);
1077-
memmove(dst, dst + copy_start_pos, sizeof(value_t) * (copy_len));
1078-
}
1079-
}
1080-
1081-
if (start_pos < m_discard) {
1082-
start_pos = m_discard;
1057+
for (size_t r = 0; r < m_result_num; ++r) {
1058+
HKU_ASSERT(m_pBuffer[r] != nullptr);
1059+
m_pBuffer[r]->resize(total, Null<value_t>());
1060+
auto *dst = this->data(r);
1061+
memmove(dst, dst + copy_start_pos, sizeof(value_t) * (copy_len));
10831062
}
10841063

1085-
_update_discard();
10861064
return start_pos;
10871065
}
10881066

@@ -1846,35 +1824,12 @@ size_t IndicatorImp::increment_execute_if() {
18461824
return null_pos;
18471825
}
18481826

1849-
if (copy_start_pos < m_discard) {
1850-
size_t old_discard = m_discard;
1851-
m_discard = m_discard - copy_start_pos;
1852-
copy_start_pos = old_discard;
1853-
copy_len = m_old_context.size() - copy_start_pos;
1854-
for (size_t r = 0; r < m_result_num; ++r) {
1855-
if (m_pBuffer[r] == nullptr) {
1856-
return false;
1857-
}
1858-
m_pBuffer[r]->resize(total, Null<value_t>());
1859-
auto *dst = this->data(r);
1860-
memmove(dst + m_discard, dst + copy_start_pos, sizeof(value_t) * (copy_len));
1861-
}
1862-
} else {
1863-
for (size_t r = 0; r < m_result_num; ++r) {
1864-
if (m_pBuffer[r] == nullptr) {
1865-
return false;
1866-
}
1867-
m_pBuffer[r]->resize(total, Null<value_t>());
1868-
auto *dst = this->data(r);
1869-
memmove(dst, dst + copy_start_pos, sizeof(value_t) * (copy_len));
1870-
}
1871-
}
1872-
1873-
if (start_pos < m_discard) {
1874-
start_pos = m_discard;
1827+
for (size_t r = 0; r < m_result_num; ++r) {
1828+
m_pBuffer[r]->resize(total, Null<value_t>());
1829+
auto *dst = this->data(r);
1830+
memmove(dst, dst + copy_start_pos, sizeof(value_t) * (copy_len));
18751831
}
18761832

1877-
_update_discard();
18781833
return start_pos;
18791834
}
18801835

@@ -1918,8 +1873,9 @@ void IndicatorImp::execute_if() {
19181873
if (start_pos == Null<size_t>()) {
19191874
size_t result_number = std::min(minp->getResultNumber(), maxp->getResultNumber());
19201875
_readyBuffer(total, result_number);
1921-
} else if (start_pos > discard) {
1922-
discard = start_pos;
1876+
start_pos = discard;
1877+
} else if (start_pos < discard) {
1878+
start_pos = discard;
19231879
}
19241880

19251881
setDiscard(discard);
@@ -1929,7 +1885,7 @@ void IndicatorImp::execute_if() {
19291885
auto *three = m_three->data(0);
19301886
for (size_t r = 0; r < m_result_num; ++r) {
19311887
auto *dst = this->data(r);
1932-
for (size_t i = discard; i < total; ++i) {
1888+
for (size_t i = start_pos; i < total; ++i) {
19331889
if (three[i - diff_cond] > 0.0) {
19341890
dst[i] = left[i - diff_left];
19351891
} else {
@@ -2022,6 +1978,9 @@ void IndicatorImp::_update_discard(bool force) {
20221978
m_discard = discard;
20231979
}
20241980
}
1981+
if (m_discard > total) {
1982+
m_discard = total;
1983+
}
20251984
}
20261985

20271986
bool IndicatorImp::alike(const IndicatorImp &other) const {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2026 hikyuu.org
3+
*
4+
* Created on: 2026-01-16
5+
* Author: fasiondog
6+
*/
7+
8+
#pragma once
9+
10+
#include "hikyuu/utilities/config.h"
11+
12+
#if defined(_OPENMP)
13+
#include <omp.h>
14+
#define HKU_OMP_PARALLEL_FOR _Pragma("omp parallel for")
15+
#define HKU_OMP_SAFETY_PARALLEL_FOR \
16+
_Pragma("omp parallel for num_threads(omp_get_max_threads()) if (!omp_in_parallel())")
17+
#define HKU_OMP_CHECK_THRESHOLD(guard, threshold) \
18+
if ((guard) > (threshold) && !omp_in_parallel()) { \
19+
omp_set_num_threads(omp_get_max_threads()); \
20+
}
21+
#else
22+
#define HKU_OMP_PARALLEL_FOR
23+
#define HKU_OMP_SAFETY_PARALLEL_FOR
24+
#define HKU_OMP_CHECK_THRESHOLD(guard, threshold)
25+
#endif

hikyuu_cpp/hikyuu/xmake.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ target("hikyuu")
2121
end
2222
end
2323

24+
if has_config("omp") then
25+
add_packages("openmp")
26+
if is_plat("macosx") then
27+
add_packages("libomp")
28+
end
29+
end
30+
2431
if has_config("http_client_zip") then
2532
add_packages("gzip-hpp")
2633
end
@@ -72,7 +79,7 @@ target("hikyuu")
7279
end
7380

7481
if is_plat("macosx") then
75-
add_links("iconv", "sqlite3")
82+
add_links("sqlite3")
7683
add_frameworks("CoreFoundation")
7784
end
7885

xmake.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ option("log_level", {description = "set log level.", default = 2, values = {1, 2
3838
option("async_log", {description = "Use async log.", default = false})
3939
option("leak_check", {description = "Enable leak check for test", default = false})
4040

41+
-- openmp 默认关闭,omp容易在数据不是全部在内存中时容易CPU占满空等,建议调测时使用
42+
option("omp", {description = "Enable openmp support.", default = false})
43+
4144
-- 不再直接包含 arrow, 此处保留仅作编译兼容,实际不再使用
4245
option("arrow", {description = "Enable arrow support.(Obsolete, kept only for compatibility)", default = false})
4346

@@ -190,6 +193,13 @@ add_requires("eigen", {system = false})
190193
add_requires("xxhash", {system = false})
191194
add_requires("utf8proc 2.11.0", {system = false})
192195

196+
if has_config("omp") then
197+
add_requires("openmp", {system = false})
198+
if is_plat("macosx") then
199+
add_requires("libomp", {system = false})
200+
end
201+
end
202+
193203
if has_config("http_client_zip") then
194204
add_requires("gzip-hpp", {system = false})
195205
end

0 commit comments

Comments
 (0)