Encoder Decoder
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © f4r33x
//@version=5
// @description Example how to encode some values into float number

library("Encoder\Decoder", overlay = true)
import f4r33x/fast_utils/1 as fu

// @function Encode parameter 
// @returns encoded value
export calctype(string src_type) =>
    int res = switch src_type
        'RMA' => 0
        'SMA' => 1
        'EMA' => 2
        'HMA' => 3
        'SWMA'=> 4
        'VWAP'=> 5
        'VWMA'=> 6
        'WMA' => 7
		//'custom' => 8
    res

// @function Decode parameter 
// @returns decoded value
export calctype(int src_type) =>  
	string res = switch src_type
        0 => 'RMA'  
        1 => 'SMA'  
        2 => 'EMA'  
        3 => 'HMA'  
        4 => 'SWMA' 
        5 => 'VWAP' 
        6 => 'VWMA' 
        7 => 'WMA'  
		//'custom' => 8
    res

// @function Encode parameter 
// @returns encoded value
export srctype(string src_type) =>
    int res = switch src_type
        'open' => 0
        'high' => 1
        'low'  => 2
        'close'=> 3
        'hl2'  => 4
        'hlc3' => 5
        'hlcc4'=> 6
        'ohlc4'=> 7
		//'custom'=> 8
    res

// @function Decode parameter 
// @returns decoded value
export srctype(int src_type) =>
    res = switch src_type
        0 => open 
        1 => high
        2 => low 
        3 => close
        4 => hl2 
        5 => hlc3
        6 => hlcc4
        7 => ohlc4
		//custom
    res

// @function 		Encodes 4 paramters into float number
// @param calc_type 1st paramter to encode (its values defined  in f_calctype functions) max number of values that can be encoded = 100
// @param src_type  2nd paramter to encode (its values defined  in f_src_type functions) max number of values that can be encoded = 100
// @param tf tf	    3rd paramter to encode (may be int number with format.price precision length!!! (i.e.))
// @param length length	4th paramter to encode (may be any int number)
// @returns  float number 
export encode(string calc_type, string src_type, int tf = na, series int length = na) => // encodes 2 two number of digits vals and 2 unknown number of digits vals
    ident = nz(length)*10000+calctype(calc_type)*100+srctype(src_type)+nz(tf/math.pow(10, fu.count_int_digits(tf)))

// @function Decodes 4 paramters into tuple
// @returns  returns tuple [calc_type, src_type, length, tf]
export decode(float ident) =>// decodes 4 vals 
    //tf_round=math.round((ident%1*math.pow(10,8)))/math.pow(10,8)
    
    tf= ident%1 == 0 ? na : str.tonumber(str.substring(str.tostring(math.round((ident%1), 9)), 2, 11))
    //tf = tf_round*(math.pow(10, f_count_floatdigits(tf_round)))
    //tf = f_count_floatdigits(tf_round)
    float src_type = srctype((int(ident)%100))
    string calc_type = calctype(int((int(ident)%1000)*0.01))
    int length = int(ident/10000)
	[calc_type, src_type, length, tf]


ident = encode('EMA', 'close', 12345678, 400)
[calc_type, src_type, length, tf] = decode(ident)

if barstate.islast
	labtext = 'Encoded value: ' + str.tostring(ident, '#.####') + '\n Decoded value: ' + '\n calc_type= ' + calc_type +  '\n src_type= ' + str.tostring(src_type) + '\n length= ' + str.tostring(length) + '\n tf= ' + str.tostring(tf)
	label=label.new(last_bar_index, high, labtext, textalign = text.align_left)
	label.delete(label[1])
