計算股價創新高的天數

  •   270 
  • 最後發表   小彬  2023 六月 01
小彬 發文於   2023/05/29

Hi 小編您好~

我利用XQ的範例來修改成XS指標,

想要求得一個歷史股價區間之最高價與最新股價比對是否有創新高以及最新股價距離歷史新高的天數

指標跑得相當緩慢才出現結果及天數也相當奇怪 不知道是否有誤 謝謝

程式碼範例如下:

settotalBar(6000);

input: min_period(3000, "最低期");

value1 = getBarOffset(Highest(GetField("最高價", "D")[1],min_period));

value2 = GetField("收盤價", "D");// 最新一期收盤價

var: idx(0);

idx = 1;

while idx <= value1 begin

    if GetField("收盤價", "D")[idx] < value2 then

        idx = idx + 1

    else

        break;

end;     

if idx >=  min_period then plot1(idx, "創新高天數");

XQ小幫手 發文於   2023/06/01

 Hello 小彬,

 

您使用 settotalBar(6000); 會讓其至少要計算6000期,且 min_period 設3000 就代表您找最高價時要找3000根,那麼自然會跑的緩慢。

另外,GetBarOffset 是依日期取得相對位置,而 Highest 回傳的是最高值,兩者是不一樣的東西。

 

如果您只是要在指標腳本上取得最高價與其相對距離,並判斷是否有創新高的話,可以這樣寫:

settotalBar(0);

setbackBar(0);

 

if currentBar = 1 then begin

    value1 = high;

    value2 = currentBar;

    end;

 

if high > value1 then begin

    value1 = high;

    value2 = currentBar;

    end;

 

value3 = currentbar - value2;

condition1 =  currentBar >= 2 and close > value1[1];

上面的value1和value3 分別記錄了最高價以及距離當下的相對位置,condition1則是判斷是否有創新高。

發表回覆
Close