[TS懶人包] [保險服務]

2010年2月17日 星期三

指標的運用 - 均線交叉程式

相信這個程式只要知道均線的運算方式…要寫出來應該不是問題才對…首先就是 20 日均線和 60 日均線的計算方式:Average(close, 20) 和 Average(close, 60)。運算出來後…接下來就是寫進場囉。

這樣的程式可以很簡單的寫成:

if average(close, 20) cross over average(close, 60) then
buy next bar at market;
if average(close, 20) cross under average(close, 60) then
sell next bar at market;


我想應該沒什麼人會看上這隻程式的績效的…雖然在 30 分 K 上執行還可以算獲利…不過在其它週期上就有點慘了…所以當然這還是只能算是個例題而已。


接下來我想談一下指標的運用…我想應該很多人在作進出場判斷的時候會運用到指標…那指標該怎麼寫進策略裡呢?

來以 MACD 為例…首先…你應該要知道怎麼去新增一個指標進入 TradeStation 的 k 線圖上。所以現在你應該可以在 k 線圖上看到 MACD 的指標了。

不管什麼樣的指標…只要是 k 線圖上顯示的出來的…那就表示你可以參考這個指標的程式寫法…並把這個寫法放到你的策略裡面。所以…就讓我們打開指標 MACD 的程式碼吧:



Inputs: FastMA(12), SlowMA(26), MacdMA(9);

Plot1(MACD(Close, FastMA, SlowMA), "MACD");
Plot2(XAverage(MACD(Close, FastMA, SlowMA), MacdMA), "MACDAvg");
Plot3(Plot1 - Plot2, "MADiff", iff(plot3>0, yellow, blue));

{Alert Criteria}
If Plot3 Crosses Over 0 Then
Alert("MACD has generated a bullish alert")
Else
If Plot3 crosses under 0 Then
Alert("MACD has generated a bearish alert");

{MACD Expert Commentary}
#BeginCmtry
Commentary(ExpertMACD(Plot1));
#End;


上面就是 TradeStation 的 MACD 指標程式碼。你必需去解讀這個程式碼的寫作過程…所幸…大部份的指標程式內容都相當的容易。

以 MACD 來說…指標程式比較重要的其實就三行而已…

Plot1(MACD(Close, FastMA, SlowMA), "MACD");
Plot2(XAverage(MACD(Close, FastMA, SlowMA), MacdMA), "MACDAvg");
Plot3(Plot1 - Plot2, "MADiff", iff(plot3>0, yellow, blue));


Plot 是畫線指令…它只能運用在指標內…所以這邊可以看到這三行就是畫出三條線。這三條線分別包含了 MACD 線、MACD均線和 Diff等。

從這裡面…你就可以簡單的找出 MACD 的計算方式了。

所以,假設我們的策略裡面需要 MACD,那就會是 MACD(Close, FastMA, SlowMA),括號裡面的三個變數分別是收盤價和二個參數…如果你策略裡面是固定的參數…那其實就直接打上去就行了。

通常我會這樣用:

MACD(Close, 12, 26);


這樣是比較多人使用的 MACD 參數;如果你需要的是 MACD 均線…那就是在把 MACD 去算平均囉:XAverage(MACD(Close, 12, 26), 9);

這邊用到的 XAverage 是個新函數…你應該去看看他的函數程式碼。

現在,你要在策略裡面使用 MACD 應該沒問題了。接下來我再用 RSI 作一個例子,首先…打開 RSI 的指標程式碼:

Inputs: Price(Close), Length(14), BuyZone(30), SellZone(70), BZColor(Green), SZColor(Magenta);

Plot1(RSI(Price, Length), "RSI");
Plot2(BuyZone, "BuyZone");
Plot3(SellZone, "SellZone");

If Plot1 > SellZone then Begin
Alert("The RSI is in overbought territory");
SetPlotColor(1, SZColor);
End
Else
If Plot1 < BuyZone then Begin
Alert("The RSI is in oversold territory");
SetPlotColor(1, BZColor);
End;

{RSI Expert Commentary }
#BeginCmtry
Commentary(ExpertRSI(Plot1, Plot2, Plot3));
#End;


重要的是什麼呢?沒錯…還是那 Plot 指令:

Plot1(RSI(Price, Length), "RSI");
Plot2(BuyZone, "BuyZone");
Plot3(SellZone, "SellZone");


在這邊可以看到其實 RSI 的運算更簡單了…在 RSI 函數裡面都作完了…所以你只需要這樣:RSI(Close, 9) 就可以計算出 9 日的 RSI 值了。


最後,練習時間又到啦…來寫個 MACD 交叉進場的進出場策略吧。

沒有留言:

張貼留言

請留下您的大名…匿名者恕不回應…

Related Posts Plugin for WordPress, Blogger...