Skip to content
The Chat Archive
o1-preview-2024-09-12· OpenAI WildChat-4.8M (AI2)

//@version=5 indicator('Ehlers Decycler', overlay=true, timeframe = '', timeframegaps =…

  1. User

    //@version=5 indicator('Ehlers Decycler', overlay=true, timeframe = '', timeframe_gaps = true)

    length = input.int ( 60, 'Length') src = input.source(close, 'Source')

    clr_mono = input.color(color.blue, 'Colors     ', inline='colors') clr_up = input.color(color.green, '', inline='colors') clr_dn = input.color(color.red, '', inline='colors')

    smart_clr_ON = input.bool(true, 'Colorize.', inline = 'line')

    line_width = input.int(2, 'Line width:  ', minval = 1, maxval=20, inline = 'line')

    //■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

    Decycler(float src, int length) => PIx2_Length = 2 * math.pi / length

    cos = math.cos(PIx2_Length)
    alpha = 0.0
    alpha := cos != 0 ? (cos + math.sin(PIx2_Length) - 1) / cos : nz(alpha[1])
    
    decycler = 0.0
    decycler := alpha / 2 * (src + nz(src[1])) + (1 - alpha) * nz(decycler[1])
    
    decycler
    

    //

    //🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰🀰

    MA_Smart_Color(float ma, bool smart_clr_ON, color color_up, color color_dn, color clr) =>

    color clr_ma = na
    
    clr_ma := switch
        not smart_clr_ON => clr     // Mono color
        ma > ma[1] => color_up      // Up
        ma < ma[1] => color_dn      // Down
        => clr_ma[1]                // The same
    //
    
    clr_ma
    

    //

    //■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

    ma = Decycler(src, length)

    clr_ma = MA_Smart_Color(ma, smart_clr_ON, clr_up, clr_dn, clr_mono)

    plot(ma, 'Decycler', clr_ma, line_width)

    //■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

    EVENT_up = ma > ma[1] and barstate.isconfirmed EVENT_dn = ma < ma[1] and barstate.isconfirmed

    alertcondition(EVENT_up, 'Trend up', 'Trend up') alertcondition(EVENT_dn, 'Trend dn', 'Trend dn')

    //■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 帮我写一个策略

  2. Assistant

    Certainly! I can help you convert your Ehlers Decycler indicator into a trading strategy in Pine Script version 5. This strategy will generate buy and sell signals based on the conditions defined in your indicator.

    Below is the strategy code:

    //@version=5
    strategy('Ehlers Decycler Strategy', overlay=true)
    
    // Inputs
    length       = input.int(60, 'Length')
    src          = input.source(close, 'Source')
    
    clr_mono = input.color(color.blue, 'Colors', inline='colors')
    clr_up   = input.color(color.green, '', inline='colors')
    clr_dn   = input.color(color.red, '', inline='colors')
    
    smart_clr_ON = input.bool(true, 'Colorize', inline = 'line')
    
    line_width = input.int(2, 'Line width', minval = 1, maxval=20, inline = 'line')
    
    // Functions
    Decycler(float src, int length) =>
        PIx2_Length = 2 * math.pi / length
        cos = math.cos(PIx2_Length)
        alpha = 0.0
        alpha := cos != 0 ? (cos + math.sin(PIx2_Length) - 1) / cos : nz(alpha[1])
        decycler = 0.0
        decycler := alpha / 2 * (src + nz(src[1])) + (1 - alpha) * nz(decycler[1])
        decycler
    //
    
    MA_Smart_Color(float ma, bool smart_clr_ON, color color_up, color color_dn, color clr) =>
        color clr_ma = na
        clr_ma := switch
            not smart_clr_ON => clr     // Mono color
            ma > ma[1] => color_up      // Up
            ma < ma[1] => color_dn      // Down
            => clr_ma[1]                // The same
        clr_ma
    //
    
    // Calculate Decycler
    ma = Decycler(src, length)
    
    // Get color for plotting
    clr_ma = MA_Smart_Color(ma, smart_clr_ON, clr_up, clr_dn, clr_mono)
    
    // Plot Decycler
    plot(ma, 'Decycler', color=clr_ma, linewidth=line_width)
    
    // Strategy Conditions
    longCondition = ma > ma[1] and ma[1] <= ma[2] and barstate.isconfirmed
    shortCondition = ma < ma[1] and ma[1] >= ma[2] and barstate.isconfirmed
    
    // Strategy Execution
    if (longCondition)
        strategy.entry("Long", strategy.long)
        strategy.close("Short")
    
    if (shortCondition)
        strategy.entry("Short", strategy.short)
        strategy.close("Long")
    

    Explanation:

    • Indicator to Strategy Conversion: Changed the script declaration from indicator to strategy. This allows us to execute trades based on the conditions defined.

    • Strategy Conditions:

      • Long Entry Condition (longCondition): Occurs when the Decycler value turns upward, indicating a potential uptrend.
        longCondition = ma > ma[1] and ma[1] <= ma[2] and barstate.isconfirmed
        
      • Short Entry Condition (shortCondition): Occurs when the Decycler value turns downward, indicating a potential downtrend.
        shortCondition = ma < ma[1] and ma[1] >= ma[2] and barstate.isconfirmed
        
      • The barstate.isconfirmed ensures that we only consider confirmed bars (not real-time updates).
      • Using ma[1] <= ma[2] and ma[1] >= ma[2] helps to detect the change in direction, reducing false signals.
    • Strategy Execution:

      • Entering a Long Position:

        if (longCondition)
            strategy.entry("Long", strategy.long)
            strategy.close("Short")
        

        Enters a long position and ensures any short positions are closed.

      • Entering a Short Position:

        if (shortCondition)
            strategy.entry("Short", strategy.short)
            strategy.close("Long")
        

        Enters a short position and ensures any long positions are closed.

    • Plotting the Decycler: The line is plotted for visualization purposes, with colors changing based on the direction.

    Notes:

    • Position Size and Risk Management: By default, the strategy uses the default position size settings. You can customize your position sizing and risk management under the "Properties" tab when adding the strategy to your chart.

    • Backtesting and Optimization: It’s recommended to test this strategy over historical data and adjust the length parameter or other settings to optimize performance for your specific trading instrument.

    • Short Selling: Ensure that short selling is enabled in the strategy's properties if you plan to use short positions. If your market or broker doesn't allow short selling, you should remove or comment out the short position code.

    Optional Adjustments:

    • Removing Short Positions: If you don't want to trade short positions, you can adjust the code as follows:

      if (longCondition)
          strategy.entry("Long", strategy.long)
      
      if (shortCondition)
          strategy.close("Long")
      
    • Using crossover and crossunder: The crossover and crossunder functions are typically used for crossing over other series or levels. Since ma and ma[1] are consecutive values, using these functions may not produce meaningful results. The conditions provided aim to detect a change in the direction of ma.

    Testing the Strategy:

    1. Add the Strategy to Your Chart: Copy and paste the strategy code into a new Pine Script editor on TradingView and add it to your chart.
    2. Review Trades: Use the "Strategy Tester" panel to review the performance, list of trades, and other metrics.
    3. Optimize Parameters: Adjust the length input to see how it affects the strategy performance.

    Final Thoughts:

    • The Ehlers Decycler is designed to remove cycles from price data, highlighting the underlying trend.
    • Always test strategies thoroughly before applying them to live trading.
    • Consider incorporating additional filters or confirmation signals to improve the strategy's robustness.

    Let me know if you need further assistance or any modifications to the strategy!