In thinkorswim, this study is not working properly. It is showing too many peaks and…
- 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);
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
lengthcandles, but they don't ensure there are no higher/lower peaks/troughs withinlengthcandles 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:
- Helper Functions:
HighestHigh(price, period): This function iterates through the lastperiodcandles to find the highest high.LowestLow(price, period): This function iterates through the lastperiodcandles to find the lowest low.
- Peak and Trough Conditions:
isPeak: Checks if the current high is the highest high within a range oflength * 2 + 1candles centred around the current candle. This ensures there are no higher highs withinlengthcandles on either side.isTrough: Checks if the current low is the lowest low within a range oflength * 2 + 1candles centred around the current candle. This ensures there are no lower lows withinlengthcandles on either side.
- 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
lengthon either side.- Helper Functions:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.