pine-scriptpine-script-v5algorithmic-trading

Calculate Stoploss Percentage in Pinescript


I know how to get the calculate the stoploss 245.6 but I don't know how to get the percentage 2.08% ... Can someone please help me get the formula for the percentage?

enter image description here

I used the following formula to get the stoploss 245.6 pips

pips = (abs(1.18047 - 1.15591) / syminfo.mintick) / 10

Solution

  • The percentage formula can be written in different forms, it is essentially an algebraic equation involving three values that you can turn into a function, for example:

    //@version=5
    indicator("", format = format.percent)
    
    percent(value1, value2) => float(value1)/float(value2) * 100
    
    val1 = 1.15591
    val2 = 1.18047
    
    percentChange = 100 - percent(val1, val2)
    
    plot(percentChange) // 2.08