大家好,
分享撰寫有點複雜的選股腳本,
有點複雜的選股條件如下( 這不是選股腳本範例腳本喔! ):
((L>2日低 and H>2日高) and (-DI<-DI[1] and -DI<21 and DIF[1]<MACD[1] and DIF>MACD and DIF>-0.05)) or ((L>2日低 and H>2日高) or L>3日低 and H>3日高or (C>C[1] and C>C[2]) and (-DI[1]<-DI[2] and -DI<+DI[1]) and -DI<18)
and (DIF[1]>MACD[1])
and DIF>MACD
and DIF>-0.05
and MA(4)>MA(20)
and C>MA(3)) and MA(3)>MA(4)
and V>368;
大致撰寫方式:
0. 將可以設定為輸入參數的變數,使用 input 宣告,方便在介面直接調整
1. 使用 DirectionMovement 系統函數,撰寫 +DI 與 -DI
2. 使用 MACD 系統函數,撰寫 DIF 與 MACD
3. 使用 OutputField 內建函數,將較有可能計算錯誤的數值,列在選股清單中,以利檢驗數值是否正確( 參考:選股欄位放大鏡:談OutputField )
資料讀取設定 200 筆:

選股腳本範例如下:
input:_SLength(2,"短天期"), _LLength(3,"長天期"), DMI_Length(14,"DMI天期"), FastLength(12,"MACD快線"), SlowLength(26,"MACD慢線"), MACDLength(9,"MACD天期"), _Hndi(21,"比較高的-DI上限"),_Lndi(18,"比較低的-DI上限"), _difValue(-0.05,"dif門檻"), SSMA_Length(3,"極短期MA天期"),SMA_Length(4,"短期MA天期"),LMA_Length(20,"長期MA天期"), _volume(368,"成交量門檻"); variable: pdi(0), ndi(0), adx_value(0), difValue(0), macdValue(0), oscValue(0); value100 = lowest(low,_SLength); value200 = highest(high,_SLength); value101 = lowest(low,_LLength); value201 = highest(high,_LLength); value299 = average(close,SSMA_Length); value300 = average(close,SMA_Length); value301 = average(close,LMA_Length); DirectionMovement(DMI_Length, pdi, ndi, adx_value); MACD(weightedclose(), FastLength, SlowLength, MACDLength, difValue, macdValue, oscValue); if ((Low > value100 and high > value200) and (ndi < ndi[1] and ndi < _Hndi and difValue[1] < macdValue[1] and difValue > macdValue and difValue> _difValue)) or ((Low > value100 and high > value200) or Low > value101 and High > value201 or (close > close[1] and close > close[2]) and (ndi[1] < ndi[2] and ndi < pdi[1]) and ndi < _Lndi) and (difValue[1] > macdValue[1]) and difValue > macdValue and difValue > _difValue and value300 > value301 and close > value299 and value299 > value300 and volume > _volume then ret = 1; outputfield1(pdi,"pdi"); outputfield2(ndi,"ndi"); outputfield3(difValue,"difValue"); outputfield4(macdValue,"macdValue");
以上供參考,有問題歡迎詢問。