自動交易異常問題

  •   244 
  • 最後發表   wei15  2024 五月 10
wei15 發文於   2024/05/07


最近用程式來試著交易選擇權,因為要設定價外兩檔來交易就好,再跑時有買進成功了,但到了賣出時,已經達到設定條件了(價格223達到了,回跌至197就該賣出),但沒有賣出。檢查有發現print列印到10:17分就停止了,也沒顯示任何異常。(有檢查過是否為return的問題,但始終找不到原因)
想請問我的程式碼有哪裡寫錯,導致商品沒有跑動嗎?

以下程式碼與圖片。

 

 

 

settotalBar(10); 
if barfreq<> "Min" or barinterval <> 1 then raiseRunTimeError("請設定1分鐘");

//if getinfo("TradeMode")=0 then return;
condition35=085000<=GetField("時間", "1") and GetField("時間", "1")<= 134500;//日盤
condition36=150500<=GetField("時間", "1") or GetField("時間", "1")<= 020000;//夜盤
Value1 = AddSpread(Close, -1);  //+1為賣出第一檔(通常買進使用)-1為買進第一檔(賣出使用)
Value2 = AddSpread(Close, 1);  //+1為賣出第一檔(通常買進使用)-1為買進第一檔(賣出使用)
if absValue(close-value1)>10 or absValue(close-value2)>10 then return;

//履約價條件
input:nx(100,"選擇權履約價差");
if absValue((GetSymbolField("FIMTXN*1.TF", "收盤價", "1")-GetSymbolInfo("履約價")))>1000 then return;
var:BSY(true);
if  GetField("日期", "D")<>GetField("日期", "D")[1] then begin
if absValue((GetSymbolField("FIMTXN*1.TF", "收盤價", "1")-GetSymbolInfo("履約價")))<=nx  
then BSY=true else BSY=false;
end;
 print("確認return",absValue((GetSymbolField("FIMTXN*1.TF", "收盤價", "1")-GetSymbolInfo("履約價")))>1000);

//做買賣權  
input:judgecp(0,"1買權,2賣權");
condition100=StrEndWith(GetSymbolInfo("買賣權"),"call");
condition101=StrEndWith(GetSymbolInfo("買賣權"),"put");
if judgecp=0 then raiseRunTimeError("請設定買賣權");
if condition100 and judgecp=2  then return;//執行賣權
if condition101 and judgecp=1  then return;//執行買權
if judgecp=2 and GetSymbolInfo("履約價")>GetSymbolField("FITXN*1.TF", "收盤價", "1")then return;//執行賣權
if judgecp=1 and GetSymbolInfo("履約價")<GetSymbolField("FITXN*1.TF", "收盤價", "1")then return;//執行買權



input: _invL(20000,"一次買多少金額,無用0506");
input:xx(1,"0為買樂透 1為執行停損(決定是否買樂透)");
input: profit_point(0, "開頭停利倍數 不設的話請打0"); 
input:tt(15,"收盤前幾分鐘平倉");
input: winloss_point(1.3, "開頭停利移動(點):倍數"); 
input: profit_point2(1.5, "獲利後移動停損(點)2停損移到上一根停利移動點"); 


input: loss_point1(0.7, "停損1(點):倍數");//拉到平均價以上 
input: loss_point2(1.15, "上移停損2(點):倍數,獲利"); 

//要賠多少錢
var:ope(0),opee(0);
input: loss_percent(30, "停損(%)");
input:lossmoney(100000,"停損金額");
ope=close-(close*(1-0.01*loss_percent));//要賠多少點
opee=floor(lossmoney/(ope*50+60));//需要買多少口
if opee*(close*50)>9000000 then raiseRunTimeError("超過保證金");



input:k1(15,"第1根漲跌多少");
input:k2(20,"第1-2根漲跌多少");
input:k3(20,"第1-3根漲跌多少");
//判斷夜盤漲跌(小台)
if GetField("日期", "D")<>GetField("日期", "D")[1] then begin
if AbsValue(GetSymbolField("FIMTXN*1.TF", "high", "1")-GetSymbolField("FIMTXN*1.TF", "low", "1"))>=k1 or 
   AbsValue(GetSymbolField("FIMTXN*1.TF", "high", "1")[1]-GetSymbolField("FIMTXN*1.TF", "low", "1"))>=k2 or
   AbsValue(GetSymbolField("FIMTXN*1.TF", "high", "1")[2]-GetSymbolField("FIMTXN*1.TF", "low", "1"))>=k3
   then  condition3=true else condition3=false;
   end;
input:opdd(1,"選擇權短時間上漲多少(通常為1)");
//判斷選擇權短時間上漲
if GetField("收盤價", "1")>=GetField("收盤價", "1")[1] and  GetField("日期", "D")<>GetField("日期", "D")[1]then begin
 if GetField("high", "1")-GetField("low", "1")>=opdd and sign(GetField("high", "1")-GetField("low", "1"))=1  or 
    GetField("收盤價", "1")-GetField("收盤價", "1")[1]>=opdd and sign(GetField("收盤價", "1")-GetField("收盤價", "1")[1])=1 or 
    GetField("收盤價", "1")-GetField("收盤價", "1")[2]>=opdd  and sign(GetField("收盤價", "1")-GetField("收盤價", "1")[2])=1
    then condition4=true else condition4=false;
    end;


if condition3 and condition4 and condition35  and  filled=0 and position=0  and BSY and not enterMarketCloseTime(tt)or condition36 and condition3 and condition4    and  filled=0 and position=0 and BSY and not enterMarketCloseTime(tt) then begin
if  opee<>0  then begin setposition(opee,market,label:="買進");
end;
end;

if winloss_point = 0 then raiseruntimeerror("開頭停損(點):倍數"); 


if condition35 and Position >0 and Filled >0 and position=filled and position<>0 or condition36 and Position >0 and Filled >0 and position=filled and position<>0 then  begin 
    { 依照成本價格設定停損/停利 } 
    var: intrabarpersist stoploss_point21(0); 
    var: intrabarpersist stoploss_point22(0); 
    { 計算停損價格 } 
    if stoploss_point21 = 0 and stoploss_point22=0 then begin 
        stoploss_point21 = FilledAvgPrice*winloss_point;//變數1 計算停利價格 
        stoploss_point22 = FilledAvgPrice*loss_point1;//變數1 計算停損價格 
    end; 


    { 如果價格上漲的話, 則往上挪動停損價格. 停損價格只會越來越高 } 

if filled>0 then begin //收盤價 大於 未平倉成本 
        if Close  >= stoploss_point21 then begin //收盤價 大於 停利價格+18 
   var: intrabarpersist stoploss_point23(0);         
            stoploss_point23 = FilledAvgPrice *loss_point2; // 194
    end;     

if filled>0 then begin //收盤價 大於 未平倉成本 
    if Close  >= filledAvgPrice*profit_point2 then begin // 
   var: intrabarpersist stoploss_point24(0);         
            stoploss_point24 = FilledAvgPrice*winloss_point; // 
    end; 

end; 
end; 
 end; 

if filled<>0 and   profit_point <> 0 and Close >= FilledAvgPrice*profit_point then begin 
        { 停利 } 
        SetPosition(0,market,label:="停利出場"); 

    end else  
if  filled<>0 and xx<>0 and  stoploss_point22<>0 and Close <= stoploss_point22  then begin 
        { 停損 } 
        SetPosition(0,market,label:="停損1出場"); 

    end else  
if  filled<>0 and   0<>stoploss_point23 and close<=stoploss_point23 then begin 
setposition(0,market,label:="停損2出場"); 


    end else  
if  filled<>0 and   0<>stoploss_point24 and close<=stoploss_point24 then begin 
setposition(0,market,label:="停損3出場"); 


end;     
if enterMarketCloseTime(tt) then begin setposition(0,market,label:="收盤不計代價平倉");end;




if filled>0 and position>0 then begin
print("口數",opee,"平均成本=",filledAvgPrice,"收盤價=",close,"開始停利損",stoploss_point21,"停損=",stoploss_point22,stoploss_point23,stoploss_point24);
end;
//測試市價

附加文件

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

是否版本太舊?我之前就有一次,因為版本太舊,沒更新,也有相同的問題。更新版本之後,就解決問題了。

wei15 發文於   2024/05/10

原來,那我更新一下再試看看好了,謝謝。

發表回覆
Close