"""
1. 期货品种的沉淀资金是未平仓的期货合约(持仓量)所占用的保证金.
期货品种的沉淀资金越大说明这个品种非常受资金喜欢.
相反,如果期货品种的沉淀资金越小,说明这个品种越冷门.
2. 期货交易的本质就是资金的重新分配, 品种的沉淀资金越大, 未来有出现较大级别行情的机会就越大.
"""
import os
import re
from public_module.tqz_extern.tools.pandas_operator.pandas_operator import pandas
from public_module.tqz_extern.tools.file_path_operator.file_path_operator import TQZFilePathOperator
from public_module.tqz_extern.tools.position_operator.position_operator import TQZJsonOperator
from server_api.api.tqz_tianqin_api import TQZTianQinDataManager
class TQZSedimentaryFund:
__sed_fund_excel_path = TQZFilePathOperator.father_path(
source_path=__file__
) + f'/sed_fund.xlsx'
@classmethod
def create_sed_fund_excel(cls, days: int = 100):
if os.path.exists(path=cls.__sed_fund_excel_path):
print(f'{cls.__sed_fund_excel_path} is exist.')
return
content = cls.__contract_multiple_jsonfile_content()
# tq_m_symbols & data source of tq
tq_m_symbols = []
for key in list(content.keys()):
tq_m_symbols.append(f'KQ.m@{key}')
tq_result = TQZTianQinDataManager.load_main_history_bars_map(
tq_m_symbols=tq_m_symbols,
tq_duration_seconds=86400,
tq_data_length=days
)
# per sed_fund_df
sed_fund_df_map = {}
for tq_m_symbol, bars in tq_result.items():
sed_fund_df = pandas.DataFrame(columns=['date', tq_m_symbol])
contract_multiple = content[tq_m_symbol.split('@')[1]]["contract_multiple"]
for bar in bars:
ret_datetime_str = re.match('(\d+)-(\d+)-(\d+)', bar.datetime).group()
new_row = len(sed_fund_df)
sed_fund_df.loc[new_row, 'date'] = ret_datetime_str
sed_fund_df.loc[new_row, tq_m_symbol] = bar.close_interest * 2 * bar.close_price * contract_multiple * 0.1
sed_fund_df.set_index('date', inplace=True)
sed_fund_df_map[tq_m_symbol] = sed_fund_df
# merge dateframe
new_sed_fund_df = pandas.DataFrame()
for tq_m_symbol, sed_fund_df in sed_fund_df_map.items():
if len(new_sed_fund_df) is 0:
new_sed_fund_df = sed_fund_df
continue
new_sed_fund_df[tq_m_symbol] = sed_fund_df[tq_m_symbol]
new_sed_fund_df.reset_index(inplace=True)
excel_writer = pandas.ExcelWriter(path=cls.__sed_fund_excel_path)
new_sed_fund_df.to_excel(excel_writer, sheet_name='sed_fund', index=False, freeze_panes=(1, 1))
excel_writer.save()
# --- private part ---
@classmethod
def __contract_multiple_jsonfile_content(cls):
return TQZJsonOperator.tqz_load_jsonfile(
jsonfile=TQZFilePathOperator.father_path(
source_path=__file__
) + f'/base_data/future_contracts_setting.json'
)
if __name__ == '__main__':
TQZSedimentaryFund.create_sed_fund_excel(days=300)
量化交易之回测篇 - 拉取&合成历史沉淀资金数据(主连合约)
于 2022-04-08 15:56:41 首次发布