diff --git a/.gitignore b/.gitignore index c033bf2e..ce1b469a 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ .metadata .settings /build +/tools/hikyuu/docs/build /docs/docs-web/build /docs/reference /docs/test-doc diff --git a/libs/hikyuu/indicator/IndicatorImp.cpp b/libs/hikyuu/indicator/IndicatorImp.cpp index 6e790d84..69e4c049 100644 --- a/libs/hikyuu/indicator/IndicatorImp.cpp +++ b/libs/hikyuu/indicator/IndicatorImp.cpp @@ -41,9 +41,6 @@ IndicatorImp::IndicatorImp(const string& name, size_t result_num) : m_name(name), m_discard(0) { memset(m_pBuffer, NULL, sizeof(PriceList*) * MAX_RESULT_NUM); m_result_num = result_num < MAX_RESULT_NUM ? result_num : MAX_RESULT_NUM; - for (size_t i = 0; i < m_result_num; ++i) { - m_pBuffer[i] = new PriceList(); - } } void IndicatorImp::_readyBuffer(size_t len, size_t result_num) { @@ -55,33 +52,22 @@ void IndicatorImp::_readyBuffer(size_t len, size_t result_num) { price_t null_price = Null(); - if (m_result_num <= result_num) { - for (size_t i = 0; i < m_result_num; ++i) { - m_pBuffer[i]->clear(); - m_pBuffer[i]->reserve(len); - for (size_t j = 0; j < len; ++j) { - m_pBuffer[i]->push_back(null_price); - } - } - - for (size_t i = m_result_num; i < result_num; ++i) { + for (size_t i = 0; i < result_num; ++i) { + if (!m_pBuffer[i]) { m_pBuffer[i] = new PriceList(len, null_price); - } - } else { - //if (m_result_num > result_num) - for (size_t i = 0; i < result_num; ++i) { + } else { m_pBuffer[i]->clear(); m_pBuffer[i]->reserve(len); for (size_t j = 0; j < len; ++j) { m_pBuffer[i]->push_back(null_price); } } + } - for (size_t i = result_num; i < m_result_num; ++i) { - delete m_pBuffer[i]; - m_pBuffer[i] = NULL; - } + for (size_t i = result_num; i < m_result_num; ++i) { + delete m_pBuffer[i]; + m_pBuffer[i] = NULL; } m_result_num = result_num; diff --git a/libs/hikyuu/indicator/IndicatorImp.h b/libs/hikyuu/indicator/IndicatorImp.h index 17117b49..f8da982c 100644 --- a/libs/hikyuu/indicator/IndicatorImp.h +++ b/libs/hikyuu/indicator/IndicatorImp.h @@ -124,7 +124,15 @@ private: ar & BOOST_SERIALIZATION_NVP(m_params); ar & BOOST_SERIALIZATION_NVP(m_discard); ar & BOOST_SERIALIZATION_NVP(m_result_num); - for (size_t i = 0; i < m_result_num; ++i) { + int act_result_num = 0; + size_t i = 0; + while (i < m_result_num) { + if (m_pBuffer[i++]) + act_result_num++; + } + ar & BOOST_SERIALIZATION_NVP(act_result_num); + + for (size_t i = 0; i < act_result_num; ++i) { std::stringstream buf; buf << "result_" << i; ar & bs::make_nvp(buf.str().c_str(), *m_pBuffer[i]); @@ -138,7 +146,9 @@ private: ar & BOOST_SERIALIZATION_NVP(m_params); ar & BOOST_SERIALIZATION_NVP(m_discard); ar & BOOST_SERIALIZATION_NVP(m_result_num); - for (size_t i = 0; i < m_result_num; ++i) { + int act_result_num = 0; + ar & BOOST_SERIALIZATION_NVP(act_result_num); + for (size_t i = 0; i < act_result_num; ++i) { m_pBuffer[i] = new PriceList(); std::stringstream buf; buf << "result_" << i; diff --git a/libs/hikyuu/indicator/imp/Ema.cpp b/libs/hikyuu/indicator/imp/Ema.cpp index e58be05c..d797b1ae 100644 --- a/libs/hikyuu/indicator/imp/Ema.cpp +++ b/libs/hikyuu/indicator/imp/Ema.cpp @@ -43,7 +43,7 @@ void Ema::_calculate(const Indicator& indicator) { price_t multiplier = 2.0 / (n + 1); for (size_t i = startPos + 1; i < total; ++i) { - ema = indicator[i] * multiplier + ema - ema * multiplier; + ema = (indicator[i] - ema) * multiplier + ema; _set(ema, i); } } diff --git a/libs/hikyuu/indicator/imp/Macd.cpp b/libs/hikyuu/indicator/imp/Macd.cpp index 71884844..0c1cd48a 100644 --- a/libs/hikyuu/indicator/imp/Macd.cpp +++ b/libs/hikyuu/indicator/imp/Macd.cpp @@ -56,8 +56,8 @@ void Macd::_calculate(const Indicator& data) { _set(dea, 0, 2); for (size_t i = 1; i < total; ++i) { - ema1 = data[i] * m1 + ema1 - ema1 * m1; - ema2 = data[i] * m2 + ema2 - ema2 * m2; + ema1 = (data[i] - ema1) * m1 + ema1; + ema2 = (data[i] - ema2) * m2 + ema2; diff = ema1 - ema2; dea = diff * m3 + dea - dea * m3; bar = diff - dea; diff --git a/libs/hikyuu/trade_manage/TradeManager.h b/libs/hikyuu/trade_manage/TradeManager.h index bb5e4216..64bf20c6 100644 --- a/libs/hikyuu/trade_manage/TradeManager.h +++ b/libs/hikyuu/trade_manage/TradeManager.h @@ -90,6 +90,9 @@ public: /** 获取交易算法指针 */ TradeCostPtr costFunc() const { return m_costfunc; } + /** 设置交易算法指针 */ + void costFunc(const TradeCostPtr& func) { m_costfunc = func; } + /** * 返回当前现金 * @note 仅返回当前信息,不会根据权息进行调整 diff --git a/libs/hikyuu/trade_sys/signal/crt/Single_SG.h b/libs/hikyuu/trade_sys/signal/crt/Single_SG.h index abea0dcf..eb96c208 100644 --- a/libs/hikyuu/trade_sys/signal/crt/Single_SG.h +++ b/libs/hikyuu/trade_sys/signal/crt/Single_SG.h @@ -8,7 +8,7 @@ #ifndef TRADE_SYS_SIGNAL_CRT_SINGLE_SG_H_ #define TRADE_SYS_SIGNAL_CRT_SINGLE_SG_H_ -#include "../../../indicator/Indicator.h" +#include "../../../indicator/Operand.h" #include "../SignalBase.h" namespace hku { @@ -22,7 +22,7 @@ namespace hku { * @return * @ingroup Signal */ -SignalPtr HKU_API Single_SG(const Indicator& ind, +SignalPtr HKU_API Single_SG(const Operand& ind, int filter_n = 20, double filter_p = 0.1, const string& kpart = "CLOSE"); diff --git a/libs/hikyuu/trade_sys/signal/imp/SingleSignal.cpp b/libs/hikyuu/trade_sys/signal/imp/SingleSignal.cpp index b23bd3a0..f5ef28ae 100644 --- a/libs/hikyuu/trade_sys/signal/imp/SingleSignal.cpp +++ b/libs/hikyuu/trade_sys/signal/imp/SingleSignal.cpp @@ -18,7 +18,7 @@ SingleSignal::SingleSignal(): SignalBase("SINGLE") { setParam("kpart", "CLOSE"); } -SingleSignal::SingleSignal(const Indicator& ind) +SingleSignal::SingleSignal(const Operand& ind) : SignalBase("SINGLE"), m_ind(ind) { setParam("filter_n", 20); setParam("filter_p", 0.1); @@ -62,7 +62,7 @@ void SingleSignal::_calculate() { } } -SignalPtr HKU_API Single_SG(const Indicator& ind, +SignalPtr HKU_API Single_SG(const Operand& ind, int filter_n, double filter_p, const string& kpart) { SingleSignal *p = new SingleSignal(ind); p->setParam("filter_n", filter_n); diff --git a/libs/hikyuu/trade_sys/signal/imp/SingleSignal.h b/libs/hikyuu/trade_sys/signal/imp/SingleSignal.h index 68cc8830..bda7f8dc 100644 --- a/libs/hikyuu/trade_sys/signal/imp/SingleSignal.h +++ b/libs/hikyuu/trade_sys/signal/imp/SingleSignal.h @@ -8,7 +8,7 @@ #ifndef TRADE_SYS_SIGNAL_IMP_SINGLESIGNAL_H_ #define TRADE_SYS_SIGNAL_IMP_SINGLESIGNAL_H_ -#include "../../../indicator/Indicator.h" +#include "../../../indicator/Operand.h" #include "../SignalBase.h" namespace hku { @@ -16,14 +16,14 @@ namespace hku { class SingleSignal: public SignalBase { public: SingleSignal(); - SingleSignal(const Indicator& ind); + SingleSignal(const Operand& ind); virtual ~SingleSignal(); virtual SignalPtr _clone(); virtual void _calculate(); private: - Indicator m_ind; + Operand m_ind; //============================================ // 序列化支持 diff --git a/libs/hikyuu_python/_KData.cpp b/libs/hikyuu_python/_KData.cpp index d932f43e..2dff92f2 100644 --- a/libs/hikyuu_python/_KData.cpp +++ b/libs/hikyuu_python/_KData.cpp @@ -24,7 +24,9 @@ void export_KData() { .def("getDatetimeList", &KData::getDatetimeList) .def("getKRecord", &KData::getKRecord) + .def("get", &KData::getKRecord) .def("getKRecordByDate", &KData::getKRecordByDate) + .def("getByDate", &KData::getKRecordByDate) .def("_getPos", &KData::getPos) //python中需要将Null的情况改写为None .def("size", &KData::size) diff --git a/libs/hikyuu_python/trade_manage/_TradeManager.cpp b/libs/hikyuu_python/trade_manage/_TradeManager.cpp index 346bcc04..15aaaff8 100644 --- a/libs/hikyuu_python/trade_manage/_TradeManager.cpp +++ b/libs/hikyuu_python/trade_manage/_TradeManager.cpp @@ -13,23 +13,6 @@ using namespace boost::python; using namespace hku; -TradeManagerPtr (*crtTM1)(const Datetime&, price_t, const TradeCostPtr&, const string&) = crtTM; -BOOST_PYTHON_FUNCTION_OVERLOADS(crtTM1_overloads, crtTM, 0, 4); - -BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(cash_overload, cash, 1, 2); -BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getFundsCurve_overload, getFundsCurve, 1, 2); -BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getProfitCurve_overload, getProfitCurve, 1, 2); - -FundsRecord (TradeManager::*getFunds_1)(KQuery::KType) const = &TradeManager::getFunds; -FundsRecord (TradeManager::*getFunds_2)(const Datetime&, KQuery::KType) = &TradeManager::getFunds; -BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getFunds_1_overload, TradeManager::getFunds, 0, 1); -BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getFunds_2_overload, TradeManager::getFunds, 1, 2); - -BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(buy_overload, buy, 4, 8); -BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(sell_overload, sell, 3, 8); -BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(buyShort_overload, buyShort, 3, 8); -BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(sellShort_overload, sellShort, 4, 8); - #if HKU_PYTHON_SUPPORT_PICKLE struct TradeManager_pickle_suite : bp::pickle_suite { static @@ -59,6 +42,27 @@ struct TradeManager_pickle_suite : bp::pickle_suite { #endif /* HKU_PYTHON_SUPPORT_PICKLE */ +TradeManagerPtr (*crtTM1)(const Datetime&, price_t, const TradeCostPtr&, const string&) = crtTM; +BOOST_PYTHON_FUNCTION_OVERLOADS(crtTM1_overloads, crtTM, 0, 4); + +BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(cash_overload, cash, 1, 2); +BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getFundsCurve_overload, getFundsCurve, 1, 2); +BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getProfitCurve_overload, getProfitCurve, 1, 2); + +FundsRecord (TradeManager::*getFunds_1)(KQuery::KType) const = &TradeManager::getFunds; +FundsRecord (TradeManager::*getFunds_2)(const Datetime&, KQuery::KType) = &TradeManager::getFunds; +BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getFunds_1_overload, TradeManager::getFunds, 0, 1); +BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getFunds_2_overload, TradeManager::getFunds, 1, 2); + +BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(buy_overload, buy, 4, 8); +BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(sell_overload, sell, 3, 8); +BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(buyShort_overload, buyShort, 3, 8); +BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(sellShort_overload, sellShort, 4, 8); + +TradeCostPtr (TradeManager::*get_costFunc)() const = &TradeManager::costFunc; +void (TradeManager::*set_costFunc)(const TradeCostPtr&) = &TradeManager::costFunc; + + void export_TradeManager() { class_("TradeManager", init) .def("setParam", &TradeManager::setParam) diff --git a/libs/hikyuu_python/trade_sys/_Signal.cpp b/libs/hikyuu_python/trade_sys/_Signal.cpp index 2b447656..16354ba2 100644 --- a/libs/hikyuu_python/trade_sys/_Signal.cpp +++ b/libs/hikyuu_python/trade_sys/_Signal.cpp @@ -49,7 +49,6 @@ void export_Signal() { .def(init()) .def(self_ns::str(self)) .add_property("name", get_name, set_name) - .add_property("kdata", &SignalBase::getTO) //因为Indicator无法使用params['name']的形式,所以统一使用setParm/getParam //.add_property("params", // make_function(&SignalBase::getParameter, diff --git a/notepad_workshop b/notepad_workshop index 4e59bac8..25d6a689 100644 --- a/notepad_workshop +++ b/notepad_workshop @@ -72,8 +72,8 @@ - + diff --git a/tools/hikyuu/docs/source/conf.py b/tools/hikyuu/docs/source/conf.py index 6bc6628d..88be5d8f 100644 --- a/tools/hikyuu/docs/source/conf.py +++ b/tools/hikyuu/docs/source/conf.py @@ -192,7 +192,7 @@ html_static_path = ['_static'] # Sphinx supports the following languages: # 'da', 'de', 'en', 'es', 'fi', 'fr', 'h', 'it', 'ja' # 'nl', 'no', 'pt', 'ro', 'r', 'sv', 'tr' -#html_search_language = 'en' +html_search_language = 'zh' # A dictionary with options for the search language support, empty by default. # Now only 'ja' uses this config value diff --git a/tools/hikyuu/docs/source/developer/FAQ.rst b/tools/hikyuu/docs/source/developer/FAQ.rst index 686371f8..2527d0cc 100644 --- a/tools/hikyuu/docs/source/developer/FAQ.rst +++ b/tools/hikyuu/docs/source/developer/FAQ.rst @@ -50,22 +50,3 @@ :: > xelatex -interaction=nonstopmode Hikyuu.tex - -如何去除 -d:\workspace\fasiondog\trunk\libs\galaxy\galaxy\tradesys\cost\../TradeCostBase.h : warning C4819: 该文件包含 -不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失 -rule configure-version-specific ( toolset : version : conditions ) -{ - toolset.push-checking-for-flags-module unchecked ; - # Starting with versions 7.0, the msvc compiler have the /Zc:forScope and - # /Zc:wchar_t options that improve C++ standard conformance, but those - # options are off by default. If we are sure that the msvc version is at - # 7.*, add those options explicitly. We can be sure either if user specified - # version 7.* explicitly or if we auto-detected the version ourselves. - if ! [ MATCH ^(6\\.) : $(version) ] - { - toolset.flags $(toolset).compile CFLAGS $(conditions) : /Zc:forScope /Zc:wchar_t ; - toolset.flags $(toolset).compile.c++ C++FLAGS $(conditions) : /wd4675 ; - toolset.flags $(toolset).compile.c++ C++FLAGS $(conditions) : /wd4819 ; - -b2 -j 4 release link=shared address-model=64 \ No newline at end of file diff --git a/tools/hikyuu/docs/source/index.rst b/tools/hikyuu/docs/source/index.rst index c1bf6f9c..6106515d 100644 --- a/tools/hikyuu/docs/source/index.rst +++ b/tools/hikyuu/docs/source/index.rst @@ -11,11 +11,11 @@ Welcome to Hikyuu's documentation! user_guide developer_guide + Indices and tables ================== * :ref:`genindex` * :ref:`modindex` -* :ref:`search` - +* :ref:`search` diff --git a/tools/hikyuu/docs/source/user/signal.rst b/tools/hikyuu/docs/source/user/signal.rst index c9966c70..72361b13 100644 --- a/tools/hikyuu/docs/source/user/signal.rst +++ b/tools/hikyuu/docs/source/user/signal.rst @@ -5,7 +5,8 @@ 信号指示器 ========== -信号指示器模块,包括各种信号指示器构造函数。信号指示器负责产生买入、卖出信号。 +信号指示器负责产生买入、卖出信号。 + 通用信号指示器 -------------- @@ -31,12 +32,20 @@ 自定义信号指示器 ---------------- +自定义信号指示器,必须实现 :py:meth:`SignalBase._clone` 和 :py:meth:`SignalBase_calculate` 方法(如示例1)。如果含有私有属性,还需实现 :py:meth:`SignalBase._reset` 方法(如示例2)。 + +示例1(不含私有变量,海龟交易策略): + +.. literalinclude:: ../../../examples/Turtle_SG.py + +示例2(含私有属性): + :: class SignalPython(SignalBase): def __init__(self): super(SignalPython, self).__init__("SignalPython") - self._x = 0 + self._x = 0 #私有属性 self.setParam("test", 30) def _reset(self): @@ -55,16 +64,16 @@ 信号指示器基类 -------------- -自定义的信号指示器,应实现_clone, _reset, _calculate接口。 +自定义的信号指示器,应实现 :py:meth:`SignalBase._clone`, :py:meth:`SignalBase._reset`, :py:meth:`SignalBase._calculate` 接口。 -.. py:class:: SignalBase +.. py:class:: SignalBase([name]) 信号指示器基类 .. py:attribute:: name 信号指示器名称 - + .. py:method:: getParam(name) 获取指定的参数 @@ -86,7 +95,7 @@ :param KData k: 设置交易对象 - .. py:method:: getTO + .. py:method:: getTO() :return: 交易对象 :rtype: KData diff --git a/tools/hikyuu/examples/Turtle_SG.py b/tools/hikyuu/examples/Turtle_SG.py index 5cc1f3bd..802a5251 100644 --- a/tools/hikyuu/examples/Turtle_SG.py +++ b/tools/hikyuu/examples/Turtle_SG.py @@ -4,7 +4,7 @@ #=============================================================================== # Aothor: fasiondog -# History: 20130128, Added by fasiondog +# History: 20160407, Added by fasiondog #=============================================================================== from hikyuu.trade_sys.signal import SignalBase @@ -22,8 +22,8 @@ class TurtleSignal(SignalBase): n = self.getParam("n") k = self.getTO() c = CLOSE(k) - h = REF(HHV(c, n), 1) - L = REF(LLV(c, n), 1) + h = REF(HHV(c, n), 1) #前n日高点 + L = REF(LLV(c, n), 1) #前n日低点 for i in range(h.discard, len(k)): if (c[i] >= h[i]): self._addBuySignal(k[i].datetime) @@ -40,6 +40,6 @@ if __name__ == "__main__": dates = k.getDatetimeList() for d in dates: if (sg.shouldBuy(d)): - print("买入:%s" % str(d)) + print("买入:%s" % d) elif (sg.shouldSell(d)): - print("卖出: %s" % str(d)) + print("卖出: %s" % d)