一、指標說明
adtm指標(動態買賣氣指標,指標解釋來自mba智庫百科)
動態買賣氣指標(adtm)是用開盤價的向上波動幅度和向下波動幅度的距離差值來描述人氣高低的指標。
計算公式(通達信)
dtm:=if(open<=ref(open,1),0,max((high-open),(open-ref(open,1))));
dbm:=if(open>=ref(open,1),0,max((open-low),(open-ref(open,1))));
stm:=sum(dtm,n);
sbm:=sum(dbm,n);
adtm:if(stm>sbm,(stm-sbm)/stm,if(stm=sbm,0,(stm-sbm)/sbm));
maadtm:ma(adtm,m);
# -*- coding: utf-8 -*-
"""created on wed jan 15 22:31:40 2020
@author: yzp
"""import scipy as sp
import numpy as np
import pandas as pd
import streamlit as st
import os
__config = 'config.dat'
__version = pd.read_pickle(__config).loc['version', 'val']
def common1(security_list, data):
if len(security_list) == 1:
res = data[0]
else:
res = dict(zip(security_list, data))
return res
def get_file_uri(security):
filename = '{}_{}.pkl'.format(security, __version)
file_dir = os.path.join(os.getcwd(), 'out')
file_uri = os.path.join(file_dir, filename)
return file_uri
#adef adtm(security_list, check_date, n = 23, m = 8, unit = '1d', include_now = true, fq_ref_date = none):
def cal(data, c_type):
# c_type=0 dtm = if(open<=ref(open,1),0,max((high-open),(open-ref(open,1))))
# c_type=1 dbm = if(open>=ref(open,1),0,max((open-low),(open-ref(open,1))))
# print(data)
if np.isnan(data['ref_open']):
# print('nan')
return np.nan
# if data['open'] <= data['ref_open']:
# res = 0 * (1 - c_type) + max(data['open'] - data['low'], data['open'] - data['ref_open']) * c_type
# else:
# res = max(data['high'] - data['open'], data['open'] - data['ref_open']) * (1 - c_type) + 0 * c_type
if data['open'] < data['ref_open']:
res = 0 * (1 - c_type) + max(data['open'] - data['low'], data['open'] - data['ref_open']) * c_type
elif data['open'] > data['ref_open']:
res = max(data['high'] - data['open'], data['open'] - data['ref_open']) * (1 - c_type) + 0 * c_type
else:
res = 0
return res
def cal2(data):
# adtm = if(stm>sbm,(stm-sbm)/stm,if(stm=sbm,0,(stm-sbm)/sbm))
if np.isnan(data['stm']):
return np.nan
if data['stm'] > data['sbm']:
res = (data['stm'] - data['sbm']) / data['stm']
else:
res = (data['stm'] - data['sbm']) / data['sbm']
return res
def helper(security, check_date, n = 23, m = 8, unit = '1d', include_now = true, fq_ref_date = none):
file_uri = get_file_uri(security)
df = pd.read_pickle(file_uri)
df['ref_open'] = df['open'].shift(1)
# print(df)
mid = pd.dataframe()
mid['stm'] = dtm.rolling(n).sum()
mid['sbm'] = dbm.rolling(n).sum()
# return res.rolling(m).mean()
return res
_res = [helper(s, check_date, n, m, unit, include_now, fq_ref_date) for s in security_list]
return common1(security_list, _res)
se = ['600000.xshg']
func_dict =
tech_index = st.sidebar.selectbox('指標', list(func_dict.keys()))
if tech_index != :
st.write('測試{} 的{}指標'.format(se[0], tech_index))
res = func_dict.get(tech_index)(se,'1')
else:
st.write('測試{} 的ma指標'.format(se[0]))
res = func_dict.get('ma')(se, '1')
st.line_chart(res, 1024, 800)
三、結果展示與驗證(與聚寬資料驗證)
四、經驗教訓
1.在輔助函式cal()中,計算dtm,和dbm時,為了統一表達,使用了當前注釋掉的部分,驗證時,無論使用通訊達版券商軟體,或者聚寬,均出現了計算結果有微小差異的情形,開始以為計算精度不一致導致的差異,但分步除錯發現dbm有遠大於精度損失誤差的差異,再次瀏覽**,發現是在不等號統一過程中,忽略對「等號」情形進行相應變化導致,隨後將**進行當前修改,測試資料完全吻合。
2.在除錯過程中,可以在券商軟體修改公式,分步驗證,券商軟體雖然顯示精度較低,但計算結果並不低,資料是很精確的,不該在出現差異時首先懷疑券商軟體精度損失。