[pshaiCmd] EasyStochRSI

0 155 Views No Comments 7 months ago
  • Easy version of custom implementation of Stoch-RSI with a bit more settings available.

    • This topic was modified 7 months ago by pshai.
    HaasScript Code
    -- Define command
    DefineCommand("EasyStochRSI", "Easy version of custom implementation of Stoch-RSI by pshai")
    
    -- Define command parameters.
    local chartIndex = DefineParameter(NumberType, 'chartIndex', 'The index on which to chart', true, 1, 'Number')
    local name = DefineParameter(StringType, 'name', 'Unique name of the indicator.', false, '', 'Text')
    local interval = DefineParameter(NumberType, 'interval', 'Used interval for price data. Default is 0 and the main interval will be used.', false, 0, 'Number,InputInterval')
    
    -- This will improve our backtest speed when using the command.
    DefineIntervalOptimization(interval)
    
    -- Input fields for the indicator.
    InputGroupHeader('EasyStochRSI '..name..' Settings')
    --local length = Input('RSI Length', 9)
    
    local sourceOptions = {
        close = "Close",
        hl = "HL/2",
        hlc = "HLC/3",
        ohlc = "OHLC/4"
    }
    
    local rsiPeriod = Input("RSI Period", 14)
    local stochPeriod = Input("Stoch Period", 9)
    local smoothingK = Input("Smooth %K Period", 3)
    local smoothingD = Input("Smooth %D Period", 3)
    local oversold = Input("Oversold", 30)
    local overbought = Input("Overbought", 70)
    local rsiSource = InputOptions("RSI Source", sourceOptions.close, sourceOptions)
    local source = ClosePrices
    
    if rsiSource == sourceOptions.close then source = ClosePrices end
    if rsiSource == sourceOptions.hl then source = HLPrices end
    if rsiSource == sourceOptions.hlc then source = HLCPrices end
    if rsiSource == sourceOptions.ohlc then source = OHLCPrices end
    
    local rsi = RSI(source(), rsiPeriod)
    local stoch = STOCH(rsi, rsi, rsi, stochPeriod, smoothingK, smoothingD)
    
    -- Determine the signal.
    local signal = SignalNone
    
    if stoch.slowK < oversold then
        signal = SignalBuy
    elseif stoch.slowK > overbought then
        signal = SignalSell
    end
    
    
    -- Plot the indicator Use the chartIndex parameter.
    PlotHorizontalLine(chartIndex, "Oversold", Green, oversold, Dashed)
    PlotHorizontalLine(chartIndex, "Overbought", Red, overbought, Dashed)
    Plot(chartIndex, '%K', stoch.slowK, White)
    Plot(chartIndex, '%D', stoch.slowD, Yellow)
    
    
    --Return the custom signal.
    DefineOutput(EnumType, signal, 'Signal result', 'TradeBotContainer, IndicatorContainer, Signal Helpers')
    
Login or Register to Comment

Unlock your crypto trading potential

Create a free account and enjoy everything we have to offer.

Join for Free