hikyuu2/hikyuu/trade_manage/trade.py
2020-07-17 00:26:03 +08:00

119 lines
4.6 KiB
C++

#!/usr/bin/python
# -*- coding: utf8 -*-
# cp936
#
# The MIT License (MIT)
#
# Copyright (c) 2010-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.
#===============================================================================
# History:
# 1. 20130213, Added by fasiondog
#===============================================================================
from hikyuu.util.slice import list_getitem
from hikyuu import *
BorrowRecordList.__getitem__ = list_getitem
PositionRecordList.__getitem__ = list_getitem
TradeRecordList.__getitem__ = list_getitem
BorrowRecordList.__str__ = lambda self: str(list(self))
BorrowRecordList.__repr__ = lambda self: repr(list(self))
PositionRecordList.__str__ = lambda self: str(list(self))
PositionRecordList.__repr__ = lambda self: repr(list(self))
TradeRecordList.__str__ = lambda self: str(list(self))
TradeRecordList.__repr__ = lambda self: repr(list(self))
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.planPrice, t.realPrice, t.goalPrice, 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 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)
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
except:
pass