AP Index - Geomagnetic disturbances
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © firerider

//@version=5
indicator('AP Index')

// from https://www.gfz-potsdam.de/en/kp-index/
varip int[] ap_index = array.from(6, 11, 12, 18, 15, 12, 31, 13, 6, 9, 10, 7, 7, 6, 5, 7, 6, 4, 4, 2, 11, 12, 8, 6, 5, 15, 11, 7, 8, 9, 9, 20, 27, 16, 8, 12, 5, 9, 10, 7, 6, 5, 4, 2, 2, 6, 5, 22, 18, 8, 5, 7, 6, 16, 25, 8, 4, 2, 11, 26, 28, 10, 9, 5, 10, 21, 11, 5, 4, 9, 8, 6, 5, 6, 12, 108, 47, 26, 22, 12, 24, 19, 10, 13, 8, 8, 8, 11, 4, 9, 7, 12, 12, 11, 6, 5, 4, 3, 11, 33, 16, 3, 6, 13, 28, 42, 22, 10, 8, 10, 22, 10, 6, 3, 0, 2, 4, 5, 4, 4, 5, 7, 8, 7, 5, 22, 6, 5, 7, 9, 14, 15, 46, 8, 6, 5, 4, 15, 13, 6, 2, 2, 3, 4, 2, 5, 4, 6, 6, 4, 4, 6, 2, 3, 2, 2, 4, 7, 36, 14, 11, 9, 6, 8, 17, 12, 10, 14, 6, 3, 1, 7, 57, 72, 16, 32, 8, 8, 13, 4, 5, 4, 2, 2, 19, 22, 10, 4, 4, 4, 8, 23, 12, 33, 6, 6, 7, 4, 2, 2, 3, 7, 6, 21, 5, 7, 6, 9, 7, 4, 12, 13, 9, 10, 6, 6, 5, 10, 18, 10, 10, 8, 7, 10, 10, 2, 35, 32, 22, 8, 18, 11, 5, 8, 26, 6, 8, 27, 52, 40, 13, 4, 4, 4, 7, 8, 20, 12, 13, 44, 24, 59, 12, 60, 16, 11, 14, 17, 11, 10, 11, 16, 44, 8, 10, 12, 7, 6, 4, 3, 3, 5, 2, 10, 12, 10, 17, 18, 18, 74, 45, 27, 11, 12, 21, 24, 21, 8, 6, 10, 19, 4, 8, 10, 4, 6, 9, 6, 1, 3, 1, 4, 8, 6, 9, 4, 29, 30, 16, 14, 38, 14, 25, 37, 22, 4, 13, 8, 8, 12, 8, 15, 8, 4, 2, 2, 1, 0, 0, 1, 6, 6, 8, 17, 13, 8, 4, 4, 14, 22, 19, 10, 8, 24, 19, 10, 7, 20, 16, 6, 6, 5, 10, 70, 38, 12, 9, 11, 7, 14, 9, 4, 6, 2, 35, 28, 9, 6, 4, 5, 16, 13, 7, 5, 8, 13, 13, 13, 7, 4, 3, 3, 4, 10, 26, 33, 13, 11, 10, 2, 3, 4, 4, 3, 2, 9, 7, 5, 12, 5, 10, 6, 8, 17, 8, 4, 8, 12, 7, 9, 11, 37, 34, 29, 16, 5, 4, 2, 5, 6, 5, 7, 3, 3, 4, 8, 6, 6, 4, 3, 31, 24, 6, 6, 7, 19, 11, 3, 12, 24, 20, 22, 7, 14, 10, 6, 6, 10, 6, 5, 2, 11, 9, 11, 11, 6, 2, 19, 14, 6, 5, 6, 17, 8, 2, 6, 4, 18, 25, 22, 8, 11, 18, 4, 2, 3, 4, 12, 11, 11, 4, 5, 9, 4, 2, 7, 14, 30, 10, 3, 6, 15, 9, 70, 30, 13, 5, 3, 6, 9, 13, 12, 12, 7, 7, 5, 15, 6, 3, 4, 2, 2, 9, 13, 6, 11, 10, 5, 3, 2, 3, 29, 23, 8, 5, 3, 6, 9, 10, 8, 21, 11, 6, 7, 7, 4, 4, 3, 12, 10, 12, 7, 10, 8, 5, 4, 6, 6, 7, 8, 5, 3, 4, 21, 19, 12, 9, 10, 18, 7, 11, 10, 7, 5, 3, 8, 19, 5, 7, 7, 14, 18, 4, 2, 14, 13, 5, 3, 2, 17, 32, 16, 16, 12, 12, 10, 12, 15, 8, 10, 4, 3, 3, 5, 8, 6, 4, 3, 8, 4, 18, 19, 10, 6, 4, 2, 6, 17, 6, 28, 37, 36, 25, 17, 13, 12, 12, 3, 3, 3, 4, 4, 8, 6, 2, 3, 6, 8, 18, 8, 3, 3, 4, 22, 20, 35, 36, 36, 20, 17, 16, 13, 23, 11, 5, 5, 7, 5, 10, 2, 5, 46, 22, 10, 17, 20, 9, 6, 2, 1, 5, 8, 16, 57, 44, 26, 16, 27, 16, 8, 10, 14, 16, 4, 2, 4, 3, 2, 6, 14, 11, 18, 21, 10, 7, 4, 3, 2, 1, 2, 5, 11, 11, 20, 30, 11, 8, 7, 5, 3, 2, 4, 2, 1, 4, 7, 9, 21, 24, 15, 14, 5, 4, 4, 2, 1, 5, 8, 5, 5, 23, 22, 19, 12, 20, 20, 11, 5, 4, 2, 12, 12, 6, 11, 10, 17, 14, 20, 16, 11, 10, 7, 4, 4, 4, 4, 2, 3, 14, 10, 10, 10, 9, 4, 2, 5, 11, 20, 9, 6, 6, 22, 28, 21, 18, 10, 15, 12, 7, 4, 6, 7, 4, 3, 5, 1, 2, 8, 19, 16, 10, 9, 4, 9, 10, 18, 6, 2, 7, 8, 35, 32, 18, 20, 18, 23, 14, 12, 13, 10, 5, 8, 2, 4, 6, 4, 2, 2, 2, 1, 24, 24, 9, 5, 3, 3, 46, 27, 20, 19, 27, 15, 7, 4, 19, 8, 6, 9, 15, 15, 4, 10, 5, 4, 11, 6, 3, 3, 6, 14, 28, 17, 47, 36, 19, 10, 8, 6, 5, 5, 5, 4, 4, 3, 5, 4, 4, 8, 5, 5, 5, 6, 7, 4, 8, 12, 8, 6, 9, 12, 22, 8, 9, 8, 3, 2, 2, 13, 47, 9, 5, 3, 6, 4, 8, 2, 5, 4, 4, 3, 3, 3, 15, 6, 6, 5, 3, 23, 14, 10, 4, 2, 3, 4, 4, 8, 9, 6, 4, 3, 4, 3, 12, 17, 5, 4, 2, 6, 4, 2, 24, 8, 6, 4, 3, 2, 3, 44, 26, 6, 3, 6, 13, 15, 12, 10, 9, 10, 5, 7, 3, 3, 2, 4, 4, 10, 22, 15, 12, 4, 4, 4, 4, 7, 9, 6, 4, 2, 5, 24, 20, 29, 20, 9, 17, 23, 10, 4, 3, 8, 2, 8, 3, 30, 16, 20, 7, 16, 11, 8, 36, 106, 3, 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, 11, 4, 3, 4, 8, 9, 5, 4, 17, 13, 5, 6, 3, 4, 3, 5, 3, 4, 6, 3, 3, 3, 4, 13, 5, 4, 3, 3, 7, 6, 5, 4, 4, 6, 3, 6, 4, 14, 8, 4, 3, 4, 7, 10, 5, 8, 3, 3, 2, 3, 4, 13, 8, 6, 6, 3, 15, 8, 4, 1, 6, 10, 4, 4, 6, 5, 3, 5, 3, 8, 7, 4, 6, 4, 6, 3, 2, 2, 4, 8, 5, 21, 14, 8, 7, 6, 4, 3, 7, 4, 5, 6, 8, 14, 4, 7, 7, 5, 9, 6, 4, 3, 22, 9, 2, 2, 7, 12, 10, 6)

calculateCandleTimeDiff() =>
    // 2015-01-01 00:00  signal day time serie start.
    referenceUnixTime = 1420066800
    candleUnixTime = time / 1000 + 1
    timeDiff = candleUnixTime - referenceUnixTime
    timeDiff < 0 ? na : timeDiff

getSignalCandleIndex() =>
    timeDiff = calculateCandleTimeDiff()
    // Day index, days count elapsed from reference date.
    candleIndex = math.floor(timeDiff / 86400)
    candleIndex < 0 ? na : candleIndex

getSignal() =>
    // Map array data items indexes to candles.
    int candleIndex = getSignalCandleIndex()
    int itemsCount = array.size(ap_index)
    // Return na for candles where indicator data is not available.
    int index = candleIndex >= itemsCount ? na : candleIndex
    signal = if index >= 0 and itemsCount > 1
        array.get(ap_index, index)
    else
        na
    signal

// Compose signal time serie from array data. 
int SignalSerie = getSignal()
// Calculate plot offset estimating market week open days

plot(series=SignalSerie, style=plot.style_histogram, linewidth=4, color=color.new(color.blue, 0))


