Ichimoku Library
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © boitoki

//@version=5

// @description Ichimoku Kinko Hyo library
library("Ichimoku", overlay=true)

// Functions
donchian (_periods, _high, _low) => math.avg(ta.lowest(_low, _periods), ta.highest(_high, _periods))

ichimoku (_coversion=9, _base=26, _leading_span2=52, _displacement1=26, _displacement2=26, _high = high, _low = low) =>
    conversion      = donchian(_coversion, _high, _low)
    base            = donchian(_base, _high, _low)
    lead1           = math.avg(conversion, base) 
    lead2           = donchian(_leading_span2 - 1, _high, _low)
    leading_offset  = _displacement1 - 1
    lagging_offset  = -(_displacement2 + 1)
    
    [conversion, base, lead1, lead2, leading_offset, lagging_offset]

// Inputs
i_conversion        = input.int(9, minval=1, title="Conversion Line")
i_base              = input.int(26, minval=1, title="Base Line")
i_leading_span2     = input.int(52, minval=1, title="Leading Span 2")
i_displacement1     = input.int(26, minval=1)
i_displacement2     = input.int(26, minval=1)

// @function: Calculate the Ichimoku Kinko Hyo values
// @param conversion Conversion line' periods
// @param base Base line's periods
// @param lead 2nd Leading line's periods
// @param displacement1 Leading line's offset
// @param displacement2 Lagging line's offset
// @returns [conversion_line, base_line, leading_span1, leading_span2, leading_offset, lagging_offset]
export calc (int conversion, int base, int lead, int displacement1, int displacement2) => ichimoku(conversion, base, lead, displacement1, displacement2)

export calc (int conversion, int base, int lead, int displacement1, int displacement2, float _high, float _low) => ichimoku(conversion, base, lead, displacement1, displacement2, _high, _low)



// Calculating
use_roc = input.bool(false)
roc_len = input.int(1)
smooth = input.int(1)
use_heikin = input.bool(true)
[hopen, hclose] = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, [open, close])
myhigh = use_heikin ? math.max(hopen, hclose, high) : high
mylow  = use_heikin ? math.min(hopen, hclose, low ) : low
[conversion, base, lead1, lead2, leading_offset, lagging_offset] = calc(i_conversion, i_base, i_leading_span2, i_displacement1, i_displacement2, myhigh, mylow)

roc = use_roc ? ta.sma(ta.roc(math.avg(hopen, hclose), roc_len), smooth) : 0

// Plotting
plot(conversion - roc, color=color.orange, title="Conversion Line")
plot(base - roc, color=color.blue, title="Base Line")
plot(close, offset=lagging_offset, color=color.maroon, title="Lagging Span")
p1 = plot(lead1, offset=leading_offset, color=color.green, title="Leading Span 1")
p2 = plot(lead2, offset=leading_offset, color=color.red,  title="Leading Span 2")

//plot(lead1[leading_offset], color=color.green, title="Leading Span 1")
//plot(lead2[leading_offset], color=color.red,  title="Leading Span 2")

fill(p1, p2, color=lead1 > lead2 ? color.new(color.green, 84) : color.new(color.red, 84), title="Cloud")


