[pshaiCmd] HighSpeedPrices

1 338 Views No Comments 7 months ago
  • HighSpeedPrices – Returns OHLCV data arrays with latest ticker data added. A custom command to keep you in the now.

    UPDATE: Added HL, HLC and OHLC data.

    • This topic was modified 6 months, 4 weeks ago by pshai.
    HaasScript Code
    DefineCommand('HighSpeedPrices', 'Returns OHLCV data arrays with latest ticker data added')
    local interval = DefineParameter(NumberType, 'interval', 'Price data interval', false, 0, 'InputInterval, Number')
    local market = DefineParameter(StringType, 'market', 'Market for data', false, PriceMarket(), 'PriceMarket, InputMarket, InputAccountMarket')
    local hlcStyle = DefineParameter(BooleanType, 'hlcStyle', 'When enabled, the data returned will be adjusted for HLC instead of OHLC. Meaning that the OHL data can change.', false, false, 'True, False, Input')
    local roundDown = function(x, a)
        return x - (x % a)
    if interval == 0 then interval = CurrentInterval() end
    local time = roundDown(Time(), interval * 60)
    local time2 = roundDown(Time(), 60)
    local cp = CurrentPrice(market)
    local fo = Load('fo')
    local fh = Load('fh')
    local fl = Load('fl')
    local fv = Load('fv')
    if Load('pt', 0) != time then
        fo = cp.open
        fh = cp.high
        fl = cp.low
        fv = cp.volume
        Save('pt', time)
        Save('pt2', time2)
        fh = ArrayGet(Max(fh, cp.high), 1)
        fl = ArrayGet(Min(fl, cp.low), 1)
        if Load('pt2', 0) != time2 then
            fv = fv + cp.volume
            Save('pt2', time2)
    Save('fo', fo)
    Save('fh', fh)
    Save('fl', fl)
    Save('fv', fv)
    local o = OpenPrices(interval, true, market, hlcStyle)
    local h = HighPrices(interval, true, market, hlcStyle)
    local l = LowPrices(interval, true, market, hlcStyle)
    local c = ClosePrices(interval, true, market, hlcStyle)
    local hl = HLPrices(interval, true, market, hlcStyle)
    local hlc = HLCPrices(interval, true, market, hlcStyle)
    local ohlc = OHLCPrices(interval, true, market, hlcStyle)
    local v = GetVolume(interval, true, market, hlcStyle)
    o = ArrayUnshift(o, fo)
    h = ArrayUnshift(h, fh)
    l = ArrayUnshift(l, fl)
    c = ArrayUnshift(c, cp.close)
    hl = ArrayUnshift(hl, (fh + fl) / 2)
    hlc = ArrayUnshift(hlc, (fh + fl + cp.close) / 3)
    ohlc = ArrayUnshift(ohlc, (fo + fh + fl + cp.close) / 4)
    v = ArrayUnshift(v, fv)
    local result = {
        open = o,
        high = h,
        low = l,
        close = c,
        hl = hl,
        hlc = hlc,
        ohlc = ohlc,
        volume = v
    DefineOutput(ListDynamicType, result)
    DefineOutputIndex(1, ListNumberType, 'open', 'Open prices')
    DefineOutputIndex(2, ListNumberType, 'high', 'High prices')
    DefineOutputIndex(3, ListNumberType, 'low', 'Low prices')
    DefineOutputIndex(4, ListNumberType, 'close', 'Close prices')
    DefineOutputIndex(5, ListNumberType, 'hl', 'Mid-prices')
    DefineOutputIndex(6, ListNumberType, 'hlc', 'Typical prices')
    DefineOutputIndex(7, ListNumberType, 'ohlc', 'Average prices')
    DefineOutputIndex(8, ListNumberType, 'volume', 'Volumes')
Login or Register to Comment

Unlock your crypto trading potential

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

Join for Free