2023年12月20日 星期三

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

EasyTrader 比特幣測試 04

這是一個運用 KD 與 MACD 柱狀體高度 作為進出邏輯的交易策略

// 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) ;

// ************ High/Low data *****************
BarPass = MaxList(HBar,LBar) ;
HLRange = Highest(High,BarPass)-Lowest(Low,BarPass) ;
if MP <> 0 then WinPoint = MaxPositionProfit/BigPointValue else WinPoint = 0 ;
HBarPos = HighestBar(High,HBar) ;
LBarPos = LowestBar(Low,LBar) ;
if MP <> MP[1] and MP > 0 then ExitL = EntryPrice-PL ;
if MP <> MP[1] and MP < 0 then ExitH = EntryPrice+PL ;

// ************ Macd *****************
vars:uDif(0),uMacd(0),uOsc(0),MacdLen(0) ;
MacdLen = MaxList(LenA1,LenB1) ;
if MacdLen > 12 then MacdLen = 12 ;
uDif = Macd((High+Low+Close*2)/4,IntPortion(MacdLen*1.3),MacdLen*3) ;
uMacd = Xaverage(uDif,MacdLen) ;
uOsc = uDif-uMacd ;

// ************ 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) ;

// ************ KD *****************
vars:vKA(0),vKB(0),vDA(0),vDB(0),vJA(0),vJB(0),DivSlowKCondA(false),DivSlowKCondB(false) ;
vKA = SlowK(LenA2) ;
vDA = SlowD(LenA2) ;
vJA = 3*vDA-2*vKA ;
vKB = FastK(LenB2) ;
vDB = FastD(LenB2) ;
vJB = 3*vDB-2*vKB ;

// ************ for week High/Low box *****************
Vars:WeekLong(5),WeekShort(5),WH(0),WL(0),HLWeek(0),WCount(0),WBox33(0),WBox67(0) ;
WH = 近週高點 ;
WL = 近週低點 ;

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

// ************* Time Set Up *************
TimeOK = ((time >= 0900 and time <= 1200) or time <= 0300) ;

// ************* Entry/Exit Condition Set Up *************
EntryExitCond = BarsSinceExit(1) > MinList(LenA1,LenB1) ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = (WH+WL)*0.5  ;
ShortPrice = NL-Range  ;

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

// ******** Slow KD Entry ***********
vars:SKbuy(false),SKshort(false) ;
SKbuy = vKA > vDA and vKA > 55;
SKshort = vKB < vDB and vKB < 45;

  if TimeOK then begin
      if MP <> 1 and SKbuy and Low > Low[LenA1*0.5] and Close < BuyPrice then 
         Buy ("LE_SKT") LotNum Contracts next bar at BuyPrice stop;
      if MP <> -1 and SKshort and High < High[LenB1*0.5] and Close > ShortPrice then 
         Sellshort ("SE_SKT") LotNum Contracts next bar at ShortPrice stop;
   end ;


// ***** Macd historism height

   if EntryExitCond then begin
      if MP <> 1 and AbsValue(uOsc-uOsc[1]) > AbsValue(uOsc[1])*2.5 and uOsc > RatioL-10 and HBarPos > BarPass*0.6 then 
         Buy ("LE_UOsc2") LotNum Contracts next bar at High+Range stop ;
      if MP <> -1 and AbsValue(uOsc-uOsc[1]) > AbsValue(uOsc[1])*2.5 and uOsc < RatioS-10 and LBarPos > BarPass*0.6 then 
         SellShort ("SE_UOsc2") LotNum Contracts next bar at Low-Range 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 ~ 2023 交易成本 交易量*0.1%








沒有留言:

張貼留言