2013年8月12日 星期一

★PJK Volume策略(含程式碼)--Eagle測試版

     由好心的讀者Eagle幫大家測試PJK Volume策略於台指期,測試內容僅供大家參考,目的在於程式碼教學,請勿用於實單交易。






測試 2000 日 手續費 1200,僅提供日K回測供參考。

inputs: Period(20),ttype(1),maxdn(0),maxdvg(0),npsd(0.5),ddays(3), stoploss(50),takeprofit(100);
vars: px(0),vx(0),kvol(0),map(0),mav(0),sdiff(0),madays(0),ma(0),dvg(0),
nd(0),madiff(0),avgv(0),sdv(0),tentry(0),avg2(0),type(0),sc(0),avghl(0),signhl(0);


{Exit type = 1, On-Balance Volume
           = 2, Chiaken Volume Accumulator
   = 3, Negative Volume Index
   = 4, Positive Volume Index
   = 5  Price and Volume Trend
   = 6  Volume Oscillator (npsd used for exit level
   = 7  Volume rate of Change
   = 8  Low Volume Exit
for Entry filter use negative type}

{test exit}
type = ttype ;
if type < 0 then Begin
tentry = 1;
    type = @absvalue(ttype);
end;

{Price index}
map = @Average(Close,period) ;
if ddays < 1 then nd = 1 else nd = ddays ;
if map[nd] = 0 then px = 0 else px = (map - map[nd])*100/map[nd];

{On Balance Volume}
if type = 1 then Begin
if Close > Close[1] then Kvol = Kvol + Volume
else if Close < Close[1] then Kvol = Kvol - Volume;
end;

{Chaikin Volume Accumulator}
if type = 2  then Kvol = Kvol + (((Close - Low)/(High - Low)) - 0.5)*2*Volume ;

{Negative Volume Index}
if type = 3 then Begin
if Volume < Volume[1] then Kvol = Kvol[1] - (Close - Close[1])*Kvol/Close[1]
else if Kvol = 0 then Kvol = Volume 
else Kvol = Kvol[1] ;
end;

{Positive Volume Index}
if type = 4 then Begin
if Volume > Volume[1] then Kvol = Kvol[1] + (Close - Close[1])*Kvol[1]/Close[1]
else if Kvol = 0 then Kvol = Volume 
else Kvol = Kvol[1] ;
end;
{Price and Volume Trend}
if type = 5 then Kvol = Kvol + Volume*(Close - Close[1])/Close[1] ;

{Volume Oscillator}
if type  = 6  then Kvol = (@Average(Volume,ddays) - @Average(Volume,Period))*100/@Average(Volume,ddays);

{Volume rate of change}
if type = 7 then Begin
if Kvol = 0 then Kvol = Volume ;
if Volume[Period] = 0 then Kvol = Kvol[1] 
else Kvol = (Volume - Volume[Period])*100/Volume[Period] ;
end;

{Moving average of Price}
madays = 2*period ;
ma = @Average(Close,madays) ;
madiff = ma - ma[nd] ;

if type < 6 or type = 9 then Begin
mav = @Average(Kvol,Period) ;
if mav[nd] = 0 then vx = 0 else vx = (mav - mav[nd])*100/mav[nd] ;
sdiff = px - vx ;

{record divergence only}

if (vx * px) < 0 then dvg = sdiff else dvg = 0 ;
if madiff > 0 and madiff[1] <= 0 then Begin
exitshort at market ;
if tentry = 0 and (vx >= maxdn and @AbsValue(dvg) <= maxdvg) then
buy at market;
end;
if madiff < 0 and madiff[1] >= 0 then Begin
exitlong at market ;
if tentry = 0 and (vx >= maxdn and @AbsValue(dvg) <= maxdvg) then
sell at market ;
end;
if vx < maxdn and @AbsValue(dvg) > maxdvg then Begin
exitlong at market ;
exitshort at market ;
end;
end;

if type = 6 or type = 7 then Begin
avgv = @Average(Kvol,Period) ;
sdv = @Stddev(Kvol,Period) ;
if tentry = 0 then Begin
if (madiff > 0 and madiff[1] <= 0 ) or Kvol <= avgv - npsd*sdv then
exitshort at market ;
if (madiff < 0 and madiff[1] >= 0 ) or Kvol <= avgv - npsd*sdv then
exitlong at market ;
if (madiff > 0 and madiff[1] <= 0 ) then Buy at market ;
if (madiff < 0 and madiff[1] >= 0 ) then Sell at market ;
        end
    else Begin
if (madiff > 0 and madiff[1] <= 0 ) then exitshort at market ;
if (madiff < 0 and madiff[1] >= 0 ) then exitlong at market ;
     if (madiff > 0 and madiff[1] <= 0 ) and Kvol <= avgv - npsd*sdv then
buy at market ;
if (madiff < 0 and madiff[1] >= 0 ) and Kvol <= avgv - npsd*sdv then
sell at market ;
end;
end;

{Low Volume exit , Comparing slow and fast average}
if type = 8 then Begin
avgv = @Average(Volume,Period) ;
avg2 = @Average(Volume,ddays) ;

if tentry = 0 then begin
if (madiff > 0 and madiff[1] <= 0 ) or avg2 <= avgv*npsd then exitshort at market;
if (madiff < 0 and madiff[1] >= 0 ) or avg2 <= avgv*npsd then exitlong at market;
        if (madiff > 0 and madiff[1] <= 0 )  then buy at market;
if (madiff < 0 and madiff[1] >= 0 )  then sell at market;
     end
   else Begin
        if (madiff > 0 and madiff[1] <= 0 )  then exitshort at market;
if (madiff < 0 and madiff[1] >= 0 )  then exitlong at market;
        if (madiff > 0 and madiff[1] <= 0 ) and avg2 <= avgv*npsd then buy at market;
if (madiff < 0 and madiff[1] >= 0 ) and avg2 <= avgv*npsd then sell at market;
    end;
end;

0 留言:

張貼留言

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

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