1513  

 

這是一個簡單的移動停損停停利的語法範例,常常有客戶問到該如何寫,這邊也給自己記錄。

當有多單部位的時候 停損守在最近3根K棒的低點減去一定的幅度;當有空單部位的時候,停損守在最近3根K棒高點加上一定的幅度;當獲利創新高,停損的點位就往上或往下來相對移動,這是一般程式交易者常用的移動停損停利方式,用程式碼說明

 input:N(10);  設定參數

 var:longstop(0),barH(0); 自訂變數

多單的部位在倉的時候設定最低點在什麼位置

 if barssinceentry=0 then begin  

 longstop=minlist(low,low[1],low[2])-N;

 barH=high;

 end;

當獲利創新高 低點相對往上提高

 if high>barH then barH=high;

 if barssinceentry>0 then begin

 if c>barH[1] then longstop=minlist(low,low[1])-N;

 end;

當多單在倉的時候停損守在某個位置

 if marketposition=1 then sell next bar at longstop stop ;

 空單的停損設定在區間的高點加上一定比例

var:sellstop(0),barll(0);

 if barssinceentry=0 then begin

 sellstop=maxlist(high,high[1])+N;

 barL=low;

 end;

 當空單的獲利創新高,表示低點不斷創新低 則移動停損的位置

if low<barL then barll=low;

if barssinceentry>0 then begin

if c<barL[1] then sellstop=maxlist(high,high[1],high[2])+N

 end;

if marketposition=-1 then buytocover next bar at sellstop stop ;

呈現出來的移動停損停利大致上就是這樣

1512  

1513  

可以舉一反三:

1.假設獲利達到一定的區間之後再啟動移動停損的機制

2.或者是進場之後過了一段時間才啟動

3.當急拉或急殺獲利瞬間創新高,移動停損的位置也會相對調整更貼近K棒,保護獲利的來源

以上是個人想法,當做研究的參考記錄

 

 

 

arrow
arrow

    元大期貨楊宗儒 發表在 痞客邦 留言(0) 人氣()