搭配不同週期在同一個視窗撰寫成策略, 這個觀念在很多國外的書中都有提到,因此我認為是一個值得學習的方向。說穿了邏輯很簡單,就是利用長週期的K線判定趨勢,再利用短週期的K線判斷進出場點位。下面是網友Eagle提供的TS歷史回測及策略開放程式碼,透過這個程式碼,希望讀者可以學會簡易多週期的語法。




 
 
原始程式碼(TS格式):
 

inputs: TradeProfit(0.04),TradeStopLoss(0.01) ;
Inputs: OverBought(95),OverSold(5),BuyLength(35),SellLength(45);
vars: IsBalanceDay(False),MP(0),KB(0),KS(0);

if DayOfMonth(Date) > 14 and DayOfMonth(Date) < 22 and DayOfWeek(Date)= 3 then IsBalanceDay = True else IsBalanceDay =False ;
MP = MarketPosition ;

{ ----- Preparation of Trade Setup -----}
{------- 1st Time Frame 60 min ------}
Condition1 = Macd(Close,12,26) of Data3 > Macd(Close,12,26)[1] of Data3 ;
Condition2 = Macd(Close,12,26) of Data3 < Macd(Close,12,26)[1] of Data3 ;

{------- 2nd Time Frame 12 mim ------}
Condition3 = FastK(9) of Data2 < OverBought and SlowD(9)of Data2 < OverBought ;
Condition4 = FastK(9) of Data2 > OverSold and SlowD(9)of Data2 > OverSold ;

{------- 3rd Time Frame 5 minutes ------}
Condition5 = Close of Data1 > Average(Close of Data1,BuyLength) ;
Condition6 = Close of Data1 < Average(Close of Data1,SellLength) ;

if Date <> Date[1] then Begin
KB = 0 ;
KS = 0 ;
end;

if (KB+KS)< 1 then Begin
if Condition1 and Condition3 and Condition5 and KB < 1 then Begin
Buy ("LB") at Market;
KB = KB +1 ;
end;

if Condition2 and Condition4 and Condition6 and KS < 1 then Begin
Sell ("SS") at Market; 
KS = KS +1 ;
end;
end;

if MP > 0 then Begin 
if MP > 0 and High > EntryPrice*(1 + TradeProfit) then ExitLong at EntryPrice*(1 + TradeProfit) stop;
if MP > 0 and Low < EntryPrice*(1 - TradeStopLoss) then ExitLong at EntryPrice *(1- TradeStopLoss) stop;
end;

{ ----- Exit of Short Trade -----}

if MP < 0 then Begin 
if MP < 0 and Low < EntryPrice*(1 - TradeProfit) then ExitShort at EntryPrice*(1 - TradeProfit) stop;
if MP < 0 and High > EntryPrice*(1 + TradeStopLoss) then ExitShort at EntryPrice*(1 + TradeStopLoss) stop;
end; 
{ ----- Balance on DayClose -----}

if MP <> 0 and time > 1335 then Begin
if MP > 0 then exitlong at market;
if MP < 0 then exitshort at market;
end;
{ ----- Balance on Monthly BalanceDay -----}
if IsBalanceDay then Begin
if MP <> 0 and time= 1315 then Begin
if MP > 0 then ExitLong at market ;
if MP < 0 then ExitShort at market ;
end ; 
end;
arrow
arrow
    全站熱搜

    wenschair 發表在 痞客邦 留言(0) 人氣()