Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-opus-4-20250514-thinking-16k· AnthropicHugging Face Arena Expert 5K

In thinkorswim, this study is not working properly. It is showing too many peaks and…

  1. User

    In thinkorswim, this study is not working properly. It is showing too many peaks and troughs. It should only show is there is no higher/lower peak/trough within 'length' candles on either side:

    #Inputs input length = 9;

    Define peak and trough conditions

    def isPeak = high >= Highest(high, length) and high >= Highest(high, length)[-1]; def isTrough = low <= Lowest(low, length) and low <= Lowest(low, length)[-1];

    Plot peak and trough markers

    plot Peak = if isPeak then high else Double.NaN; Peak.SetDefaultColor(Color.GREEN); Peak.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);

    plot Trough = if isTrough then low else Double.NaN; Trough.SetDefaultColor(Color.RED); Trough.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);

  2. Claude Assistant

    The issue with your code is that it's not properly checking for peaks and troughs on both sides of the current candle. Your current logic only looks backward and one bar forward, but you need to check length bars in both directions.

    Here's the corrected code:

    #Inputs
    input length = 9;
    
    # Define peak condition - current high must be >= all highs within 'length' bars on either side
    def isPeak = fold i = 1 to length + 1 
                 with p = true 
                 do p and high >= GetValue(high, i) and high >= GetValue(high, -i);
    
    # Define trough condition - current low must be <= all lows within 'length' bars on either side  
    def isTrough = fold j = 1 to length + 1 
                   with t = true 
                   do t and low <= GetValue(low, j) and low <= GetValue(low, -j);
    
    # Plot peak and trough markers
    plot Peak = if isPeak then high else Double.NaN;
    Peak.SetDefaultColor(Color.GREEN);
    Peak.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
    
    plot Trough = if isTrough then low else Double.NaN;
    Trough.SetDefaultColor(Color.RED);
    Trough.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
    

    The key changes:

    1. Using fold loops: This allows us to check each bar within the specified range on both sides
    2. GetValue(high, i): Gets the high value i bars ago (positive i = past)
    3. GetValue(high, -i): Gets the high value i bars ahead (negative i = future)
    4. Checking both directions: The condition ensures the current bar is the highest/lowest compared to all bars within length distance on both sides

    This will significantly reduce the number of peaks and troughs shown, as it now properly identifies only the true local extremes within the specified window.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.