Python技術分析API使用手冊

Python技術分析API範例程式

1. 系統需求

  • Python 3.0以上版本

  • 交易python package

2. 環境準備

1

安裝交易API package: tech_analysis_api_v2-0.0.2-py3-none-win32.whl

2

建議可先參考執行Sample內容,確認執行內容後,再依此文件說明進行API開發

3. 執行Sample

3.1 執行登入:

Docs UI Kit

輸入畫面

Docs UI Kit

3.2 選取1指標

Docs UI Kit

3.3 設定商品,輸入證券代碼

Docs UI Kit

3.4 設定分K, 輸入1 ,3, 5K

Docs UI Kit

3.5 設定技術指標, 輸入SMA, EMA, WMA, SAR, RSI, MACD, KD, CDP, BBands

Docs UI Kit

3.6 設定日期, 輸入查詢日期

Docs UI Kit

3.7 選取2歷史成交

Docs UI Kit

如範例查詢2330日期20230724當天的成交資料

                
				elif opt == '2':
					lsBS, sErrMsg = ta.GetHisBS_Stock('2330', '20230724')
					for x in lsBS:
							msg = f'代號: {x.Prod}, 成交時間: {x.March_Time}, ' \
							    + f'成交價格: {x.March_Price}, 成交數量: {x.March_Quantity}, ' \
								+ f'試搓: {x.Is_TryMarch}, 買賣: {x.BS}, ' \
							
							print(msg)
                
              

回傳結果

Docs UI Kit

4. 回報狀態事件

4.1 OnDigitalSSOEvent

數位API驗證成功與否
                
                  def OnDigitalSSOEvent(aIsOK, aMsg):
                        print(f'OnDigitalSSOEvent: {aIsOK} {aMsg}')
                
              

4.2 OnTAConnStuEvent

連線狀態回報
                
                  def OnTAConnStuEvent(aIsOK):
                        print(aIsOK)
                
              

4.3 OnUpdate

當日指標資料
                
				def OnUpdate(ta_Type: eTA_Type, aResultPre, aResultLast):
					if aResultPre != None:
						if ta_Type == eTA_Type.SMA:
							print(f'前K {str(aResultPre)}')
						if ta_Type == eTA_Type.EMA:
							print(f'前K {str(aResultPre)}')
						if ta_Type == eTA_Type.WMA:
							print(f'前K {str(aResultPre)}')
						if ta_Type == eTA_Type.SAR:
							print(f'前K {str(aResultPre)}')
						if ta_Type == eTA_Type.RSI:
							print(f'前K {str(aResultPre)}')
						if ta_Type == eTA_Type.MACD:
							print(f'前K {str(aResultPre)}')
						if ta_Type == eTA_Type.KD:
							print(f'前K {str(aResultPre)}')
						if ta_Type == eTA_Type.CDP:
							print(f'前K {str(aResultPre)}')
						if ta_Type == eTA_Type.BBands:
							print(f'前K {str(aResultPre)}')
					
					if aResultLast != None:
						if ta_Type == eTA_Type.SMA:
							print(f'最新 Time:{aResultLast.KBar.TimeSn_Dply}, SMA:{aResultLast.Value}')
						if ta_Type == eTA_Type.EMA:
							print(f'最新 Time:{aResultLast.KBar.TimeSn_Dply}, EMA:{aResultLast.Value}')
						if ta_Type == eTA_Type.WMA:
							print(f'最新 Time:{aResultLast.KBar.TimeSn_Dply}, EMA:{aResultLast.Value}')
						if ta_Type == eTA_Type.SAR:
							print(f'最新 Time:{aResultLast.KBar.TimeSn_Dply}, SAR:{aResultLast.SAR}, EPh:{aResultLast.EPh}, EPl:{aResultLast.EPl},
							AF:{aResultLast.AF}, RaiseFall:{aResultLast.RaiseFall}')
						if ta_Type == eTA_Type.RSI:
							print(f'最新 Time:{aResultLast.KBar.TimeSn_Dply}, RSI:{aResultLast.RSI}, UpDn:{aResultLast.UpDn},
							UpAvg:{aResultLast.UpAvg}, DnAvg:{aResultLast.DnAvg}')
						if ta_Type == eTA_Type.MACD:
							print(f'最新 Time:{aResultLast.KBar.TimeSn_Dply}, DIF:{aResultLast.DIF}, OSC:{aResultLast.OSC}')
						if ta_Type == eTA_Type.KD:
							print(f'最新 Time:{aResultLast.KBar.TimeSn_Dply}, K:{aResultLast.K}, D:{aResultLast.D}')
						if ta_Type == eTA_Type.CDP:
							print(f'最新 Time:{aResultLast.KBar.TimeSn_Dply}, CDP:{aResultLast.CDP}, AH:{aResultLast.AH}, NH:{aResultLast.NH},
							AL:{aResultLast.AL}, NL:{aResultLast.NL}')
						if ta_Type == eTA_Type.BBands:
							print(f'最新 Time:{aResultLast.KBar.TimeSn_Dply}, MA:{aResultLast.MA}, UB2:{aResultLast.UB2}, LB2:{aResultLast.LB2}')

                
              

4.4 OnRcvDone

歷史行情回補
                
				  def OnRcvDone(ta_Type: eTA_Type, aResult):
				      if ta_Type == eTA_Type.SMA:
				        for x in aResult:
				           print(f'回補 {x}')
				      if ta_Type == eTA_Type.EMA:
				        for x in aResult:
				           print(f'回補 {x}')
				      if ta_Type == eTA_Type.WMA:
				        for x in aResult:
				           print(f'回補 {x}')
				      if ta_Type == eTA_Type.SAR:
				        for x in aResult:
				           print(f'回補 {x}')
				      if ta_Type == eTA_Type.RSI:
				        for x in aResult:
				           print(f'回補 {x}')
				      if ta_Type == eTA_Type.MACD:
				        for x in aResult:
				           print(f'回補 {x}')
				      if ta_Type == eTA_Type.KD:
				        for x in aResult:
				           print(f'回補 {x}')
				      if ta_Type == eTA_Type.CDP:
				        for x in aResult:
				           print(f'回補 {x}')
					if ta_Type == eTA_Type.BBands:
						for x in aResult:
							print(f'回補 {x}')

                
              

5. API物件欄位說明

5.1 回傳類別共通部分

class TKBarRec

參數名稱 型別 說明
Date str 日期
Product str 商品代號
TimeSn str 時間
TimeSn_Dply str 時間
Quantity int
Volume int 總量
OPrice float 開盤價
HPrice float 最高價
LPrice float 最低價
CPrice float 收盤價

5.2 SMA: 簡單移動平均線

週期 20

class ta_sma

參數 類別 說明
Value float SMA值

5.3 WMA: 加權移動平均線

週期 5

class ta_wma

參數 類別 說明
Value float WMA值

5.4 EMA: 指數移動平均線

週期 5

class ta_ema

參數 類別 說明
Value float EMA值

5.5 KD: 隨機指標

週期 9
RSV移動平均週期 3

class ta_kd

參數 類別 說明
K float K值
D float D值

5.6 MACD: 指數平滑異同移動平均線

短週期 12
長週期 26
DIF週期 9

class ta_macd

參數 類別 說明
DIF float DIF值
OSC float OSC值

5.7 SAR: 拋物線指標

加速因子起始值 0.02M
加速因子遞增值 0.02M
加速因子最大值 0.2M

class ta_sar

參數 類別 說明
SAR float SAR值
EPh float Extreme High
EPl float Extreme Low
AF float 加速因子
RaiseFall float 漲跌勢

5.8 RSI: 相對強弱指標

週期 6

class ta_rsi

參數 類別 說明
RSI float RSI值
UpDn float UpDn值
UpAvg float UpAvg值
DnAvg float DnAvg值

5.9 CDP: 逆勢操作系統

class ta_rsi

參數 類別 說明
CDP float RSI值
AH float 最高值
NH float 近高值
AL float 最低值
NL float 近低值

5.10 BBands: 布林通道

週期 20
上值加幾倍標準差 2
下值減幾倍標準差 2

5.11 eTA_Type技術指標

class eTA_Type

參數 類別 說明
SMA Enum Value=0
WMA Enum Value=1
EMA Enum Value=2
KD Enum Value=3
MACD Enum Value=4
SAR Enum Value=5
RSI Enum Value=6
CDP Enum Value=7

5.12 eNK_Kind分K

class eNK_Kind

參數 類別 說明
DAY Enum Value=0
K_1m Enum Value=1
K_3m Enum Value=2
K_5m Enum Value=5

5.13 TBSRec歷史成交

class TBSRec

參數 類別 說明
Prod str 商品代號
Sequence int 成交序號
Match_Time float 成交時間
Match_Price float 成交價
Match_Quantity int 成交單量
Match_Volume int 成交總量
Is_TryMatch bool 是否為試撮
BS int 0=無法辨別, 1=內盤, 2=外盤
BP_1_Pre float 前買1價
SP_1_Pre float 前賣1價

Python技術分析API實作使用說明

1. 載入API物件

                
                  from tech_analysis_api.api import *
				  from tech_analysis_api.model import *
				
              

2. 初始化物件TechAnalysis

                
                  ta = TechAnalysis(OnDigitalSSOEvent, OnTAConnStuEvent, OnUpdate, OnRcvDone)
                
              

3. 設定帳號密碼

                
                  ta.Login('帳號', '密碼')
                
              

4. 設定技術指標參數

                
                  class k_settnig:
				      def __init__(self, ProdID, NK, TA_Type, DateBegin):
				        self.ProdID = ProdID
				        self.NK = NK
				        self.TA_Type = TA_Type
				        self.DateBegin = DateBegin
                
              
參數 類別 說明
ProdID str 股票代碼
NK enum 技術指標型態列舉(
TechAnalysisAPI.eNK_Kind.DAY,
TechAnalysisAPI.eNK_Kind.K_1m,
TechAnalysisAPI.eNK_Kind.K_3m,
TechAnalysisAPI.eNK_Kind.K_5m)
TA_Type enum 技術指標型態列舉(
TechAnalysisAPI.eTA_Type.SMA, TechAnalysisAPI.eTA_Type.WMA ,
TechAnalysisAPI.eTA_Type.EMA, TechAnalysisAPI.eTA_Type.KD,
TechAnalysisAPI.eTA_Type.MACD, TechAnalysisAPI.eTA_Type.SAR,
TechAnalysisAPI.eTA_Type.RSI, TechAnalysisAPI.eTA_Type.CDP, TechAnalysisAPI.eTA_Type.BBands)
DateBegin str 查詢日期(ex.20230619)

4.1 訂閱技術指標 SubTA

                
                  ta.SubTA(k_config)
                
              

4.2 取消訂閱技術指標 UnSubTA

                
                  ta.UnSubTA(k_config)
                
              

5. 歷史成交

                
                  lsBS, sErrMsg = ta.GetHisBS_Stock('2330', '20230724')
                
              

lsBS為5.13 TBSRec的list

元富證券股份有限公司 |台北市敦化南路二段97號22樓|客服專線:0800-088-148|手機及國外客服專線:(02)2708-3972 |108年金管證總字第0031號

元富證券股份有限公司
台北市敦化南路二段97號22樓
客服專線:0800-088-148
手機及國外客服專線:(02)2708-3972
108年金管證總字第0031號