Search code examples
pine-scriptpine-script-v5

How to access data from strategy()


I'm trying to access data from the strategy() function to save this in a variable so I can use this in the script for plotting and or table data. I don't know if this is possible?

//@version=5
strategy("My strategy", overlay=true, margin_long=25, margin_short=25)

leverageLong = 100 / margin_long
leverageShort = 100 / strategy.margin_short

I tried both margin_long & strategy.margin_short. Is there a way of accessing this data?


Solution

  • In pinescript, the margin_long and margin_short is a fixed percentage.
    You can' retrieve it once declare in the 'strategy' statement.
    If you want it in a variable, that you will be able to change in the parameter panel on your charte, you can do this :

    marginLong = input.float(25,"Margin Long")
    marginShort = input.float(25,"Margin Short")
    strategy("My strategy", overlay=true, margin_long=marginLong, margin_short=marginShort)
    

    This way, you will be able to use it after in your code.