因為市場不會永遠存在趨勢,所以才會有人想說希望有一種交易系統可以適用在趨勢市場和擺盪市場。而ThermostatTradingStrategy(恆溫器交易系統)的設計原理,就是在趨勢市場中採用順勢系統,在擺盪市場中採用擺盪系統。有點像是換檔的感覺,而決定換檔時機的指標,就是之前報告過的ChoppyMarketIndex,這個指標是會介於0-100之間,指數越大,代表現在的市場越有趨勢。整個系統的架構,可以簡化的寫成下面這樣:
If ChoppyMarketIndex < 20 then begin
擺盪系統內容;
end;
If ChoppyMarketIndex >= 20 then begin
順勢系統內容;
End;
架構就是這麼簡單而已,剩下的只是把擺盪系統和順勢系統的內容放進去這個架構裡面而已。順勢系統的內容主要是延續使用 BollingerBandit系統的內容,而擺盪系統則是加上的簡單的型態識別(patternrecognition)的開盤區間突破系統而已。下面分別就這兩種系統作報告:
擺盪系統:
在擺盪市場中,通常會存在一種現象,就是如果今天價格上漲的話,那麼明天的價格就比較傾向於會下跌。而今天價格如果下跌的話,那麼明天的價格就比較傾向於上漲,而這也正是擺盪市場的特性。所以我們定義如果今天的收盤價如果高於昨天的(最高價+最低價+收盤價) / 3的話,那麼明天就會一個 sell easierday,代表明天價格應該會比較傾向下跌。相反的,我們也定義如果今天的收盤價低於昨天的 (最高價+最低價+收盤價) /3的話,那麼明天就會是一個buy easier day,代表明天價格應該會比較傾向上漲。
在buy easierday的時候,只有代表著價格比較具有上漲的可能性而已,並不是指價格一定會上漲。所以我們必須設定做多和做空這兩邊的entry,只是這兩邊entry的門檻不一樣而已,做多的門檻比較低,比較容易。反而做空的門檻比較高,比較難。所以在buyeasier day的時候,我們會設定進場的規則是這樣的:
Initiate a long position at the open price + 50% of the ten-dayaverage true range.
Initiate a short position at the open price - 75% of the ten-dayaverage true range.
而如果是sell easier day的話,那我們則會把進場的規則設為這樣:
Initiate a short position at the open price - 50% of the ten-dayaverage true range.
Initiate a long position at the open price + 75% of the ten-dayaverage true range.
而在擺盪市場中,有時候市場會有假的,失敗的波動,這種假的波動常常會讓我們被巴來巴去,所以這裡我們加入了一個簡單的濾網來避免這種情形。如果我們的buystop 低於三天的最低價的平均,則就把buy stop提高到三天的最低價的平均。而如果我們的sellstop高於三天最高價的平均,則把sell stop下降到三天最高價的平均。
順勢系統:
如果當ChoppyMarketIndex的指標高於20的時候,代表現在市場進入趨勢了,所以我們也跟著改用順勢系統。這裡我們所採用的順勢系統就是之前報告過的BollingerBandit系統。當價格突破上通道的時候建立多頭部位,當價格跌破下通道的時候則建立空頭部位。而當有部位在手上的時候,而價格回到50天移動平均線的時候,我們就平倉出場。
而當這個系統在擺盪和趨勢這兩種模式當中轉換的時候,有時候會有部位在手上。當從趨勢市場轉換成擺盪市場的時候,如果有在趨勢市場當中建立的部位,則我們就讓擺盪系統的進場訊號發生的時候才來結束這個部位。但是當市場從擺盪市場變成趨勢市場的時候,如果我們有在擺盪市場裡面建立的部位的話,那麼我們就用三倍ATR的保護性停損來保護我們的部位。因為如果要用50天移動平均線才讓我們出場的話,那可能會讓我們保留這個錯誤的部位太久而造成太多的損失。
下面就是這個系統的程式碼:
{Thermostat by George Pruitt
Two systems in one. If the ChoppyMarketIndex is less than 20 thenwe are in a
swing mode. If it is greater than or equal to 20 then we are in atrend mode.
Swing system is an open range breakout incorporating a buyeasier/sell easier
concept. The trend following system is based on bollinger bands andis
similar to the Bollinger Bandit program.}
Inputs:bollingerLengths(50),trendLiqLength(50),numStdDevs(2),swingPrcnt1(0.50),swingPrcnt2(0.75),atrLength(10),swingTrendSwitch(20);
Vars:cmiVal(0),buyEasierDay(0),sellEasierDay(0),trendLokBuy(0),trendLokSell(0),keyOfDay(0),swingBuyPt(0),swingSellPt(0),trendBuyPt(0),trendSellPt(0),swingProtStop(0);
cmiVal = ChoppyMarketIndex(30);
buyEasierDay = 0;
sellEasierDay = 0;
trendLokBuy = Average(Low,3);
trendLokSell= Average(High,3);
keyOfDay = (High + Low + Close)/3;
if(Close > keyOfDay) then sellEasierDay = 1;
if(Close <= keyOfDay) then buyEasierDay = 1;
if(buyEasierDay = 1) then
begin
swingBuyPt = Open of tomorrow +swingPrcnt1*AvgTrueRange(atrLength);
swingSellPt = Open of tomorrow -swingPrcnt2*AvgTrueRange(atrLength);
end;
if(sellEasierDay = 1) then
begin
swingBuyPt = Open of tomorrow +swingPrcnt2*AvgTrueRange(atrLength);
swingSellPt = Open of tomorrow -swingPrcnt1*AvgTrueRange(atrLength);
end;
swingBuyPt = MaxList(swingBuyPt,trendLokBuy);
swingSellPt = MinList(swingSellPt,trendLokSell);
trendBuyPt =BollingerBand(Close,bollingerLengths,numStdDevs);
trendSellPt = BollingerBand(Close,bollingerLengths,-numStdDevs);
if(cmiVal < swingTrendSwitch)then
begin
if (MarketPosition <> 1) thenBuy("SwingBuy") next bar at swingBuyPt stop;
if(MarketPosition <> -1) thenSellShort("SwingSell") next bar at swingSellPt stop;
end
else
begin
swingProtStop = 3*AvgTrueRange(atrLength);
Buy("TrendBuy") next bar at trendBuyPt stop;
SellShort("TrendSell") next bar at trendSellPt stop;
Sell from Entry("TrendBuy") next bar atAverage(Close,trendLiqLength) stop;
BuyToCover from Entry("TrendSell") next bar atAverage(Close,trendLiqLength) stop;
Sell from Entry("SwingBuy") next bar at EntryPrice – swingProtStopstop;
BuyToCover from Entry("SwingSell") next bar at EntryPrice +swingProtStop stop;
end;
而下面則是這個系統在1982-2002這20年的績效表現,看來比之前報告的King Kelthner和BollingerBandit系統績效來的好。
有興趣的朋友可以拿來測試一下看看,不過我自己測試了最近幾年的外匯,指數,金屬,能源,農產品等等。發現績效還好而已,所以想要直接拿來套用的人可能會失望了。不過這樣的系統開發的邏輯,倒是可以值得我們學習的。謝謝。
判斷趨勢盤or擺盪盤的指標
http://www.programtrading.tw/viewtopic.php?f=24&t=404
今天報告另一個簡單的指標,則是George & John在書中所提出的一個指標,叫做ChoppyMarketIndex。這個指標也是用來判斷目前盤勢的方法之一。計算方式如下:
ChoppyMarketIndex = (Abs(Close-Close[29]) /(Highest(High,30)-Lowest(Low,30)) * 100)
分母是最近30天最高價 –最近30天的最低價。分子則是今天的收盤價-29天前的收盤價,然後再取絕對值。ChoppyMarketIndex的數值也是會介於0-100之間,數值越大,代表市場趨勢越明顯。數值越小,則代表目前市場可能陷入擺盪狀況。
ChoppyMarketIndex這個Function的程式碼如下:
{Choppy Market Index Function
This function returns a value from 0 to 100.
A lower value denotes higher market indecisiveness(choppiness),
whereas a higher value denotes a trending market.
The only input is the number of bars that we look back.}
Inputs: periodLength(Numeric);
Vars: num(0),denom(1);
if(periodLength<>0) then
begin
denom = Highest(High,periodLength) -Lowest(Low,periodLength);
num = Close[periodLength-1]- Close;
num = AbsValue(num);
ChoppyMarketIndex = 0.0;
if(denom<>0) then ChoppyMarketIndex =num/denom*100;
end;
所以不管用的是ADX,或是ChoppyMarketIndex,我們只要有了這種可以判斷目前盤勢的指標之後,就可以用來接著開發後續要報告的會換檔的交易系統了。謝謝。
http://karime.blog.hexun.com/3232156_d.html
[thermostat]
if the cmi function returns a value of less than 20,thenuse
the short-term swing approach.
atr10=averagetruerange(10)
keyofday=(high+low+close)/3
buyeasierday=0
selleasierday=0
if(close>keyofday) then selleasierday=1
if(close<=keyofday) then buyeasierday=1
avg3hi=average(high,3)
avg3lo=average(low,3)
if(buyeasierday=1) then
longentrypoint=open+atr10*0.5
shortentrypoint=open-atr10*0.75
if(selleasierday=1) then
longentrypoint=open+atr10*0.75
shortentrypoint=open-atr10*0.5
longentrypoint=maxlist(longentrypoint,avg3lo)
shortentrypoint=minlist(shortentrypoint,avg3hi)
initiate a long position of today's marketaction>=longentrypoint
initiate a short position of today's marketaction<=shortentrypoint
..................................
if the cmi function returns a value greater than or equal to20,
then use the long-term trend following approach.
if have a short position that was initiated by the short-term swingapproach then
shortliqpoint=entryprice+3*atr10
liquidate short position if today's marketaction>=shortliqpoint
if have a long position that was initiated by the short-termswing
approach then
longliqpoint=entryprice-3*atr10
liquidate long position if today's marketaction<=longliqpoint
upband=average(close,50)+stddev(close,50)*2.00
dnband=average(close,50)-stddev(close,50)*2.00
avg50=average(close,50)
initiate a long position if today's marketaction>=upband
initiate a short position if today's marketaction<=dnband
liquidate long position if today's marketaction<=avg50
liquidate short position if today's marketaction>=avg50
[ts code]
inputs:bollingerlengths(50),trendliqlength(50),numstddevs(2),
swingprcnt1(0.50),swingprcnt2(0.75),atrlength(10),
swingtrendswitch(20);
vars:cmival(0),buyeasierday(0),selleasierday(0),trendlokbuy(0),
trendloksell(0),keyofday(0),swingbuypt(0),swingsellpt(0),
trendbuypt(0),trendsellpt(0),swingprotstop(0);
cmival=choppymarketindex(30);
buyeasierday=0;
selleasierday=0;
trendlokbuy=average(low,3);
trendloksell=average(high,3);
keyofday=(high+low+close)/3;
if(close>keyofday) then selleasierday=1;
if(close<=keyofday) then buyeasierday=1;
if(buyeasierday=1) then
begin
swingbuypt=open of tomorrow +swingprcnt1*avgtruerange(atrlength);
swingsellpt=open of tomorrow -swingprcnt2*avgtruerange(atrlength);
end;
if(selleasierday=1) then
begin
swingbuypt=open of tomorrow +swingprcnt2*avgtruerange(atrlength);
swingsellpt=open of tomorrow -swingprcnt1*avgtruerange(atrlength);
end;
swingbuypt=maxlist(swingbuypt,trendlokbuy);
swingsellpt=minlist(swingsellpt,trendloksell);
trendbuypt=bollingerband(close,bollingerlengths,numstddevs);
trendsellpt=bollingerband(close,bollingerlengths,-numstddevs);
if(cmival<swingtrendswitch) then
begin
if(marketposition<>1) thenbuy("swingbuy") next bar at swingbuypt
stop;
if(marketposition<>-1) thensellshort("swingsell") next bar at
swingsellpt stop;
end
else
begin
swingprotstop=3*avgtruerange(atrlength);
buy("trendbuy") next bar attrendbuypt stop;
sellshort("trendsell") nextbar at trendsellpt stop;
sell from entry("trendbuy")next bar at average(close,trendliqlenth)
stop;
buytocover fromentry("trendsell") next bar at
average(close,trendliqlength) stop;
sell from entry("swingbuy")next bar at entryprice - swingprotstop
stop;
buytocover fromentry("swingsell") next bar at entryprice +
swingprotstop stop;
end;
指标介绍:
http://www.mql5.com/zh/articles/190
恒温器系统:
http://www.yafco.com/show.php?contentid=264203