[pshaiCmd] Heikin-Ashi prices

0 230 Views No Comments 3 months ago
  • Custom command that creates all Heikin-Ashi prices.
    And since loops can be slow, you can optimize and set the amount of data you want it to process and output with [dataLimit].

    Usage testample (set Price Chart Style to Heikin-Ashi):

    -- 0 interval will use current interval, limiting processing to 10 candles
    local prices = CC_GetHeikinAshiPrices(0, 10)
    
    Plot(0, 'o', prices.heikinOpen, Blue)
    Plot(0, 'h', prices.heikinHigh, Red)
    Plot(0, 'l', prices.heikinLow, Green)
    Plot(0, 'c', prices.heikinClose, Purple)
    HaasScript Code
    DefineCommand('GetHeikinAshiPrices', 'Returns all Heikin Ashi prices')
    
    local interval = DefineParameter(NumberType, 'interval', 'Interval', false, 0, 'InputInterval, Number')
    local dataLimit = DefineParameter(NumberType, 'dataLimit', 'Optimization parameter. Use this to limit the amount of data to your need. Leaving this to default value will process as many Heikin Ashi prices as possible on each update cycle.', false, 0, 'Number')
    
    DefineIntervalOptimization(interval)
    
    local o = OpenPrices(interval)
    local h = HighPrices(interval)
    local l = LowPrices(interval)
    local c = ClosePrices(interval)
    
    local len = dataLimit == 0 and #c or dataLimit
    
    local ho, hh, hl, hc = {}, {}, {}, {}
    local o_1, h_1, l_1, c_1
    
    for i = len, 1, -1 do
        o_1 = ArrayGet(o, i)
        h_1 = ArrayGet(h, i)
        l_1 = ArrayGet(l, i)
        c_1 = ArrayGet(c, i)
    
        hc = ArrayUnshift(hc, (o_1 + h_1 + l_1 + c_1) / 4)
        ho = ArrayUnshift(ho, i >= len and ArrayGet(hc, 1) or (ArrayGet(hc, 2) + ArrayGet(ho, 1)) / 2)
        hh = ArrayUnshift(hh, ArrayGet(Max(ArrayGet(hc, 1), ArrayGet(ho, 1), h_1), 1))
        hl = ArrayUnshift(hl, ArrayGet(Min(ArrayGet(hc, 1), ArrayGet(ho, 1), l_1), 1))
    end
    
    local result = {
        hopen = ho,
        hhigh = hh,
        hlow = hl,
        hclose = hc
        }
    
    DefineOutput(ListDynamicType, result, 'The prices')
    DefineOutputIndex(1, ListNumberType, 'heikinOpen', '')
    DefineOutputIndex(2, ListNumberType, 'heikinHigh', '')
    DefineOutputIndex(3, ListNumberType, 'heikinLow', '')
    DefineOutputIndex(4, ListNumberType, 'heikinClose', '')
Login or Register to Comment

Unlock your crypto trading potential

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

Join for Free