Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put plot statement inside if statement

Tags:

pine-script

I want to plot the equity curve on the price, to compare the strategy to simple buy and hold. To make the graph useful, the equity curve could either start on the initial equity, or in line with the first price on the graph or no equity curve at all depending on manual input.

Using the code below, I get this:

  • line xx: Cannot use 'plot' in local scope.

  • line xx: Cannot use 'plot' in local scope.

equitycurvetype = input(defval="No", title='Equity Curve Type', options=["No","Yes","Yes same start"])
if equitycurvetype == "Yes" 
    plot(strategy.equity, title="equity", color=color.red, linewidth=2, style=plot.style_areabr)  
if equitycurvetype == "Yes same start" 
    plot(strategy.equity * close[bar_index]/strategy.initial_capital, title="equity", color=color.red, linewidth=2, style=plot.style_areabr)
like image 810
Benster Avatar asked Oct 15 '22 10:10

Benster


2 Answers

plot(Trailingsl, title = "SuperTrend", color = linecolor ,  linewidth = 2, transp = showSuperTrend ? 0 : 100)

Workaround:

transp = showSuperTrend ? 0 : 100
like image 96
gencerinc Avatar answered Oct 21 '22 06:10

gencerinc


Can't plot from if blocks. Also, while using close[bar_index] is syntactically correct, it will throw runtime error because of far away referencing in the past, so this code saves first bar's close in firstClose variable:

//@version=4
strategy("")
equitycurvetype = input(defval="No", title='Equity Curve Type', options=["No","Yes","Yes same start"])
// Save close of 1st bar in dataset using "var" to initialize only once.
var firstClose = close
float equity = na
if equitycurvetype == "Yes"
    equity := strategy.equity
else
    if equitycurvetype == "Yes same start"
        equity := strategy.equity * firstClose / strategy.initial_capital

plot(equity, title="Equity", color=color.red, linewidth=2, style=plot.style_areabr)
like image 36
PineCoders-LucF Avatar answered Oct 21 '22 06:10

PineCoders-LucF