賣出選擇權勒式價外一檔如何寫呢?求救

  •   379 
  • 最後發表   灰熊  5 週前
灰熊 發文於   2025/05/08

請問:如何讓程式自動在9:10下出勒式價外一檔的選擇權呢?(目前我寫不出來,都用手動)問題如下:
1.程式要抓取目前期貨價位來判斷價平
2.各選取價外一檔(例:期貸20000,則取20050C和19950P,作出雙賣)---只限週選擇權不考慮月選擇權
3.當選擇權的價位比成交價多20點,則鎖單邊的的單,例:若20500C,賣出100,19950P也賣出100,當指數上漲到20500C為120時,則單獨買進20500C,120,但19950P則維持不動。
4.若上例:20500C回跌到100時,則將之前買進的120平倉,維持原先勒式雙賣的組合
5.收盤前全數平倉
新手,功力不夠,再麻煩教授能否寫個範例,感激不盡,謝謝

排序方式: 標準 | 最新
灰熊 發文於   2025/05/08

寫在同一策略,分開下也無妨,不一定要組合單,只差別在權利金而己,主要能跑就行。

虎科大許教授 發文於   2025/05/08

1. 你對我期待太高了。已達代工水準的需求,需要花很多時間寫程式,除非很有意思,值得也花時間研究,不然很難找到義工幫忙處理。會得到協助的,大部份都是那些努力寫程式解決問題但卡關的人。

2. 你的需求邏輯,可思考如下:可在開盤後某個時間點(例如9點5分)啟動策略。執行商品選擇啟動當時,台指期價內、價外各5檔,合計11檔的商品監控。當然,也可以用商品組合,把最有機會監控的商品加入組合,然後指定組合監控。

(1)用getSymbolField抓台指期的價格。

(2)在9點10分時,判斷最接近台指期成交價的買權及賣權的履約價,以作為下單標的。

(3)用symbol判斷目前洗價的選擇權是否是下單的標的,若是則賣出。

(4)後續都用symbol判斷商品,以及加碼與出場的條件是否觸發。

虎科大許教授 發文於   2025/05/09

當洗價的商品是CALL,則close就是該CALL當下的成交價。若有庫存,則filledAvgPrice就是它的平均庫存成本。

灰熊 發文於   2025/05/09

3.value1和value2皆出現函數GetSymbolField:第一個數應該是String/Input/Group,這樣寫法有問題嗎?我的value1=getsymbolfield(strcalls,"收盤價","s");這不是字串嗎?

input:futureprice(20650);

var:_putout(0),_callout(0);

var:strcall(" "),strput(" "),strcalls(" "),strputs(" ");

_callout=futureprice+50;

_putout=futureprice-50;

strcall=numtostr(_callout,0);

strput=numtostr(_putout,0);

 

strcalls="TX2N05C"+strcall+".TF";

strputs ="TX2N05P"+strPUT+".TF";

 

if time=091000 then begin

    if symbol=strcalls then setposition(-1,market);  //賣20700Call         

    if symbol=strputs then setposition(-1,market); //賣20600PUT

 

 

   end;

   

//需要判斷目前價位與庫存價位是否有20點range

value1=getsymbolfield(strcalls,"收盤價","s");

value2=getsymbolfield(strputs,"收盤價","s"); 

 

 

 

 

 

//收盤前出場

   

 

if currenttime >= 133500 and  Filled<>0 then setposition(0, market);

虎科大許教授 發文於   2025/05/09

getsymbolfield第一個參數不接受變數。

灰熊 發文於   2025/05/09

謝謝教授

 

虎科大許教授 發文於   2025/05/09

//需要判斷目前價位與庫存價位是否有20點range

if symbol=strcalls and position<0 then

      if c-filledAvgPrice>20 then setposition(0);

if symbol=strputs and position<0 then

      if filledAvgPrice-c>20 then setposition(0);

灰熊 發文於   2025/05/10

謝謝教授,正在一點一點推進中,主要卡在c-filledAvgPrice是指CALL,那PUT是p-filledAvgPrice是嗎?就庫存會同時存在賣出的CALL和賣出的PUT,如果程式可以分辦,那後面就問題不大,我爬文,也找不到同時存在的CALL和PUT的指令,所以卡關

灰熊 發文於   2025/05/10

補充:用監控期貨是無成交,但改為20700C,和20600P,只有成交CALL,但PUT則是無成交

虎科大許教授 發文於   2025/05/10

(1)你的策略賣出的時候都用目前觸發價減2當作委託價,這可能會造成單邊無法成交。

(2)避免重複下無效單,不能只用filled控制流程,還要加上position控制。

(3)basecallprice、baseputprice、count等變數都要用intrabarpersist宣告。

(4)雙賣時,記錄當下庫存成本,兩者會都是0,因為下單,不會馬上成交,你只能記錄當下的即時價格。且兩者要分開記錄(下買權時記錄買權價格,下賣權時記錄賣權價格)。若要記錄庫存,一定要等到filled<0。

灰熊 發文於   2025/05/11

 教授您好,現跑出來只有鎖單,但卻沒有執行回檔的解單(新賣出單),不知是那裏有問題,以及第一筆只成交單邊的CALL,另一邊的PUT沒成交,麻請幫忙看看,不吝指教,謝謝

//只需輸入目前期貨的價位,程式自動下單作勒式雙賣

input:futureprice(20650),space(20);

var:_putout(0),_callout(0),newfutureprice(0);

variable: intrabarpersist basecallprice(0),baseputprice(0);//雙賣的成交價

variable: intrabarpersist count(0);//計算被停損次數要小於9

var:strcall(" "),strput(" "),strcalls(" "),strputs(" ");

 

if Date > date[1] then count = 0; // 開盤那根要歸0次數

 

//計算價平履約價,

value1=futureprice-(floor(futureprice/100)*100);//無條件捨去法來計算進位後的整數值會變小

if (value1<25) then begin

   newfutureprice=floor(futureprice/100)*100;//差值小於25,則價平為取無條件捨去法

 end else begin

   if (value1>75) then begin

   newfutureprice=ceiling(futureprice/100)*100;//差值大於75,價平為取無條件進位法

 end else begin

   newfutureprice=ceiling(futureprice/100)*100-50;//餘數算出來小於75且大於25則為無條件進位法-50點,因週選擇權為50點一跌,也可無條件捨去法+50點 

end; //2個 if....then begin.....end;

end;  

   

_callout=newfutureprice+50;//取價平價外一檔的CALL

_putout=newfutureprice-50;//取價平價外一檔的PUT

strcall=numtostr(_callout,0);//小數點0即取整數,將買權履約價轉為字串

strput=numtostr(_putout,0);//小數點0即取整數,將賣權履約價轉為字串

 

strcalls="TX2N05C"+strcall+".TF";//字串組合成XQ的履約價ID,CALL

strputs ="TX2N05P"+strPUT+".TF";//字串組合成XQ的履約價ID,PUT

 

//value1=getsymbolfield(strcalls,"收盤價","s");//卡關,getsymbolfield("ID","欄位名稱,"秒"),ID 不能用變數

//value2=getsymbolfield(strputs,"收盤價","s");

//9:10取作勒式雙賣價外一檔

if time=091000 then begin

    if symbol=strcalls then setposition(-1,market);  //賣Call         

    if symbol=strputs then setposition(-1,market); //賣PUT

   if position=1 and filled<0 then begin

basecallprice=filledavgprice;//取雙賣CALL的成交價為基準值

baseputprice=filledavgprice;//取雙賣PUT的成交價為基準值

   end;

end;    

if time>091000 and time<134500 then begin

//風險條件成立,要鎖單  

if symbol=strcalls and filled<0 then //表示有成交雙賣

      if c-filledAvgPrice>space then setposition(0);//己達條件,鎖單邊CALL的風險

if symbol=strputs and filled<0 then//表示有成交雙賣

      if c-filledAvgPrice>space then setposition(0);//風險條件有成立,鎖PUT單邊的風險

  

//風險條件解除,故要重新操作,因12:30後己無時間價值可扣,故就不再作賣出動作,己鎖單就當平倉,監控另一邊即可 

   //if time>091000 and time<123000 then begin

   if symbol=strcalls and filled=0 then begin //9:10後庫存為0,表示鎖單邊的CALL,故若再壓回20點回到原雙賣價位,則要再賣回

      if c=basecallprice then setposition(-1,market);

  count=count+1;

   end;   

  //鎖單邊CALL的風險條件己解除,再重新作

   if symbol=strputs and filled=0 then begin //9:10後庫存為0,表示鎖單邊的CALL,故若再壓回20點回到原雙賣價位,則要再賣回

      if c=baseputprice then setposition(-1,market);//鎖單邊CALL的風險條件己解除,再重新作 

      count=count+1;

   //end;

end;   

//停損9次或13:35分,則全部平倉

if (time >= 133500) or (count=9) then begin

    if symbol=strcalls and filled<>0 then setposition(0); //平倉call

end else 

    if symbol=strputs and filled<>0 then setposition(0);//平倉PUT

end;

 

灰熊 發文於   2025/05/11

補充,即後半段都沒執行到,還是條件不符合呢,那個basecallprice,baseputprice是否有問題呢

 if symbol=strcalls and filled=0 then begin //9:10後庫存為0,表示鎖單邊的CALL,故若再壓回20點回到原雙賣價位,則要再賣回

      if c=basecallprice then setposition(-1,market);

  count=count+1;

   end;   

  //鎖單邊CALL的風險條件己解除,再重新作

   if symbol=strputs and filled=0 then begin //9:10後庫存為0,表示鎖單邊的CALL,故若再壓回20點回到原雙賣價位,則要再賣回

      if c=baseputprice then setposition(-1,market);//鎖單邊CALL的風險條件己解除,再重新作 

      count=count+1;

   //end;

end;   

//停損9次或13:35分,則全部平倉

if (time >= 133500) or (count=9) then begin

    if symbol=strcalls and filled<>0 then setposition(0); //平倉call

end else 

    if symbol=strputs and filled<>0 then setposition(0);//平倉PUT

end;

灰熊 發文於   2025/05/11

更改如下,但新的賣出價和鎖單價卻是一樣,很怪,

 

//只需輸入目前期貨的價位,程式自動下單作勒式雙賣

input:futureprice(20650),space(20);

var:_putout(0),_callout(0),newfutureprice(0);

variable:  basecallprice(0),baseputprice(0);//雙賣的成交價

variable: intrabarpersist count(0);//計算被停損次數要小於9

var:strcall(" "),strput(" "),strcalls(" "),strputs(" ");

 

if Date > date[1] then count = 0; // 開盤那根要歸0次數

 

//計算價平履約價,

value1=futureprice-(floor(futureprice/100)*100);//無條件捨去法來計算進位後的整數值會變小

if (value1<25) then begin

   newfutureprice=floor(futureprice/100)*100;//差值小於25,則價平為取無條件捨去法

 end else begin

   if (value1>75) then begin

   newfutureprice=ceiling(futureprice/100)*100;//差值大於75,價平為取無條件進位法

 end else begin

   newfutureprice=ceiling(futureprice/100)*100-50;//餘數算出來小於75且大於25則為無條件進位法-50點,因週選擇權為50點一跌,也可無條件捨去法+50點 

end; //2個 if....then begin.....end;

end;  

   

_callout=newfutureprice+50;//取價平價外一檔的CALL

_putout=newfutureprice-50;//取價平價外一檔的PUT

strcall=numtostr(_callout,0);//小數點0即取整數,將買權履約價轉為字串

strput=numtostr(_putout,0);//小數點0即取整數,將賣權履約價轉為字串

 

strcalls="TX2N05C"+strcall+".TF";//字串組合成XQ的履約價ID,CALL

strputs ="TX2N05P"+strPUT+".TF";//字串組合成XQ的履約價ID,PUT

 

//value1=getsymbolfield(strcalls,"收盤價","s");//卡關,getsymbolfield("ID","欄位名稱,"秒"),ID 不能用變數

//value2=getsymbolfield(strputs,"收盤價","s");

//9:10取作勒式雙賣價外一檔

if time=091000 then begin

    if symbol=strcalls then setposition(-1,market);  //賣Call         

    if symbol=strputs then setposition(-1,market); //賣PUT

   if filled<0 then begin

basecallprice=filledavgprice;//取雙賣CALL的成交價為基準值

baseputprice=filledavgprice;//取雙賣PUT的成交價為基準值

   end;

end;    

if time>091000 and time<134500 then begin

//風險條件成立,要鎖單  

if symbol=strcalls and filled<0 then //表示有成交雙賣

      if c-filledAvgPrice>=space then setposition(0);//己達條件,鎖單邊CALL的風險

if symbol=strputs and filled<0 then//表示有成交雙賣

      if c-filledAvgPrice>=space then setposition(0);//風險條件有成立,鎖PUT單邊的風險

  

//風險條件解除,故要重新操作,因12:30後己無時間價值可扣,故就不再作賣出動作,己鎖單就當平倉,監控另一邊即可 

  if time>091000 and time<123000 then begin

   if symbol=strcalls and filled=0 then begin //9:10後庫存為0,表示鎖單邊的CALL,故若再壓回20點回到原雙賣價位,則要再賣回

      if c=basecallprice then setposition(-1,market);

  count=count+1;

   end;   

  //鎖單邊CALL的風險條件己解除,再重新作

   if symbol=strputs and filled=0 then begin //9:10後庫存為0,表示鎖單邊的CALL,故若再壓回20點回到原雙賣價位,則要再賣回

      if c=baseputprice then setposition(-1,market);//鎖單邊CALL的風險條件己解除,再重新作 

      count=count+1;

  end;

end;   

//停損9次或13:35分,則全部平倉

if (time >= 133500) or (count=9) then begin

    if symbol=strcalls and filled<>0 then setposition(0); //平倉call

end else 

    if symbol=strputs and filled<>0 then setposition(0);//平倉PUT

end;

 

虎科大許教授 發文於   2025/05/11

input:futureprice(20650),space(20);
var:_putout(0),_callout(0),newfutureprice(0);
var:intraBarPersist basecallprice(0),intraBarPersist baseputprice(0);//雙賣的成交價
var: intraBarPersist count(0);//計算被停損次數要小於9
var:strcall(""),strput(""),strcalls(""),strputs("");
if isFirstCall("Date") then 
    begin
        count=0;
        basecallprice=0;
        baseputprice=0;
    end;
//計算價平履約價,
value1=futureprice-(floor(futureprice/100)*100);//無條件捨去法來計算進位後的整數值會變小
if (value1<25) then 
    begin
        newfutureprice=floor(futureprice/100)*100;//差值小於25,則價平為取無條件捨去法
    end 
else 
    begin
        if (value1>75) then 
            begin
                newfutureprice=ceiling(futureprice/100)*100;//差值大於75,價平為取無條件進位法
            end 
        else 
            begin
                newfutureprice=ceiling(futureprice/100)*100-50;//餘數算出來小於75且大於25則為無條件進位法-50點,因週選擇權為50點一跌,也可無條件捨去法+50點 
            end; //2個 if....then begin.....end;
    end;  
_callout=newfutureprice+50;//取價平價外一檔的CALL
_putout=newfutureprice-50;//取價平價外一檔的PUT
strcall=numtostr(_callout,0);//小數點0即取整數,將買權履約價轉為字串
strput=numtostr(_putout,0);//小數點0即取整數,將賣權履約價轉為字串
strcalls="TX2N05C"+strcall+".TF";//字串組合成XQ的履約價ID,CALL
strputs ="TX2N05P"+strPUT+".TF";//字串組合成XQ的履約價ID,PUT
//9:10取作勒式雙賣價外一檔
if time=091000 then 
    begin
        if symbol=strcalls and position=0 then setposition(-1,market);  //賣Call     
        if symbol=strputs and position=0 then setposition(-1,market); //賣PUT        
    end;
if time>091000 and time<134500 then 
    begin
        //風險條件成立,要鎖單  
        if symbol=strcalls and position<0 and filled<0 then //表示有成交雙賣
            begin
                basecallprice=filledAvgPrice;
                if c-filledAvgPrice>space then setposition(0,market);//己達條件,鎖單邊CALL的風險
            end;
        if symbol=strputs and position<0 and filled<0 then//表示有成交雙賣
            begin
                baseputprice=filledAvgPrice;
                if c-filledAvgPrice>space then setposition(0,market);//風險條件有成立,鎖PUT單邊的風險
            end;
    end;
//風險條件解除,故要重新操作,因12:30後己無時間價值可扣,故就不再作賣出動作,己鎖單就當平倉,監控另一邊即可   
if time>091000 and time<123000 then 
    begin
        if symbol=strcalls and position=0 and filled=0 then 
            begin //9:10後庫存為0,表示鎖單邊的CALL,故若再壓回20點回到原雙賣價位,則要再賣回
                if c=basecallprice then 
                    begin
                        setposition(-1,market);
                        count=count+1;
                    end;
            end;   
        //鎖單邊CALL的風險條件己解除,再重新作
        if symbol=strputs and position=0 and filled=0 then 
            begin //9:10後庫存為0,表示鎖單邊的CALL,故若再壓回20點回到原雙賣價位,則要再賣回
                if c=baseputprice then 
                begin
                    setposition(-1,market);//鎖單邊CALL的風險條件己解除,再重新作 
                    count=count+1;
                end;
            end;  
    end;
//停損9次或13:35分,則全部平倉
if (time >= 133500) or (count=9) then 
    begin
        if symbol=strcalls and position<>0 and filled<>0 then setposition(0,market); //平倉call
        if symbol=strputs and position<>0 and filled<>0 then setposition(0,market);//平倉PUT
    end;

灰熊 發文於   2025/05/11

謝謝教授,感謝

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

雖然你用相同的變數count,嘗試想累加PUT及CALL的出場次數,但這樣是行不通的,因為兩個商品的變數是各自獨立的,彼此都無法得知對方的變數數值。這會造成你期待兩者出場合計9次就停止策略的目的無法達成。

灰熊 發文於   2025/05/12

許教授您好:早上丟進去跑,時間到,但沒送單,是不是我的時間有錯呢?,成交變數是否要各自累加,用countc,countp,countt=countc+countp,,用countt替代原先的count,是否可行,謝謝

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

(1)我這邊測試是OK的,21050CALL與20950PUT,有正常進場,CALL也停損,在價格回到當初進場價,又再次進場賣出。你的程式參數應修改,不然太價外的PUT,可能流動性差,造成沒有進場訊號。

(2)用countt替代原先的count,不可行。這是跨域操作,目前XQ並不支援。

灰熊 發文於   2025/05/12

我忘了說,我把期貨價位改成電腦自動去抓,不知是否這行寫錯呢,麻煩教授再幫我看看,目標是最後是電腦自動執行,計數器也改成countc(CALL次數),countp(PUT次數),countt(總次數),這樣是否可行,謝謝

 

input:space(20);

var:_putout(0),_callout(0),futureprice(0),newfutureprice(0);

var:intraBarPersist basecallprice(0),intraBarPersist baseputprice(0);//雙賣的成交價

var: intraBarPersist countc(0),intraBarPersist countp(0);//計算被停損次數要小於9

var:countt(0);

var:strcall(""),strput(""),strcalls(""),strputs("");

if isFirstCall("Date") then 

    begin

        countc=0;

countp=0;

countt=0;

        basecallprice=0;

        baseputprice=0;

    end;

//計算價平履約價,

futureprice = GetSymbolField("FITXN*1.TF","收盤價");

value1=futureprice-(floor(futureprice/100)*100);//無條件捨去法來計算進位後的整數值會變小

if (value1<25) then 

    begin

        newfutureprice=floor(futureprice/100)*100;//差值小於25,則價平為取無條件捨去法

    end 

else 

    begin

        if (value1>75) then 

            begin

                newfutureprice=ceiling(futureprice/100)*100;//差值大於75,價平為取無條件進位法

            end 

        else 

            begin

                newfutureprice=ceiling(futureprice/100)*100-50;//餘數算出來小於75且大於25則為無條件進位法-50點,因週選擇權為50點一跌,也可無條件捨去法+50點 

            end; //2個 if....then begin.....end;

    end;  

_callout=newfutureprice+50;//取價平價外一檔的CALL

_putout=newfutureprice-50;//取價平價外一檔的PUT

strcall=numtostr(_callout,0);//小數點0即取整數,將買權履約價轉為字串

strput=numtostr(_putout,0);//小數點0即取整數,將賣權履約價轉為字串

strcalls="TX2N05C"+strcall+".TF";//字串組合成XQ的履約價ID,CALL

strputs ="TX2N05P"+strPUT+".TF";//字串組合成XQ的履約價ID,PUT

//9:10取作勒式雙賣價外一檔

if time=091000 then 

    begin

        if symbol=strcalls and position=0 then setposition(-1,market);  //賣Call     

        if symbol=strputs and position=0 then setposition(-1,market); //賣PUT        

    end;

if time>091000 and time<134500 then 

    begin

        //風險條件成立,要鎖單  

        if symbol=strcalls and position<0 and filled<0 then //表示有成交雙賣

            begin

                basecallprice=filledAvgPrice;

                if c-filledAvgPrice>space then setposition(0,market);//己達條件,鎖單邊CALL的風險

            end;

        if symbol=strputs and position<0 and filled<0 then//表示有成交雙賣

            begin

                baseputprice=filledAvgPrice;

                if c-filledAvgPrice>space then setposition(0,market);//風險條件有成立,鎖PUT單邊的風險

            end;

    end;

//風險條件解除,故要重新操作,因12:30後己無時間價值可扣,故就不再作賣出動作,己鎖單就當平倉,監控另一邊即可   

if time>091000 and time<123000 then 

    begin

countt=countc+countp;

        if symbol=strcalls and position=0 and filled=0 then 

            begin //9:10後庫存為0,表示鎖單邊的CALL,故若再壓回20點回到原雙賣價位,則要再賣回

                if c=basecallprice then 

                    begin

                        setposition(-1,market);

                        countc=countc+1;

                    end;

            end;   

        //鎖單邊CALL的風險條件己解除,再重新作

        if symbol=strputs and position=0 and filled=0 then 

            begin //9:10後庫存為0,表示鎖單邊的CALL,故若再壓回20點回到原雙賣價位,則要再賣回

                if c=baseputprice then 

                begin

                    setposition(-1,market);//鎖單邊CALL的風險條件己解除,再重新作 

                    countp=countp+1;

                end;

            end;  

end;

//停損9次或13:35分,則全部平倉

if (time >= 133500) or (countt=9) then 

    begin

        if symbol=strcalls and position<>0 and filled<>0 then setposition(0,market); //平倉call

        if symbol=strputs and position<>0 and filled<>0 then setposition(0,market);//平倉PUT

    end;

 

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

(1)將count拆成CALL及PUT是沒有意義的。

(2)處理價平時的期貨價格,應該在9點10分第一個Tick進來時計算。

灰熊 發文於   2025/05/12

許教授您好,期貨我加一個迴圈if time=091000 then begin .......end;其它沒變,本想附上程式,但一直出現bot access is not allow ,所以我用敍述的,這樣可行嗎,關於停損9次停程式,我實在才疏學淺,想不出解決辧法,請問教授還有其他方法可以解決嗎,謝謝

灰熊 發文於   2025/05/12

自動交易中心,在執行程式前的商品,CALL和PUT,都設多一點的履約價來,控,而不是只設目前的價外一檔的履約價,這有是否有差?早上的交易中心如下:

 

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

由於用變數動態地決定執行商品,因此必須確保最後計算出來的履約價,執行商品裡面是有它的。可多加幾檔商品(全部第二週選擇權都加入也可以),然後在不是執行商品時,停止該商品的監控。

灰熊 發文於   2025/05/12

好的,謝謝教授不厭其煩的教導,誠摯感謝

灰熊 發文於   2025/05/13

許教授您好,今天早上實際開程式,因個疑問,就是下圖紅圈都是有執行,但藍圈都是沒有執行,21450P,有點問題,因21550C,一直沒有來碰停損的空間,今天要測試故改為9:00進場,space改成10,讓他多碰幾次,想測停損9次,但只跑了一次就打住了,我看程式沒問題,不知那出錯,麻煩教授再幫忙看一下之前修改的程式,謝謝

藍色圈起來都部份都沒再執行了

虎科大許教授 發文於   2025/05/13

若你使用的是最新貼上來的腳本,將futureprice從參數改成變數,則會發生這種問題就很正常。不是說改成變數不行,而是改成變數後,你不能一直讓該變數計算新的履約價,否則後來的履約價變成別的履約價,原本建立部位的履約價選擇權會因為strputs及strcalls不同而無法出場。建議,只有在9點10分的第一個Tick計算履約價,這樣的話,應該就可解決這個問題。

灰熊 發文於   2025/05/13

謝謝教授,我都一直圍在後面的庫存段在打轉,沒想到是自動算價平的問題,我再修改如下:

 

input:space(10);

var:_putout(0),_callout(0),futureprice(0),newfutureprice(0);

var:intraBarPersist basecallprice(0),intraBarPersist baseputprice(0);//雙賣的成交價

var: intraBarPersist count(0);//計算被停損次數要小於9

var:strcall(""),strput(""),strcalls(""),strputs("");

if isFirstCall("Date") then 

    begin

        count=0;

        basecallprice=0;

        baseputprice=0;

    end;

//計算價平履約價,

futureprice = GetSymbolField("FITXN*1.TF","收盤價");

value1=futureprice-(floor(futureprice/100)*100);//無條件捨去法來計算進位後的整數值會變小

if (value1<25) then 

    begin

        newfutureprice=floor(futureprice/100)*100;//差值小於25,則價平為取無條件捨去法

    end 

else 

    begin

        if (value1>75) then 

            begin

                newfutureprice=ceiling(futureprice/100)*100;//差值大於75,價平為取無條件進位法

            end 

        else 

            begin

                newfutureprice=ceiling(futureprice/100)*100-50;//餘數算出來小於75且大於25則為無條件進位法-50點,因週選擇權為50點一跌,也可無條件捨去法+50點 

            end; //2個 if....then begin.....end;

    end;  

_callout=newfutureprice+50;//取價平價外一檔的CALL

_putout=newfutureprice-50;//取價平價外一檔的PUT

strcall=numtostr(_callout,0);//小數點0即取整數,將買權履約價轉為字串

strput=numtostr(_putout,0);//小數點0即取整數,將賣權履約價轉為字串

strcalls="TX2N05C"+strcall+".TF";//字串組合成XQ的履約價ID,CALL

strputs ="TX2N05P"+strPUT+".TF";//字串組合成XQ的履約價ID,PUT

//9:10取作勒式雙賣價外一檔

if time=090000 then begin

        if symbol=strcalls and position=0 then setposition(-1,market);  //賣Call     

        if symbol=strputs and position=0 then setposition(-1,market); //賣PUT  

        if symbol=strcalls and position<0 and filled<0 then basecallprice=filledAvgPrice; //表示有成交雙賣,記錄CALL進場價當基準

        if symbol=strputs and position<0 and filled<0 then  baseputprice=filledAvgPrice;    //表示有成交雙賣,記錄PUT進場價當基準

            

    end;

if time>090000 and time<134500 then 

    begin

       //風險條件成立,要鎖單  

        if symbol=strcalls and position<0 and filled<0 then      //表示有成交雙賣

   begin

               if c-filledAvgPrice>space then setposition(0,market);//己達條件,鎖單邊CALL的風險

           end;

        if symbol=strputs and position<0 and filled<0 then//表示有成交雙賣

           begin

               if c-filledAvgPrice>space then setposition(0,market);//風險條件有成立,鎖PUT單邊的風險

           end;

    end;

//風險條件解除,故要重新操作,因12:30後己無時間價值可扣,故就不再作賣出動作,己鎖單就當平倉,監控另一邊即可   

if time>091000 and time<134500 then 

    begin

        if symbol=strcalls and position=0 and filled=0 then 

            begin                                  //9:10後庫存為0,表示鎖單邊的CALL,故若再壓回20點回到原雙賣價位,則要再賣回

                if c=basecallprice then 

                    begin

                        setposition(-1,market);

                        count=count+1;

                    end;

            end;   

        //鎖單邊CALL的風險條件己解除,再重新作

        if symbol=strputs and position=0 and filled=0 then 

            begin //9:10後庫存為0,表示鎖單邊的CALL,故若再壓回20點回到原雙賣價位,則要再賣回

                if c=baseputprice then 

                begin

                    setposition(-1,market);//鎖單邊CALL的風險條件己解除,再重新作 

                    count=count+1;

                end;

            end;  

    end;

//停損9次或13:35分,則全部平倉

if (time >= 133500) or (count=9) then 

    begin

        if symbol=strcalls and position<>0 and filled<>0 then setposition(0,market); //平倉call

        if symbol=strputs and position<>0 and filled<>0 then setposition(0,market);//平倉PUT

    end;

 

虎科大許教授 發文於   2025/05/13

(1)你這樣一樣是任何時候都在計算履約價。

(2)count應該是在平倉時計算,而非進場時計算。

(3)count等於9,代表CALL平倉9次,PUT平倉9次才結束交易。按照你原本的想法,應該是4次比較合適吧?

灰熊 發文於   2025/05/13

許教授您好,我將time=090000提到最前面,將次數改成count=5,想不出來怎麼讓CALL和PUT平倉加總共停損9次,只好取一半來作,這是用作再次下原始位置的次數,再麻煩許教授批改一下,謝謝

 

input:space(10);

var:_putout(0),_callout(0),futureprice(0),newfutureprice(0);

var:intraBarPersist basecallprice(0),intraBarPersist baseputprice(0);//雙賣的成交價

var: intraBarPersist count(0);//計算被停損次數要小於9

var:strcall(""),strput(""),strcalls(""),strputs("");

if isFirstCall("Date") then 

    begin

        count=0;

        basecallprice=0;

        baseputprice=0;

    end;

//計算價平履約價,

if time=090000 then begin

futureprice = GetSymbolField("FITXN*1.TF","收盤價");

value1=futureprice-(floor(futureprice/100)*100);//無條件捨去法來計算進位後的整數值會變小

if (value1<25) then 

    begin

        newfutureprice=floor(futureprice/100)*100;//差值小於25,則價平為取無條件捨去法

    end 

else 

    begin

        if (value1>75) then 

            begin

                newfutureprice=ceiling(futureprice/100)*100;//差值大於75,價平為取無條件進位法

            end 

        else 

            begin

                newfutureprice=ceiling(futureprice/100)*100-50;//餘數算出來小於75且大於25則為無條件進位法-50點,因週選擇權為50點一跌,也可無條件捨去法+50點 

            end; //2個 if....then begin.....end;

    end;  

_callout=newfutureprice+50;//取價平價外一檔的CALL

_putout=newfutureprice-50;//取價平價外一檔的PUT

strcall=numtostr(_callout,0);//小數點0即取整數,將買權履約價轉為字串

strput=numtostr(_putout,0);//小數點0即取整數,將賣權履約價轉為字串

strcalls="TX2N05C"+strcall+".TF";//字串組合成XQ的履約價ID,CALL

strputs ="TX2N05P"+strPUT+".TF";//字串組合成XQ的履約價ID,PUT

//9:10取作勒式雙賣價外一檔

 

        if symbol=strcalls and position=0 then setposition(-1,market);  //賣Call     

        if symbol=strputs and position=0 then setposition(-1,market); //賣PUT  

        if symbol=strcalls and position<0 and filled<0 then basecallprice=filledAvgPrice; //表示有成交雙賣,記錄CALL進場價當基準

        if symbol=strputs and position<0 and filled<0 then  baseputprice=filledAvgPrice;    //表示有成交雙賣,記錄PUT進場價當基準

            

end;

if time>090000 and time<134500 then 

    begin

       //風險條件成立,要鎖單  

        if symbol=strcalls and position<0 and filled<0 then      //表示有成交雙賣

   begin

               if c-filledAvgPrice>space then setposition(0,market);//己達條件,鎖單邊CALL的風險

           end;

        if symbol=strputs and position<0 and filled<0 then//表示有成交雙賣

           begin

               if c-filledAvgPrice>space then setposition(0,market);//風險條件有成立,鎖PUT單邊的風險

           end;

    end;

//風險條件解除,故要重新操作,因12:30後己無時間價值可扣,故就不再作賣出動作,己鎖單就當平倉,監控另一邊即可   

if time>091000 and time<134500 then 

    begin

        if symbol=strcalls and position=0 and filled=0 then 

            begin                                  //9:10後庫存為0,表示鎖單邊的CALL,故若再壓回20點回到原雙賣價位,則要再賣回

                if c=basecallprice then 

                    begin

                        setposition(-1,market);

                        count=count+1;

                    end;

            end;   

        //鎖單邊CALL的風險條件己解除,再重新作

        if symbol=strputs and position=0 and filled=0 then 

            begin //9:10後庫存為0,表示鎖單邊的CALL,故若再壓回20點回到原雙賣價位,則要再賣回

                if c=baseputprice then 

                begin

                    setposition(-1,market);//鎖單邊CALL的風險條件己解除,再重新作 

                    count=count+1;

                end;

            end;  

    end;

//停損9次或13:35分,則全部平倉

if (time >= 133500) or (count=5) then 

    begin

        if symbol=strcalls and position<>0 and filled<>0 then setposition(0,market); //平倉call

        if symbol=strputs and position<>0 and filled<>0 then setposition(0,market);//平倉PUT

    end;

.

虎科大許教授 發文於   2025/05/13

這樣就好多了。

灰熊 發文於   2025/05/13

真心謝謝許教授花這麼多的時間幫我修改,也讓我學到了很多,誠心感謝,謝謝許教授

虎科大許教授 發文於   2025/05/14

不過,你在9點的這根5分K,雖然不會重複下單,但會一直設定履約價。若這5分鐘內波動太大,5分K結束時計算的履約價可能與第一個Tick計算的不同,這樣的話,會有相同的問題。應該宣告一個變數控制9點只會在第一個Tick進到If裡面。另外,兩個履約價變數也應該用intrabarpersist宣告。

var: intrabarpersist hasSetUp(false);

if isFirstCall("Date") then 

begin

        count=0;

basecallprice=0;

baseputprice=0;

hasSetUp=false;

end;

if time=090000 and hasSetUp=false then 

begin

//設定履約價的程式碼

hasSetUp=true;

end;

 

灰熊 發文於   2025/05/14

許教授您好,太佩服您了,今早就出現教授說的波動大,結果我就同時間成交了2組勒式,4個履約價,還在想說9點這個時間點,怎麼會成交2組呢,還在想不明白,剛好看到教授的這篇指導,太感謝了,程式修改如下,再麻煩教授看一下,謝謝

 

input:space(10);

var:_putout(0),_callout(0),futureprice(0),newfutureprice(0);

var:intraBarPersist basecallprice(0),intraBarPersist baseputprice(0);//雙賣的成交價

var: intraBarPersist count(0);//計算被停損次數要小於9

var:strcall(""),strput(""),strcalls(""),strputs("");

if isFirstCall("Date") then 

    begin

        count=0;

        basecallprice=0;

        baseputprice=0;

    end;

//計算價平履約價,

if time=090000 then begin

futureprice = GetSymbolField("FITXN*1.TF","收盤價");

value1=futureprice-(floor(futureprice/100)*100);//無條件捨去法來計算進位後的整數值會變小

if (value1<25) then 

    begin

        newfutureprice=floor(futureprice/100)*100;//差值小於25,則價平為取無條件捨去法

    end 

else 

    begin

        if (value1>75) then 

            begin

                newfutureprice=ceiling(futureprice/100)*100;//差值大於75,價平為取無條件進位法

            end 

        else 

            begin

                newfutureprice=ceiling(futureprice/100)*100-50;//餘數算出來小於75且大於25則為無條件進位法-50點,因週選擇權為50點一跌,也可無條件捨去法+50點 

            end; //2個 if....then begin.....end;

    end;  

_callout=newfutureprice+50;//取價平價外一檔的CALL

_putout=newfutureprice-50;//取價平價外一檔的PUT

strcall=numtostr(_callout,0);//小數點0即取整數,將買權履約價轉為字串

strput=numtostr(_putout,0);//小數點0即取整數,將賣權履約價轉為字串

strcalls="TX2N05C"+strcall+".TF";//字串組合成XQ的履約價ID,CALL

strputs ="TX2N05P"+strPUT+".TF";//字串組合成XQ的履約價ID,PUT

//9:10取作勒式雙賣價外一檔

 

        if symbol=strcalls and position=0 then setposition(-1,market);  //賣Call     

        if symbol=strputs and position=0 then setposition(-1,market); //賣PUT  

        if symbol=strcalls and position<0 and filled<0 then basecallprice=filledAvgPrice; //表示有成交雙賣,記錄CALL進場價當基準

        if symbol=strputs and position<0 and filled<0 then  baseputprice=filledAvgPrice;    //表示有成交雙賣,記錄PUT進場價當基準

            

end;

if time>090000 and time<134500 then 

    begin

       //風險條件成立,要鎖單  

        if symbol=strcalls and position<0 and filled<0 then      //表示有成交雙賣

   begin

               if c-filledAvgPrice>space then setposition(0,market);//己達條件,鎖單邊CALL的風險

           end;

        if symbol=strputs and position<0 and filled<0 then//表示有成交雙賣

           begin

               if c-filledAvgPrice>space then setposition(0,market);//風險條件有成立,鎖PUT單邊的風險

           end;

    end;

//風險條件解除,故要重新操作,因12:30後己無時間價值可扣,故就不再作賣出動作,己鎖單就當平倉,監控另一邊即可   

if time>091000 and time<134500 then 

    begin

        if symbol=strcalls and position=0 and filled=0 then 

            begin                                  //9:10後庫存為0,表示鎖單邊的CALL,故若再壓回20點回到原雙賣價位,則要再賣回

                if c=basecallprice then 

                    begin

                        setposition(-1,market);

                        count=count+1;

                    end;

            end;   

        //鎖單邊CALL的風險條件己解除,再重新作

        if symbol=strputs and position=0 and filled=0 then 

            begin //9:10後庫存為0,表示鎖單邊的CALL,故若再壓回20點回到原雙賣價位,則要再賣回

                if c=baseputprice then 

                begin

                    setposition(-1,market);//鎖單邊CALL的風險條件己解除,再重新作 

                    count=count+1;

                end;

            end;  

    end;

//停損9次或13:35分,則全部平倉

if (time >= 133500) or (count=5) then 

    begin

        if symbol=strcalls and position<>0 and filled<>0 then setposition(0,market); //平倉call

        if symbol=strputs and position<>0 and filled<>0 then setposition(0,market);//平倉PUT

    end;

 

虎科大許教授 發文於   2025/05/14

你並沒有依照我的建議,宣告一個變數來控制9點的時候,只有第一個Tick會進到IF裡面設定履約價。

灰熊 發文於   2025/05/14

許教授,不好意思,因為我不會寫啊,不知要如何寫,SORRY,這樣怎麼抓第一個TICK呢?我跑1分鐘的,

虎科大許教授 發文於   2025/05/14

前面不是提供範例了嗎?

var: intrabarpersist hasSetUp(false);

if isFirstCall("Date") then 

begin

        count=0;

basecallprice=0;

baseputprice=0;

hasSetUp=false;

end;

if time=090000 and hasSetUp=false then 

begin

//這裡寫設定履約價的程式碼

hasSetUp=true;

end;

灰熊 發文於   2025/05/14

許教授您好,我去XQ,對isfirstcall('date")認知不太懂,說明是說當日第一次洗價,但若我的程式在9:00才計算履約價,則開盤到9:00,不是會洗了很多次嗎,真的不懂,有稍微加進教授的寶貴意見,也不知對不對,還煩請教授幫忙看看,不吝賜教,謝謝

灰熊 發文於   2025/05/14

 

input:space(10);

var:_putout(0),_callout(0),futureprice(0),newfutureprice(0);

var:intraBarPersist basecallprice(0),intraBarPersist baseputprice(0);//雙賣的成交價

var: intraBarPersist count(0);//計算被停損次數要小於9

var:strcall(""),strput(""),strcalls(""),strputs("");

var: intrabarpersist hasSetUp(false);

 

if isFirstCall("Date") then 

 

begin

        count=0;

basecallprice=0;

baseputprice=0;

hasSetUp=false;

end;

 

if time=090000 and hasSetUp=false then 

begin

//計算價平履約價,

futureprice = GetSymbolField("FITXN*1.TF","收盤價");

value1=futureprice-(floor(futureprice/100)*100);//無條件捨去法來計算進位後的整數值會變小

if (value1<25) then 

    begin

        newfutureprice=floor(futureprice/100)*100;//差值小於25,則價平為取無條件捨去法

    end 

else 

    begin

        if (value1>75) then 

            begin

                newfutureprice=ceiling(futureprice/100)*100;//差值大於75,價平為取無條件進位法

            end 

        else 

            begin

                newfutureprice=ceiling(futureprice/100)*100-50;//餘數算出來小於75且大於25則為無條件進位法-50點,因週選擇權為50點一跌,也可無條件捨去法+50點 

            end; //2個 if....then begin.....end;

    end;  

_callout=newfutureprice+50;//取價平價外一檔的CALL

_putout=newfutureprice-50;//取價平價外一檔的PUT

strcall=numtostr(_callout,0);//小數點0即取整數,將買權履約價轉為字串

strput=numtostr(_putout,0);//小數點0即取整數,將賣權履約價轉為字串

strcalls="TX2N05C"+strcall+".TF";//字串組合成XQ的履約價ID,CALL

strputs ="TX2N05P"+strPUT+".TF";//字串組合成XQ的履約價ID,PUT

//9:10取作勒式雙賣價外一檔

 

        if symbol=strcalls and position=0 then setposition(-1,market);  //賣Call     

        if symbol=strputs and position=0 then setposition(-1,market); //賣PUT  

        if symbol=strcalls and position<0 and filled<0 then basecallprice=filledAvgPrice; //表示有成交雙賣,記錄CALL進場價當基準

        if symbol=strputs and position<0 and filled<0 then  baseputprice=filledAvgPrice;    //表示有成交雙賣,記錄PUT進場價當基準

        hasSetUp=true;

            

end;

if time>090000 and time<134500 then 

    begin

       //風險條件成立,要鎖單  

        if symbol=strcalls and position<0 and filled<0 then      //表示有成交雙賣

   begin

               if c-filledAvgPrice>space then setposition(0,market);//己達條件,鎖單邊CALL的風險

           end;

        if symbol=strputs and position<0 and filled<0 then//表示有成交雙賣

           begin

               if c-filledAvgPrice>space then setposition(0,market);//風險條件有成立,鎖PUT單邊的風險

           end;

    end;

//風險條件解除,故要重新操作,因12:30後己無時間價值可扣,故就不再作賣出動作,己鎖單就當平倉,監控另一邊即可   

if time>091000 and time<134500 then 

    begin

        if symbol=strcalls and position=0 and filled=0 then 

            begin                                  //9:10後庫存為0,表示鎖單邊的CALL,故若再壓回20點回到原雙賣價位,則要再賣回

                if c=basecallprice then 

                    begin

                        setposition(-1,market);

                        count=count+1;

                    end;

            end;   

        //鎖單邊CALL的風險條件己解除,再重新作

        if symbol=strputs and position=0 and filled=0 then 

            begin //9:10後庫存為0,表示鎖單邊的CALL,故若再壓回20點回到原雙賣價位,則要再賣回

                if c=baseputprice then 

                begin

                    setposition(-1,market);//鎖單邊CALL的風險條件己解除,再重新作 

                    count=count+1;

                end;

            end;  

    end;

//停損9次或13:35分,則全部平倉

if (time >= 133500) or (count=5) then 

    begin

        if symbol=strcalls and position<>0 and filled<>0 then setposition(0,market); //平倉call

        if symbol=strputs and position<>0 and filled<>0 then setposition(0,market);//平倉PUT

    end;

 

虎科大許教授 發文於   2025/05/14

這樣應該就OK了。

灰熊 發文於   2025/05/15

謝謝教授的幫忙和指導,再三感謝

發表回覆
Close