Skip to content
TheChatArchiveThe Chat Archive
Amazon Novaamazon-nova-experimental-chat-05-14Hugging 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. Amazon Nova Assistant

    Certainly! Let's address the issue with your thinkorswim study. The problem lies in the conditions used to define peaks and troughs. Your current conditions only check that the current high or low is the highest or lowest within the last length candles, but they don't ensure there are no higher/lower peaks/troughs within length candles on either side. To fix this, you need to look both forward and backward.

    Here is the corrected script:

    #Inputs
    input length = 9;
    
    # Helper functions to find the highest high and lowest low within a range
    def HighestHigh(price, period) {
        def hh = Double.NEGATIVE_INFINITY;
        for i = 0 to period {
            if price[i] > hh {
                hh = price[i];
            }
        }
        return hh;
    }
    
    def LowestLow(price, period) {
        def ll = Double.POSITIVE_INFINITY;
        for i = 0 to period  {
            if price[i] < ll {
                ll = price[i];
            }
        }
        return ll;
    }
    
    
    # Define peak and trough conditions
    def isPeak = high == HighestHigh(high, length *2 + 1)[length];
    def isTrough = low == LowestLow(low, length *2 + 1)[length];
       
    
    # 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);
    

    Explanation:

    1. Helper Functions:
      • HighestHigh(price, period): This function iterates through the last period candles to find the highest high.
      • LowestLow(price, period): This function iterates through the last period candles to find the lowest low.
    2. Peak and Trough Conditions:
      • isPeak: Checks if the current high is the highest high within a range of length * 2 + 1 candles centred around the current candle. This ensures there are no higher highs within length candles on either side.
      • isTrough: Checks if the current low is the lowest low within a range of length * 2 + 1 candles centred around the current candle. This ensures there are no lower lows within length candles on either side.
    3. Plotting:
      • Peak: Plots a green wedge up if a peak is detected.
      • Trough: Plots a red wedge down if a trough is detected.

    This script will now only show peaks and troughs if there are no higher/lower peaks/troughs within the specified length on either side.

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