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

//@version=5

// @description Provides functions for converting input strings 'open','high','low','close','hl2','hlc3','ohlc4','hlcc4' to corresponding source values.
library("srcCalc")

src1_str = input.string('high', 'Source 1', options=['open', 'high', 'low', 'close', 'hl2', 'hlc3', 'ohlc4', 'hlcc4'])
src2_str = input.string('low', 'Source 2', options=['open', 'high', 'low', 'close', 'hl2', 'hlc3', 'ohlc4', 'hlcc4'])
ext_src = input.source(close, 'External Source')
ext_long_op = input.string('>', 'Operator', options=['>','>=','==','<=','<','!='], group='Ext Src Long Condition', inline='l2')
ext_long_val = input.float(0, 'Value', group='Ext Src Long Condition', inline='l2')
ext_short_op = input.string('<', 'Operator', options=['>','>=','==','<=','<','!='], group='Ext Src Short Condition', inline='l3')
ext_short_val = input.float(0, 'Value', group='Ext Src Short Condition', inline='l3')

// @function Converts string to source float value
// @param src String to use (`close` is used if no argument is supplied).
// @returns Returns the float value of the string
export get_src(string src='close') =>
    switch src
        'open' => open
        'high' => high
        'low' => low
        'close' => close
        'hl2' => hl2
        'hlc3' => hlc3
        'ohlc4' => ohlc4
        'hlcc4' => hlcc4
        '' => na
        
// @function Evaluates a conditional expression of 2 values given a string of the conditional operator.
// @param op Conditional operator string to use. (options: '>','>=','==','<=','<','!=')(default: '==')
// @param left_val Left value of expression. (default: float 0.0)
// @param right_val Right value of expression. (default: float 0.0)
// @returns Returns the boolean value of conditional expression
export return_bool(string op='==', float left_val=0.0, float right_val=0.0) =>
    switch op
        '>'     => left_val > right_val
        '>='    => left_val >= right_val
        '=='    => left_val == right_val
        '<='    => left_val <= right_val
        '<'     => left_val < right_val
        '!='    => left_val != right_val
        ''      => na

src1 = get_src(src1_str)
src2 = get_src(src2_str)

ext_longCond = return_bool(ext_long_op, ext_src, ext_long_val)
ext_shortCond = return_bool(ext_short_op, ext_src, ext_short_val)

plot(src1, 'source 1: input', color.red)
plot(src2, 'source 2: input', color.maroon)

bgcolor(ext_longCond ? color.new(color.green,90) : ext_shortCond ? color.new(color.red,90) : na)

// debugging to data window
plotchar(ext_src, 'ext src: input', '')
plotchar(ext_longCond, 'ext_longCond','')
plotchar(ext_shortCond, 'ext_shortCond','')