5

Looking for a workaround, can't use plotshape in this way because it doesn't work in a local scope.

//@version=3
study("MA test ", overlay=true)
FastMA = sma(close, 9)
SlowMA = sma(close, 15)
Diff = FastMA - SlowMA
if Diff > 0
    plotshape(Diff, style=shape.arrowup, location=location.belowbar, color=green)
sunspore
  • 127
  • 1
  • 4
  • 13
  • What exactly are you trying to do? – vitruvius Oct 18 '18 at 15:10
  • I want to plot an arrow under each bar where the fast ma is above the slow ma. I'm going to build on this, but first I need to figure out how to use the plotshape function. – sunspore Oct 18 '18 at 15:47

1 Answers1

9

You can directly apply your condition to series argument of the plot() function (also to color argument).

I also added another plotshape() that uses crossover() in its series and it only plots the triangles when FastMA crosses over SlowMA (orange triangle). I thought it might come handy for you in the future :)

//@version=3
study("MA test ", overlay=true)
FastMA = sma(close, 9)
SlowMA = sma(close, 15)
Diff = FastMA - SlowMA

plot(series=FastMA, title="FastMA", color=color.green, linewidth=3)
plot(series=SlowMA, title="SlowMA", color=color.red, linewidth=3)
bgcolor(color=Diff > 0 ? green : red)
plotshape(series=Diff > 0, style=shape.arrowup, location=location.belowbar, color=color.green, size=size.normal)
plotshape(series=crossover(FastMA, SlowMA), style=shape.triangledown, location=location.abovebar, color=color.orange, size=size.normal)

enter image description here

dknaack
  • 60,192
  • 27
  • 155
  • 202
vitruvius
  • 15,740
  • 3
  • 16
  • 26
  • Thanks a lot! This is an extremely helpful answer. I didn't realize that you could put conditions directly into the plot function. That solves a lot of problems. I appreciate your taking the time. – sunspore Oct 20 '18 at 00:46
  • Taking this further, ultimately I want to highlight areas where the two MAs are diverging, and then converging. Do you think this would be better coded as a function, or as separate calculations in the global scope? I was working on a function to encapsulate all the calculations, but as a newbie, I'm not sure this is the best way. – sunspore Oct 20 '18 at 00:49
  • If my answer helped you, could you please accept it as an answer by clicking the checkmark next to my answer? Coming to your question, if you are going to need to calculate the same thing again and again with different parameters, you should definitely use a function. If you are going to use the outcome only once, global declaration is okay. – vitruvius Oct 20 '18 at 08:13