|
本帖最後由 maison6579 於 12-11-8 00:22 編輯
看到Amibroker版有人貼上SafeZone指標,
上網找了easylanguage版本。- [prettyprint]
- { Barry Silberman's version of Alexander Elder's "SafeZone Stop" from book "Come Into My Trading Room", page 173.
- For Tradestation 2000
- Amendments: (1) John Lynch: code for variable lookback period based on prior swing extremes;
- (2) John Lynch: code to indicate stop for next period --- plotted on last bar on chart.
- (3) Gary Fritz: use of TS variable instead of arrays
- (4) Eric Svendsen: Coloured Stop and trend variable
-
- Notes:
- A) Set the chart up with one space to the right. Set plot2's chart style type to point and its color to
- your background color if your background is not black (5th line of each Set Plot section).
- B) This is an adaptation of Dr. Elder's stop based on the following points that he emphasizes in his book:
- (1) you may use the slope of a 22 day EMA to define the trend
- (2) SafeZone is not a mechanical gadget to replace independent thought.
- (3) Calculate the stops separately for uptrends and downtrends.
- (4) The lookback period should not go back beyond the last important turning point. If the market has
- reversed from down to up two weeks ago, then the SafeZone for the current long trades should not
- look back more than 10 trading days.
- (5) An important decision is choosing the coefficient for the SafeZone stop. He says a coefficient
- between 2 and 3 provides a margin of safety, but you must research it on your own market data.
- (6) You can add it to almost any trading system, including Triple Screen.
- (7) The excel code that Dr. Elder used in his book kept the stop from declining for 3 days, by which
- time either the uptrend resumes or the stop is hit. This code uses a period of 5. }
-
-
- INPUTS:
- Price((H+L)/2), MALength(22), Lookback(15), Trend(3), AutomaticLookBack(True), Multiplelong(2), Multipleshort(2),
- ColourShort(red), ColourLong(blue);
-
- VARIABLES:
- MA(0), Ldiff(0), Lday(0), Sdiff(0), Sday(0), Longsum(0), Shortsum(0) , Countlong(0), Countshort(0), Avglow(0),
- Avghigh(0), Longstop(0), Shortstop(0), Lstop(0), Sstop(0), ii(0), LookBackTemp(0);
-
- {Determine trend by slope of EMA}
- MA = xaverage( Price, MALength );
- Condition1 = MA > MA[Trend];
- Condition2 = MA < MA[Trend];
-
- {Determine Lookback Period (to prior swing extreme)}
- IF LookBackTemp<LookBack THEN LookBackTemp=LookBackTemp+1;
- IF Condition1<>Condition1[1] THEN BEGIN
- LookBackTemp=LookBack;
- Value1=MinList(MALength*0.66,LookBack);
- IF Condition1 and AutomaticLookBack THEN IF LowestBar(L,Value1)<LookBack THEN LookBackTemp=LowestBar(L,Value1);
- IF Condition2 and AutomaticLookBack THEN IF HighestBar(H,Value1)<LookBack THEN LookBackTemp=HighestBar(H,Value1);
- END;
-
- {determine and count number of days with:
- for longs - lows being lower than prior lows
- for shorts - highs being higher than prior highs}
-
- Ldiff = IFF(L[1] > L, L[1] - L, 0); { Difference of lows }
- Lday = IFF(L[1] > L, 1, 0); { 1 if this is a "low's" day }
- Sdiff = IFF(H[1] < H, H - H[1], 0); { Difference of highs }
- Sday = IFF(H[1] < H, 1, 0); { 1 if this is a "high's" day }
-
- {SAFEZONE FOR LONGS}
- {Get average of lows lower than prior low}
- Longsum = summation(Ldiff, Lookbacktemp);
- Countlong = summation(Lday, Lookbacktemp);
- IF Countlong <> 0 THEN Avglow = Longsum / Countlong;
-
- {Calculate stop at "X" times avglow}
- Lstop = LOW - (Multiplelong * Avglow);
-
- {Prevent stop from being lowered}
- Longstop = MAXLIST(Lstop, Lstop[1], Lstop[2], Lstop[3], Lstop[4], Lstop[5]);
-
- {Set Plot}
- IF Condition1 THEN BEGIN
- PLOT1(Longstop[1], "STOP", ColourLong);
- IF lastbaronchart THEN BEGIN
- Plot1[-1](Longstop, "STOP", ColourLong); {Shows tomorrows stop on lastbar+1}
- Plot2(Longstop, "StopNext"); {Indicates tomorrows stop: set indicator to point/black}
- END;
- END;
-
- {SAFEZONE FOR SHORTS}
- {Get average of highs higher than prior high}
- Shortsum = summation(Sdiff, Lookbacktemp);
- Countshort = summation(Sday, Lookbacktemp);
- IF Countshort <> 0 THEN Avghigh = Shortsum / Countshort;
-
- {Calculate stop at "X" times avghigh}
- Sstop = HIGH + (Multipleshort * Avghigh);
-
- {Prevent stop from rising}
- Shortstop = MINLIST(Sstop, Sstop[1], Sstop[2], Sstop[3], Sstop[4], Sstop[5]);
-
- {Set Plot}
- IF Condition2 THEN BEGIN
- PLOT1(Shortstop[1], "STOP", ColourShort);
- IF lastbaronchart THEN BEGIN
- Plot1[-1](Shortstop, "STOP", ColourShort); {Shows tomorrows stop on lastbar+1}
- Plot2(Shortstop, "StopNext", Black); {Indicates tomorrows stop: set indicator to point/black}
- END;
- END;
- [/prettyprint]
複製代碼 |
評分
-
查看全部評分
|