# 匯入模組 from Talib_GetData import GetKBar import matplotlib.pyplot as plt import talib N = 5 # 當前價格要突破近5日的最高或最低價 TakeProfit = 0.03 # 固定式停利百分比 StopLoss = 0.02 # 固定式停損百分比 TotalProfit = [] # 用來記錄每筆損益 flag = 0 # 手中部位狀態,0代表空手,1代表持有多單,-1代表持有空單 KBar = GetKBar('301','2330','Stock','1') # 取Talib套件適用的K棒資料 KBar['AvgVolume'] = talib.MA(KBar['volume'],N) # 近N日的成交均量 for i in range(N,len(KBar['date'] )): Date = KBar['date'][i] # 當前日期 NowPrice = KBar['close'][i] # 當前價格 MaxPrice = max(KBar['close'][i-N:i]) # 近N日最高價 MinPrice = min(KBar['close'][i-N:i]) # 近N日最低價 NowVolume = KBar['volume'][i] # 當前成交量 AvgVolume = KBar['AvgVolume'][i] # 當前成交均量 # 多單進場 (空手 且 當前價格突破近N日最高價 且 成交量突破近N日均量的1.5倍) if flag == 0 and NowPrice > MaxPrice and NowVolume > AvgVolume * 1.5: flag = 1 OrderDate = Date OrderPrice = NowPrice # 多單進場 (空手 且 當前價格跌破近N日最低價 且 成交量突破近N日均量的1.5倍) elif flag == 0 and NowPrice < MinPrice and NowVolume > AvgVolume * 1.5: flag = -1 OrderDate = Date OrderPrice = NowPrice
# 匯入模組 from Talib_GetData import GetKBar from talib.abstract import * KBar = GetKBar('100', '2317', 'Stock', '1') # 取Talib套件適用的K棒資料 TotalProfit = [] # 用來記錄每筆損益 flag = 0 # 手中部位狀態,0代表空手,1代表持有多單,-1代表持有空單 K, D = STOCH(KBar, 9) # 利用Talib套件計算期數為9日的KD指標 for i in range(0, 12): # 將K及D的空值預設為50 K[i], D[i] = 50, 50 for i in range(13, len(K) - 1): nextDate = KBar['date'][i + 1] nextOpen = KBar['open'][i + 1] thisK = K[i] thisD = D[i] lastK = K[i - 1] lastD = D[i - 1] # K值由下往上穿越D值 if lastK < lastD and thisK > thisD: # 多單進場 if flag == 0: flag = 1 OrderDate = nextDate OrderPrice = nextOpen # 將空單平倉,並多單進場 elif flag == -1: # 先平倉,並記錄損益 CoverDate = nextDate CoverPrice = nextOpen
# 匯入模組 from Talib_GetData import GetKBar from talib.abstract import * KBar = GetKBar('100', '1101', 'Stock', '1') # 取Talib套件適用的K棒資料 TotalProfit = [] # 用來記錄每筆損益 flag = 0 # 手中部位狀態,0代表空手,1代表持有多單,-1代表持有空單 K, D = STOCH(KBar, 9) # 利用Talib套件計算期數為9日的KD指標 for i in range(0, 12): # 將K及D的空值預設為50 K[i], D[i] = 50, 50 for i in range(13, len(K) - 1): nextDate = KBar['date'][i + 1] nextOpen = KBar['open'][i + 1] thisK = K[i] thisD = D[i] lastK = K[i - 1] lastD = D[i - 1] # K值由下往上穿越D值 if lastK < lastD and thisK > thisD: # 多單進場 if flag == 0: flag = 1 OrderDate = nextDate OrderPrice = nextOpen # 將空單平倉,並多單進場 elif flag == -1: # 先平倉,並記錄損益 CoverDate = nextDate CoverPrice = nextOpen
from Talib_GetData import GetKBar flag = 0 KBar = GetKBar('500','TXF','Future','1') # 取Talib套件適用的K棒資料 for i in range(0,len(KBar['date'] )): Date = KBar['date'][i] # 當前日期 NowOpen = KBar['open'][i] # 當前日K棒的開盤價 NowHigh = KBar['high'][i] # 當前日K棒的最高價 NowLow = KBar['low'][i] # 當前日K棒的最低價 NowClose = KBar['close'][i] # 當前日K棒的收盤價 # 當日K棒有經過9950點就進場 if flag == 0 and NowHigh >= 9950 and NowLow <= 9950: flag = 1 OrderDate = Date OrderPrice = 9950 # 進場後價格超過10000點 elif flag == 1 and NowHigh >= 10000: flag = 2 MaxHigh = NowHigh # 開始出場判斷 if flag == 2: # 持續更新最高價 MaxHigh = max( MaxHigh , NowHigh ) # 超過10000點後的高點減掉9950的30% StopLoss = (MaxHigh-9950)*0.3 # 停損點位 StopPoint = MaxHigh - StopLoss