上传文件至 ''

main
yz 2 years ago
parent 89e2c9e89d
commit 77e588d16a

@ -0,0 +1,214 @@
import itertools
from datetime import datetime
import pandas as pd
from pprint import pprint
import pyTSL
TINYSOFT_HOSTNAME = 'tsl.tinysoft.com.cn'
TINYSOFT_PORT = 443
TINYSOFT_USERNAME = 'xunuo2005'
TINYSOFT_PASSWORD = '20220613'
class TSLfm:
def __init__(self):
self.timeout_default=100000
def __enter__(self):
print('entering')
self.c = pyTSL.Client(TINYSOFT_USERNAME, TINYSOFT_PASSWORD, TINYSOFT_HOSTNAME, 443)
self.c.login()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print('logging out')
self.c.logout()
del(self.c)
def get_code_list(self, bk_name='国债期货'):
scpt = """return getbk('{bk_name}');
""".format(bk_name=bk_name)
print(scpt)
r = self.c.exec(scpt,timeout=self.timeout_default)
return r.value()
def get_trade_tick(self, start_date, end_date, code_list):
"""
(source: 天软数据字典 ver 2022-05-25)
vol: 周期内的成交量如果周期为分钟则表示这分钟的成 交量并非从开盘累计.
对于期货高频和日线的单位都是合约数量如果要建立成交量和成交金额之间的关系还需考虑合约乘数
amount: 周期内的成交金额如果周期为分钟则表示这分钟的成交金额并非从开盘累计
cjbs: 对于期货/期权而言是周期内的持仓的变动量日线的话表示当日持仓量单位为:
wb: 公式:委买/委卖(w_buy/w_sale)
lb: 由天软计算:
1对于交易明细:
量比=现成交总手/[(过去 5 个交易日平均每分钟成交 )×当日累计开市时间()]
2对于秒线/1 分钟线: 量比取周期内最后一条交易明细的数据
zmm: tradetable 公式:按下面的前后逻辑优先判定
没有成交 -> 0
当前成交价>上一笔买一价 ->主买 1
当前成交价<上一笔卖一价 ->主卖 2
否则->3(一般是集合竞价涨停跌停)
对于非交易明细(秒线或分钟线等):没有意义
buy_vol: 主动性买盘成交量:周期内买卖标识为 1 的成交量+ 卖标识为 3 的成交量/2
buy_amount: 主动性买盘成交金额:周期内买卖标识为 1 的成交金额 +买卖标识为 3 的成交金额/2
sale_vol: 主动性卖盘成交量:周期内买卖标识为 2 的成交量之和 +买卖标识为 3 的成交量/2
sale_amount: 主动性卖盘成交金额:周期内买卖标识为 2 的成交金额 +买卖标识为 3 的成交金额/2
w_buy: 一个周期内的委买量
sectional: 从开盘到当前时间的
sectional_yclose: 上一交易日(周期是日)的收盘价上次价不一 上次价表示前一个周期的收盘价
sectional_vol: 从开盘到当前时间的成交量之和(主买+主卖)
sectional_amount: 从开盘到当前时间的成交金额之和(主买+主卖)
sectional_cjbs: 股指期货/期权:市场现有持仓量
sectional_wb: 当日累计委买/当日累计委卖
['sectional_open'] as 'm_nAccOpen', //时点当日开盘, 没必要存取上日2059的即刻
"""
code_list_input = ', '.join(['\'%s\'' % code_id for code_id in code_list])
scpt = """
stock_list := Array({code_list_input});
r := select
['StockID'] as 'code',
DateTimeToStr(['date']) as 'datetime',
['price'] as 'm_nPrice',
['open'] as 'm_nOpen',
['high'] as 'm_nHigh',
['low'] as 'm_nLow',
['close'] as 'm_nClose',
['sectional_high'] as 'm_nAccHigh',
['sectional_low'] as 'm_nAccLow',
['vol'] as 'm_iVolume', //成交量
['sectional_vol'] as 'm_iAccVolume', //时点当日累计成交量
['cjbs'] as 'm_nMatchItems', // 成交笔数 周期内的持仓的变动量
['sectional_cjbs'] as 'm_nAccMatchItems',
['amount'] as 'm_iTurover', //成交金额
['sectional_amount'] as 'm_iAccTurover',
['yclose'] as 'm_nPreClose', //上一周期的收盘价
['sectional_yclose'] as 'm_nAccPreClose', // 前日收盘
['buy1'] as 'm_nBidPrice', //买一价叫卖价?
['bc1'] as 'm_nBidVolume', //买一量 当前以买一价出价的委买量
['sale1'] as 'm_nAskPrice',
['sc1'] as 'm_nAskVolume',
['zmm'] as 'm_iABFlag', //买卖标识
['buy_vol'] as 'm_nBidAvVolume', //主买量
['sectional_buy_vol'] as 'm_nAccBidAvVolume', //时点当日累计主买量
['buy_amount'] as 'm_nBidAvTurover', //主买金额
['sectional_buy_amount'] as 'm_nAccBidAvTurover',
['sale_vol'] as 'm_nAskAvVolume',
['sectional_sale_vol'] as 'm_nAccAskAvVolume',
['sale_amount'] as 'm_nAskAvTurover',
['sectional_sale_amount'] as 'm_nAccAskAvTurover',
['w_buy'] as 'm_nBidOrder', //委买
['sectional_w_buy'] as 'm_nAccBidOrder',
['w_sale'] as 'm_nAskOrder',
['sectional_w_sale'] as 'm_nAccAskOrder',
['wb'] as 'm_nABOrderRate', //委比
['sectional_wb'] as 'm_nAccABOrderRate', //时点当日累计委比
['lb'] as 'm_nMItemsVolRate'//量比
from tradetable
datekey {start_date}T to {end_date}T+0.999
of stock_list
end;
return r;
""".format(
code_list_input=code_list_input,
start_date=start_date,
end_date=end_date
)
print(scpt)
r = self.c.exec(scpt,timeout=self.timeout_default)
return r
def get_mkt_min_k(self, start_date, end_date, code_list):
code_list_input = ', '.join(['\'%s\'' % code_id for code_id in code_list])
scpt = """
SetSysParam(pn_cycle(), cy_1m());
stock_list := Array({code_list_input});
r := select
['StockID'] as 'code',
DateTimeToStr(['date']) as 'datetime',
['price'] as 'm_nPrice',
['open'] as 'm_nOpen',
['high'] as 'm_nHigh',
['low'] as 'm_nLow',
['close'] as 'm_nClose',
['sectional_open'] as 'm_nAccOpen', //时点当日开盘
['sectional_high'] as 'm_nAccHigh',
['sectional_low'] as 'm_nAccLow',
['vol'] as 'm_iVolume', //成交量
['sectional_vol'] as 'm_iAccVolume', //时点当日累计成交量
['cjbs'] as 'm_nMatchItems', // 成交笔数 周期内的持仓的变动量
['sectional_cjbs'] as 'm_nAccMatchItems',
['amount'] as 'm_iTurover', //成交金额
['sectional_amount'] as 'm_iAccTurover',
['yclose'] as 'm_nPreClose', //上一周期的收盘价
['sectional_yclose'] as 'm_nAccPreClose', // 前日收盘
['buy1'] as 'm_nBidPrice', //买一价叫卖价?
['bc1'] as 'm_nBidVolume', //买一量 当前以买一价出价的委买量
['sale1'] as 'm_nAskPrice',
['sc1'] as 'm_nAskVolume',
['zmm'] as 'm_iABFlag', //买卖标识
['buy_vol'] as 'm_nBidAvVolume', //主买量
['sectional_buy_vol'] as 'm_nAccBidAvVolume', //时点当日累计主买量
['buy_amount'] as 'm_nBidAvTurover', //主买金额
['sectional_buy_amount'] as 'm_nAccBidAvTurover',
['sale_vol'] as 'm_nAskAvVolume',
['sectional_sale_vol'] as 'm_nAccAskAvVolume',
['sale_amount'] as 'm_nAskAvTurover',
['sectional_sale_amount'] as 'm_nAccAskAvTurover',
['w_buy'] as 'm_nBidOrder', //委买
['sectional_w_buy'] as 'm_nAccBidOrder',
['w_sale'] as 'm_nAskOrder',
['sectional_w_sale'] as 'm_nAccAskOrder',
['wb'] as 'm_nABOrderRate', //委比
['sectional_wb'] as 'm_nAccABOrderRate', //时点当日累计委比
['lb'] as 'm_nMItemsVolRate'//量比
from markettable
datekey {start_date}T to {end_date}T+0.999
of stock_list
end;
return r;
""".format(
code_list_input=code_list_input,
start_date=start_date,
end_date=end_date
)
print(scpt)
r = self.c.exec(scpt,timeout=self.timeout_default)
return r
Loading…
Cancel
Save