Peaks & Bottoms Detector

stable
By HashSwingBagholder in Other Published May 2024 👁 762 views 💬 1 comments

Description

Simple algo that detects unusual price moves. Outputs 1 or 0 or -1, is means price is likely at its peak, -1 means its likely at the bottom. Returned 0 means nothing unusual is happening. Have fun :D
HaasScript
-- Author: HashSwingBagholder

DefineCommand('PeaksBottomsDetector', 'Detect local peaks and bottoms in price.')
local thresholdHigh = DefineParameter(NumberType, 'thresholdHigh', 'higherthreshold (for tops)', false, 15)
local thresholdLow = DefineParameter(NumberType, 'thresholdLow', 'lower threshold (for bottoms)', false, 15)
local deviationRatio = DefineParameter(NumberType, 'deviationRatio', 'devation ratio', false, 0.15)
local len = DefineParameter(NumberType, 'len', 'length of SMA', false, 15)
local plot = DefineParameter(BooleanType, 'plot', 'draw plot', false, true)
local plotNum = DefineParameter(NumberType, 'plotNum', 'plot number', false, 99)
local label = DefineParameter(NumberType, 'label', 'label', false, 'Peaks & Bottoms')
local peakColor = DefineParameter(NumberType, 'peakColor', 'peak color', false, Green)
local bottomColor = DefineParameter(NumberType, 'bottomColor', 'bottom color', false, Red)

local getPeaksBottoms = function(thresholdHigh, thresholdLow, deviationRatio, len, plot)
    local close_data = ClosePrices()
    local length = len
    local thresholdHi = thresholdHigh
    local thresholdLo = -thresholdLow
    local deviation_ratio = deviationRatio

    local meanPrice = SMA(close_data[1], length)
    local stdDevPrice = STDDEV(close_data[1], length, deviation_ratio)

    -- Calculate Z-score
    local zScore = (close_data[1] - meanPrice) / stdDevPrice

    -- Define conditions for price pumps
    local isPricePump = zScore > thresholdHi

    local isPriceDump = zScore < thresholdLo

    local isNeutral = zScore >= thresholdLo and zScore <= thresholdHi
    
    local indicator = (isPricePump) and 1 or (isPriceDump and -1 or (isNeutral and 0))

    if plot then
        local lineId = Plot(plotNum, label, indicator, peakColor)
        PlotDoubleColor(lineId, 0, bottomColor)
    end

    return indicator

end

local res = getPeaksBottoms(thresholdHigh, thresholdLow, deviationRatio, len, plot)
DefineOutput(NumberType, res)

1 Comment

Sign in to leave a comment.

1
1inchIntradayDipper about 2 years ago

I have copied and pasted this script, but nothing happens in the backtests, not even 1 entry. (??) Am I doing something wrong?