2014年6月30日 星期一

●布林通道壓縮噴出策略(續) [程式碼]

EasyTrader ArtNo 171
參閱 布林通道壓縮噴出策略 先依內容建立指標作觀察
{ 布林通道寬度指標}
input:BarNo(20),Sueeze(300),StdMulti(4) ;
Vars: BandWidth(0) ;
BandWidth = StdDev(Close,BarNo)*StdMulti ;
Plot1(BandWidth,"BandWidth") ;
Plot2(Sueeze,"Sqeeze") ;
{ % BBand 指標 }
input:BarNo(60),HB(1),MB(0.5),LB(0) ;
Value1 = StdDev(Close,20) ;
Value2 = (Close-(Average(Close,20)-2*Value1))/(Value1*4) ;

Plot1(Value2,"%B") ;
Plot2(HB,"HB") ;
Plot3(MB,"MB") ;
Plot4(LB,"LB") ;

策略程式碼
input:EntryType(1),ExitType(1);
inputs:LongLen(20),Multi_L(4),CheckBar_L(10),Ratio_L(0.8),ShortLen(20),Multi_S(4),CheckBar_S(10),Ratio_S(0.8),SqueezeL(0.07),SqueezeS(0.07) ;
input:NBarL(2),NbarS(2),TradeProfit(0.019),TradeStopLoss(0.017),ATRs_L(19),ATRs_S(7) ;
Vars: SQZ_L(0),Pnt_L(0),Setup_L1(false),Setup_L2(false),SQZ_S(0),Pnt_S(0),Setup_S1(false),Setup_S2(false) ;
Vars:Exit_L(0),Exit_S(0),EntryL(false),EntryS(false) ;
vars:isBalanceDay(false),MP(0),PL(0),PF(0) ;

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

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

MP = MarketPosition ;
{ 計算多空通道寬度 }
SQZ_L = StdDev(Close,LongLen)* Multi_L ;
SQZ_S = StdDev(Close,ShortLen)* Multi_S ;

{ 計算收盤價在通道中的位階 }
if SQZ_L <> 0 then Pnt_L = (Close-(Average(Close,LongLen)-Multi_L*0.5))/SQZ_L ;
if SQZ_S <> 0 then Pnt_S = (Close-(Average(Close,ShortLen)-Multi_S*0.5))/SQZ_S ;

{ 設定收盤價在上下通道中的持續時間 }
Setup_L1 = Countif(Pnt_L >= 0.5 and Pnt_L <= 1 , CheckBar_L ) >= CheckBar_L * Ratio_L ;
Setup_L2 = Countif(Pnt_L >= 0 and Pnt_L <= 0.5 , CheckBar_L ) >= CheckBar_L * Ratio_L ;
Setup_S1 = Countif(Pnt_S >= 0.5 and Pnt_S <= 1 , CheckBar_S ) >= CheckBar_S * Ratio_S ;
Setup_S2 = Countif(Pnt_S >= 0 and Pnt_S <= 0.5 , CheckBar_S ) >= CheckBar_S * Ratio_S ;

if EntryType = 1 then Begin
{ 當通道壓縮到某定值 - 以收盤價的百分比作計算 , 且符合 SetUp_L1 SetUp_L2 條件作多}
if SQZ_L < Close*SqueezeL and (Setup_L1 or SetUp_L2) then Begin
Buy next bar at Highest(High,3) stop ;
Exit_L = Average(Low,CheckBar_L) ;
End;
{ExitLong next bar at Exit_L stop ;}

{ 當通道壓縮到某定值 - 以收盤價的百分比作計算 , 且符合 SetUp_S1 SetUp_S2 條件作空}
if SQZ_S < Close*SqueezeS and (Setup_S1 or SetUp_S2) then Begin
Sell next bar at Lowest(Low,3) stop ;
Exit_S = Average(High,CheckBar_S) ;
End;
{ExitShort next bar at Exit_S stop ;}

End;

if EntryType = 2 then Begin
{ 另一種思維,單純突破壓縮值即作多或作空 }
if SQZ_L Cross over Close*SqueezeL then EntryL = true ;
if MP <> 0 then EntryL = false ;
if SQZ_L Cross under Close*SqueezeL then EntryL = false ;
if EntryL then Begin
Buy next bar at Highest(High,3) stop ;
Sell next bar at Lowest(Low,3) stop ;
Exit_L = Average(Low,CheckBar_L)[1] ;
end ;
{ExitLong next bar at Exit_L stop ;}

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
{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;

{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 ;

台指期 60分K 留倉 2004/5/31 ~ 2014/5/30 交易成本 1200


台指期 30分K 留倉 2004/5/31 ~ 2014/5/30 交易成本 1200

0 留言:

張貼留言

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

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