2023年11月15日 星期三

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

EasyTrader 比特幣測試 01

2023 年初至今比特幣漲幅超過100% ,而全球最大基金管理公司貝萊德(BlackRock),在今年(2023)6月15日向美國證券交易委員會(SEC)申請現貨比特幣ETF。其他包括富達、WisdomTree和VanEck也搭上這波風潮,申請現貨ETF的趨勢,成為今年以來幣圈最多人關注的議題。而近期的比特幣在牛市中快速的拉升與回調,對於主觀交易者而言 實是對是否眼明手快的最佳考驗 ,對量化程式交易者而言,透過策略自動下單,則免除了盯盤的痛苦 !

 MultiCharts 12 專業版提供了串接幣安交易的功能,我們又可以在熟悉的交易平台上開發一些比特幣的交易策略,有了歷史資料 以及在QM裡做好相關的設定 接下來的策略開發就要請出 
㊣㊣㊣ 策略程式產生器 ㊣㊣㊣ 

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

//計算近3週高低區間價格
// ************3 Week High or Low *****************
vars:WeekH(High),WeekL(Low),WeekHL(0),WeekHPB(0),WeekLPB(0),Week33(0),Week67(0) ;
WeekH = MaxList(HighW(0),HighW(1),HighW(2)) ;
WeekL = MinList(LowW(0),LowW(1),LowW(2)) ;
WeekHL = WeekH - WeekL ;
Week33 = WeekL+WeekHL*0.33 ;
Week67 = WeekL+WeekHL*0.67 ;
WeekHPB = _BarsLast(High = WeekH) ;
WeekLPB = _BarsLast(Low = WeekL) ;

//計算波動率
// ************ Volatility ExPansion System  *****************
vars: VED(1),DBody(1) ;
VED = HighD(1)-LowD(1) ;
if absvalue(CloseD(1)-OpenD(1)) <> 0 and VED <> 0 then DBody = absvalue(CloseD(1)-OpenD(1))/VED ;

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

// ************* Entry/Exit Condition Set Up *************
EntryExitCond = (EntriesToday(Date)=0 and ExitsToday(Date)=0) ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = CloseD(1)+MaxList(VED*FracA/6,21) ;
ShortPrice = OpenD(0)+MaxList(TrueRange*RatioS/10,6) ;

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

vars: NBarHigh(0),NBarLow(0),PeriodRangeA(0),PeriodRangeB(0) ;

NBarHigh = Average(High,NBarL)+AvgTrueRange(NBarL*0.5) ;
NBarLow = Average(Low,NBarS)-AvgTrueRange(NBarS*0.5) ;

PeriodRangeA = Highest(High,LenA1)-Lowest(Low,LenA1) ;
PeriodRangeB = Highest(High,LenB1)-Lowest(Low,LenB1) ;

Condition21 = Average(AvgPrice,LenA1) > Average(AvgPrice,IntPortion(LenA1*FracA)) ;
Condition22 = Average(AvgPrice,LenB1) < Average(AvgPrice,IntPortion(LenB1*FracB)) ;


   if EntryExitCond  then begin

//近期高低區間價大於設定範圍 且收盤價大於均高價+ATR 且短均線大於長均線 
則突破設定價位做多
      If MP <> 1 and PeriodRangeA > Close*RatioL/100 and Close > NBarHigh and Close < BuyPrice and Condition21 then  Buy ("LE_3Line") LotNum Contracts next bar at BuyPrice stop ;

//近期高低區間價大於設定範圍 且收盤價小於均低價-ATR  且短均線小於長均線 
則跌破設定價位做空
      If MP <> -1 and PeriodRangeB > Close*RatioS/100 and Close < NBarLow and Close > ShortPrice and Condition22 then  SellShort ("SE_3Line") LotNum Contracts next bar at ShortPrice stop ;
   end ;


// *************  WeeK high or low Entry*************

   if EntryExitCond then begin
//連續4根K棒收盤價在近週位階中值上 則突破設定價位做多
    if MP <> 1 and Countif(Close > WeekL+WeekHL*0.5 and Close < WeekL+WeekHL*0.67,4) = 4 and Close < BuyPrice then  Buy ("LE_Week067") LotNum Contracts next bar at BuyPrice stop;

//連續4根K棒收盤價在近週位階中值下 則跌破設定價位做空
    if MP <> -1 and Countif(Close < WeekL+WeekHL*0.5 and Close > WeekL+WeekHL*0.33,4) = 4 and Close > ShortPrice then sellshort ("SE_Week067") 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 ~ 2023 交易成本 交易量*0.1%







2 則留言: