[pshaiCmd] Advanced Stochastics

0 205 Views No Comments 7 months ago
  • Advanced Stochastics by pshai – Multiple outputs for more detailed control.

    HaasScript Code
    DefineCommand('AdvSTOCH', 'Advanced Stochastics by pshai - Multiple outputs for more detailed control.')
    
    -- Command inputs
        local high  = DefineParameter(ListNumberType, 'high', 'High-prices', true, HighPrices(), 'HighPrices, Max, GetHighs')
        local low   = DefineParameter(ListNumberType, 'low', 'Low-prices', true, LowPrices(), 'LowPrices, Min, GetLows')
        local close = DefineParameter(ListNumberType, 'close', 'Close-prices', true, ClosePrices(), 'ClosePrices')
        local fastK = DefineParameter(NumberType, 'fastK', 'Period length of Fast %K', true, 5, 'Input, Number')
        local slowK = DefineParameter(NumberType, 'slowK', 'Period length of Slow %K', true, 2, 'Input, Number')
        local slowD = DefineParameter(NumberType, 'slowD', 'Period length of Slow %D', true, 3, 'Input, Number')
        local name = DefineParameter(StringType, 'name', 'Name for user input', false, 'Advanced Stochastics', 'Input, Text')
    
    -- User inputs
        local k_overbought = Input('1.1. %K Overbought Level', 80, 'Stochastic overbought level value, between 0 and 100.', name)
        local k_oversold = Input('1.2. %K Oversold Level', 20, 'Stochastic oversold level value, between 0 and 100.', name)
        local k_upwardsLookback = Input('1.3. %K Upwards Lookback', 2, 'Lookback value for %K's upward movement definition.', name)
        local k_downwardsLookback = Input('1.4. %K Downwards Lookback', 2, 'Lookback value for %K's downward movement definition.', name)
        local k_crossOverLookback = Input('1.5. %K Cross-over %D Lookback', 5, 'Defines how far back cross-overs are still valid.', name)
        local k_crossUnderLookback = Input('1.6. %K Cross-under %D Lookback', 5, 'Defines how far back cross-unders are still valid.', name)
    
        local d_overbought = Input('2.1. %D Overbought Level', 80, 'Stochastic overbought level value, between 0 and 100.', name)
        local d_oversold = Input('2.2. %D Oversold Level', 20, 'Stochastic oversold level value, between 0 and 100.', name)
        local d_upwardsLookback = Input('2.3. %D Upwards Lookback', 2, 'Lookback value for %K's upward movement definition.', name)
        local d_downwardsLookback = Input('2.4. %D Downwards Lookback', 2, 'Lookback value for %K's downward movement definition.', name)
        local d_crossOverLookback = Input('2.5. %D Cross-over %D Lookback', 5, 'Defines how far back cross-overs are still valid.', name)
        local d_crossUnderLookback = Input('2.6. %D Cross-under %D Lookback', 5, 'Defines how far back cross-unders are still valid.', name)
    
        local accelerationLookback = Input('3.1. Acceleration Lookback', 5, 'Defines how far back acceleration is calculated.', name)
        local decelerationLookback = Input('3.2. Deceleration Lookback', 5, 'Defines how far back deceleration is calculated.', name)
    
    -- Data
        local stoch = STOCH(high, low, close, fastK, slowK, slowD)
    
        local results = {
            -- %K
            K_IsAboveD = false,
            K_IsAboveMid = false,
            K_InTheZone = false,
            K_OverBought = false,
            K_OverSold = false,
            K_Upwards = false,
            K_Downwards = false,
            K_CrossOverD = false,
            K_CrossUnderD = false,
            
            -- %D
            D_IsAboveK = false,
            D_IsAboveMid = false,
            D_InTheZone = false,
            D_OverBought = false,
            D_OverSold = false,
            D_Upwards = false,
            D_Downwards = false,
    
            -- General
            IsAccelerating = false,
            IsDecelerating = false
        }
    
        -- %K
        if stoch.slowK > stoch.slowD then results.K_IsAboveD = true end
        if stoch.slowK > 50 then results.K_IsAboveMid = true end
        if stoch.slowK <= k_overbought and stoch.slowK >= k_oversold then results.K_InTheZone = true end
        if stoch.slowK > k_overbought then results.K_OverBought = true end
        if stoch.slowK < k_oversold then results.K_OverSold = true end
        if IsRising(stoch.slowK, k_upwardsLookback) then results.K_Upwards = true end
        if IsFalling(stoch.slowK, k_downwardsLookback) then results.K_Downwards = true end
        if CrossOver(stoch.slowK, stoch.slowD) or CrossOverSince(stoch.slowK, stoch.slowD) <= k_crossOverLookback then
            results.K_CrossOverD = true end
        if CrossUnder(stoch.slowK, stoch.slowD) or CrossUnderSince(stoch.slowK, stoch.slowD) <= k_crossUnderLookback then
            results.K_CrossUnderD = true end
        
        -- %D
        if stoch.slowD > stoch.slowK then results.D_IsAboveK = true end
        if stoch.slowD > 50 then results.D_IsAboveMid = true end
        if stoch.slowD > d_overbought then results.D_OverBought = true end
        if stoch.slowD < d_oversold then results.D_OverSold = true end
        if IsRising(stoch.slowD, d_upwardsLookback) then results.D_Upwards = true end
        if IsFalling(stoch.slowD, d_downwardsLookback) then results.D_Downwards = true end
        
        -- General
        if IsRising(Abs(stoch.slowK - stoch.slowD), accelerationLookback) then results.IsAccelerating = true end
        if IsFalling(Abs(stoch.slowK - stoch.slowD), decelerationLookback) then results.IsDecelerating = true end
    
    
    DefineOutput(ListDynamicType, results, 'True/False values object.')
    -- %K
    DefineOutputIndex(1, BooleanType, 'K_IsAboveD', 'True if %K is above %D', 'IsTrue, IsFalse, Not')
    DefineOutputIndex(2, BooleanType, 'K_IsAboveMid', 'True if %K is above middle (50)', 'IsTrue, IsFalse, Not')
    DefineOutputIndex(3, BooleanType, 'K_InTheZone', 'True if %K is below overbought and above oversold levels', 'IsTrue, IsFalse, Not')
    DefineOutputIndex(4, BooleanType, 'K_OverBought', 'True if %K is above overbought level', 'IsTrue, IsFalse, Not')
    DefineOutputIndex(5, BooleanType, 'K_OverSold', 'True if %K is below oversold level', 'IsTrue, IsFalse, Not')
    DefineOutputIndex(6, BooleanType, 'K_Upwards', 'True if %K is moving upwards', 'IsTrue, IsFalse, Not')
    DefineOutputIndex(7, BooleanType, 'K_Downwards', 'True if %K is moving downwards', 'IsTrue, IsFalse, Not')
    DefineOutputIndex(8, BooleanType, 'K_CrossOverD', 'True if %K cross over %D', 'IsTrue, IsFalse, Not')
    DefineOutputIndex(9, BooleanType, 'K_CrossUnderD', 'True if %K cross under %D', 'IsTrue, IsFalse, Not')
    
    -- %D
    DefineOutputIndex(10, BooleanType, 'D_IsAboveK', 'True if %D is above %K', 'IsTrue, IsFalse, Not')
    DefineOutputIndex(11, BooleanType, 'D_IsAboveMid', 'True if %D is above middle (50)', 'IsTrue, IsFalse, Not')
    DefineOutputIndex(12, BooleanType, 'D_InTheZone', 'True if %D is below overbought and above oversold levels', 'IsTrue, IsFalse, Not')
    DefineOutputIndex(13, BooleanType, 'D_OverBought', 'True if %D is above overbought level', 'IsTrue, IsFalse, Not')
    DefineOutputIndex(14, BooleanType, 'D_OverSold', 'True if %D is below oversold level', 'IsTrue, IsFalse, Not')
    DefineOutputIndex(15, BooleanType, 'D_Upwards', 'True if %D is moving upwards', 'IsTrue, IsFalse, Not')
    DefineOutputIndex(16, BooleanType, 'D_Downwards', 'True if %D is moving downwards', 'IsTrue, IsFalse, Not')
    
    -- General
    DefineOutputIndex(17, BooleanType, 'IsAccelerating', 'True if momentum is increasing', 'IsTrue, IsFalse, Not')
    DefineOutputIndex(18, BooleanType, 'IsDecelerating', 'True if momentum is decreasing', 'IsTrue, IsFalse, Not')
    
    
Login or Register to Comment

Unlock your crypto trading potential

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

Join for Free