mirror of
https://gitee.com/fasiondog/hikyuu.git
synced 2024-11-29 18:39:10 +08:00
fixed stock.get_timeline_list 缺失 to_df
This commit is contained in:
parent
fd4720ba76
commit
339ab7b4ae
155
hikyuu/extend.py
155
hikyuu/extend.py
@ -1,9 +1,9 @@
|
||||
#
|
||||
# 对 C++ 引出类和函数进行扩展, pybind11 对小函数到导出效率不如 python 直接执行
|
||||
#
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from datetime import *
|
||||
from .util.slice import list_getitem
|
||||
from .core import *
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
@ -293,94 +293,87 @@ def new_Query_init(self, start=0, end=None, ktype=Query.DAY, recover_type=Query.
|
||||
|
||||
Query.__init__ = new_Query_init
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 增加转化为 np.array、pandas.DataFrame 的功能
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
try:
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
def KData_to_np(kdata):
|
||||
"""转化为numpy结构数组"""
|
||||
if kdata.get_query().ktype in ('DAY', 'WEEK', 'MONTH', 'QUARTER', 'HALFYEAR', 'YEAR'):
|
||||
k_type = np.dtype(
|
||||
{
|
||||
'names': ['datetime', 'open', 'high', 'low', 'close', 'amount', 'volume'],
|
||||
'formats': ['datetime64[D]', 'd', 'd', 'd', 'd', 'd', 'd']
|
||||
}
|
||||
)
|
||||
else:
|
||||
k_type = np.dtype(
|
||||
{
|
||||
'names': ['datetime', 'open', 'high', 'low', 'close', 'amount', 'volume'],
|
||||
'formats': ['datetime64[ms]', 'd', 'd', 'd', 'd', 'd', 'd']
|
||||
}
|
||||
)
|
||||
return np.array(
|
||||
[(k.datetime.datetime(), k.open, k.high, k.low, k.close, k.amount, k.volume) for k in kdata], dtype=k_type
|
||||
)
|
||||
|
||||
def KData_to_df(kdata):
|
||||
"""转化为pandas的DataFrame"""
|
||||
return pd.DataFrame.from_records(KData_to_np(kdata), index='datetime')
|
||||
|
||||
KData.to_np = KData_to_np
|
||||
KData.to_df = KData_to_df
|
||||
|
||||
def PriceList_to_np(data):
|
||||
"""仅在安装了numpy模块时生效,转换为numpy.array"""
|
||||
return np.array(data, dtype='d')
|
||||
|
||||
def PriceList_to_df(data):
|
||||
"""仅在安装了pandas模块时生效,转换为pandas.DataFrame"""
|
||||
return pd.DataFrame(data.to_np(), columns=('Value', ))
|
||||
|
||||
PriceList.to_np = PriceList_to_np
|
||||
PriceList.to_df = PriceList_to_df
|
||||
|
||||
def DatetimeList_to_np(data):
|
||||
"""仅在安装了numpy模块时生效,转换为numpy.array"""
|
||||
return np.array(data, dtype='datetime64[D]')
|
||||
|
||||
def DatetimeList_to_df(data):
|
||||
"""仅在安装了pandas模块时生效,转换为pandas.DataFrame"""
|
||||
return pd.DataFrame(data.to_np(), columns=('Datetime', ))
|
||||
|
||||
DatetimeList.to_np = DatetimeList_to_np
|
||||
DatetimeList.to_df = DatetimeList_to_df
|
||||
|
||||
def TimeLine_to_np(data):
|
||||
"""转化为numpy结构数组"""
|
||||
t_type = np.dtype({'names': ['datetime', 'price', 'vol'], 'formats': ['datetime64[ms]', 'd', 'd']})
|
||||
return np.array([(t.date.datetime(), t.price, t.vol) for t in data], dtype=t_type)
|
||||
|
||||
def TimeLine_to_df(kdata):
|
||||
"""转化为pandas的DataFrame"""
|
||||
return pd.DataFrame.from_records(TimeLine_to_np(kdata), index='datetime')
|
||||
|
||||
TimeLineList.to_np = TimeLine_to_np
|
||||
TimeLineList.to_df = TimeLine_to_df
|
||||
|
||||
def TransList_to_np(data):
|
||||
"""转化为numpy结构数组"""
|
||||
t_type = np.dtype(
|
||||
def KData_to_np(kdata):
|
||||
"""转化为numpy结构数组"""
|
||||
if kdata.get_query().ktype in ('DAY', 'WEEK', 'MONTH', 'QUARTER', 'HALFYEAR', 'YEAR'):
|
||||
k_type = np.dtype(
|
||||
{
|
||||
'names': ['datetime', 'price', 'vol', 'direct'],
|
||||
'formats': ['datetime64[ms]', 'd', 'd', 'd']
|
||||
'names': ['datetime', 'open', 'high', 'low', 'close', 'amount', 'volume'],
|
||||
'formats': ['datetime64[D]', 'd', 'd', 'd', 'd', 'd', 'd']
|
||||
}
|
||||
)
|
||||
return np.array([(t.date.datetime(), t.price, t.vol, t.direct) for t in data], dtype=t_type)
|
||||
else:
|
||||
k_type = np.dtype(
|
||||
{
|
||||
'names': ['datetime', 'open', 'high', 'low', 'close', 'amount', 'volume'],
|
||||
'formats': ['datetime64[ms]', 'd', 'd', 'd', 'd', 'd', 'd']
|
||||
}
|
||||
)
|
||||
return np.array(
|
||||
[(k.datetime.datetime(), k.open, k.high, k.low, k.close, k.amount, k.volume) for k in kdata], dtype=k_type
|
||||
)
|
||||
|
||||
def TransList_to_df(kdata):
|
||||
"""转化为pandas的DataFrame"""
|
||||
return pd.DataFrame.from_records(TransList_to_np(kdata), index='datetime')
|
||||
|
||||
TransList.to_np = TransList_to_np
|
||||
TransList.to_df = TransList_to_df
|
||||
def KData_to_df(kdata):
|
||||
"""转化为pandas的DataFrame"""
|
||||
return pd.DataFrame.from_records(KData_to_np(kdata), index='datetime')
|
||||
|
||||
except:
|
||||
pass
|
||||
|
||||
KData.to_np = KData_to_np
|
||||
KData.to_df = KData_to_df
|
||||
|
||||
|
||||
def DatetimeList_to_np(data):
|
||||
"""仅在安装了numpy模块时生效,转换为numpy.array"""
|
||||
return np.array(data, dtype='datetime64[D]')
|
||||
|
||||
|
||||
def DatetimeList_to_df(data):
|
||||
"""仅在安装了pandas模块时生效,转换为pandas.DataFrame"""
|
||||
return pd.DataFrame(data.to_np(), columns=('Datetime', ))
|
||||
|
||||
|
||||
DatetimeList.to_np = DatetimeList_to_np
|
||||
DatetimeList.to_df = DatetimeList_to_df
|
||||
|
||||
|
||||
def TimeLine_to_np(data):
|
||||
"""转化为numpy结构数组"""
|
||||
t_type = np.dtype({'names': ['datetime', 'price', 'vol'], 'formats': ['datetime64[ms]', 'd', 'd']})
|
||||
return np.array([(t.date.datetime(), t.price, t.vol) for t in data], dtype=t_type)
|
||||
|
||||
|
||||
def TimeLine_to_df(kdata):
|
||||
"""转化为pandas的DataFrame"""
|
||||
return pd.DataFrame.from_records(TimeLine_to_np(kdata), index='datetime')
|
||||
|
||||
|
||||
TimeLineList.to_np = TimeLine_to_np
|
||||
TimeLineList.to_df = TimeLine_to_df
|
||||
|
||||
|
||||
def TransList_to_np(data):
|
||||
"""转化为numpy结构数组"""
|
||||
t_type = np.dtype(
|
||||
{
|
||||
'names': ['datetime', 'price', 'vol', 'direct'],
|
||||
'formats': ['datetime64[ms]', 'd', 'd', 'd']
|
||||
}
|
||||
)
|
||||
return np.array([(t.date.datetime(), t.price, t.vol, t.direct) for t in data], dtype=t_type)
|
||||
|
||||
|
||||
def TransList_to_df(kdata):
|
||||
"""转化为pandas的DataFrame"""
|
||||
return pd.DataFrame.from_records(TransList_to_np(kdata), index='datetime')
|
||||
|
||||
|
||||
TransList.to_np = TransList_to_np
|
||||
TransList.to_df = TransList_to_df
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 增强 Parameter
|
||||
|
@ -62,30 +62,20 @@ Indicator.__getitem__ = indicator_getitem
|
||||
Indicator.__iter__ = indicator_iter
|
||||
|
||||
|
||||
try:
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
def indicator_to_df(indicator):
|
||||
"""转化为pandas.DataFrame"""
|
||||
if indicator.get_result_num() == 1:
|
||||
return pd.DataFrame(indicator.to_np(), columns=[indicator.name])
|
||||
data = {}
|
||||
name = indicator.name
|
||||
columns = []
|
||||
for i in range(indicator.get_result_num()):
|
||||
data[name + str(i)] = indicator.get_result(i)
|
||||
columns.append(name + str(i + 1))
|
||||
return pd.DataFrame(data, columns=columns)
|
||||
|
||||
def indicator_to_df(indicator):
|
||||
"""转化为pandas.DataFrame"""
|
||||
if indicator.get_result_num() == 1:
|
||||
return pd.DataFrame(indicator.to_np(), columns=[indicator.name])
|
||||
|
||||
data = {}
|
||||
name = indicator.name
|
||||
columns = []
|
||||
for i in range(indicator.get_result_num()):
|
||||
data[name + str(i)] = indicator.get_result(i)
|
||||
columns.append(name + str(i + 1))
|
||||
return pd.DataFrame(data, columns=columns)
|
||||
|
||||
Indicator.to_df = indicator_to_df
|
||||
|
||||
except:
|
||||
print(
|
||||
"warning:can't import numpy or pandas lib, ",
|
||||
"you can't use method Inidicator.to_np() and to_df!"
|
||||
)
|
||||
Indicator.to_df = indicator_to_df
|
||||
|
||||
|
||||
def concat_to_df(dates, ind_list, head_stock_code=True, head_ind_name=False):
|
||||
|
@ -31,80 +31,81 @@
|
||||
|
||||
from hikyuu.util.slice import list_getitem
|
||||
from hikyuu import *
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
try:
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
def TradeList_to_np(t_list):
|
||||
"""转化为numpy结构数组"""
|
||||
t_type = np.dtype(
|
||||
{
|
||||
'names': [
|
||||
'交易日期', '证券代码', '证券名称', '业务名称', '计划交易价格', '实际成交价格', '目标价格', '成交数量', '佣金', '印花税', '过户费', '其它成本',
|
||||
'交易总成本', '止损价', '现金余额', '信号来源'
|
||||
],
|
||||
'formats':
|
||||
['datetime64[D]', 'U10', 'U20', 'U10', 'd', 'd', 'd', 'i', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'U5']
|
||||
}
|
||||
)
|
||||
return np.array(
|
||||
[
|
||||
(
|
||||
t.datetime, t.stock.market_code, t.stock.name, get_business_name(t.business), t.plan_price,
|
||||
t.real_price, t.goal_price, t.number, t.cost.commission, t.cost.stamptax, t.cost.transferfee,
|
||||
t.cost.others, t.cost.total, t.stoploss, t.cash, get_system_part_name(t.part)
|
||||
) for t in t_list
|
||||
def TradeList_to_np(t_list):
|
||||
"""转化为numpy结构数组"""
|
||||
t_type = np.dtype(
|
||||
{
|
||||
'names': [
|
||||
'交易日期', '证券代码', '证券名称', '业务名称', '计划交易价格', '实际成交价格', '目标价格', '成交数量', '佣金', '印花税', '过户费', '其它成本',
|
||||
'交易总成本', '止损价', '现金余额', '信号来源'
|
||||
],
|
||||
dtype=t_type
|
||||
)
|
||||
'formats':
|
||||
['datetime64[D]', 'U10', 'U20', 'U10', 'd', 'd', 'd', 'i', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'U5']
|
||||
}
|
||||
)
|
||||
return np.array(
|
||||
[
|
||||
(
|
||||
t.datetime, t.stock.market_code, t.stock.name, get_business_name(t.business), t.plan_price,
|
||||
t.real_price, t.goal_price, t.number, t.cost.commission, t.cost.stamptax, t.cost.transferfee,
|
||||
t.cost.others, t.cost.total, t.stoploss, t.cash, get_system_part_name(t.part)
|
||||
) for t in t_list
|
||||
],
|
||||
dtype=t_type
|
||||
)
|
||||
|
||||
def TradeList_to_df(t):
|
||||
"""转化为pandas的DataFrame"""
|
||||
return pd.DataFrame.from_records(TradeList_to_np(t), index='交易日期')
|
||||
|
||||
TradeRecordList.to_np = TradeList_to_np
|
||||
TradeRecordList.to_df = TradeList_to_df
|
||||
def TradeList_to_df(t):
|
||||
"""转化为pandas的DataFrame"""
|
||||
return pd.DataFrame.from_records(TradeList_to_np(t), index='交易日期')
|
||||
|
||||
def PositionList_to_np(pos_list):
|
||||
"""转化为numpy结构数组"""
|
||||
t_type = np.dtype(
|
||||
{
|
||||
'names': ['证券代码', '证券名称', '买入日期', '已持仓天数', '持仓数量', '投入金额', '当前市值', '盈亏金额', '盈亏比例'],
|
||||
'formats': ['U10', 'U20', 'datetime64[D]', 'i', 'i', 'd', 'd', 'd', 'd']
|
||||
}
|
||||
)
|
||||
|
||||
sm = StockManager.instance()
|
||||
query = Query(-1)
|
||||
data = []
|
||||
for pos in pos_list:
|
||||
invest = pos.buy_money - pos.sell_money + pos.total_cost
|
||||
k = pos.stock.get_kdata(query)
|
||||
cur_val = k[0].close * pos.number
|
||||
bonus = cur_val - invest
|
||||
date_list = sm.get_trading_calendar(Query(Datetime(pos.take_datetime.date())))
|
||||
data.append(
|
||||
(
|
||||
pos.stock.market_code, pos.stock.name, pos.take_datetime, len(date_list), pos.number, invest,
|
||||
cur_val, bonus, 100 * bonus / invest
|
||||
)
|
||||
TradeRecordList.to_np = TradeList_to_np
|
||||
TradeRecordList.to_df = TradeList_to_df
|
||||
|
||||
|
||||
def PositionList_to_np(pos_list):
|
||||
"""转化为numpy结构数组"""
|
||||
t_type = np.dtype(
|
||||
{
|
||||
'names': ['证券代码', '证券名称', '买入日期', '已持仓天数', '持仓数量', '投入金额', '当前市值', '盈亏金额', '盈亏比例'],
|
||||
'formats': ['U10', 'U20', 'datetime64[D]', 'i', 'i', 'd', 'd', 'd', 'd']
|
||||
}
|
||||
)
|
||||
sm = StockManager.instance()
|
||||
query = Query(-1)
|
||||
data = []
|
||||
for pos in pos_list:
|
||||
invest = pos.buy_money - pos.sell_money + pos.total_cost
|
||||
k = pos.stock.get_kdata(query)
|
||||
cur_val = k[0].close * pos.number
|
||||
bonus = cur_val - invest
|
||||
date_list = sm.get_trading_calendar(Query(Datetime(pos.take_datetime.date())))
|
||||
data.append(
|
||||
(
|
||||
pos.stock.market_code, pos.stock.name, pos.take_datetime, len(date_list), pos.number, invest,
|
||||
cur_val, bonus, 100 * bonus / invest
|
||||
)
|
||||
)
|
||||
return np.array(data, dtype=t_type)
|
||||
|
||||
return np.array(data, dtype=t_type)
|
||||
|
||||
def PositionList_to_df(pos_list):
|
||||
"""转化为pandas的DataFrame"""
|
||||
return pd.DataFrame.from_records(PositionList_to_np(pos_list), index='证券代码')
|
||||
def PositionList_to_df(pos_list):
|
||||
"""转化为pandas的DataFrame"""
|
||||
return pd.DataFrame.from_records(PositionList_to_np(pos_list), index='证券代码')
|
||||
|
||||
PositionRecordList.to_np = PositionList_to_np
|
||||
PositionRecordList.to_df = PositionList_to_df
|
||||
|
||||
def Performance_to_df(per):
|
||||
"""将 Performance 统计结果转换为 DataFrame 格式"""
|
||||
return pd.DataFrame(dict(name=per.names(), value=per.values()))
|
||||
PositionRecordList.to_np = PositionList_to_np
|
||||
PositionRecordList.to_df = PositionList_to_df
|
||||
|
||||
Performance.to_df = Performance_to_df
|
||||
|
||||
except:
|
||||
pass
|
||||
def Performance_to_df(per):
|
||||
"""将 Performance 统计结果转换为 DataFrame 格式"""
|
||||
return pd.DataFrame(dict(name=per.names(), value=per.values()))
|
||||
|
||||
|
||||
Performance.to_df = Performance_to_df
|
||||
|
Loading…
Reference in New Issue
Block a user