이 전략은 EMA 필터와 세션 시간 프레임과 함께
핵심 논리는 최근 룩백 기간 동안
세부적인 논리:
긴 신호는: inSession true (거래 세션에서) 와 upCloseCount > downCloseCount (더 많은 가까운 촛불) 와 close > ema (EMA보다 높은 닫기 가격) 과 currentSignal가
짧은 신호는: inSession true and downCloseCount > upCloseCount (더 아래로 닫는 촛불) 및 close < ema (EMA보다 낮은 닫는 가격) 및 currentSignal가
해결책:
이 전략은 미리 설정된 거래 세션 내에서,
/*backtest start: 2023-11-26 00:00:00 end: 2023-12-26 00:00:00 period: 1h basePeriod: 15m exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}] */ //@version=5 strategy("Up vs Down Close Candles Strategy with EMA and Session Time Frames", shorttitle="UvD Strat EMA Session", overlay=true) // User input to define the lookback period, EMA period, and session strings for time frames int lookback = input(20, title="Lookback Period") int emaPeriod = input(50, title="EMA Period") string session1 = input("0900-1200", title="Time Frame 1 Session") string session2 = input("1300-1600", title="Time Frame 2 Session") // Calculate the EMA float ema = ta.ema(close, emaPeriod) // State variable to track the current signal var string currentSignal = na // Counting up-close and down-close candles within the lookback period int upCloseCount = 0 int downCloseCount = 0 if barstate.isnew upCloseCount := 0 downCloseCount := 0 for i = 0 to lookback - 1 if close[i] > close[i + 1] upCloseCount += 1 else if close[i] < close[i + 1] downCloseCount += 1 // Define the long (buy) and short (sell) conditions with EMA filter and session time frame bool inSession = time(timeframe.period, session1) or time(timeframe.period, session2) bool longCondition = inSession and upCloseCount > downCloseCount and close > ema and currentSignal != "long" bool shortCondition = inSession and downCloseCount > upCloseCount and close < ema and currentSignal != "short" // Enter or exit the market based on conditions if longCondition currentSignal := "long" strategy.entry("Buy", strategy.long) if shortCondition currentSignal := "short" strategy.entry("Sell", strategy.short) // Exit logic for long and short positions if currentSignal == "long" and strategy.position_size <= 0 strategy.close("Sell") if currentSignal == "short" and strategy.position_size >= 0 strategy.close("Buy") plot(ema, color=color.blue, title="EMA")