當沖交易買賣語法

  •   291 
  • 最後發表   XQYi  2024 二月 02
XQYi 發文於   2023/12/29

當沖交易買賣,希望依訊號進場後,再依停損停利而出場。避免依訊號而進出場(目前情況)

如何避開程式碼中的

1. =====>B買進 被 =======>S賣出  而賣出?

2.=======>S賣出 被 =====>B買進 而買進?

買進// 如果條件成立時

if  position=0 and filled=0  and condition91  /如果條件成立時

then begin 

if condition92  then setposition(position+1,close,label:="買");  //92條件也成立時買進======>B

if position<>0 and filled<>0 then //假如成交買進

value95=100 * (Close - FilledAvgPrice) / FilledAvgPrice;//停利停損% 

if filled=1  and value95 >=X1  then setposition(0,close,label:=">買進續漲%停利"); //獲利停利

if filled=1  and value95 <=-x2 then setposition(0,close,label:="<買進回跌%停損"); //虧損停損

end;

 

//賣出

if  position=1 and filled=1 and condition91 //如果有該股且條件符合

then begin 

if  condition93  then setposition(position-1,close,label:="賣"); //條件93也成立時賣出=====>S

if position=0 and filled=-1 then 

value95=100 * (Close - FilledAvgPrice) / FilledAvgPrice;//停利停損% 

if filled=-1 and value95 <=-X3 then setposition(0,close,label:="賣出續跌%停利"); 

if filled=-1 and value95 >=x4  then setposition(0,close,label:="賣出回漲%停損");

end;

XQ小幫手 發文於   2024/02/02

Hello, xqyi.

小幫手將您貼上的程式,簡單整理了一下,發現您的begin end;包含了很長一段,所以有些條件是不會觸發到的,

建議您可以用回測以及print(...),確認程式的Flow是否與您設計的一樣,

另外,無論是多、空方進場,都在postion = 0的時候進場,這樣就不會有多方進場的庫存被空方進場給賣掉的狀況發生,

建議您先閱覽網站上的教學區,裡面有XS語法的基礎和應用。

 

https://www.xq.com.tw/learn

謝謝。

 

if 
    position = 0 
    and filled = 0 
    and condition91  /如果條件成立時
then begin 
    if 
        condition92  
    then 
        setposition(position+1,close,label:="買");  //92條件也成立時買進======>B

    if 
        position <> 0 
        and filled <> 0 
    then //假如成交買進
        value95 = 100 * (Close - FilledAvgPrice) / FilledAvgPrice;//停利停損% 

    if 
        filled = 1 
        and value95 >= X1 
    then 
        setposition(0,close,label:=">買進續漲%停利"); //獲利停利

    if 
        filled = 1 
        and value95 <= -x2 
    then 
        setposition(0,close,label:="<買進回跌%停損"); //虧損停損
end;

//賣出
if 
    position = 1 
    and filled = 1 
    and condition91 //如果有該股且條件符合
then begin 
    if 
        condition93 
    then 
        setposition(position-1,close,label:="賣"); //條件93也成立時賣出=====>S

    if 
        position = 0 
        and filled = -1 
    then
        value95 = 100 * (Close - FilledAvgPrice) / FilledAvgPrice;//停利停損% 

    if 
        filled = -1 
        and value95 <= -X3 
    then 
        setposition(0,close,label:="賣出續跌%停利"); 
    if 
        filled = -1 
        and value95 >= x4 
    then 
        setposition(0,close,label:="賣出回漲%停損");
end;

發表回覆
Close