Skip to content

Commit

Permalink
Merge pull request #193 from fasiondog/feature/factor
Browse files Browse the repository at this point in the history
优化日志在不同环境下输出; 优化系统部件reset和clone操作
  • Loading branch information
fasiondog committed Mar 11, 2024
2 parents debee37 + 4711e25 commit 40d53ee
Show file tree
Hide file tree
Showing 31 changed files with 419 additions and 251 deletions.
28 changes: 20 additions & 8 deletions docs/source/trade_sys/system.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@
* **ev_open_position=False** *(bool)*: 是否使用市场环境判定进行初始建仓
* **cn_open_position=False** *(bool)*: 是否使用系统有效性条件进行初始建仓

* **shared_tm=False** *(bool)*: tm 部件是否为共享部件
* **shared_ev=True** *(bool)*: ev 部件是否为共享部件
* **shared_cn=False** *(bool)*: cv 部件是否为共享部件
* **shared_mm=False** *(bool)*: mm 部件是否为共享部件
* **shared_sg=False** *(bool)*: sg 部件是否为共享部件
* **shared_st=False** *(bool)*: st 部件是否为共享部件
* **shared_tp=False** *(bool)*: tp 部件是否为共享部件
* **shared_pg=False** *(bool)*: pg 部件是否为共享部件
* **shared_sp=False** *(bool)*: sp 部件是否为共享部件


创建系统并执行回测
-----------------------
Expand Down Expand Up @@ -195,18 +205,20 @@

:param Stock stock: 交易的证券
:param Query query: K线数据查询条件
:param bool reset: 是否同时复位所有组件,尤其是tm实例

.. py:method:: reset(self, with_tm, with_ev)
:param bool reset: 执行前是否依据系统部件共享属性复位
:param bool reset_all: 强制复位所有部件

.. py:method:: reset(self)
复位操作。TM、EV是和具体系统无关的策略组件,可以在不同的系统中进行共享,复位将引起系统运行时被重新清空并计算。尤其是在共享TM时需要注意!

:param bool with_tm: 是否复位TM组件
:param bool with_ev: 是否复位EV组件
复位,但不包括已有的交易对象,以及共享的部件

.. py:methon:: force_reset_all(self)
强制复位所有组件以及清空已有的交易对象,忽略组件的共享属性

.. py:method:: clone(self)
克隆操作
克隆操作,会依据部件的共享特性进行克隆,共享部件不进行实际的克隆操作,保持共享



Expand Down
2 changes: 0 additions & 2 deletions hikyuu/draw/drawplot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@
from .echarts_draw import ibar as ec_ibar
from .echarts_draw import kplot as ec_kplot

from .common import in_ipython_frontend

g_draw_engine = 'matplotlib'


Expand Down
19 changes: 9 additions & 10 deletions hikyuu/draw/drawplot/bokeh_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""

from hikyuu import *
from .common import in_ipython_frontend

from bokeh.plotting import figure, ColumnDataSource
from bokeh.models import DatetimeTickFormatter, HoverTool, Title, Label
Expand Down Expand Up @@ -65,7 +64,7 @@ def show_gcf():

def create_one_axes_figure(figsize=(800, 450)):
"""生成一个仅含有1个坐标轴的figure,并返回其坐标轴对象

:param figsize: (宽, 高)
:return: ax
"""
Expand All @@ -78,7 +77,7 @@ def create_one_axes_figure(figsize=(800, 450)):

def create_two_axes_figure(figsize=(800, 450)):
"""生成一个含有2个坐标轴的figure,并返回坐标轴列表

:param figsize: (宽, 高)
:return: (ax1, ax2)
"""
Expand All @@ -93,7 +92,7 @@ def create_two_axes_figure(figsize=(800, 450)):

def create_three_axes_figure(figsize=(800, 450)):
"""生成一个含有2个坐标轴的figure,并返回坐标轴列表

:param figsize: (宽, 高)
:return: (ax1, ax2)
"""
Expand Down Expand Up @@ -132,7 +131,7 @@ def get_date_format(kdata):
def kplot(kdata, new=True, axes=None, colorup='r', colordown='g'):
"""绘制K线图
:param KData kdata: K线数据
:param bool new: 是否在新窗口中显示,只在没有指定axes时生效
:param axes: 指定的坐标轴
Expand Down Expand Up @@ -226,7 +225,7 @@ def kplot(kdata, new=True, axes=None, colorup='r', colordown='g'):
def mkplot(kdata, new=True, axes=None, colorup='r', colordown='g', ticksize=3):
"""绘制美式K线图
:param KData kdata: K线数据
:param bool new: 是否在新窗口中显示,只在没有指定axes时生效
:param axes: 指定的坐标轴
Expand Down Expand Up @@ -258,7 +257,7 @@ def iplot(
**kwargs
):
"""绘制indicator曲线
:param Indicator indicator: indicator实例
:param axes: 指定的坐标轴
:param new: 是否在新窗口中显示,只在没有指定axes时生效
Expand Down Expand Up @@ -357,7 +356,7 @@ def ibar(
**kwargs
):
"""绘制indicator柱状图
:param Indicator indicator: Indicator实例
:param axes: 指定的坐标轴
:param new: 是否在新窗口中显示,只在没有指定axes时生效
Expand Down Expand Up @@ -436,7 +435,7 @@ def ibar(
def ax_draw_macd(axes, kdata, n1=12, n2=26, n3=9):
"""绘制MACD
:param axes: 指定的坐标轴
:param KData kdata: KData
:param int n1: 指标 MACD 的参数1
Expand Down Expand Up @@ -581,4 +580,4 @@ def sgplot(sg, new=True, axes=None, style=1, kdata=None):
x=[d.datetime() for d in sell_dates], y=sell_y, fill_color='blue', line_color='blue', size=10
)
return gcf()
return gcf()
45 changes: 2 additions & 43 deletions hikyuu/draw/drawplot/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

#===============================================================================
# ===============================================================================
# history:
# 1. 20171122, Added by fasiondog
#===============================================================================
# ===============================================================================

from hikyuu import Query

Expand Down Expand Up @@ -87,44 +87,3 @@ def get_draw_title(kdata):
stitle = stock.market + "/" + stock.code + ' ' + name + s1

return stitle


def in_interactive_session() -> bool:
"""
Check if we're running in an interactive shell.

Returns
-------
bool
True if running under python/ipython interactive shell.
"""
def check_main():
try:
import __main__ as main
except ModuleNotFoundError:
return False
return not hasattr(main, "__file__")

try:
# error: Name '__IPYTHON__' is not defined
return __IPYTHON__ or check_main() # type: ignore[name-defined]
except NameError:
return check_main()


def in_ipython_frontend() -> bool:
"""
Check if we're inside an IPython zmq frontend.

Returns
-------
bool
"""
try:
# error: Name 'get_ipython' is not defined
ip = get_ipython() # type: ignore[name-defined]
return "zmq" in str(type(ip)).lower()
except NameError:
pass

return False
2 changes: 1 addition & 1 deletion hikyuu/draw/drawplot/matplotlib_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from hikyuu import *

from .common import get_draw_title, in_interactive_session
from .common import get_draw_title


def set_mpl_params():
Expand Down
14 changes: 8 additions & 6 deletions hikyuu/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@
from hikyuu.data.hku_config_template import generate_default_config
from hikyuu import *

# 重定向C++ stdout/stderr输出至python
iodog = OstreamRedirect()
iodog.open()

# 如果是在 jupyter 环境中运行,重定向C++ stdout/stderr输出至python
if in_ipython_frontend():
sm.python_in_jupyter = True
hku_info("hikyuu version: {}", get_version_with_build())
iodog = OstreamRedirect()
iodog.open()

# ==============================================================================
# 引入扯线木偶
Expand Down Expand Up @@ -118,10 +122,8 @@
continue
kdata_param[p] = ini.get('kdata', p)

# set_log_level(LOG_LEVEL.INFO)
# sm = StockManager.instance()
sm.init(base_param, block_param, kdata_param, preload_param, hku_param)
set_log_level(LOG_LEVEL.INFO)
# set_log_level(LOG_LEVEL.INFO)

# 启动行情接收代理
start_spot_agent(False)
Expand Down
9 changes: 6 additions & 3 deletions hikyuu/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
# -*- coding: utf8 -*-
# cp936

#===============================================================================
# ===============================================================================
# 作者:fasiondog
# 历史:120090527, Added by fasiondog
#===============================================================================
# ===============================================================================

#from singleton import Singleton
# from singleton import Singleton

from .mylog import *
from .check import *
from .timeout import *
from .notebook import *

__all__ = [
'spend_time',
Expand Down Expand Up @@ -41,4 +42,6 @@
'with_trace',
'capture_multiprocess_all_logger',
'LoggingContext',
'in_interactive_session',
'in_ipython_frontend',
]
47 changes: 47 additions & 0 deletions hikyuu/util/notebook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/python
# -*- coding: utf8 -*-
#
# Create on: 2024-03-11
# Author: fasiondog

# 来源于 pandas 中对 ipython,notebook 环境的检测代码

def in_interactive_session() -> bool:
"""
Check if we're running in an interactive shell.

Returns
-------
bool
True if running under python/ipython interactive shell.
"""
def check_main():
try:
import __main__ as main
except ModuleNotFoundError:
return False
return not hasattr(main, "__file__")

try:
# error: Name '__IPYTHON__' is not defined
return __IPYTHON__ or check_main() # type: ignore[name-defined]
except NameError:
return check_main()


def in_ipython_frontend() -> bool:
"""
Check if we're inside an IPython zmq frontend. 检测是否在 jupyter 环境中

Returns
-------
bool
"""
try:
# error: Name 'get_ipython' is not defined
ip = get_ipython() # type: ignore[name-defined]
return "zmq" in str(type(ip)).lower()
except NameError:
pass

return False

0 comments on commit 40d53ee

Please sign in to comment.