2014年11月3日 星期一

●紅黑三兵的變形策略 [程式碼]

EasyTrader ArtNo 215
     三根紅K,依次上升,形成紅三兵形態。紅三兵又稱三陽開泰,它是一種很常見的K線組合,這種K線組合出現時,後勢看漲的情況居多。
【注意事項】
紅三兵如果發生在下降趨勢中,一般是市場的強烈反轉信號;如果股價在較長時間的橫盤後出現紅三兵的走勢形態,並且伴隨著成交量的逐漸放大,則是股票啟動的前奏。但若出現三根紅K逐漸縮小,特別是第三根紅K實體比前二根小得多,出現這種形態時要注意可能是反轉下跌走勢。紅三兵的型態若發生在反彈過程或中段整理時出現,如果搭配著第三天的收盤開高收長紅,而且收盤離第二根紅棒越遠越好,代表盤勢向上的強度越強,後續上漲的機會就很大,但上漲的成交量切記不能爆量,否則容易反轉下跌。

這裡有個很重要的關鍵點必須要注意,當出現紅三兵的型態時,即使後面有拉回整理,最好是收盤能不跌破第三根紅棒的收盤,不跌破代表能維持之前紅三兵多頭氣勢與拉抬決心,股價才有可能續漲。黑三兵就原理上是紅三兵的反向,前述的K棒基本上是以連續同色的K棒作參考。

本篇的想法如下:
1.取最近三根紅K棒中的最高點 H1 , 最低點 L1 ;
2.取最近三根黑K棒中的最高點 H2 , 最低點 L2 ;
3.通道上緣 = MaxList(H1,H2) ;
4.通道下緣 = MinList(L1,L2) ;

不同點在於同色K棒可以不連續

指標程式碼
input:LBar(3),SBar(3) ;
Vars: CountL(0),CountS(0),HighBar1(0),LowBar1(0),HighBar2(0),LowBar2(0),RangeHigh(0)
,RangeLow(0);

宣告陣列來儲存資料
Arrays:ArrayLongL[5](0),ArrayShortH[5](0),ArrayLongH[5](0),ArrayShortL[5](0);

{ 記錄最近 5 根紅K 的高低點 }
if Close > Open then Begin
ArrayLongL[4] = ArrayLongL[3] ;
ArrayLongL[3] = ArrayLongL[2] ;
ArrayLongL[2] = ArrayLongL[1] ;
ArrayLongL[1] = ArrayLongL[0] ;
ArrayLongL[0] = Low ;

ArrayLongH[4] = ArrayLongH[3] ;
ArrayLongH[3] = ArrayLongH[2] ;
ArrayLongH[2] = ArrayLongH[1] ;
ArrayLongH[1] = ArrayLongH[0] ;
ArrayLongH[0] = High ;

CountL = CountL+1 ;
end;

{ 記錄最近 5 根黑K 的高低點 }
if Close < Open then Begin
ArrayShortH[4] = ArrayShortH[3] ;
ArrayShortH[3] = ArrayShortH[2] ;
ArrayShortH[2] = ArrayShortH[1] ;
ArrayShortH[1] = ArrayShortH[0] ;
ArrayShortH[0] = High ;

ArrayShortL[4] = ArrayShortL[3] ;
ArrayShortL[3] = ArrayShortL[2] ;
ArrayShortL[2] = ArrayShortL[1] ;
ArrayShortL[1] = ArrayShortL[0] ;
ArrayShortL[0] = Low ;

CountS = CountS+1 ;
end;

{ 紅黑K棒數量達到時 , 計算所有紅K中的最高點與最低點 }
{ NthMaxList - 取一群數值中的第 N 個最大值}
{ NthMinList - 取一群數值中的第 N 個最小值}


if CountL >=LBar and CountS >= SBar then Begin
if LBar = 5 then LowBar1 = NthMinList(1,ArrayLongL[0],ArrayLongL[1],ArrayLongL[2],ArrayLongL[3],ArrayLongL[4])
else if LBar = 4 then LowBar1 = NthMinList(1,ArrayLongL[0],ArrayLongL[1],ArrayLongL[2],ArrayLongL[3])
else if LBar = 3 then LowBar1 = NthMinList(1,ArrayLongL[0],ArrayLongL[1],ArrayLongL[2])
else if LBar = 2 then LowBar1 = NthMinList(1,ArrayLongL[0],ArrayLongL[1])
else if LBar = 1 then LowBar1 = ArrayLongL[0] ;

if LBar = 5 then HighBar1 = NthMaxList(1,ArrayLongH[0],ArrayLongH[1],ArrayLongH[2],ArrayLongH[3],ArrayLongH[4])
else if LBar = 4 then HighBar1 = NthMaxList(1,ArrayLongH[0],ArrayLongH[1],ArrayLongH[2],ArrayLongH[3])
else if LBar = 3 then HighBar1 = NthMaxList(1,ArrayLongH[0],ArrayLongH[1],ArrayLongH[2])
else if LBar = 2 then HighBar1 = NthMaxList(1,ArrayLongH[0],ArrayLongH[1])
else if LBar = 1 then HighBar1 = ArrayLongH[0] ;
end;

if CountS >= SBar then Begin
if SBar = 5 then HighBar2 = NthMaxList(1,ArrayShortH[0],ArrayShortH[1],ArrayShortH[2],ArrayShortH[3],ArrayShortH[4])
else if SBar = 4 then HighBar2 = NthMaxList(1,ArrayShortH[0],ArrayShortH[1],ArrayShortH[2],ArrayShortH[3])
else if SBar = 3 then HighBar2 = NthMaxList(1,ArrayShortH[0],ArrayShortH[1],ArrayShortH[2])
else if SBar = 2 then HighBar2 = NthMaxList(1,ArrayShortH[0],ArrayShortH[1])
else if SBar = 1 then HighBar2 = ArrayShortH[0] ;

if SBar = 5 then LowBar2 = NthMinList(1,ArrayShortL[0],ArrayShortL[1],ArrayShortL[2],ArrayShortL[3],ArrayShortL[4])
else if SBar = 4 then LowBar2 = NthMinList(1,ArrayShortL[0],ArrayShortL[1],ArrayShortL[2],ArrayShortL[3])
else if SBar = 3 then LowBar2 = NthMinList(1,ArrayShortL[0],ArrayShortL[1],ArrayShortL[2])
else if SBar = 2 then LowBar2 = NthMinList(1,ArrayShortL[0],ArrayShortL[1])
else if SBar = 1 then LowBar2 = ArrayShortL[0] ;
end;

RangeHigh = MaxList(HighBar1[1],HighBar2[1]) ;
RangeLow = MinList(LowBar1[1],LowBar2[1]) ;

Plot1(RangeHigh,"RH") ;
Plot2(RangeLow,"RL") ;


測試程式碼
input:ExitType(3) ;
inputs:NBarL(28),NBarS(18),TradeProfit(0.045),TradeStopLoss(0.025),ATRs_L(5.4),ATRs_S(10.9);
vars: IsBalanceDay(False),MP(0),PF(0),PL(0),HLRange(100);

inputs:LBar(3),SBar(3),LongLen(12),ShortLen(12),OscValueL(80),OscValueS(20);
Vars: CountL(0),CountS(0),HighBar1(0),LowBar1(0),HighBar2(0),LowBar2(0);
Arrays:ArrayLongL[5](0),ArrayShortH[5](0),ArrayLongH[5](0),ArrayShortL[5](0);

MP = MarketPosition ;
if DAYofMonth(Date) > 14 and DAYofMonth(Date) < 22 and DAYofWeek(Date)= 3 then isBalanceDay = True else isBalanceDay =False ;

PF = AvgPrice*TradeProfit ;
PL = AvgPrice*TradeStopLoss ;

{ 依上述指標程式碼計算出 高低點 RangeHigh 與 RangeLow}
HLRange = RangeHigh - RangeLow ;

{ 這裡是作一個虛擬的買賣單 , 讓後續的 EntryDate 與 ExitDate 可以作運用 }
if BarNumber = 1 then Begin
Buy this bar on Close ;
Sell this bar on Close ;
end;

{ 利用 RSI 區間與通道作策略元素 }
if EntryDate <> date and ExitDate <> Date then Begin
if time >= 915 and time <= 1245 then Begin
if RSI(Close,LongLen) < OscValueL then Buy next bar at RangeHigh stop ;
if RSI(Close,ShortLen) > OscValueS then Sell next bar at RangeLow Stop ;
end;
end ;

if ExitType = 1 then SetStopLoss(PL * BigPointValue) ;

if ExitType = 2 then Begin
SetStopLoss(PL * BigPointValue) ;
SetProfitTarget(PF * BigPointValue) ;
end;

if ExitType = 3 then Begin
if MP > 0 and BarsSinceEntry = NBarL then ExitLong next bar at Market ;
if MP < 0 and BarsSinceEntry = NBarS then ExitShort next bar at Market ;
end;

if ExitType = 4 then Begin
SetStopLoss(PL * BigPointValue) ;
setProfitTarget(PF * BigPointValue) ;
if MP > 0 and BarsSinceEntry = NBarL then {Sell } ExitLong next bar at Market ;
if MP < 0 and BarsSinceEntry = NBarS then {Buy} ExitShort next bar at Market ;
end;

if ExitType = 5 then Begin
{*******************************************************************
Description : ATR Trailing Stop Long Exit
Provided By : Omega Research, Inc. (c) Copyright 1999
********************************************************************}
{Inputs: ATRs_L(3);}
Variables: PosHigh(0), ATRVal_L(0);

ATRVal_L = AvgTrueRange(10) * ATRs_L;
If BarsSinceEntry = 0 Then PosHigh = High;
If MarketPosition = 1 Then Begin
If High > PosHigh Then PosHigh = High;
ExitLong ("ATR") Next Bar at PosHigh - ATRVal_L Stop;
End else ExitLong ("ATR eb") Next bar at High - ATRVal_L Stop;

{*******************************************************************
Description : ATR Trailing Stop Short Exit
Provided By : Omega Research, Inc. (c) Copyright 1999
*******************************************************************}
{Inputs: ATRs_S(3);}
Variables: PosLow(0), ATRVal_S(0);
ATRVal_S = AvgTrueRange(10) * ATRs_S;

If BarsSinceEntry = 0 Then PosLow = Low;
If MarketPosition = -1 Then Begin
If Low < PosLow Then PosLow = Low;
ExitShort ("ATR_1") Next Bar at PosLow + ATRVal_S Stop;
End else ExitShort ("ATR_1 eb") Next bar at Low + ATRVal_S Stop;
end;

if IsBalanceDay then setExitonClose ;
台指期 30 min K 多空留倉 交易週期 2004/8/31~ 2014/8/29 交易成本 1200

0 留言:

張貼留言

如果有私人問題想請教,請透過網站右方『與站長聯絡』之表單,謝謝!

----------------------------------------------------------------------------------------------------
網站聲明(Disclaimer)
本教學網站內所提供之程式碼(包括函數、指標、訊號)屬開放程式碼,用意在於讓使用者學習程式語法之撰寫,使用者可以任意修改語法內容並調整參數。本網站所有之內容(包括文章、影片、歷史紀錄、程式碼、教材)限用於個人學習使用,請勿轉寄、濫用,嚴禁私自串接帳戶交易。
-------------------------------------------------------------------------------------------------