|
通达信公式02
1.公式
```python
TYP:=(HIGH+LOW+CLOSE)/3;
攻击:(TYP-MA(TYP,10))/(0.015*AVEDEV(TYP,10)),LINETHICK1;
力量:(TYP-MA(TYP,30))/(0.015*AVEDEV(TYP,30)),COLORYELLOW,LINETHICK2;
通达信翻译:
TYP赋值:(最高价+最低价+收盘价)/3
输出攻击:(TYP-TYP的10日简单移动平均)/(0.015*TYP的10日平均绝对偏差),线宽为1
输出力量:(TYP-TYP的30日简单移动平均)/(0.015*TYP的30日平均绝对偏差),画黄色,线宽为2
```
2.代码化处理
```python
#1.该策略数据来源于baostock
#2.相关的计算与通达信的不同,由于对于计算机等常识的不懂,所以无法调用通达信数据进行分析
#但是实际计算的结果和通达信展示的不同,故而先进行计算,看看效果。
#3.重要的一点是,该数据貌似不是用前复权数据,因为从通达信的公式导出来的数据完全没有。
#4.先用回测看一下,所以将数据导入接入进来了。
import baostock as bs
import pandas as pd
import matplotlib.pyplot as plt
pd.set_option('expand_frame_repr',False)
#py文本名字:AttackPower.py
def dfload(code="sh.600789",startday='1990-01-01',endday='2020-01-01'):
#导入数据
lg=bs.login()
rs = bs.query_history_k_data_plus(code,
"date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,isST",
start_date=startday, end_date=endday,
frequency="d", adjustflag="2")
data_list = []
while (rs.error_code == '0') & rs.next():
# 获取一条记录,将记录合并在一起
data_list.append(rs.get_row_data())
result = pd.DataFrame(data_list, columns=rs.fields)
return result
#5.由于baostock的数据导出均为object,所以需要转化
def astypedata(df):
df['high'] = df['high'].astype(float)
df['low'] = df['low'].astype(float)
df['open'] = df['open'].astype(float)
df['close'] = df['close'].astype(float)
df['date'] = pd.to_datetime(df['date'])
return df
#6.计算绝对误差值
def avedev(df):
'''
TYP:=(HIGH+LOW+CLOSE)/3;
攻击:(TYP-MA(TYP,10))/(0.015*AVEDEV(TYP,10)),LINETHICK1;
力量:(TYP-MA(TYP,30))/(0.015*AVEDEV(TYP,30)),COLORYELLOW,LINETHICK2;
金钻突破:力量-REF(力量,1)>90 OR 攻击-REF(攻击,1)>90;
由于金钻突破只有买点,没有买点,所以选择忽视,只以攻击和力量的交叉点进行判断买点卖点。
:param df:
:return:
'''
df['typ'] = (df['high'] + df['low'] + df['close']) / 3
df['avedev'] = abs(df['typ'] - sum(df['typ']) / len(df))
df['avedev10'] = df['avedev'].rolling(10).mean()
df['avedev30'] = df['avedev'].rolling(30).mean()
df['attack'] = (df['typ'] - df['typ'].rolling(10).mean()) / (0.015 * df['avedev10'])
df['power'] = (df['typ'] - df['typ'].rolling(30).mean()) / (0.015 * df['avedev30'])
df=df.fillna(0)
df=round(df,2)
return df
def sign(df):
for i in range(df.shape[0]):
if df['attack'][i]>df['power'][i] and df['attack'][i-1]<=df['power'][i-1]:
df.ix[i,'signal']=1
if df['attack'][i]df['power'][i-1]:
df.ix[i,'signal']=-1
return df
def position(df):
df['position'] = df['signal'].shift()
df['position'].fillna(method='ffill', inplace=True)
# 不能买的信号
cond_cannot_buy = df['open'] > df['close'].shift(1) * 1.097
# 将开盘涨停日、并且当天position为1时的'pos'设置为空值
df.loc[cond_cannot_buy & (df['position'] == 1), 'position'] = None
# 不能卖的信号
# 找出开盘跌停的日期
cond_cannot_sell = df['open'] < df['close'].shift(1) * 0.903 # 今天的开盘价相对于昨天的收盘价下得了9.7%
# 将开盘跌停日、并且当天position为0时的'pos'设置为空值
df.loc[cond_cannot_sell & (df['position'] == -1), 'position'] = None
# position为空的日期,不能买卖。position只能和前一个交易日保持一致。
df['position'].fillna(method='ffill', inplace=True)
# 在position为空值的日期,将position补全为0
df['position'].fillna(value=-1, inplace=True)
return df
#后面就是回测阶段,写到这里就可以了。
```
|
|