percentageLib
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © crypto1-ejarehkhodro

//@version=5

// @description : every thing need anout percentage
// @useage : import crypto1-ejarhkhodro/percentageLib/1.0 as percentageLib
library("percentageLib")
error(txt) =>
    // Create label on the first bar.
    var lbl = label.new(bar_index, na, txt, xloc.bar_index, yloc.price, color(na), label.style_none, color.gray, size.large, text.align_left)
    // On next bars, update the label's x and y position, and the text it displays.
    label.set_xy(lbl, bar_index, ta.highest(10)[1])
    label.set_text(lbl, txt)
    
    
    
// @function : get percentage change of of two value
// @param entry : value of entry price
// @param exit : value of exit price
// @returns : negative or positive value
export getPercentage(float entry,float exit,bool ignoreError = false) =>
    var float percentage = 0 
    if entry!= 0
    	percentage := ((exit - entry) / entry ) * 100
    else
        if ignoreError == false
            error("Error : getPercentage(float entry,float exit) Entry can not price is zero!")
        percentage := 0
    percentage
    

// @function : apply percentage change on value decrease or increase
// @param price : value of price
// @param percentage : percentage change can be negative or positive 
// @returns : return only positive value
export applyPercentageNoAddUp(float price,float percentage) =>
	((price * percentage)/100)

	
// @function : apply percentage change on value decrease or increase
// @param price : value of price
// @param percentage : percentage change can be negative or positive 
// @returns : return only positive value
export applyPercentageAddUp(float price,float percentage) =>
	price +  applyPercentageNoAddUp(price,percentage)
	
	
	
// @function : get percentage (positive or negative) and return the percentage need to back to previous price
// @param percentage : percentage change can be negative or positive 
// @returns : return positive/negative value
// @example : reversePercentage(-10) =>11.11111111111111111111111 , reversePercentage(10) =>9.0909090909090909
export reversePercentage(float percentage) =>
    (100 * percentage)/(100 + percentage)
//plot(reversePercentage(10))
//plot(reversePercentage(-10))



// @function : get two prices and return the percentage need to back to previous price
// @param price : value of price
// @param percentage : percentage change can be negative or positive 
// @returns : return only positive value
// @example : getReversePercentage(100,90) =>11.11111111111111111111111
export getReversePercentage(float entry,float exit) =>
    reversePercentage(getPercentage(entry,exit))
//plot(getReversePercentage(100,90))
//plot(getReversePercentage(100,110))





// @function : get array of percentage change and return final percentage change 
// @param percentage : array of percentage change can be positive and negative
// @returns : return negative or positive percentage value
// @example : following

    //var float[] a = array.new_float()
    //var bool doOnce = true
    //if doOnce == true
    //    array.push(a,50)
    //    array.push(a,20)
    //    array.push(a,10)
    //    array.push(a,-10.0)
    //    array.push(a,-40.0)
    //    doOnce := false
    //plot(multipeBarTotalPercentage(a))
    //draw 6.92 line
export multipeBarTotalPercentage(float[] percentages) =>
    float multipeBarPercentage = 0
    for i = 0 to array.size(percentages) - 1
        multipeBarPercentage := multipeBarPercentage + array.get(percentages,i) + applyPercentageNoAddUp(multipeBarPercentage,array.get(percentages,i))
    multipeBarPercentage

