龙听期货论坛's Archiver

龙听 发表于 2018-11-23 14:46

從內建指標或訊號程式碼學怎麼運用函式

[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]對於PowerLanguage初學者而言,很大的困擾就是不知該怎麼用程式表達想要達到的功能或計算,有些最基本的函式如Average、Highest等,學習別無他法,先背起來再說,但例如想以MACD指標來作為交易訊號,遇到第一個難題就是用什麼程式碼來計算(表達)MACD各項值,其實最簡單的方式就是直接參考PowerLanguage內建的訊號或指標範例。[/font][/color]


[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]我們開啟內建的「MACD」指標,程式碼如下:[/font][/color]



[align=center][color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif][size=13px][url=https://2.bp.blogspot.com/-ofKeNklddzg/WGTwdM2Xn-I/AAAAAAAABjw/akU1xHGuO0A-QHLcBmoaLjEmXxqvin0NQCLcB/s1600/2016122901.png][img=1,0]https://2.bp.blogspot.com/-ofKeNklddzg/WGTwdM2Xn-I/AAAAAAAABjw/akU1xHGuO0A-QHLcBmoaLjEmXxqvin0NQCLcB/s1600/2016122901.png[/img][/url][/size][/font][/color][/align]



[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]==========================================[/font][/color]
[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]inputs: FastLength( 12 ), SlowLength( 26 ), MACDLength( 9 ) ;[/font][/color]
[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]variables: var0( 0 ), var1( 0 ), var2( 0 ) ;[/font][/color]


[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]var0 = MACD( Close, FastLength, SlowLength ) ;[/font][/color]
[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]var1 = XAverage( var0, MACDLength ) ;[/font][/color]
[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]var2 = var0 - var1 ;[/font][/color]


[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]Plot1( var0, "MACD" ) ;[/font][/color]
[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]Plot2( var1, "MACDAvg" ) ;[/font][/color]
[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]Plot3( var2, "MACDDiff" ) ;[/font][/color]
[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]Plot4( 0, "ZeroLine" ) ;[/font][/color]
[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]===========================================[/font][/color]


[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]了解MACD的交易人都知道MACD指標共畫三條線:快線(DIF)、慢線(MACD)、[/font][/color][color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]柱狀線(DIF-MACD),對一下上面MACD指標程式碼應該不難猜到var0=快線、var1=慢線、var2=柱狀線,也可以參考PowerLanguage Editor內建的函式說明或是這個[url=http://help.tradestation.com/09_01/tsdevhelp/subsystems/elword/elword_Left.htm#CSHID=function%2Fslowk_function_.htm|StartTopic=function%2Fslowk_function_.htm|SkinName=ts_slate_skin]函式說明網頁[/url](TS函式,與MC幾乎共通),程式碼清楚示範如何MACD及Xaverage這兩個函式來計算MACD指標值。因此再進一步舉例,假設想要幫[url=http://seekingalpha-tainan.blogspot.com/2016/12/2.html]均線交叉策略[/url]加一個MACD柱狀線在零軸以上才做多的濾網,程式碼就可以寫成:[/font][/color]


[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]===========================================[/font][/color]
[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]inputs:Len1(5),Len2(20);[/font][/color]
[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]Vars:MA1(0),MA2(0),var0(0),var1(0),var2(0);[/font][/color]


[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]MA1=Average(close,Len1);[/font][/color]
[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]MA2=Average(close,Len2);[/font][/color]
[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]var0 = MACD( Close, 12, 26 ) ;[/font][/color]
[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]var1 = XAverage( var0, 9 ) ;[/font][/color]
[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]var2 = var0 - var1 ;[/font][/color]


[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]If var2[/font][/color][align=center][color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]>[/font][/color][/align] [color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]0 and MA1 cross over MA2 then Buy this bar on close;[/font][/color]
[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]If var2[/font][/color][align=center][color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]<[/font][/color][/align] [color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]0 and MA1 cross below MA2 then SellShort this bar on close;[/font][/color]
[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]===========================================[/font][/color]


[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]上面這個例子就是示範如何從PowerLanguage內建的指標或訊號範例去複製各個函式的引用方式,其他如KD、RSI等等都以此類推。[/font][/color]


[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]有興趣的讀者可以再開啟MACD相關的內建訊號範例,就可以學到如何用MACD建構交易訊號:[/font][/color]
[align=center][color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif][size=13px][url=https://1.bp.blogspot.com/-XAkGHOo9n30/WGTxHIWFiZI/AAAAAAAABj0/1INp3mSSMtA935RahI6auMa_cE0VuZ-SwCLcB/s1600/2016122902.png][img=1,0]https://1.bp.blogspot.com/-XAkGHOo9n30/WGTxHIWFiZI/AAAAAAAABj0/1INp3mSSMtA935RahI6auMa_cE0VuZ-SwCLcB/s1600/2016122902.png[/img][/url][/size][/font][/color][/align]



[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]以上,我們示範了如何快速上手運用內建的函式,知其然還想知其所以然的讀者可以再進一步打開函式的程式碼去看,裡頭就是指標的細部計算內容了。[/font][/color]


[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]由於內建函式、指標及訊號名稱皆為英文名稱,有時候要找特定指標會兜不起來,例如我們熟悉的KD指標老外稱為「Stochastic」,目前已有指標中文化可以安裝([url=https://www.dropbox.com/s/bi3tv013xszleht/Multicharts%20%E6%8C%87%E6%A8%99%E4%B8%AD%E6%96%87%E5%8C%96.zip?dl=0]載點[/url]),不過還是建議把指標的英文名稱學起來,以後到國外網站看程式交易相關資料才知道意思。[/font][/color]


[color=#444444][font=Arial, Tahoma, Helvetica, FreeSans, sans-serif]就用使用EXCEL一樣,會運用的函式越多就能越快解決計算問題,對於PowerLanguage初學者而言,能盡量多掌握內建函式,在寫程式時就較能得心應手,許多想要達到的功能或計算,只要把適當的函式抓進來用,就可以大幅減輕寫程式的負擔。[/font][/color]

奇貨可居 发表于 2023-11-11 15:05

謝謝分享

页: [1]