# source: https://raw.githubusercontent.com/joocy75-hash/TradingView-Strategy/4fe4fc89d84f2c311fa332d5cd3221760826df2a/freqtrade/user_data/strategies/BollingerBandBounceStrategy.py
"""
Bollinger Band Lower Band Bounce Strategy

Generated by TradingView Strategy Research Lab
Date: 2026-01-19 13:42:17
Original Strategy: Bollinger Band Bounce

Backtest Results:
- Win Rate: 58.0%
- Profit Factor: 1.90
- Max Drawdown: 10.0%
"""

from freqtrade.strategy import IStrategy, IntParameter, DecimalParameter
from pandas import DataFrame
import talib.abstract as ta


class Github_joocy75_hash_TradingView_Strategy__BollingerBandBounceStrategy__20260124_154927(IStrategy):
    """
    Bollinger Band Lower Band Bounce Strategy
    """
    
    # 전략 설정
    INTERFACE_VERSION = 3
    
    # 타임프레임
    timeframe = '4h'
    
    # 리스크 관리
    stoploss = -0.04
    trailing_stop = True
    trailing_stop_positive = 0.01
    trailing_stop_positive_offset = 0.02
    trailing_only_offset_is_reached = True
    
    # ROI 테이블 (시간별 목표 수익률)
    minimal_roi = {
        "0": 0.10,    # 즉시 10% 수익 시 청산
        "30": 0.05,   # 30분 후 5% 수익 시 청산
        "60": 0.025,  # 1시간 후 2.5% 수익 시 청산
        "120": 0.01,  # 2시간 후 1% 수익 시 청산
    }
    
    # 주문 설정
    order_types = {
        'entry': 'limit',
        'exit': 'limit',
        'stoploss': 'market',
        'stoploss_on_exchange': True
    }
    
    # 최적화 가능한 파라미터
    bb_period = IntParameter(10, 40, default=20, space='buy')
    rsi_period = IntParameter(5, 34, default=14, space='buy')
    
    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        지표 계산
        """
        dataframe['bb'] = ta.BBANDS(dataframe["close"], timeperiod=20, nbdevup=2, nbdevdn=2)
        dataframe['rsi'] = ta.RSI(dataframe["close"], timeperiod=14)
        
        return dataframe
    
    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        진입 조건
        """
        dataframe.loc[
            (dataframe['close'] < dataframe['bb_lowerband'])
            & (dataframe['rsi'] < 40)
            ,
            'enter_long'] = 1
        
        return dataframe
    
    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        """
        청산 조건
        """
        dataframe.loc[
            (dataframe['close'] > dataframe['bb_middleband'])
            ,
            'exit_long'] = 1
        
        return dataframe
