## 2014年12月25日 星期四

### ★即使是最簡單的型態的策略...還是離我很遠!

以前剛切入技術分析的時候，大家都在討論型態該如何畫線，背離的時候要記得反向作單，我曾經看了不少國外型態的書，從國外網站搜刮與型態有關的語法，想要把型態這種東西量化成策略。但是研究了一陣子，最後還是選擇放棄，雖然有些人說型態很準，但是這類複雜的東西，還是留給更高的高手去研究好了。 下面這個是一個最簡單的型態策略範例語法，單純只是在撰寫簡單的指標與價格的背離而已，希望給想要研究型態語法的人一些想法。

input: diverge(1), swing(2.0), strength(5), length(5), type(0), exit(0),
fastx(10);

vars:  pcswing(0), last(0), curhigh(0), curlow(0), swhigh(0), swlow(0) ,
swhigh1(0), swlow1(0), highbar(0), highbar1(0), STslowk(0),
lowbar(0), lowbar1(0), chighbar(0), clowbar(0), exittype(0),
STtoday(0), SThigh(0), STlow(0), curSThigh(0), curSTlow(0),
xhigh(0), xlow(0), xclose(0),signal(0),
highclose(0), lowclose(0), chighprice(0), clowprice(0);

pcswing = swing/100.;
STslowk = SlowK(length);
STtoday = SlowD(length);

xclose = close;
xhigh = high;
xlow = low;
if type = 1 then begin
xclose = 100. - close;
xhigh   = 100. - low;
xlow = 100. - high;
end;
if type = 2 then begin
xclose = 800 / close;
xhigh = 800 / low;
xlow = 800 / high;
end;
{ SWINGS: INITIALIZE MOST RECENT HIGH AND LOW }
if currentbar = 1 then begin
{ Initialize curhigh and curlow }
curhigh = xhigh;                {current high price}
curlow  = xlow;                  {current low price}
end;

{ SEARCH FOR A NEW HIGH }
if  last<>1 then begin
if xhigh > curhigh  then begin
curhigh = xhigh;          {save values at new high}
curSThigh = STtoday;
chighbar = currentbar;
end;
if xlow < curhigh - curhigh*pcswing  then begin
last = 1;                       {last high fixed}
if type = 0 and exittype = -1 then exittype = 0;
if type <> 0 and exittype = 1 then exittype = 0;
swhigh1  = swhigh;     {previous high}
highbar1 = highbar;
swhigh   = curhigh;     {new verified high}
highbar   = chighbar;
curlow = xlow;           {initialize new lows}
if type = 0 then begin
SThigh = curSThigh;
highclose = swhigh;
end
else begin
STlow = curSThigh;
lowclose = close[currentbar - highbar];
end;
clowbar = currentbar;
end;
end;

{ SEARCH FOR A NEW LOW }
if last <> -1 then begin
if xlow < curlow then begin
curlow = xlow;                  {save values at new lows}
curSTlow = STtoday;
clowbar = currentbar;
end;
if xhigh > curlow + curlow*pcswing then begin
last = -1;
if type = 0 and exittype = 1 then exittype = 0;
if type <> 0 and exittype = -1 then exittype = 0;
swlow1  = swlow;
lowbar1 = lowbar;
swlow   = curlow;
lowbar   = clowbar;
curhigh = xhigh;          {initialize current high}
if type = 0 then begin
STlow = curSTlow;
lowclose = swlow;
end
else begin
SThigh = curSTlow;
highclose = close[currentbar - lowbar];
end;
chighbar = currentbar;
end;
end;

{ DIVERGENCE LOGIC }
if type = 0 then begin
chighprice = curhigh;
clowprice   = curlow;
end
else begin
chighprice = high[currentbar - lowbar];
clowprice = low[currentbar - highbar];
end;

{ SINGLE DIVERGENCE }
if diverge = 1 then begin
If (((type = 0 and last = -1) or (type <> 0 and last = 1)) and
exittype <> -1 and close > highclose and high = chighprice and
STslowk > fastx and (STtoday > 50+exit or STtoday < STtoday) and
STtoday < SThigh - strength) then begin
Sell Short This Bar  on close;
signal = -1;
exittype = 0;
end;
if (((type = 0 and last = 1) or (type <> 0 and last = -1)) and
exittype <> 1 and close < lowclose and low = clowprice and
STslowk < 100-fastx and (STtoday < 50-exit or STtoday > STtoday) and
STtoday > STlow + strength) then begin
Buy This Bar  on close;
signal = 1;
exittype = 0;
end;
end;

{ DOUBLE DIVERGENCE - MIN ON CURRENT STOCH ONLY }
if diverge = 2 then begin
If last = -1 and xhigh = curhigh and STslowk > fastx and
(STtoday > 50+exit or STtoday < STtoday) then begin
If xclose > swhigh and swhigh > swhigh1 and
STtoday < SThigh - strength and
SThigh < SlowD(length)[currentbar - highbar1] then begin
if type = 0 then begin
Sell Short This Bar  on close;
signal = -1;
exittype = 0;
end
else begin
Buy This Bar  on close;
signal = 1;
exittype = 0;
end;
end;
end;
if last = 1 and xlow = curlow and STslowk < 100-fastx and
(STtoday < 50-exit or STtoday > STtoday) then begin
if  xclose < swlow and swlow < swlow1 and
STtoday > STlow + strength and
STlow > SlowD(length)[currentbar - lowbar1] then begin
if type = 0 then Buy This Bar  on close else Sell Short This Bar  on close;
end;
end;
end;

{ Get out if divergence disappears or swing reverses }
if (STtoday > SThigh or STtoday=100) and signal = -1 then begin
Buy to Cover This Bar  on close;
signal = 0;
exittype = -1;
end;
if (STtoday < STlow or STtoday=0) and signal = 1 then begin
Sell This Bar  on close;
signal = 0;
exittype = 1;
end;

{ Get out if Stochastic reverses after crossing thresholds }
if STslowk <= fastx or (STtoday < 50+exit and STtoday > STtoday)
and signal = -1 then begin
Buy to Cover This Bar  on close;
exittype = -1;
signal = 0;
end;
if STslowk >= 100-fastx or (STtoday > 50-exit and STtoday < STtoday)
and signal = 1 then begin
Sell This Bar  on close;
signal = 0;
exittype = 1;
end;

#### 張貼留言

----------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------