初階&進階的停損停利出場範例 / Multicharts 語法教學

會買是徒弟 會賣才是師傅,停損/停利 是交易策略中至關重要的一環,但是很難有絕對完美的停利方法,不同的策略需要選擇不同的停利邏輯,不同的商品也應該搭配的出場條件

以下是我整理從初階到進階常用到的程式交易出場範例

絕對金額/點數出場

1.百分比移動停利出場(% Trailing stop)

當獲利達到一定金額後,如果從最高點回落設定的百分比,就平倉出場
例如 當獲利超國100點 拉回30%停利

setpercenttrailing(100*bigpointvalue,30) 

2.固定點數移動停利出場 ($ Dollar Trailing)

進場後,未平倉獲利從最高點回落超過固定點數,就立刻出場 。
語法如下

setdollartrailing(20*bigpointvalue)

3.保本不輸出場

進場後 獲利達到一定點數(例如100點) 若回檔至進場的價格 立刻平倉出場 使用到setbreakeven的函數

setbreakeven(100*bigpointvalue);

4. 簡單移動平均線移動停利

這是很最簡單的移動停利方式,例如使用5日線進行移動停利,進場後只要跌破近N日的移動平均價,則立刻出場。範例如下

inputs: len(5);
vars:SMA(0);
SMA=averagefc(close,len);
if marketposition=1 and close cross under SMA then sell next bar market;
if marketposition=-1 and close cross over SMA then buytocover next bar market;

5.動態移動平均線停利

均線有個缺點,參數設太少太敏感容易短進短出被洗出場,設太大又反應太慢,沒有達到保護的效果;分享一個改良的移動平均出場。用均線當作出場的基本依據,但是隨K棒數增加,均線的參數也會隨著越來越小

Inputs: xMA(50);
Vars: Len(0);

If MarketPosition = 0 then Len = xMA; 
If MarketPosition = 1 then 
Len = Len - 1 else Len = MaxList(Len,1); 

If MarketPosition = 1 then sell next bar at Average(Close,Len) stop;

6. High Low線移動停利法 (HL average)

計算方式很簡單,使用過去一段時間的最高價及最低價加起來除以2 當作基準線 ,多單跌破該平均線出場,空單站上該平均線出場,程式碼範例如下:

value1=0.5*(Highest(high,Len)+Lowest(low,Len));
if marketposition=1 and Close>value1 then sell next bar  at value1 stop;
if marketposition=-1 and Close<value1 then buytocover next bar at value1 stop;

接下來是一些比較進階的移動停利法則

7. RSI循環移動停利:

利用RSI 進行循環停利法。當RSI>80的時候啟動停利法,之後第一次回跌到RSI<60再反彈 到80隨即停利出場,若RSI直接跌破40則立即出場;作空時當RSI<20時觸發移動停利機制,之後第 一次漲到RSI>40之後若再回跌到RSI<20則停利出場,若RSI直接漲超過60則停損出場。

vars: myRSI(0),LState(0), SState(0); 

myRSI = RSI(Close, Len);
If MarketPosition = 1 then begin

    if LState = 0 and myRSI > 80 then 
        LState = 1;  
    if LState = 1 and myRSI < 60 then 
        LState = 2; 
    if LState = 2 and myRSI >= 80 then begin
        Sell next bar at market;
        LState = 0;
    end;    
    if LState >= 1 and myRSI < 40 then begin
        Sell next bar at market;
        LState = 0;
    end;
end else begin
    LState = 0; 
end;

8. K線回顧移動停利

這是一個簡單且好用的移動停利方法。進場後 一開始的停損點是採用固定的點數停損,過了一段時間(固定的K棒數)之後 這段期間的最低點都已經大於進場成本價 則停利出場守在近期最低點。多單出場範例如下

Vars:  StopPrice(0), IsSafe(false);   

If MarketPosition = 1 then Sell next bar at entryprice - StopPrice stop;
    
    If BarsSinceEntry = 0 then  IsSafe = false;

    If BarsSinceEntry >= 50 then begin
 
        If Lowest(Low, BarsSinceEntry) > EntryPrice then begin
            IsSafe = true;
        end;
    end;

    If IsSafe then begin
        StopPrice = MaxList(StopPrice, Lowest(Low, 50));
    end;

9.階梯線移動停利法

邏輯大致如下: 當部位是持有多單 該K棒的最高價來到進場後的新高,且K棒收紅K,停損(停利)點則 移動到該K棒的最低點;這是一個非常強勢且貼近盤勢的移動停利邏輯,通常用於捕捉行情噴出段

Vars:
    MaxHighSinceEntry(0),
    TrailingStop(0);    
If MarketPosition = 1 then MaxHighSinceEntry = High;
         
     If High > MaxHighSinceEntry and Close > Open then begin
        MaxHighSinceEntry = High;             
         TrailingStop = Low;          
    end;
    
If marketposition=1 and TrailingStop > 0 then  Sell  next bar at TrailingStop stop;

10.波動率發散移動停利

邏輯核心為 當市場波動變大時,原有的防守範圍會變得太寬,必須縮短看過去的 K 棒根數,讓停利線更貼近價格。進場初期可能還在盤整,給予較寬的空間(例如近10根 K 棒最低點)避免被掃單。當行情噴發時:一旦 Volatility 達到 原本的兩倍則立即將防守縮短至近 5根K棒最低點

後續當波動率開始收斂 ,Volatility 從最高的位置開始縮小 (行情進入盤整)到原本的一半,直接提前停利出場

Vars:BaselineVol(0),CurrentVol(0),StopPrice(0);  
         
 CurrentVol = Volatility(10);
If MarketPosition = 1 then StopPrice = Lowest(Low, 10);

If CurrentVol > (BaselineVol*2) then StopPrice = Lowest(Low, 5)
    else begin
        StopPrice = Lowest(Low, 10);
    end;    
if marketposition=1 then Sell next bar at StopPrice stop;

交易策略多樣化,沒有最好的 只有最適合的: 不同的策略 不同的商品 適用的停利法完全不同

只要能提高獲利或是降低 MDD,都是有效的出場方法; 設定移動停利,雖然可能少賺一點點,但能有效大幅降低獲利回吐的風險,讓績效曲線更平穩 。

留一點空間,有興趣取得完整程式碼或是想了解更多有關multicharts 的操作方式,歡迎聯繫我!

分享你的喜愛