I have a very simple strategy in pinescript.
//@version=4
strategy("My Strategy", overlay=true)
longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition)
strategy.entry("My Long Entry Id", strategy.long)
shortCondition = crossunder(sma(close, 14), sma(close, 28))
if (shortCondition)
strategy.entry("My Short Entry Id", strategy.short)
...
And I'd like to plot a line at value 0 or 1 depending on whether the position size is 0 or not.
...
x = strategy.position_size == 0 ? 0 : 1
plot(x)
However the result of plotting x is a line that is only at value 1. On the chart
it's clearly displaying that at some points no positions are open. Therefore the
variable position_size
should be 0 at certain points but it appears its only 1.
Why is this the case?
Your strategy flip from long to short and constantly is in a position after the first trade. See the attached screenshot.
You should use
strategy.exit
orstrategy.close
functions to exit the position before the opposite direction signal occurs.Example: