EasyTrader YM_Test 05
KD指標背離
背離為股價趨勢與技術指標趨勢呈現反向走勢,有著不一致價格波動的關係,暗示股價可能將有所反轉,為一項非常實用的觀察指標。建議在觀察背離現象時,可同步觀察多項指標是否同時出現背離 ! 將進一步強化價格反轉的可能性。
這種背離現象有兩種,具體說明如下:
正背離:通常發生在市場價格達到新低,而 KD 指標卻未同步達到相應的新低。這種現象透露出賣壓正在減弱,即使價格下跌,動能卻沒有相對應的增強,代表下跌動力的耗盡,可視為一個潛在的看漲轉折信號。
負背離:通常出現在市場價格創出新高,但 KD 指標未能同步達到新高的情況下。這表示即便價格在上漲,買方的動能卻在衰減,不足以支撐價格進一步上漲,從而透露出上漲趨勢即將結束,可視為回調或轉向下跌趨勢的潛在信號。
// Public Variable
vars:MP(0),PF(0),PL(0),BuyPrice(0),ShortPrice(0),BasePF(600),BasePL(300) ;
vars:BarPass(5),HLRange(0),WinPoint(0),HBarPos(0),LBarPos(0),ExitH(0),ExitL(0),TimeOK(false),EntryExitCond(false),SelectNo(0) ;
vars:jumpPoint(0) ;
jumpPoint = MinMove/PriceScale ;
//****************** Basic Setup ****************************************
MP = MarketPosition ;
PF = 6000/BigPointValue ;
PL = 1500/BigPointValue ;
// ******** Long/Short win/loss point *********
vars:WinPoint_LE(0),LossPoint_LE(0),WinPoint_SE(0),LossPoint_SE(0),PF_Ref(800),SL_Ref(-800) ;
if barssinceentry > 0 then begin
if MP > 0 then begin
WinPoint_LE = HighSinceEntry_N - EntryPrice ;
LossPoint_LE = LowSinceEntry_N - EntryPrice ;
end ;
if MP < 0 then begin
WinPoint_SE = EntryPrice - LowSinceEntry_N ;
LossPoint_SE = EntryPrice - HighSinceEntry_N ;
end ;
end else begin
WinPoint_LE = 0 ;
LossPoint_LE = 0 ;
WinPoint_LE = 0 ;
LossPoint_LE = 0 ;
end ;
//計算五日平均振幅
// ************* 5 Day Avg Range *************
vars:Day5Range(0) ;
Day5Range = ((HighD(1)-LowD(1))+(HighD(2)-LowD(2))+(HighD(3)-LowD(3))+(HighD(4)-LowD(4))+(HighD(5)-LowD(5)))/5 ;
//計算近三週區間價
// ************3 Week High or Low *****************
vars:WeekH(High),WeekL(Low),WeekHL(0),WeekHPB(0),WeekLPB(0),Week33(0),Week67(0) ;
WeekH = MaxList(HighW(0),HighW(1),HighW(2)) ;
WeekL = MinList(LowW(0),LowW(1),LowW(2)) ;
WeekHL = WeekH - WeekL ;
Week33 = WeekL+WeekHL*0.33 ;
Week67 = WeekL+WeekHL*0.67 ;
WeekHPB = _BarsLast(High = WeekH) ;
WeekLPB = _BarsLast(Low = WeekL) ;
//計算KDJ值
// ************ KD *****************
vars:vKA(0),vKB(0),vDA(0),vDB(0),vJA(0),vJB(0),DivSlowKCondA(false),DivSlowKCondB(false) ;
vKA = SlowK(LenA2) ;
vDA = SlowD(LenA2) ;
vJA = 3*vDA-2*vKA ;
vKB = FastK(LenB2) ;
vDB = FastD(LenB2) ;
vJB = 3*vDB-2*vKB ;
//計算 Vidya 指標值
//* Vidya Setup
vars:Vidya1(1),Vidya2(1) ;
Vidya1 = _SS_Vidya(LenA1,FracA) ;
Vidya2 = _SS_Vidya(LenB1,FracB) ;
// ************* Time Set Up *************
TimeOK = (time >= 1100 or time <= 0300) ;
//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = Highest(High,Hbar) ;
ShortPrice = OpenD(0)+OpenD(0)*RatioS/10000 ;
// ********** Main Strategy *********
// ********** Entry Method
if TimeOK then begin
//Vidya指標上升且收盤價大於週區間價2/3 且回調一段時間 則收盤價突破設定價位買進作多
if MP <> 1 and Countif(Vidya1 > Vidya1[1],3) = 3 and Close > Week67 and WeekHPB > LenB1 and Close < BuyPrice then Buy ("LE_Vidya7T") next bar at BuyPrice stop;
//Vidya指標下降且收盤價小於週區間價1/3 且回調一段時間 則收盤價跌破設定價位賣出作空
if MP <> -1 and Countif(Vidya2 < Vidya2[1],3) = 3 and Close < Week33 and WeekLPB > LenB1 and Close > ShortPrice then SellShort ("SE_Vidya7T") next bar at ShortPrice stop;
end ;
if TimeOK then begin
DivSlowKCondA = HighestBar(High,34)=1 and vKA < Highest(vKA,34)*0.75 ;
if DivSlowKCondA then ExitL = Lowest(Low,2) ;
//近期出現KD高檔背離 則收盤價跌破設定價位賣出作空
if MP <> -1 and MRO(DivSlowKCondA,HBar,1) > 1 and Close > (ExitL-Range*0.5) then
sellshort ("SE_DivKT") next bar at ExitL-Range*0.5 stop ;
DivSlowKCondB = LowestBar(Low,34)=1 and vKA > Lowest(vKB,34)*1.25 ;
if DivSlowKCondB then ExitH = Highest(High,2) ;
//近期出現KD低檔背離 則收盤價突破設定價位買進作多
if MP < 0 and MRO(DivSlowKCondB,LBar,1) > 1 and Close < (ExitH+Range*0.5) then
Buy ("LE_DivKT") next bar at ExitH+Range*0.5 stop ;
end ;
//基本停利+移動停損出場
// ************* Base Exit *************
if MP > 0 then begin
Sell ("LX_PF5D_"+NumtoStr(PF+Day5RAnge,0)) next bar at EntryPrice+PF+Day5RAnge limit ;
if WinPoint_LE <= PL*1.5 then Sell ("LX_EPL"+NumtoStr(PL,0)) next bar at EntryPrice-PL stop
else if WinPoint_LE <= PF then Sell ("LX_H2PL_"+NumtoStr(PL*2,0)) next bar at HighSinceEntry_N-PL*2 stop
else if WinPoint_LE > PF then Sell ("LX_PF_"+NumtoStr(WinPoint_LE*0.5,0))
next bar at HighSinceEntry_N-WinPoint_LE*0.5 stop ;
end ;
if MP < 0 then begin
BuytoCover ("SX_PF5D_"+NumtoStr(PF+Day5RAnge,0)) next bar at EntryPrice-PF-Day5RAnge limit ;
if WinPoint_SE <= PL*1.5 then BuytoCover ("SX_EPL_"+NumtoStr(PL,0)) next bar at EntryPrice+PL stop
else if WinPoint_SE <= PF then BuytoCover ("SX_L2PL_"+NumtoStr(PL*2,0)) next bar at LowSinceEntry_N+PL*2 stop
else if WinPoint_SE > PF then BuytoCover ("SX_PF_"+NumtoStr(WinPoint_SE*0.5,0))
next bar at LowSinceEntry_N+WinPoint_SE*0.5 stop ;
end ;
小道瓊 留倉 交易週期 2018/1/1 ~ 2024/11/11 交易成本 USD 30
請問_BarsLast這函數是做什麼的計算? 不過高或過低的K棒數嗎? 不知是否能貼一下函數裡面的code參考一下😅
回覆刪除計算符合條件當下到目前K棒的距離
刪除