2017-09-23 02:15:15 +08:00
|
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- coding: utf8 -*-
|
|
|
|
|
# cp936
|
|
|
|
|
#
|
|
|
|
|
# The MIT License (MIT)
|
|
|
|
|
#
|
|
|
|
|
# Copyright (c) 2017 fasiondog
|
|
|
|
|
#
|
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
|
#
|
|
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
# copies or substantial portions of the Software.
|
|
|
|
|
#
|
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
# SOFTWARE.
|
|
|
|
|
|
2020-06-25 15:59:37 +08:00
|
|
|
|
from hikyuu.cpp.core import *
|
2023-12-27 22:48:41 +08:00
|
|
|
|
from hikyuu import toPriceList, Datetime
|
2017-09-23 02:15:15 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def indicator_iter(indicator):
|
|
|
|
|
for i in range(len(indicator)):
|
|
|
|
|
yield indicator[i]
|
|
|
|
|
|
2017-09-24 03:25:14 +08:00
|
|
|
|
|
2019-05-20 01:45:46 +08:00
|
|
|
|
def indicator_getitem(data, i):
|
2019-05-21 23:10:50 +08:00
|
|
|
|
"""
|
|
|
|
|
:param i: int | Datetime | slice | str 类型
|
2019-05-20 01:45:46 +08:00
|
|
|
|
"""
|
|
|
|
|
if isinstance(i, int):
|
|
|
|
|
length = len(data)
|
|
|
|
|
index = length + i if i < 0 else i
|
|
|
|
|
if index < 0 or index >= length:
|
|
|
|
|
raise IndexError("index out of range: %d" % i)
|
|
|
|
|
return data.get(index)
|
2020-06-25 15:59:37 +08:00
|
|
|
|
|
2019-05-20 01:45:46 +08:00
|
|
|
|
elif isinstance(i, slice):
|
|
|
|
|
return [data.get(x) for x in range(*i.indices(len(data)))]
|
|
|
|
|
|
|
|
|
|
elif isinstance(i, Datetime):
|
2020-07-11 18:26:23 +08:00
|
|
|
|
return data.get_by_date(i)
|
2019-05-20 01:45:46 +08:00
|
|
|
|
|
|
|
|
|
elif isinstance(i, str):
|
2020-07-11 18:26:23 +08:00
|
|
|
|
return data.get_by_date(Datetime(i))
|
2019-05-20 01:45:46 +08:00
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
raise IndexError("Error index type")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Indicator.__getitem__ = indicator_getitem
|
|
|
|
|
Indicator.__iter__ = indicator_iter
|
2017-09-24 03:25:14 +08:00
|
|
|
|
|
2020-07-11 18:26:23 +08:00
|
|
|
|
|
|
|
|
|
def PRICELIST(data, result_index=0, discard=0):
|
2017-09-23 02:15:15 +08:00
|
|
|
|
"""
|
|
|
|
|
将 list、tuple、Indicator 转化为普通的 Indicator
|
2023-12-27 22:48:41 +08:00
|
|
|
|
|
2017-09-23 02:15:15 +08:00
|
|
|
|
:param data: 输入数据,可以为 list、tuple、Indicator
|
2020-07-11 18:26:23 +08:00
|
|
|
|
:param int result_index: 当data为Indicator实例时,指示Indicator的第几个结果集
|
2017-09-23 02:15:15 +08:00
|
|
|
|
:param int discard: 在 data 为 Indicator类型时无效。表示前端抛弃的数据点数,抛弃的值使用 constant.null_price 填充
|
|
|
|
|
:return: Indicator
|
|
|
|
|
"""
|
2020-07-12 20:46:34 +08:00
|
|
|
|
import hikyuu.cpp.core as ind
|
2017-09-23 02:15:15 +08:00
|
|
|
|
if isinstance(data, ind.Indicator):
|
2020-07-11 18:26:23 +08:00
|
|
|
|
return ind.PRICELIST(data, result_index)
|
2017-09-23 02:15:15 +08:00
|
|
|
|
else:
|
|
|
|
|
return ind.PRICELIST(toPriceList(data), discard)
|
2020-06-25 15:59:37 +08:00
|
|
|
|
|
2017-09-23 02:15:15 +08:00
|
|
|
|
|
2020-09-13 21:41:39 +08:00
|
|
|
|
VALUE = PRICELIST
|
|
|
|
|
|
2017-09-23 02:15:15 +08:00
|
|
|
|
try:
|
|
|
|
|
import numpy as np
|
|
|
|
|
import pandas as pd
|
2020-06-25 15:59:37 +08:00
|
|
|
|
|
2017-09-23 02:15:15 +08:00
|
|
|
|
def indicator_to_np(indicator):
|
|
|
|
|
"""转化为np.array,如果indicator存在多个值,只返回第一个"""
|
|
|
|
|
return np.array(indicator, dtype='d')
|
2020-06-25 15:59:37 +08:00
|
|
|
|
|
2017-09-23 02:15:15 +08:00
|
|
|
|
def indicator_to_df(indicator):
|
|
|
|
|
"""转化为pandas.DataFrame"""
|
2020-07-11 18:26:23 +08:00
|
|
|
|
if indicator.get_result_num() == 1:
|
2017-09-23 02:15:15 +08:00
|
|
|
|
return pd.DataFrame(indicator_to_np(indicator), columns=[indicator.name])
|
2020-06-25 15:59:37 +08:00
|
|
|
|
|
2017-09-23 02:15:15 +08:00
|
|
|
|
data = {}
|
|
|
|
|
name = indicator.name
|
|
|
|
|
columns = []
|
2020-07-11 18:26:23 +08:00
|
|
|
|
for i in range(indicator.get_result_num()):
|
|
|
|
|
data[name + str(i)] = indicator.get_result(i)
|
2020-06-25 15:59:37 +08:00
|
|
|
|
columns.append(name + str(i + 1))
|
2017-09-23 02:15:15 +08:00
|
|
|
|
return pd.DataFrame(data, columns=columns)
|
2020-06-25 15:59:37 +08:00
|
|
|
|
|
2017-09-23 02:15:15 +08:00
|
|
|
|
Indicator.to_np = indicator_to_np
|
|
|
|
|
Indicator.to_df = indicator_to_df
|
|
|
|
|
|
|
|
|
|
except:
|
2020-06-25 15:59:37 +08:00
|
|
|
|
print(
|
|
|
|
|
"warning:can't import numpy or pandas lib, ",
|
|
|
|
|
"you can't use method Inidicator.to_np() and to_df!"
|
|
|
|
|
)
|
2017-09-25 03:29:18 +08:00
|
|
|
|
|
2023-12-27 22:48:41 +08:00
|
|
|
|
VALUE = PRICELIST
|