[kob] Advanced Mad Hatter Indicators:BBands

0 257 Views No Comments 6 months ago
  • Added Input Menu Groups, InputInterval [kob]
    MadHatterBBandsv2 Added Input Menu Groups And Indicator Timeframe [kob] – Custom signal implementation for Mad Hatter Extended v3.1 / Mad Hatter Basic

    • This topic was modified 5 months, 2 weeks ago by kobalt.
    HaasScript Code
    -- Define command
    DefineCommand("MadHatterBBandsv2", "Custom BBands signal implementation for MadHatter")
    -- Added Input Menu Groups  [kob]
    -- Define command parameters.
    local chartIndex = DefineParameter(NumberType, 'chartIndex', 'The index on which to chart', true, 0, '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')
    
    DefineIntervalOptimization(interval)
    
    -- Input fields for the indicator. Add the name in front.
    InputGroupHeader('MadHatter BBands '..name)
    local interval = InputInterval('        BBands Interval', 1, 'The timeframe for MadHatter BBands.', 'MHB BB Settings')
    local period = Input('       BBands Length', 12, {group = "MHB BB Settings"}) 
    local devUp =  Input('    BBands DevUp', 2, {group = "MHB BB Settings"}) 
    local devDn = Input('    BBands DevDown', 2, {group = "MHB BB Settings"}) 
    local maType =  InputMaTypes('    BBands MA Type', 'sma', {group = "MHB BB Settings"}) 
    local deviation = Input('   BBands Deviation', 0.2, {group = "MHB BB Settings"})  
    local requireFCC = Input('  Require FCC', false, {group = "MHB BB Settings"}) 
    local resetMiddle = Input(' Reset Middle', false, {group = "MHB BB Settings"}) 
    local allowMidSells = Input('Allow Mid Sells', false, {group = "MHB BB Settings"}) 
    
    -- Calculate the indicator
    local prices = ClosePrices(interval)
    local open = OpenPrices(interval)
    
    local bbandsData = BBANDS(prices, period, devUp, devDn, maType)
    local upper = bbandsData[1]
    local middle = bbandsData[2]
    local lower = bbandsData[3]
    
    PlotBBandsChart(chartIndex, 'MHB BBands', upper, middle, lower)
    
    local wentAbove = Load('wentAbove', false)
    local wentBelow = Load('wentBelow', false)
    local result = SignalNone
    
    if prices < lower then
        wentBelow = true
        if resetMiddle == false then
            wentAbove = false
        end
    end
    
    if prices > lower and wentBelow then
        if requireFCC then
            if open > lower then
                result = SignalBuy
            end
        else
            result = SignalBuy
        end
    
        if resetMiddle == false then
            wentBelow = false
        end
    end
    
    if resetMiddle and prices > middle and wentBelow then
        wentBelow = false
    end
    
    if prices > upper then
        if resetMiddle == false then
            wentBelow = false
        end
        wentAbove = true
    end
    
    if prices < upper and wentAbove then
        if requireFCC then
            if open < upper then
                result = SignalSell
            end
        else
            result = SignalSell
        end
    
        if resetMiddle == false then
            wentAbove = false
        end
    end
    
    if resetMiddle and prices < middle and wentAbove then
        wentAbove = false
    end
    
    if allowMidSells and prices > middle then
        result = SignalSell
    end
    
    local finalSignal = SignalNone
    if open < prices and result == SignalBuy then
        finalSignal = SignalBuy
    elseif result == SignalSell then
        finalSignal = SignalSell
    end
    
    Save('wentAbove', wentAbove)
    Save('wentBelow', wentBelow)
    
    --Return the custom signal.
    DefineOutput(EnumType, finalSignal, '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