XS語法 begin end的執行差異?

  •   221 
  • 最後發表   XQYi  2023 十二月 15
XQYi 發文於   2023/11/27

一種
//有庫存先賣再買

if position=1 and filled=1 and value3 < 0 and getField("均價") < close then  setposition(0,close);

if close <= filledAvgPrice *0.99 then setposition(1,close);

if close <= filledAvgPrice *0.995 then setposition(1,close);

 

//無庫存先放空賣再買

if position=0 and filled =0  and value3 < 0 and getField("均價") < close then setposition(-1,close);

if filled=-1 and close <= filledAvgPrice*0.99 then setposition(0,close);

if filled=-1 and close <= filledAvgPrice *0.995 then setposition(0,close);

 

以下有Begin end


第二種

//有庫存先賣再買

if position=1 and filled=1 and value3 < 0 and getField("均價") < close then 

begin setposition(0,close);

if close <= filledAvgPrice *0.99 then setposition(1,close);

if close <= filledAvgPrice *0.995 then setposition(1,close);

end

else

begin   //無庫存先放空賣再買

if position=0 and filled =0  and value3 < 0 and getField("均價") < close then setposition(-1,close);

if filled=-1 and close <= filledAvgPrice*0.99 then setposition(0,close);

if filled=-1 and close <= filledAvgPrice *0.995 then setposition(0,close);

end;

 

第三種

// 有庫存先賣再買

if position=1 and filled=1 and value3 < 0 and getField("均價") < close then 

begin 

  setposition(0,close);

  if close <= filledAvgPrice *0.99 then setposition(1,close);

  if close <= filledAvgPrice *0.995 then setposition(1,close);

end

else

begin   

  // 無庫存先放空賣再買

  if position=0 and filled =0  and value3 < 0 and getField("均價") < close then 

  begin 

    setposition(-1,close);

    if filled=-1 and close <= filledAvgPrice*0.99 then setposition(0,close);

    if filled=-1 and close <= filledAvgPrice *0.995 then setposition(0,close);

  end

end;

 

 

此三種,無包圍和有包圍,再包圍的執行結果差異有哪些?

排序方式: 標準 | 最新
XQ小幫手 發文於   2023/11/30

Hello xqyi,

 

您可以參考 XSHelp 上的說明,如果看不懂的話或許可以麻煩您說明一下那裡看不懂,小幫手請相關人員研究看要如何修改會比較容易理解。

 

簡單來說,被 begin / end 包住的地方是會受到前面的條件或指令影響,舉例:

 

if position=1 and filled=1 and value3 < 0 and getField("均價") < close then begin 

    setposition(0,close);

    if close <= filledAvgPrice *0.99 then setposition(1,close);

    if close <= filledAvgPrice *0.995 then setposition(1,close);

    end

 

上面這段要符合 position=1 and filled=1 and value3 < 0 and getField("均價") < close 這些條件,才會運算

setposition(0,close);

if close <= filledAvgPrice *0.99 then setposition(1,close);

if close <= filledAvgPrice *0.995 then setposition(1,close);

這些腳本。

 

if position=1 and filled=1 and value3 < 0 and getField("均價") < close then  setposition(0,close);

if close <= filledAvgPrice *0.99 then setposition(1,close);

if close <= filledAvgPrice *0.995 then setposition(1,close);

 

這樣寫的話只有 setposition(0,close) 會受到 position=1 and filled=1 and value3 < 0 and getField("均價") < close 條件的影響。

if close <= filledAvgPrice *0.99 then setposition(1,close);

if close <= filledAvgPrice *0.995 then setposition(1,close);

這些則是每次都會執行。

 

另外小幫手已經有提過,在庫存 (filled) 為0的情況下,filledavgprice (未平倉成本) 一樣會為0。

所以在平倉後庫存為0,close <= filledAvgPrice *0.99 和 close <= filledAvgPrice *0.995 是不會符合的 (收盤價不太可能小於等於0)。

XQYi 發文於   2023/11/30

你說明的第一種情況先前已說過,所以改了第二種和第三種,哪一種才是正確的交易方式寫法?

XQ小幫手 發文於   2023/12/04

Hello xqyi,

 

要視您的需求才能決定。

而您註解的需求小幫手要知道完整的狀況 (包含腳本內容、策略設定 和 實際庫存 等等) 才能夠給建議。

就以這邊狀況來說,小幫手認為不加 begin end 才是正確的。

因為一般來說 出場 / 進場 的條件是分開的,不需要兩邊都符合才能 進 / 出場。

 

XQYi 發文於   2023/12/04

此次依腳本策略下,無任何庫存、進場一次,回測結果日K、1K,逐筆洗價、觸發即判斷成交的六種組合狀況都與實際不符。如附圖實際交易。
其他因無法附掛檔案和多張圖片,無法提供這6組的資料!


//先買後賣 if position=0 and filled=0 and opend(0) > closeD(1) and close<>getField("漲停價", "D") then setposition(1,market,label:="買1");//上漲先買 if position=1 and filled=1 and (close >= filledAvgPrice*1.005 or high >= filledAvgPrice+0.5 ) then setposition (0,close,label:="賣1"); //前一日有庫存先賣再買 if position[1]=1 and filled[1]=1 and openD(0) < closeD(1) then begin setposition(0 ,market,label:="賣2");//下跌先賣 if position=1 and filled=1 and (close <= filledAvgPrice*0.995 or close <= filledAvgPrice-0.5) then setposition(1,close,label:="買2"); end else begin //無庫存先放空再回補 if position=0 and filled=0 and opend(0) < closed(1) then short(1 ,market,label:="賣3");//下跌先賣 if position=-1 and filled=-1 and (q_bestbid1 <= filledAvgPrice*0.995 or q_bestbid1 <= filledAvgPrice-0.5) then cover(1,q_bestbid1,label:="買3"); end;

 

附加文件

XQ小幫手 發文於   2023/12/15

Hello xqyi,

 

不同頻率回測出來的結果自然不一樣 (理論上來說,最接近的會是相同頻率與設定的回測),且回測無法完全模擬實際情況。

若您希望回測和實際執行盡量相近,那麼請使用分鐘頻率非逐筆洗價,這樣兩者的差距最低。

 

除非使用日頻率(強制逐筆洗價)且策略從昨日運行到現在,這樣 position[1]=1 and filled[1]=1 才會判斷前一日有部位庫存。

小幫手會建議先用其他策略出掉前一日的庫存,例如 

if filled <> 0 then setposition(0, market);

且設定為依庫存,出掉昨日庫存後再啟動當日要執行的策略。

發表回覆
Close