2024年1月9日 星期二

★ 比特幣交易回測篇 [05]

EasyTrader 比特幣測試 05

這是一個運用 CDP 與 凱特通道 作為進出邏輯的交易策略

// Public Variable
vars:MP(0),PF(0),PL(0),LotNum(1),BuyPrice(0),ShortPrice(0),BasePF(150),BasePL(100),FilterA(0),FilterB(0) ;
vars:BarPass(5),HLRange(0),WinPoint(0),HBarPos(0),LBarPos(0),ExitH(0),ExitL(0),TimeOK(false),EntryExitCond(false),SelectNo(0) ;

//****************** Basic Setup ****************************************
MP = MarketPosition ;
if MP <> 0 then Begin
   PF = EntryPrice*TradeProfit ;
   PL = EntryPrice*TradeStopLoss ;
end else begin
   PF = AvgPrice*TradeProfit ;
   PL = AvgPrice*TradeStopLoss ;
end ;

PF = MinList(PF,2700) ;
PL = MinList(PL,1350) ;

// ************ CDP *****************
vars:CDP(0),AH(0),NH(0),NL(0),AL(0),CDPrange(0),CrossA(false),CrossB(false),TH1(0),TL1(0),TH2(0),TL2(0) ;
if Date <> Date[1] then begin
   CDP = (HighD(1)+LowD(1)+2*CloseD(1))/4;
   AH = CDP + (HighD(1) - LowD(1));
   NH = CDP*2 - LowD(1);
   NL = 2*CDP - HighD(1);
   AL = CDP - (HighD(1) - LowD(1));
end ; 
CDPrange = HighD(1) - LowD(1) ;

// ************ for Day High/Low box *****************
Vars:DayLong(5),DayShort(5),DH(0),DL(0),HLDay(0),DCount(0),DayBox33(0),DayBox67(0) ;
DH = 近日高點 ;
DL = 近日低點 ;

if BarNumber = 1 then begin
   Buy this bar on Close ;
   Sell this bar on Close ;
end ;

if DataCompression > 1 then begin
   Condition1 = Close > Open and Open > Close[1] ;
   Condition2 = Close < Open and Open < Close[1] ;
end else begin
   Condition1 = Close > OpenD(0) and OpenD(0) > CloseD(1) ;
   Condition2 = Close < OpenD(0) and OpenD(0) < CloseD(1) ;
end;

// ************* Time Set Up *************
if LE01 = 5 then TimeOK = True ;

// ************* Entry/Exit Condition Set Up *************
vars:MoneyLoss(false),LossCount(false),MoneyProfit(false),ProfitCount(false),ProfitCond(false),LossCond(false),ExitLen(0) ;
if Totaltrades > 6 then begin
   MoneyLoss = positionprofit(1)+positionprofit(2) < -1*(Close*0.01)*200 ;
   LossCount = positionprofit(1) < 0 and positionprofit(2) < 0 and positionprofit(3) < 0 ;
   LossCond = MoneyLoss or LossCount ;
   MoneyProfit = positionprofit(1)+positionprofit(2) > (Close*0.02)*200 ;
   ProfitCount = positionprofit(1) > 0 and positionprofit(2) > 0 and positionprofit(3) > 0 ;
   ProfitCond = MoneyProfit or ProfitCount ;
end else begin
   LossCond = true ;
   ProfitCond = true ;
end ;   

ExitLen = iff(ProfitCond,1080/BarInterval,iff(LossCond,2160/BarInterval,MaxList(LenA1,LenB1))) ;

EntryExitCond = TimeOK and BarsSinceExit(1) > ExitLen ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = Maxlist(HighD(0),HighD(1),HighD(2))-Range*0.5 ;
ShortPrice = DL ;

// ********** Main Strategy *********
// ********** Entry Method

// ******** CDP Entry ***********
   if EntryExitCond then begin
   if MP <> 1 and Close > Cdp and Close < NH and Condition1 then 
      Buy ("LE_CDP4") LotNum Contracts next bar at DH stop ;
   if MP <> -1 and Close < Cdp and Close > NL and Condition2 then 
      SellShort ("SE_CDP4") LotNum Contracts next bar at DL stop ;
   end ;


// ******** Keltner Entry ***********
vars:KeltnerL(0),KeltnerS(0),KAvgL(0),KAvgS(0) ;

KAvgL = Average(TypicalPrice,LenA1) ;
KAvgS = Average(TypicalPrice,LenB1) ;

KeltnerL = KavgL+AvgTrueRange(LenA1)*FracA ;
KeltnerS = KavgS-AvgTrueRange(LenB1)*FracB ;

   if TimeOK then begin
   if MP <> 1 and MRO(High Cross over KeltnerL,NBarL,1) > 0 and Close < BuyPrice then 
      Buy ("LE_KeltnerAT") LotNum Contracts next bar at BuyPrice stop;
   if MP <> -1 and MRO(Low Cross under KeltnerS,NBarS,1) > 0 and Close > ShortPrice then 
      SellShort ("SE_KeltnerAT") LotNum Contracts next bar at ShortPrice stop;
   end ;

// ************* Base Exit *************
if MP > 0 then Sell ("Trail_PL1_"+NumtoStr(Absvalue(EntryPrice-(HighSinceEntry-MinList(PL,PF))),2)) 
   All Contracts next bar at HighSinceEntry-MinList(PL,PF) stop ;
if MP > 0 then Sell ("PF1_"+NumtoStr(PF,0)) All Contracts next bar at EntryPrice+PF limit ;
if MP < 0 then BuytoCover ("Trail_PL2_"+NumtoStr(Absvalue(EntryPrice-(LowSinceEntry+MinList(PL,PF))),2)) 
   All Contracts next bar at LowSinceEntry+MinList(PL,PF) stop ;
if MP < 0 then BuytoCover ("PF2_"+NumtoStr(PF,0)) All Contracts next bar at EntryPrice-PF limit ;

比特幣  留倉 交易週期 2017/6/1 ~ 2024 交易成本 交易量*0.1%










3 則留言: