跨商品/跨頻率的語法介紹
選擇單元...
XS策略雷達
單元目錄
單元目錄
基本功能介紹
同樣是參考指數趨勢當作進出的條件,我們可以用前一篇基準商品參照功能。也可以直接在腳本中利用跨商品/跨頻率的語法來實現。今天我們將會說明如何在XS中使用GetSymbolField語法取得其他商品的資訊來應用。
GetSymbolField
如果你已經會使用GetField語法,那恭喜你,你馬上就能上手跨商品/跨頻率(GetSymbolField)的語法。GetSymbolField顧名思義,就是取某個商品、某個欄位的資料。所以你只要在原本的GetField語法前加一個指定商品的參數就行了。GetField("收盤價"); → 取得目前商品的收盤價GetSymbolField("TSE.TW","收盤價"); → 取得加權指數的收盤價如果是要跨頻率的話,GetField和GetSymbolField都一樣在最後一個參數可以指定頻率,例如:GetField("收盤價","M"); → 取得商品當下的月收盤價GetSymbolField("TSE.TW","收盤價","M"); → 取得加權指數當下的月收盤價在跨頻率取資料的時候,取得的都是該頻率當下的數值。例如回測時要從日頻率取週頻率的TSE開高低收量,在2018/07/16~2018/07/20期間中:- 週一07/16的週線開盤價為10867.97、週線最高價為10890.20、週線最低價為10817.45、週線收盤價為10817.45、週線成交量為1265.69億;
- 週二07/17的週線開盤價為10867.97、週線最高價為10890.20、週線最低價為10758.83、週線收盤價為10778.99、週線成交量為2619.59億;
- 週三07/18的週線開盤價為10867.97、週線最高價為10890.20、週線最低價為10758.83、週線收盤價為10842.46、週線成交量為4278.96億;
- 週四07/19的週線開盤價為10867.97、週線最高價為10896.25、週線最低價為10758.83、週線收盤價為10835.38、週線成交量5725.45億;
- 週五07/20的週線開盤價為10867.97、週線最高價為10963.95、週線最低價為10758.83、週線收盤價為10932.11、週線成交量7401.97億。
範例
接下來我們看一樣均線多頭排列的策略腳本,在改用GetSymbolField時,該怎麼調整。原本的警示腳本input: shortlength(5,"短期均線期數"); input: midlength(10,"中期均線期數"); input: Longlength(20,"長期均線期數"); input: SuperLong(60,"超長期均線期數"); variable: shortaverage(0); variable: midaverage(0); variable: Longaverage(0); variable: SuperLongaverage(0); settotalbar(maxlist(shortlength,midlength,Longlength,SuperLong) + 3); if Close > close[1] then begin shortaverage=Average(close,shortlength); midaverage=Average(close,midlength) ; Longaverage = Average(close,Longlength); SuperLongaverage = Average(close,SuperLong); if close>shortaverage and shortaverage>midaverage and midaverage>Longaverage and Longaverage>SuperLongaverage then ret=1; end;這是XS內建的警示腳本,主要是用到了4條不同期間的均線來確認股票是否是多頭排列。我們拿這個腳本當基礎,修改成大盤和個股要同時滿足均線多頭排列條件才會觸發。調整後警示腳本
input: shortlength(5,"短期均線期數"); input: midlength(10,"中期均線期數"); input: Longlength(20,"長期均線期數"); input: SuperLong(60,"超長期均線期數"); variable: shortaverage(0),shortaverageTSE(0); variable: midaverage(0),midaverageTSE(0); variable: Longaverage(0),LongaverageTSE(0); variable: SuperLongaverage(0),SuperLongaverageTSE(0); settotalbar(maxlist(shortlength,midlength,Longlength,SuperLong) + 3); if Close > close[1] then begin shortaverage=Average(close,shortlength); midaverage=Average(close,midlength); Longaverage = Average(close,Longlength); SuperLongaverage = Average(close,SuperLong); shortaverageTSE=Average(GetSymbolField("TSE.TW","收盤價"),shortlength); midaverageTSE=Average(GetSymbolField("TSE.TW","收盤價"),midlength) ; LongaverageTSE = Average(GetSymbolField("TSE.TW","收盤價"),Longlength); SuperLongaverageTSE = Average(GetSymbolField("TSE.TW","收盤價"),SuperLong); if GetSymbolField("TSE.TW","收盤價")>shortaverageTSE and shortaverageTSE>midaverageTSE and midaverageTSE>LongaverageTSE and LongaverageTSE>SuperLongaverageTSE then if close>shortaverage and shortaverage>midaverage and midaverage>Longaverage and Longaverage>SuperLongaverage then ret=1; end;我們需要把基準商品要執行的邏輯,直接寫在腳本當中。所以要計算加權指數的均線,把四條均線的程式碼修改一下,數列的部份換成用GetSymbolField取價,例如:shortaverage=Average(close,shortlength);shortaverageTSE=Average(GetSymbolField("TSE.TW","收盤價"),shortlength);在判斷是否觸發的條件中,也變成先檢查加權指數的條件,成立後再去檢查標的條件是否也成立。這樣子就完成了跨商品應用的策略調整。跨頻率的部份就留給大家自己練習一下。