再次遭遇讀取外資持股張數與持股比率問題

  •   308 
  • 最後發表   蔣大哥  2025 二月 17
蔣大哥 發文於   2025/02/16

XS小編, 你好 

 

我編寫以下程式碼,以讀取過去五天代號 2330台積電外資持股張數與持股佔比 :

 

 

//===================  TEST外資持股張數比率 =============

//===================   2025/02/15  =====================

SetBarFreq("D"); // 指定選股腳本只能執行日線的頻率上面

settotalBar(60);

input : _len(5, "追蹤期間");

VAR : FRNshrhold(0);    //    每日外資持股張數 

VAR : FRNrtihold(0);    //    每日外資持股比率

VAR : idx(0); 

VAR : strfrnsh(""),strfrnrt(""), strTmp1(""), strTmp2("");

VAR : ifmatch(false);

ifmatch = false;

IF symbol = "2330.TW" then 

   begin

        

     ifmatch = true;

     while idx < _len

      Begin

   FRNshrhold = Getfield("外資持股","D")[idx];

   strTmp1 =  text(NumtoStr(FRNshrhold,0));   

           strfrnsh = strfrnsh  +  strTmp1 + " / ";

   FRNrtihold =  GetField("外資持股比例","D")[idx];

   strTmp2 =  text(NumtoStr(FRNrtihold,2));   

           strfrnrt = strfrnrt  +  strTmp2 + " / ";

           idx +=  1;

      End;  

   end;  

if ifmatch then ret =1;

 

OutputField(1,strfrnsh, "----外資持股五天變化");  

OutputField(2,strfrnrt, "----外資持股五天比率");  

 

 

執行出來結果為:

2/14日  19,094,532張,  2/13日 19,130,762張 ,........。而持股百分比卻為 73.63% / 73.7/73.77/.......

 

以上結果與個股籌碼分佈2/14日 減持 20,870 張致最新總持張數為 19,049,737張與佔比為 73.46並不完全相符。不知我的程式碼是否有bug, 還是系統有bug。請指正。

 

 

輸出檔案如附件。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

附加文件

排序方式: 標準 | 最新
虎科大許教授 發文於   2025/02/16

20250210.000000 19082537.000000 73.580000 

20250211.000000 19082732.000000 73.580000 

20250212.000000 19082107.000000 73.580000 

20250213.000000 19071222.000000 73.540000 

20250214.000000 19049738.000000 73.450000 

setBarFreq("D");
setTotalBar(5);
if symbol="2330.TW" then
    begin
        ret=1;
        print(date,Getfield("外資持股","D"),GetField("外資持股比例","D"));
    end;

蔣大哥 發文於   2025/02/17

感謝許教授迅速回覆指點。

只是我還是不解我的編程碼為什麼會產生不相同的結果,尚請 教授指點迷津。 謝謝。

蔣大哥 發文於   2025/02/17

還有 PRINT 指令指要求輸出 DATE , 外資持股 與 外資持股比例 , 實際輸出 DATE 後面 外資持股前 出現 000000 ,  不知細代表甚麼欄位 ?

虎科大許教授 發文於   2025/02/17

(1)跑迴圈是解決問題的最後選項,它比較沒效率。以後若要解決問題,能夠不跑迴圈就不跑迴圈。

(2)你的問題是兩個用來累加字串的變數,從第一根K棒(亦即60根的第一根)開始跑迴圈然後累加字串,數值當然會是錯誤的,就算setTotalBar(5)也無濟於事。正確的處理方式是在最後一根K棒時才往前累加字串。

(3)XQ的數值預設都是小數點6位數。

SetBarFreq("D"); // 指定選股腳本只能執行日線的頻率上面
settotalBar(60);
input : _len(5, "追蹤期間");
VAR : FRNshrhold(0);    //    每日外資持股張數 
VAR : FRNrtihold(0);    //    每日外資持股比率
VAR : idx(0); 
VAR : strfrnsh(""),strfrnrt(""), strTmp1(""), strTmp2("");
VAR : ifmatch(false);
ifmatch = false;
IF symbol = "2330.TW" then 
    begin
        ifmatch = true;
        if isLastBar then
            begin
                while idx < _len
                    Begin
                        FRNshrhold = Getfield("外資持股","D")[idx];
                        strTmp1 =  text(NumtoStr(FRNshrhold,0));   
                        strfrnsh = strfrnsh  +  strTmp1 + " / ";
                        FRNrtihold =  GetField("外資持股比例","D")[idx];
                        strTmp2 =  text(NumtoStr(FRNrtihold,2));   
                        strfrnrt = strfrnrt  +  strTmp2 + " / ";
                        idx +=  1;
                    End; 
            end;
    end;  
if ifmatch then ret =1;
OutputField(1,strfrnsh, "----外資持股五天變化");
OutputField(2,strfrnrt, "----外資持股五天比率");

 

蔣大哥 發文於   2025/02/17

萬分感謝許教授 畫龍點眼地 加入  if isLastBar , 現在跑出來就出現正確數字了.

 

發表回覆
Close