写在前面:
1. 本文中提到的“K线形态查看工具”的具体使用操作请查看该博文;
2. K线形体所处背景,诸如处在上升趋势、下降趋势、盘整等,背景内容在K线形态策略代码中没有体现;
3. 文中知识内容来自书籍《K线技术分析》by邱立波。
目录
十字线是上下影线都比较短的同价位线,十字星是实体和上下影线都很短的K线,一般实体长度不超过上一交易日价格的0.5%,大盘十字星的实体长度要更小一些。

单根十字星和十字线表明交易双方都无心应战,只是试探性地进行了一些攻防。多方或空方仅仅取得微弱优势,基本上可以看作双方势均力敌,表明行情将会进一步沿着原有的方向运行。但处在支撑与压力位的十字线和十字星是转势信号,这是因为在支撑与压力位收出十字星和十字线,表明进攻的一方气力将尽,而另一方已经开始试探性反击。
1)既可以出现在涨势中,也可以出现在跌势中。
2)十字线的开盘价和收盘价相同,十字星的实体长度很短。
3)上下影线都很短。
1)在股价或指数已有较大涨幅后出现,是见顶信号。
2)在股价或指数已有较大跌幅后出现,是见底信号。
3)在上涨途中出现十字星和十字线,继续看涨,是持仓信号。
4)在下跌途中出现十字线和十字星,继续看跌,应持币观望。
- def excute_strategy(daily_file_path):
- '''
- 名称:十字星和十字线
- 识别:实体长度不超过上一交易日价格0.5%,上下影线很短
- 自定义:影线很短=》不超过上一交易日价格1.5%
- 前置条件:计算时间区间 2021-01-01 到 2022-01-01
- :param daily_file_path: 股票日数据文件路径
- :return:
- '''
- import pandas as pd
- import os
- start_date_str = '2021-01-01'
- end_date_str = '2022-01-01'
- df = pd.read_csv(daily_file_path,encoding='utf-8')
- # 删除停牌的数据
- df = df.loc[df['openPrice'] > 0].copy()
- df['o_date'] = df['tradeDate']
- df['o_date'] = pd.to_datetime(df['o_date'])
- df = df.loc[(df['o_date'] >= start_date_str) & (df['o_date']<=end_date_str)].copy()
- # 保存未复权收盘价数据
- df['close'] = df['closePrice']
- # 计算前复权数据
- df['openPrice'] = df['openPrice'] * df['accumAdjFactor']
- df['closePrice'] = df['closePrice'] * df['accumAdjFactor']
- df['highestPrice'] = df['highestPrice'] * df['accumAdjFactor']
- df['lowestPrice'] = df['lowestPrice'] * df['accumAdjFactor']
-
- # 开始计算
- df.loc[df['closePrice']>=df['openPrice'],'type'] = 1
- df.loc[df['closePrice']
'openPrice'],'type'] = -1 -
- df['body_length'] = abs(df['closePrice'] - df['openPrice'])
- df.loc[df['type']==1,'top_shadow_length'] = df['highestPrice'] - df['closePrice']
- df.loc[df['type']==-1,'top_shadow_length'] = df['highestPrice'] - df['openPrice']
- df.loc[df['type']==1,'bottom_shadow_length'] = df['openPrice'] - df['lowestPrice']
- df.loc[df['type']==-1,'bottom_shadow_length'] = df['closePrice'] - df['lowestPrice']
-
- df['signal'] = 0
- df['signal_name'] = 0
- df.loc[(df['body_length']/df['closePrice'].shift(1)<=0.005) & (df['top_shadow_length']/df['closePrice'].shift(1)<0.015) & (df['bottom_shadow_length']/df['closePrice'].shift(1)<0.015),'signal'] = 1
- df.loc[(df['body_length']/df['closePrice'].shift(1)<=0.005) & (df['top_shadow_length']/df['closePrice'].shift(1)<0.015) & (df['bottom_shadow_length']/df['closePrice'].shift(1)<0.015),'signal_name'] = (df['body_length']/df['closePrice'].shift(1))*100
- df = df.round({'signal_name':4})
-
- file_name = os.path.basename(daily_file_path)
- title_str = file_name.split('.')[0]
-
- line_data = {
- 'title_str':title_str,
- 'whole_header':['日期','收','开','高','低'],
- 'whole_df':df,
- 'whole_pd_header':['tradeDate','closePrice','openPrice','highestPrice','lowestPrice'],
- 'start_date_str':start_date_str,
- 'end_date_str':end_date_str,
- 'signal_type':'line'
- }
- return line_data
