我們先將三日高點/低點畫縣成指標如圖
現在讓我們把這些概念整理一下。
作多邏輯
- 短期均線大於長期均線
- 價格回落至N日均低線買進。
- 價格回漲至N日均高線賣出
- 短期均線小於長期均線。
- 收盤價格反彈至 M日均高線賣出。
- 收盤價格回跌至 M日均低線回補。
測試程式碼
input:EntryType(1),ExitType(0) ;vars: IsBalanceDay(False),MP(0),PF(0),PL(0);
Vars: FastL(5),TrendL(10),EntBarL(9),FastS(8),TrendS(19),EntBarS(7),TradeProfit(0.055),TradeStopLoss(0.028),NBarL(12),NBarS(10),ATRs_L(5.2),ATRs_S(11.5),RangeL(200),RangeS(200),ATR_Ratio(1.5);
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 ;
{ 計算 均高與均低線 }
vars: NBarHigh(0),NBarLow(0) ;
NBarHigh = Average(High,EntBarL) ;
NBarLow = Average(Low,EntBarS) ;
{ 短期/長期均線相對位置判斷趨勢 }
if EntryType = 2 then Begin
If Close < NBarLow and Average(AvgPrice,FastL) > Average(AvgPrice,TrendL) then Buy next bar at NBarLow Stop ;
If Close > NBarHigh and Average(AvgPrice,FastS) < Average(AvgPrice,TrendS) then Sell next bar at NBarHigh Stop ;
end;
{ 進場後幾根K棒後 ,以高低點均線位置出場 }
if ExitType = 6 then Begin
if MP > 0 and BarsSinceEntry > NBarL and Close > NbarHigh then ExitLong next bar at NbarHigh limit ;
if MP < 0 and BarsSinceEntry > NBarS and Close < Nbarlow then ExitShort next bar at NbarLow limit ;
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 ;
台指期 日K 留倉 近3000 日 交易成本 1200
台指期 60 分K 留倉 近3000 日 交易成本 1200
在開發策略元素的過程中,將文字概念轉換成程式語言是需要不斷揣摩練習的!以本篇文章而言,作者所述的振蕩點辨識我是改用均線方向作趨勢替代。
0 意見:
張貼留言