Donchian Channel(唐奇安通道指标)是用于交易突破的最古老、最简单的技术指标之一。是由Richard Donchian 提出的一个由三条线(上阻力线、下支撑线、中心线)组成的通道指标。
一般来说,唐奇安通道的宽度越宽,市场波动就越大,而唐奇安通道越窄,市场波动性也就越小 。此外,价格走势可以穿过布林带,但你看不到唐奇安通道的这种特征,因为其波段正在测量特定时期的最高价和最低价
两融新开永久利率100个资金4.2%,300个4.0%
如果有任何疑问或者碰到困难不好解决,可以找下面图片。
代码(一):设定MACD指标函数,通道函数用data.rolling().max()/min()
def MACD(data,short_,long_,m):
'''
data是包含高开低收成交量的标准dataframe
short_,long_,m分别是macd的三个参数
https://zhida.zhihu.com/search?q=%E8%BF%94%E5%9B%9E%E5%80%BC&zhida_source=entity&is_preview=1是包含原始数据和diff,dea,macd三个列的dataframe
'''
data['MACD']=data['close'].ewm(adjust=False,alpha=2/(short_+1),ignore_na=True).mean()-\
data['close'].ewm(adjust=False,alpha=2/(long_+1),ignore_na=True).mean()
data['MACDAvg']=data['MACD'].ewm(adjust=False,alpha=2/(m+1),ignore_na=True).mean()
data['MACDDiff']=2*(data['MACD']-data['MACDAvg'])
data['0'] = data['MACDDiff']*0
return data
'''通道函数'''
def highest(data, length=20):
data = data.rolling(length, min_periods=1).max()
return data
def lowest(data, length=20):
data = data.rolling(length, min_periods=1).min()
return data
代码(二):获取数据并带入MACD指标函数,通道函数
这里为了简化,使用了预先下载好的数据表,也可以替换为读取数据的代码,注意匹配dataframe中变量的名字
"""-----------------获取1小时数据-------------"""
df = pd.read_csv("rb000_1h.csv")[0:50000]
k_length = 4
k_A = []
"-----------4小时数据-------------"
for i in https://zhida.zhihu.com/search?q=range&zhida_source=entity&is_preview=1(len(df)):
if i % (k_length) == 0:
k_A.append(df['close'][i])
k_df = pd.DataFrame(data=k_A, columns=['close'])
"-----------MACD------------"
macd_1 = MACD(df,12,26,9 )
macd_5 = MACD(k_df,12,26,9 )
"-----------1小时通道------------"
highest = highest(df['high'],20)
lowest = lowest(df['low'],20)
k_list_MACD = []
k_list_MACDAvg = []
k_list_MACDDiff = []
k_list_0 = []
for i in range(len(macd_1)):
if i % k_length == 0:
k_list_MACD.append(macd_5['MACD'][i // k_length])
k_list_MACDAvg.append(macd_5['MACDAvg'][i // k_length])
k_list_MACDDiff.append(macd_5['MACDDiff'][i // k_length])
k_list_0.append(0)
.............省略..........
k_macd_value = pd.DataFrame(data=k_list_MACD,columns=['MACD'] )
k_macd_value['k_list_MACDAvg'] = k_list_MACDAvg
k_macd_value['k_list_MACDDiff'] = k_list_MACDDiff
k_macd_value['k_list_0'] = k_list_0
代码(三):计算交易触发信号并储存
"------------空头信号------------"
Sellshort = []
for i in range(len(df)):
if i ==0:
Sellshort.append(0)
if i >0:
if df['low'][i]<lowest[i-1] and k_macd_value['k_list_MACDAvg'][i-1]<0:
Sellshort.append(-1)
elif df['high'][i]>highest[i-1]:
Sellshort.append(1)
else:
Sellshort.append(0)
if Sellshort[i] == 0:
Sellshort[i]=Sellshort[i-1]
"------------多头信号------------"
buy = []
for i in range(len(df)):
if i == 0:
buy.append(0)
if i > 0:
if df['high'][i] > highest[i - 1] and k_macd_value['k_list_MACDAvg'][i - 1] > 0:
buy.append(1)
elif df['low'][i] < lowest[i - 1]:
buy.append(-1)
else:
buy.append(0)
if buy[i] == 0:
buy[i] = buy[i - 1]
"------------多空开仓价格------------"
buy_entry = []
buy_exit = []
for i in range(len(df)):
if buy[i]==1 and buy[i-1] == 0:
buy_entry.append(highest[i]+1)
if buy[i]==1 and buy[i-1] == -1:
buy_entry.append(highest[i]+1)
if buy[i] == -1 and buy[i - 1] == 1:
buy_exit.append(lowest[i-1] - 1)
Sellshort_entry = []
Sellshort_exit = []
for i in range(len(df)):
if Sellshort[i]==-1 and Sellshort[i-1] == 0:
Sellshort_entry.append(lowest[i]-1)
if Sellshort[i]==-1 and Sellshort[i-1] == 1:
Sellshort_entry.append(lowest[i]-1)
if Sellshort[i] == 1 and Sellshort[i - 1] == -1:
Sellshort_exit.append(highest[i-1] + 1)
代码(四):对交易结果进行统计并展示
"-----统计空头累积、单笔盈亏、盈亏次数-----"
for i in range(min(len(Sellshort_entry),len(Sellshort_exit) )):
S = S + (Sellshort_entry[i] - Sellshort_exit[i]) #-------"空总盈亏"
sellshort_profit.append(Sellshort_entry[i] - Sellshort_exit[i]) #-------"空单笔盈亏"
if sellshort_profit[i]>0:
sellshort_win_times = sellshort_win_times + 1 #-------"空盈利次数"
sellshort_win_profit = sellshort_win_profit + sellshort_profit[i]
if sellshort_profit[i]<=0:
sellshort_fail_times = sellshort_fail_times + 1 #-------"空亏损次数"
sellshort_fail_profit = sellshort_fail_profit + sellshort_profit[i]
"-----统计多头累积、单笔盈亏、盈亏次数-----"
for i in range(min(len(buy_entry),len(buy_exit) )):
B = B + (-buy_entry[i] + buy_exit[i]) #-------"多总盈亏"
buy_profit.append(-buy_entry[i] + buy_exit[i]) #-------"多单笔盈亏"
if buy_profit[i]>0:
buy_win_times = buy_win_times + 1 #-------"多盈利次数"
buy_win_profit = buy_win_profit + buy_profit[i]
if buy_profit[i]<=0:
buy_fail_times = buy_fail_times + 1 #-------"多亏损次数"
buy_fail_profit = buy_fail_profit + buy_profit[i]
win_times_all = sellshort_win_times+buy_win_times
fail_times_all = sellshort_fail_times+buy_fail_times
win_profit_all =sellshort_win_profit+buy_win_profit
fail_profit_all =sellshort_fail_profit+buy_fail_profit
胜率 = win_times_all/(win_times_all+fail_times_all)
https://zhida.zhihu.com/search?q=%E7%9B%88%E4%BA%8F%E6%AF%94&zhida_source=entity&is_preview=1 = win_profit_all /-fail_profit_all
print('累积盈亏:', win_profit_all+fail_profit_all)
print('胜率:', 胜率)
print('盈亏比:', 盈亏比)
print('盈利次数:', win_times_all)
print('亏损次数:', fail_times_all)
print('总盈利:', win_profit_all)
print('总亏损:', fail_profit_all)
总结
此模型仅仅让读者学习其思路并不能作为实盘模型。
本文所提到的观点仅代表个人的意见,所涉及标的不作推荐,据此买卖,风险自负。