要篩選歷史高或是同期新高,標的物可以提供資料的月份不足,會篩選不到,請問是哪邊要修改,若是月份不足就用現有的資料下去比較

  •   39 
  • 最後發表   gama360  2025 八月 11
gama360 發文於   2025/08/09

input:

  monthCount(36, "統計月份"),

  minScore(5, "通過門檻項目數");

 

Vars:

  revenueNow(0), epsNow(0), grossNow(0), opNow(0),

  netNow(0), roaNow(0), roeNow(0),

 

  revenueHistoryHigh(false), revenueSamePeriodHigh(true),

  epsHistoryHigh(false), epsSamePeriodHigh(true),

  grossHistoryHigh(false), grossSamePeriodHigh(true),

  opHistoryHigh(false), opSamePeriodHigh(true),

  netHistoryHigh(false), netSamePeriodHigh(true),

  roaHistoryHigh(false), roaSamePeriodHigh(true),

  roeHistoryHigh(false), roeSamePeriodHigh(true),

 

  score(0), i(0),

  realMonthCount(0), realQuarterCount(0),

 

  tempVal(0),

  quitLoop(false),

 

  maxMonthDataCount(0),

  maxQuarterDataCount(0);

 

// 預設最大資料長度

maxMonthDataCount = monthCount;

maxQuarterDataCount = monthCount / 3;

 

// 計算有效月資料長度,遇到0或無資料停止計數

realMonthCount = 0;

quitLoop = false;

for i = 0 to maxMonthDataCount - 1 begin

  if quitLoop then break;

  tempVal = GetField("月營收", "M")[i];

  if tempVal > 0 then

    realMonthCount += 1

  else

    quitLoop = true;

end;

 

// 計算有效季資料長度,遇到0或無資料停止計數

realQuarterCount = 0;

quitLoop = false;

for i = 0 to maxQuarterDataCount - 1 begin

  if quitLoop then break;

  tempVal = GetField("EPS", "Q")[i];

  if tempVal > 0 then

    realQuarterCount += 1

  else

    quitLoop = true;

end;

 

// 若有效資料比設定少,使用有效資料數

maxMonthDataCount = realMonthCount;

maxQuarterDataCount = realQuarterCount;

 

// 取得最新資料 (index 0 是最新)

revenueNow = GetField("月營收", "M");

epsNow = GetField("EPS", "Q");

grossNow = GetField("營業毛利率", "Q");

opNow = GetField("營業利益率", "Q");

netNow = GetField("稅後淨利率", "Q");

roaNow = GetField("資產報酬率", "Q");

roeNow = GetField("股東權益報酬率", "Q");

 

// 歷史新高判斷 (需至少3筆資料)

if maxMonthDataCount >= 3 then

  revenueHistoryHigh = (revenueNow >= Highest(GetField("月營收", "M"), maxMonthDataCount))

else

  revenueHistoryHigh = false;

 

if maxQuarterDataCount >= 3 then begin

  epsHistoryHigh = (epsNow >= Highest(GetField("EPS", "Q"), maxQuarterDataCount));

  grossHistoryHigh = (grossNow >= Highest(GetField("營業毛利率", "Q"), maxQuarterDataCount));

  opHistoryHigh = (opNow >= Highest(GetField("營業利益率", "Q"), maxQuarterDataCount));

  netHistoryHigh = (netNow >= Highest(GetField("稅後淨利率", "Q"), maxQuarterDataCount));

  roaHistoryHigh = (roaNow >= Highest(GetField("資產報酬率", "Q"), maxQuarterDataCount));

  roeHistoryHigh = (roeNow >= Highest(GetField("股東權益報酬率", "Q"), maxQuarterDataCount));

end else begin

  epsHistoryHigh = false;

  grossHistoryHigh = false;

  opHistoryHigh = false;

  netHistoryHigh = false;

  roaHistoryHigh = false;

  roeHistoryHigh = false;

end;

 

// 同期新高判斷(非歷史高時且有足夠資料才判斷)

// 月營收同期新高:需有13筆以上資料,且從12期開始比

if (not revenueHistoryHigh) and (maxMonthDataCount >= 13) then begin

  revenueSamePeriodHigh = true;

  for i = 12 to maxMonthDataCount - 1 begin

    if GetField("月營收", "M")[i] >= revenueNow then

      revenueSamePeriodHigh = false;

  end;

end else

  revenueSamePeriodHigh = revenueHistoryHigh;

 

排序方式: 標準 | 最新
gama360 發文於   2025/08/09

// EPS及其他財報同期新高判斷,需5筆以上資料,且從第4期開始比

if (not epsHistoryHigh) and (maxQuarterDataCount >= 5) then begin

  epsSamePeriodHigh = true;

  for i = 4 to maxQuarterDataCount - 1 begin

    if GetField("EPS", "Q")[i] >= epsNow then

      epsSamePeriodHigh = false;

  end;

end else

  epsSamePeriodHigh = epsHistoryHigh;

 

if (not grossHistoryHigh) and (maxQuarterDataCount >= 5) then begin

  grossSamePeriodHigh = true;

  for i = 4 to maxQuarterDataCount - 1 begin

    if GetField("營業毛利率", "Q")[i] >= grossNow then

      grossSamePeriodHigh = false;

  end;

end else

  grossSamePeriodHigh = grossHistoryHigh;

 

if (not opHistoryHigh) and (maxQuarterDataCount >= 5) then begin

  opSamePeriodHigh = true;

  for i = 4 to maxQuarterDataCount - 1 begin

    if GetField("營業利益率", "Q")[i] >= opNow then

      opSamePeriodHigh = false;

  end;

end else

  opSamePeriodHigh = opHistoryHigh;

 

if (not netHistoryHigh) and (maxQuarterDataCount >= 5) then begin

  netSamePeriodHigh = true;

  for i = 4 to maxQuarterDataCount - 1 begin

    if GetField("稅後淨利率", "Q")[i] >= netNow then

      netSamePeriodHigh = false;

  end;

end else

  netSamePeriodHigh = netHistoryHigh;

 

if (not roaHistoryHigh) and (maxQuarterDataCount >= 5) then begin

  roaSamePeriodHigh = true;

  for i = 4 to maxQuarterDataCount - 1 begin

    if GetField("資產報酬率", "Q")[i] >= roaNow then

      roaSamePeriodHigh = false;

  end;

end else

  roaSamePeriodHigh = roaHistoryHigh;

 

if (not roeHistoryHigh) and (maxQuarterDataCount >= 5) then begin

  roeSamePeriodHigh = true;

  for i = 4 to maxQuarterDataCount - 1 begin

    if GetField("股東權益報酬率", "Q")[i] >= roeNow then

      roeSamePeriodHigh = false;

  end;

end else

  roeSamePeriodHigh = roeHistoryHigh;

 

// 計算得分

score = 0;

if (revenueHistoryHigh or revenueSamePeriodHigh) then score += 1;

if (epsHistoryHigh or epsSamePeriodHigh) then score += 1;

if (grossHistoryHigh or grossSamePeriodHigh) then score += 1;

if (opHistoryHigh or opSamePeriodHigh) then score += 1;

if (netHistoryHigh or netSamePeriodHigh) then score += 1;

if (roaHistoryHigh or roaSamePeriodHigh) then score += 1;

if (roeHistoryHigh or roeSamePeriodHigh) then score += 1;

// 判斷是否通過門檻

ret = (score >= minScore);

 

虎科大許教授 發文於   2025/08/11

在選股腳本,你可以使用GetFieldStartOffset抓欄位的第一筆資料距離現在幾根K棒,再用simpleHighest函數抓歷史高點。

gama360 發文於   2025/08/11

感謝教授幫忙,以解決

發表回覆
Close