程式重開後,如何抓到從買入第一天到現在的最高價

  •   27 
  • 最後發表   蓬蘇王杜  3 小時前
蓬蘇王杜 發文於   2025/11/30

我本來是使用這樣的語法  , 但明明  hold_days 算出來都是大於一個 甚至還有大於2  可每次從開後都只能抓到今天的最高

之前的發問教授說 應該是因為抓到的  hold_days  都是 = 1  , 但我print出不是這樣  ,實在很好其  明明就大於1 卻都抓不到

我附上print圖片

截圖圖片

hold_days = DateDiff( CurrentDate,FilledEntryDate ) + 1 ;
HIGHDD = highest(high,hold_days) ; //進場後的高 


我之前是用這樣的寫法 可以做到重開記錄到高點  但這樣太低效了  

hold_days = DateDiff( CurrentDate,FilledRecordDate(1) ) + 1 ;


   if hold_days = 2 and highD(1) > filledavgPrice then begin
      if highx < highD(1) then highx = highD(1) ;
   end;

   if hold_days = 3 then begin
   Array: arrA[2](0);
   arrA[1] = highD(1);  arrA[2] = highD(2);
   value10 = HighestArray(arrA,2);
   if value10 > filledavgPrice and highx < value10 then highx = value10 ;
   end;

   if hold_days = 4 then begin
   Array: arrB[3](0);
   arrB[1] = highD(1);  arrB[2] = highD(2); arrB[3] = highD(3);
   value11 = HighestArray(arrB,3);
   if value11 > filledavgPrice and highx < value11 then highx = value11 ;
   end;

 實在很納悶 hold_days 就不等於一 也不等於0    HIGHDD = highest(high,hold_days)  的寫法卻都抓不到  , 那如果print出的是假資料 但我用 hold_days 推算的其他參數 又是正常 

例如我有用持有天數上移停損點  是有效的 

if hold_days >= 1 then  newstop1 = (-14)+(hold_days*1) ; //一天上移1%

 

以上 ,謝謝好心人

 

補充全部程式碼

inputs : pos1(18,"下單金額(萬)") ;

input:stoploss_en(1,"尾盤清股票降低風險",inputkind:=dict(["Yes",1],["No",0]));
var: intrabarpersist opposition (true); //開關
if stoploss_en=1 then opposition = true;  //開
if stoploss_en=0 then opposition = false;  //不開


//是否開單
input:op_not(1,"是否開單",inputkind:=dict(["Yes",1],["No",0]));
var: intrabarpersist op_x (true); //是否開單
if op_not=1 then op_x = true;  //開單
if op_not=0 then op_x = false;  //不開單




value1 = highest(close,60)[1]; //60天高
value2 = lowest(low,30)[1]; //30天低點


var : order_pos(0) , hold_days(0),HIGHDD(0),Profit(0),newstop1(0),newstop2(0),
      newstop3(0);

condition1 = close > value1 ; //突破60天高
condition2 = close < value2 ; //跌破30天低
condition3 = close >= close[1]*1.09;
condition4 = currentTime >= 132000 ;


if filled = 0 and condition1 and not condition4 and op_x then begin
   order_pos = IntPortion((pos1 * 10000) / (close * 1000));
    if order_pos > 0 then 
       setposition(order_pos, label:="進場");
end;

 // 停損邏輯
if filled >0  and close < filledavgPrice then begin
   //第1層 躲跌停 & 12%停損
   if close < (close[1]*0.91) then setposition(0, label:="-9%有跌停風險")
    else if close < (filledavgPrice*0.88) then setposition(0, label:="停損") ;
end;

   //第2層 停損上移
if filled >0  then begin
   hold_days = DateDiff( CurrentDate,FilledEntryDate ) + 1 ;
   if hold_days >= 1 then  newstop1 = (-14)+(hold_days*1) ; //一天上移1%
   HIGHDD = highest(high,hold_days) ; //進場後的高 
   Profit = HIGHDD / filledavgPrice ; //獲利百分比
   newstop2 = (-12)+((Profit-1)*100)/3 ; 
   //決定最終價
    if newstop1 = 0 then newstop1 = (-12) ;     
    newstop3 = MaxList(newstop1, newstop2);
       //停損邏輯開始
        if newstop3 > (-12) then begin  
          if newstop3 >= 2 then newstop3 = 2 ;
            if close < filledavgPrice*(1+(newstop3*0.01)) then
             setposition(0, label:="移動停損") ;
         end;
end;



if filled > 0 and close >filledavgPrice then begin
   if condition2 then setposition(0, label:="停利") ;
end;

//避免系統性風險
if opposition then begin
   if not condition3 and  close < filledavgPrice*1.1 and condition4
   then setposition(0, label:="避風險") ;

end;




print("HIGHDD",HIGHDD,"hold_days",hold_days,"newstop1",newstop1,"newstop2",newstop2,"newstop3",newstop3,"currentTime",currentTime);

 

 

 

 

 

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

如你圖片所示,hold_days為15,則highest(h, hold_days)會傳回這15天(包含今天)的最高價。若你今天的最高價是這15天的最高價,傳回今天的最高價數值就是正確的。若今天最高價並非15天以來最高價,就有問題。請舉實際例子說明有問題的情況。

蓬蘇王杜 發文於   2025/11/30

教授,文中的print資料就是實際例子 8150南茂持有15天 我設定的頻率是日,最高價應該為51.4。但print出只有抓到當日最高,也就是上周五最高

蓬蘇王杜 發文於   2025/11/30

我在主文補充一下 全部的程式碼

虎科大許教授 發文於   2025/12/01

我測試的結果是正確的,都是51.4。

if barfreq<>"D" then raiseRunTimeError("限用日頻率!");
input: hold_days(15);
var: HIGHDD(0),HIGHDD2(0);
HIGHDD = highest(high,hold_days);
HIGHDD2 = simplehighest(high,hold_days);
print(date,"HIGHDD",HIGHDD,"HIGHDD2",HIGHDD2);

蓬蘇王杜 發文於   2025/12/01

 好懸

虎科大許教授 發文於   2025/12/01

若hold_days為0,就會傳回當天最高價。我看你的程式碼hold_days經過運算為15,不應該變成0,除非運算後,下一個Tick才顯示數值。避免這種情況,你可嘗試用intrabarpersist宣告它看看。

var: intrabarpersist hold_days(0);

蓬蘇王杜 發文於   2025/12/01

宣告成 intrabarpersist  目前還是一樣抓不到

虎科大許教授 發文於   2025/12/01

按照你提供的程式碼,不應該出現你所提的問題。我猜測可能是受其他沒有揭露的程式碼影響。你在明天盤中試著執行我提供的程式,若也是得到正確的數據,代表問題出在沒有揭露的程式碼。這種情況下,要找出問題,就需要提供完整的程式碼才行。

蓬蘇王杜 發文於   2025/12/01

教授我後來在文中最下面給的是全部的程式碼了

就有點懸了

虎科大許教授 發文於   2025/12/01

我明天盤中用其他商品測試你的程式看看。

顯示更多回應 發表回覆
Close