執行時發生錯誤(1303)嘗試除以0

  •   69 
  • 最後發表   小小的茶米  4 週前
小小的茶米 發文於   2026/05/04

程式中這段造成自動交易執行錯誤,把它拿掉就不會出現.

if not (GetSymbolInfo("買賣現沖") or GetSymbolInfo("可放空")) then return;

可是這幾檔股票今天都可以買賣現沖

排序方式: 標準 | 最新
虎科大許教授 發文於   2026/05/04

檢查你程式中放在分母的變數或運算數值,看看是否可能會有0的情況。

小小的茶米 發文於   2026/05/04

許教授您好,我把這行移除就不會出現問題,程式裡也沒有分母變數會為0的狀況0.0

if not (GetSymbolInfo("買賣現沖") or GetSymbolInfo("可放空")) then return;

虎科大許教授 發文於   2026/05/04

(1)程式除以0的錯誤只會發生在計算分數時分母為0。

(2)若你控制不可買賣現沖或不可放空的股票就Return,則請改成

if GetSymbolInfo("買賣現沖")=false or GetSymbolInfo("可放空")=false then return;

 

小小的茶米 發文於   2026/05/05

許教授您好,改成一樣會出現,只有把這段拿掉才不會出現0.0

if GetSymbolInfo("買賣現沖")=false or GetSymbolInfo("可放空")=false then return;

虎科大許教授 發文於   2026/05/05

沒看到完整的程式碼,實在很難幫得上忙。若不方便上傳程式碼,就只能這樣了。

小小的茶米 發文於   2026/05/06

許教授您好,程式碼如下,只要拿掉這行就不會有問題. 麻煩您了, 謝謝 if not (GetSymbolInfo("買賣現沖") or GetSymbolInfo("可放空")) then return;//現股放空

程式碼

SetTotalBar(300);

if barfreq <> "D" then raiseRunTimeError("限用日K");

 

input: _exit_period(3, "收盤前N分鐘平倉");

 

var: _market_close_condition(false);

var: intrabarpersist oncePerBar(0);

var: _TradeMaxSize(1);

var:

_short_condition1(false),        { 是否做空 }

_exit__short_condition1(false),  { 是否空單出場 }

_short_condition2(false),        { 是否做空 }

_exit__short_condition2(false);  { 是否空單出場 }

 

var: nonPG(0), PG1(1), PG2(2);

var: intrabarpersist _isTradePG(PG1);

 

if GetSymbolInfo("處置股") then return;

if not (GetSymbolInfo("買賣現沖") or GetSymbolInfo("可放空")) then return;//現股放空

 

_market_close_condition = EnterMarketCloseTime(_exit_period);

 

value1 = Average(close[1], 10); // 10日均價(前一日)

value2 = Average(close[1], 20); // 20日均價(前一日)

value3 = Average(close[1], 60); // 60日均價(前一日)

 

value993 = GetSymbolField("TSE.TW","close","D");

 

value10 = getfield("參考價","D");           // 今日參考價(平盤)

value11 = getfield("漲停價","D");           // 今日漲停價

value12 = getfield("估計量");               // 今日預估量

value13 = GetField("漲停價","D")[1];        // 昨日漲停價

value16 = GetField("投信買賣超","D")[1];    // 昨日投信買賣超

value17 = GetField("外資買賣超","D")[1];    // 昨日外資買賣超

 

condition1 = CurrentTime <= 120000;

condition2 = value993 >= value993[1]*0.99;

 

condition301 = Puu_SubTurnOverRatio(1) >= 3;

if (lowest(low[1],60) <> 0) then

condition350 = ((highest(high[1],60)-lowest(low[1],60))/lowest(low[1],60))*100 >= 30;

condition351 = value1 > value2 and value2 > value3;

condition354 = close[1] < open[1]

               and close[1] < ((high[1]-low[1])/3+low[1])

               and close[1] > low[1];

condition359 = value16 < volume[1]/100;

condition360 = value17 > volume[1]/50;

 

condition401 = close <= value10*(1-0.09);

condition402 = close >= value10*1.065;

condition450 = close <= value10*(1-0.09);

condition451 = close >= value10*1.065;

condition999 = oncePerBar <> currentbar; // 一根Bar最多只交易一次

 

_short_condition1 =

condition1 and condition2 and condition301 and condition999;

 

_short_condition2 =

condition1 and condition350 and condition351 and condition354

and (condition359 or condition360) and condition999;

 

_exit__short_condition1 = _isTradePG = PG1

and (_market_close_condition or condition401 or condition402);

 

_exit__short_condition2 = _isTradePG = PG2

and (_market_close_condition or condition450 or condition451);

 

if Position = 0 and filled = 0

and _short_condition1

then begin

oncePerBar = currentbar; // 若當根Bar已經執行過,currentbar就會記錄當根Bar的編號避免再度執行

SetPosition(-_TradeMaxSize, AddSpread(close,-5));

_isTradePG = PG1;

end

else if Position = 0 and filled = 0

and _short_condition2

then begin

oncePerBar = currentbar; // 若當根Bar已經執行過,currentbar就會記錄當根Bar的編號避免再度執行

SetPosition(-_TradeMaxSize, AddSpread(close,-5));

_isTradePG = PG2;

end;

 

if Position < 0 and filled < 0

and _exit__short_condition1

then begin

oncePerBar = currentbar;

SetPosition(0, value11);

end

else if Position < 0 and filled < 0

and _exit__short_condition2

then begin

oncePerBar = currentbar;

SetPosition(0, value11);

end;

 

虎科大許教授 發文於   2026/05/06

你的問題出在lowest(low[1],60)為0,雖然過去60天最低價沒有為0,但用lowest函數,儘管較有效率,但容易有邏輯錯誤的問題。若你把它改成simplelowest問題就迎刃而解。

if simplelowest(low[1],60) <> 0 then

condition350 = ((simplehighest(high[1],60)-simplelowest(low[1],60))/simplelowest(low[1],60))*100 >= 30;

小小的茶米 發文於   2026/05/06

謝謝許教授, 的確改為simplelowest就不會出現問題.

只是不了解是Extremes 函數的效能優化機制造成這個問題的嗎?

虎科大許教授 發文於   2026/05/06

是的。

小小的茶米 發文於   2026/05/09

謝謝許教授!

發表回覆
Close