settotalbar在特定頻率會失效

  •   228 
  • 最後發表   小富  2025 六月 20
小富 發文於   2025/06/13

親愛的小編 &版上各位高手,

       小弟有寫了一些自動交易程式。但最近發現在15minK的環境中,早上開盤前開啟的程式交易,計算出來的值會錯誤。我有使用settotalbar的函數。在45分之後,關閉再立即開啟,才會正常。其他頻率程式都沒問題,甚至有程式跨頻率計算的也都正確,就這隻純15分鐘的會有問題,不知可能

var: price(0),dif_s(0),macd_s(0),osc_s(0);
var: stopprice(0),dif_s2(0),macd_s2(0),osc_s2(0);
var: riskpoint(0),riskpoint2(0),riskpoint3(0),profit(0);
var: intrabarpersist controlbar(0);

// if date<>date[1] then highestprice =0; //自訂義變數歸零
if barfreq <> "Min" or barinterval<>15  then return; {交易時間頻率設定}

settotalbar((maxlist(FastLength2,SlowLength2,MACDLength2)+1)*40);

price = WeightedClose();

//趨勢環境 macd89計算式
dif_s2 = XAverage(price, FastLength2) - XAverage(price, SlowLength2);
macd_s2 = XAverage(dif_s2, MACDLength2) ;
osc_s2 = dif_s2 - macd_s2 ;

問題在哪? 請幫忙解決,謝謝!

附加文件

排序方式: 標準 | 最新
小富 發文於   2025/06/16

親愛的小編 &版上各位高手,

       小弟有寫了一些自動交易程式。但最近發現在15minK的環境中,早上開盤前開啟的程式交易,計算出來的值會錯誤。我有使用settotalbar的函數。在45分之後,關閉再立即開啟,才會正常。其他頻率程式都沒問題,甚至有程式跨頻率計算的也都正確,就這隻純15分鐘的會有問題,不知可能

var: price(0),dif_s(0),macd_s(0),osc_s(0);
var: stopprice(0),dif_s2(0),macd_s2(0),osc_s2(0);
var: riskpoint(0),riskpoint2(0),riskpoint3(0),profit(0);
var: intrabarpersist controlbar(0);

// if date<>date[1] then highestprice =0; //自訂義變數歸零
if barfreq <> "Min" or barinterval<>15  then return; {交易時間頻率設定}

settotalbar((maxlist(FastLength2,SlowLength2,MACDLength2)+1)*40);

price = WeightedClose();

//趨勢環境 macd89計算式
dif_s2 = XAverage(price, FastLength2) - XAverage(price, SlowLength2);
macd_s2 = XAverage(dif_s2, MACDLength2) ;
osc_s2 = dif_s2 - macd_s2 ;

問題在哪? 請幫忙解決,謝謝!

小富 發文於   2025/06/16

今天一早再度發生,兩個不同的程式,但都是計算15分鐘頻率的MACD值,設定抓的筆數是一樣的,但計算結果差很大。

 

計算錯誤那程式必須在(期貨)開盤後關掉重開就恢復正常

 

親愛的小編 &版上各位高手,

       小弟有寫了一些自動交易程式。但最近發現在15minK的環境中,早上開盤前開啟的程式交易,計算出來的值會錯誤。我有使用settotalbar的函數。在45分之後,關閉再立即開啟,才會正常。其他頻率程式都沒問題,甚至有程式跨頻率計算的也都正確,就這隻純15分鐘的會有問題,不知可能

var: price(0),dif_s(0),macd_s(0),osc_s(0);
var: stopprice(0),dif_s2(0),macd_s2(0),osc_s2(0);
var: riskpoint(0),riskpoint2(0),riskpoint3(0),profit(0);
var: intrabarpersist controlbar(0);

// if date<>date[1] then highestprice =0; //自訂義變數歸零
if barfreq <> "Min" or barinterval<>15  then return; {交易時間頻率設定}

settotalbar((maxlist(FastLength2,SlowLength2,MACDLength2)+1)*40);

price = WeightedClose();

//趨勢環境 macd89計算式
dif_s2 = XAverage(price, FastLength2) - XAverage(price, SlowLength2);
macd_s2 = XAverage(dif_s2, MACDLength2) ;
osc_s2 = dif_s2 - macd_s2 ;

問題在哪? 請幫忙解決,謝謝!

附加文件

虎科大許教授 發文於   2025/06/16

我可以幫忙測試看看,但你需要提供input的參數並告知執行的商品。

小富 發文於   2025/06/16

//15Min MACD做多當沖交易

{執行時間控制}
input: time1(084500,"程式開始時間");
if currenttime >= time1 then Begin

// 宣告參數
input: FastLength(5,"DIF短天數"), SlowLength(25,"DIF長天數"), MACDLength(2,"MACD天數");
input: FastLength2(13,"趨勢DIF短天數"), SlowLength2(89,"趨勢DIF長天數"), MACDLength2(34,"趨勢MACD天數");
input: FastLength15(5,"DIF短天數"), SlowLength15(25,"DIF長天數"), MACDLength15(2,"MACD天數");
input: riskpoint1(35,"停利停損點數"),riskpercent(0.2,"停利百分比");

var: price(0),dif_s(0),macd_s(0),osc_s(0);
var: stopprice(0),dif_s2(0),macd_s2(0),osc_s2(0);
var: riskpoint(0),riskpoint2(0),riskpoint3(0),profit(0);
var: intrabarpersist controlbar(0),highestprice(0);

// if date<>date[1] then highestprice =0; //自訂義變數歸零
if barfreq <> "Min"or barinterval<>15  then return; {交易時間頻率設定}

//settotalbar((maxlist(FastLength2,SlowLength2,MACDLength2)+1)*4);

price = WeightedClose();
//macd25計算式
dif_s = XAverage(price, FastLength) - XAverage(price, SlowLength);
macd_s = XAverage(dif_s, MACDLength) ;
osc_s = dif_s - macd_s ;

//趨勢環境 macd89計算式
dif_s2 = XAverage(price, FastLength2) - XAverage(price, SlowLength2);
macd_s2 = XAverage(dif_s2, MACDLength2) ;
osc_s2 = dif_s2 - macd_s2 ;


//進場條件
condition1 = osc_s2 > 0 and dif_s2 >0 and osc_s >=osc_s[1] and osc_s[1]>=0;
condition2 = controlbar <> currentbar;

// 多方進場策略:MACD由負轉正。
if Position = 0 and condition1 and condition2 then begin
   SetPosition(1);
end;

//停利停損處理

if Position = 1 and Filled = 1 then begin

    if Close <= FilledAvgPrice -riskpoint1 then begin
        { 停損 }
        SetPosition(0);
        highestprice = 0;
        controlbar = currentbar;
    end else begin

        { 判斷是否要啟動停利 }
        if highestprice = 0 and Close >= FilledAvgPrice {and controlbar <>currentbar} then begin  { -riskpoint}
           highestprice = Close;
        {停利點數}
         profit = highestprice - FilledAvgPrice;  {用highestprice才能抓住最大利潤去計算停利點}
         riskpoint2 = close *(riskpercent/100);
         riskpoint3 = (highestprice - FilledAvgPrice)/3;
         end;

         if profit < riskpoint2 then riskpoint = riskpoint1
         else if profit > riskpoint2 and profit <200 then riskpoint = riskpoint2
         else if profit > 200 then riskpoint = riskpoint3;

        if highestprice <> 0 then begin       
            if Close <= highestprice - riskpoint or osc_S cross below 0 then begin
                SetPosition(0);
                highestprice = 0;
                controlbar = currentbar;
            end else if Close > highestprice then begin
                highestprice = Close;
            end;    
        end;        
    end;
end;

//監控
print("highestprice=",numtostr(highestprice,0),"riskpoint=",numtostr(riskpoint,0),"FilledAvgPrice=",numtostr(FilledAvgPrice,0),"stopprice=",numtostr(stopprice,0),"close=",numtostr(close,0));
print("osc_s2=",numtostr(osc_s2,2),"dif_s2=",numtostr(dif_s2,2),"osc_s=",numtostr(osc_s,2),"osc_s[1]=",numtostr(osc_s[1],2));
print("controlbar=",numtostr(controlbar,0),"currentbar=",numtostr(currentbar,0));

//尾盤當沖平倉策略
if Position <> 0 AND  currenttime >= 134300 then begin
SetPosition(0, market);
if position =0 then RaiseRunTimeError("尾盤沖銷,程式結束");
end;
end;

小富 發文於   2025/06/16

謝謝&麻煩許教授,商品是微型台指近月(一般)。

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

我測試以下的交易腳本,是OK的,你試試看。

//15Min MACD做多當沖交易
//微型台指近月(一般)
{執行時間控制}
if barfreq <> "Min"or barinterval<>15  then raiseRunTimeError("限用15分鐘"); {交易時間頻率設定}
// 宣告參數
input: time1(084500,"程式開始時間");
input: FastLength(5,"DIF短天數"), SlowLength(25,"DIF長天數"), MACDLength(2,"MACD天數");
input: FastLength2(13,"趨勢DIF短天數"), SlowLength2(89,"趨勢DIF長天數"), MACDLength2(34,"趨勢MACD天數");
input: FastLength15(5,"DIF短天數"), SlowLength15(25,"DIF長天數"), MACDLength15(2,"MACD天數");
input: riskpoint1(35,"停利停損點數"),riskpercent(0.2,"停利百分比");
settotalbar((maxlist(FastLength2,SlowLength2,MACDLength2)+1)*4);
var: price(0),dif_s(0),macd_s(0),osc_s(0);
var: stopprice(0),dif_s2(0),macd_s2(0),osc_s2(0);
var: riskpoint(0),riskpoint2(0),riskpoint3(0),profit(0);
var: intrabarpersist controlbar(0),intrabarpersist highestprice(0);

if currenttime >= time1 then 
    Begin
        price = WeightedClose();
        //macd25計算式
        dif_s = XAverage(price, FastLength) - XAverage(price, SlowLength);
        macd_s = XAverage(dif_s, MACDLength) ;
        osc_s = dif_s - macd_s ;
        //趨勢環境 macd89計算式
        dif_s2 = XAverage(price, FastLength2) - XAverage(price, SlowLength2);
        macd_s2 = XAverage(dif_s2, MACDLength2) ;
        osc_s2 = dif_s2 - macd_s2 ;
        //進場條件
        condition1 = osc_s2 > 0 and dif_s2 >0 and osc_s >=osc_s[1] and osc_s[1]>=0;
        condition2 = controlbar <> currentbar;
        // 多方進場策略:MACD由負轉正。
        if Position = 0 and condition1 and condition2 then 
            begin
                SetPosition(1);
            end;
        //停利停損處理
        if Position = 1 and Filled = 1 then 
            begin
                if Close <= FilledAvgPrice -riskpoint1 then 
                    begin
                        { 停損 }
                        SetPosition(0);
                        highestprice = 0;
                        controlbar = currentbar;
                    end 
                else 
                    begin
                        { 判斷是否要啟動停利 }
                        if highestprice = 0 and Close >= FilledAvgPrice {and controlbar <>currentbar} then 
                            begin  { -riskpoint}
                                highestprice = Close;
                                {停利點數}
                                profit = highestprice - FilledAvgPrice;  {用highestprice才能抓住最大利潤去計算停利點}
                                riskpoint2 = close *(riskpercent/100);
                                riskpoint3 = (highestprice - FilledAvgPrice)/3;
                            end;
                        if profit < riskpoint2 then riskpoint = riskpoint1
                        else if profit > riskpoint2 and profit <200 then riskpoint = riskpoint2
                        else if profit > 200 then riskpoint = riskpoint3;
                        if highestprice <> 0 then 
                            begin       
                                if Close <= highestprice - riskpoint or osc_S cross below 0 then 
                                    begin
                                        SetPosition(0);
                                        highestprice = 0;
                                        controlbar = currentbar;
                                    end 
                                else 
                                    if Close > highestprice then 
                                        begin
                                            highestprice = Close;
                                        end;    
                            end;        
                    end;
            end;
        //監控
        print("highestprice=",numtostr(highestprice,0),"riskpoint=",numtostr(riskpoint,0),"FilledAvgPrice=",numtostr(FilledAvgPrice,0),"stopprice=",numtostr(stopprice,0),"close=",numtostr(close,0));
        print("osc_s2=",numtostr(osc_s2,2),"dif_s2=",numtostr(dif_s2,2),"osc_s=",numtostr(osc_s,2),"osc_s[1]=",numtostr(osc_s[1],2));
        print("controlbar=",numtostr(controlbar,0),"currentbar=",numtostr(currentbar,0));
        //尾盤當沖平倉策略
        if Position <> 0 AND  currenttime >= 134300 then 
            begin
                SetPosition(0, market);
                if position =0 then RaiseRunTimeError("尾盤沖銷,程式結束");
            end;
    end;
print(date,time,close);

小富 發文於   2025/06/17

我試試看,感謝許教授!

小富 發文於   2025/06/18

許教授,

    今天測試,結果似乎仍然一樣。您可以看一下附件.....第一張是45分前開啟程式計算的結果,計算值依然超高。一開盤就觸發進場,後來關掉再重開,計算值就掉下來了(附圖2)。

附加文件

小富 發文於   2025/06/18

我另一組程式是雙頻率(5分鐘與15分鐘),那組計算出來的就都很正常。[提供您參考]

虎科大許教授 發文於   2025/06/18

這個有點奇怪,我明天盤前啟動策略測試看看。

顯示更多回應 發表回覆
Close