2014年4月15日 星期二

★國外買斷的舊策略(含程式碼)

     今天從電腦硬碟記憶最深處,控出一個幾年前跟美國人買斷的策略程式碼,在這邊demo給大家免費看。認識我的人都知道,我除了自己會寫策略,每年我會拿出一筆預算,購買或租用海外的策略,購買或租用的策略我通常不會拿來交易,我主要是用來增廣見聞,看看不同文化的人寫出來的策略會長什麼樣子,也順便認識幾個外國trader;雖然我覺得台灣人寫策略的實力遠大於老美,但是我倒是滿享受看美國人寫code的方式(通常都有很詳細的註解,而且按部就班,每次看完都會學到一點東西)。

     下面這個策略已經過時了,不過移動停利的方式倒是可以學學,有興趣的自己拿去測試歐指及美指,時間週期的設定我已經忘記(記得是日K),我主要是欣賞他的移動停利法,其他對我來說沒什麼幫助。


MINMAX STRATEGY

Input: EntryFrL   (.6),    { mult. of prior day's range for entering long }
        EntryFrS   (.3),    { mult. of prior day's range for entering short }
        ExitFrL    (.4),    { multiple of average range for long exits }
        ExitFrS    (.8),    { multiple of average range for short exits }
     
   TrendFr    (1.0),   { multiple of average true range for trend }
        TargFr1    (1.0),   { multiple of ATR for target exit against trend }
        TargFr2    (2.0),   { multiple of ATR for target exit with trend }
        Equity     (0),     { current account equity }
        RiskPer    (4.0),   { risk percentage; % of equity to risk on trade }
        OrderFN    ("c:\orders.txt");  { file name for orders }


 Var:   EntPrL     (0),     { long entry price }
        EntPrS     (0),     { short entry price }
        BrkAmntL   (0),     { breakout amount for long entry }
        BrkAmntS   (0),     { breakout amount for short entry }
        MMSizeL    (0),     { size of money management stop for longs }
        MMSizeS    (0),     { size of money management stop for shorts }
        XMML       (0),     { money management long exit price }
        XMMS       (0),     { money management short exit price }
        TrRisk     (0),     { risk in dollars for next trade }
        XTrailL    (0),     { trailing stop exit for long trades }     
        XTrailS    (9999999),{ trailing stop exit for short trades }  
        XTargL     (0),     { limit/target exit price for long trades }
        XTargS     (0),     { limit/target exit price for short trades }
        ExitL      (0),     { exit price for long position }
        ExitS      (0),     { exit price for short position }
        SetupL     (false), { set up conditions for long entry }
        SetupS     (false), { set up conditions for short entry }
        Trend      (0),     { trend direction: 1=up; -1=down }
        HC         (0),     { highest close }
        LC         (9999999), { lowest close }
        TrRev      (0),     { trend reversal amount }
        NConL      (0),     { number of contracts for long trade }
        NConS      (0),     { number of contracts for short trade }
        XTypeL     (""),    { type of exit for longs: mm, trail }
        XTypeS     (""),    { type of exit for shorts: mm, trail }
        PosString  (""),    { position: long, flat, short }
        StrOut     ("");    { text string for file output }

 { determine trend }
 TrRev = TrendFr * Average(TrueRange, 10);
 If BarNumber = 1 then Begin
    Trend = 1;
    HC = Close;
 End;
 If Trend > 0 then Begin
    If Close > HC then
       HC = Close
       else
       If (HC - Close) > TrRev then Begin
          Trend = -1;
          LC = Close;
       End;
 End;
 If Trend < 0 then Begin
    If Close < LC then
       LC = Close
       else
       If (Close - LC) > TrRev then Begin
          Trend = 1;
          HC = Close;
       End;
 End;

 { Cancel set up once a trade is entered }
 If BarNumber > 1 and SetupL and High >= EntPrL then
    SetupL = false;
 If BarNumber > 1 and SetupS and Low <= EntPrS then
    SetupS = false;

 { Look for set up conditions for long trade }
 If BarNumber > 1 and Low < Low[1] and Close < Close[2] and
    Trend[1] > 0 and MarketPosition <> 1 then
       SetupL = true;
         
 { Look for set up conditions for short trade }
 If BarNumber > 1 and High > High[1] and Close > Close[2] and
    Trend[1] < 0 and MarketPosition <> -1 then
       SetupS = true;

 { If setup conditions are met, look for long entry }
 If SetupL then Begin
      
    { Set the breakout amount using maximum and minimum limits }
    BrkAmntL = MaxList(EntryFrL * TrueRange, 0.5 * Average(TrueRange, 10));
    BrkAmntL = MinList(BrkAmntL, 1.5 * Average(TrueRange, 10));

    { Define the entry price for next day as a breakout from today's close }
    EntPrL = C + BrkAmntL;

    { Define the money management stop initially relative to expected entry }
    MMSizeL = ExitFrL * Average(TrueRange, 10);
    XMML = EntPrL - MMSizeL;
    TrRisk = MMSizeL * BigPointValue;

    { Calculate number of contracts for next trade }
    If Equity <= 0 then
       NConL = 1
    else
       NConL = IntPortion(RiskPer/100 * Equity/ABSVALUE(TrRisk));

       { Place order to enter next bar on a breakout from today's close }
    Buy("EnterL") NConL contracts next bar at EntPrL stop;

    { Place target exit orders }
    if Trend < 0 then
       XTargL = EntPrL + TargFr1 * Average(TrueRange, 10)
    else
       XTargL = EntPrL + TargFr2 * Average(TrueRange, 10);
    SELL("TargL0") next bar at XTargL limit;
 End;

 { If setup conditions are met, look for short entry }
 If SetupS then Begin
      
    { Set the breakout amount using maximum and minimum limits }
    BrkAmntS = MaxList(EntryFrS * TrueRange, 0.5 * Average(TrueRange, 10));
    BrkAmntS = MinList(BrkAmntS, 1.5 * Average(TrueRange, 10));

    { Define the entry price for next day as a breakout from today's close }
    EntPrS = C - BrkAmntS;

    { Define the money management stop initially relative to expected entry }
    MMSizeS = ExitFrS * Average(TrueRange, 10);
    XMMS = EntPrS + MMSizeS;
    TrRisk = MMSizeS * BigPointValue;

    { Calculate number of contracts for next trade }
    If Equity <= 0 then
       NConS = 1
    else
       NConS = IntPortion(RiskPer/100 * Equity/ABSVALUE(TrRisk));

       { Place order to enter next bar on a breakout from today's close }
    Sellshort ("EnterS") NConS contracts next bar at EntPrS stop;

    { Place target exit orders }
    if Trend > 0 then
       XTargS = EntPrS - TargFr1 * Average(TrueRange, 10)
    else
       XTargS = EntPrS - TargFr2 * Average(TrueRange, 10);
    buytocover("TargS0") next bar at XTargS limit;
 End;

 { If long, calculate money management stop relative to entry }
 If MarketPosition = 1 and BarsSinceEntry = 0 then Begin
    XMML = EntryPrice - MMSizeL;
    XTrailL = 0;
 End;

 { If short, calculate money management stop relative to entry }
 If MarketPosition = -1 and BarsSinceEntry = 0 then Begin
    XMMS = EntryPrice + MMSizeS;
    XTrailS = 9999999;
 End;

 { Place money management stops for longs and shorts }
 sell("MM StopL") next bar at XMML stop;
 buytocover("MM StopS") next bar at XMMS stop;

 { Calculate trailing stop for long trade }
 If MarketPosition = 1 then Begin
    XTrailL = MaxList(XTrailL, C - ExitFrL * TrueRange);
    sell("TrailL") next bar at XTrailL stop;
 End;

 { Calculate trailing stop for short trade }
 If MarketPosition = -1 then Begin
    XTrailS = MinList(XTrailS, C + ExitFrS * TrueRange);
    buytocover("TrailS") next bar at XTrailS stop;
 End;

 { Exit on close of first profitable open if against trend }
 If MarketPosition = 1 and Trend[1] < 0 and open > EntryPrice then
    sell("BailL") this bar at close;

 If MarketPosition = -1 and Trend[1] > 0 and open < EntryPrice then
    buytocover("BailS") this bar at close;

 { Exit at a target; use a tigher target if trade is agains the trend }
 If MarketPosition = 1 then Begin
    if Trend < 0 then
       XTargL = EntryPrice + TargFr1 * Average(TrueRange, 10)
    else
       XTargL = EntryPrice + TargFr2 * Average(TrueRange, 10);
    sell("TargL") next bar at XTargL limit;
 End;
 If MarketPosition = -1 then Begin
    if Trend > 0 then
       XTargS = EntryPrice - TargFr1 * Average(TrueRange, 10)
    else
       XTargS = EntryPrice - TargFr2 * Average(TrueRange, 10);
    buytocover("TargS") next bar at XTargS limit;
 End;

 { Determine whether mm stop or trailing stop is closer to market }
 If MarketPosition = 1 then Begin
    If XTrailL >= XMML then Begin
       ExitL = XTrailL;
       XTypeL = "trail";
    End
    Else Begin
       ExitL = XMML;
       XTypeL = "mm";
    End;
 End;
 If MarketPosition = -1 then Begin
    If XTrailS <= XMMS then Begin
       ExitS = XTrailS;
       XTypeS = "trail";
    End
    Else Begin
       ExitS = XMMS;
       XTypeS = "mm";
    End;

 End;




後記:

我告訴讀者,我曾經在程式交易受了不少傷,當初是因為貪心,而且網路上也沒人分享透徹的知識,總想要用最低的成本學到最好的東西,但事實證明這是不容易的,免費的東西通常很貴,所以建議有志於程式交易的讀者,多花點時間及金錢在投資自己上面,不管是買書、買策略、租策略、上課、認識VIP大戶,以學習為出發點,任何投資都比自己埋頭試單來得有效率,我認為就算再爛的策略或交易員,都有我可以學習的地方。

學無止盡,無知的代價很貴,共勉之。


0 留言:

張貼留言

如果有私人問題想請教,請透過網站右方『與站長聯絡』之表單,謝謝!

----------------------------------------------------------------------------------------------------
網站聲明(Disclaimer)
本教學網站內所提供之程式碼(包括函數、指標、訊號)屬開放程式碼,用意在於讓使用者學習程式語法之撰寫,使用者可以任意修改語法內容並調整參數。本網站所有之內容(包括文章、影片、歷史紀錄、程式碼、教材)限用於個人學習使用,請勿轉寄、濫用,嚴禁私自串接帳戶交易。
-------------------------------------------------------------------------------------------------