问题
What I'm trying to achieve is a "gap" of a few candles, say 4 in this case before the next trade happens Here nexT is the number of bars, before which the next trade should not take place But the value of nextT always resets to 0, everytime the code runs. What can be a possible modification to the code that the value of nexT does not reset automatically.
//@version=4
strategy("RSI Strategy", overlay=true)
//timeframe
//timframe
FromMonth = input(defval = 9, title = "From Month", minval = 1)
FromDay = input(defval = 9, title = "From Day", minval = 1)
FromYear = input(defval = 2019, title = "From Year", minval = 2000)
ToMonth = input(defval = 11, title = "To Month", minval = 1)
ToDay = input(defval = 23, title = "To Day", minval = 1)
ToYear = input(defval = 2020, title = "To Year", minval = 2014)
nexT = input(defval=0)
gap = input(4,defval=4)
testPeriod() =>
(time > timestamp(FromYear, FromMonth, FromDay, 00, 00)) and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59))
//Indicator
length = input( 14 )
overSold = input( 30 )
overBought = input( 70 )
price = close
vrsi = rsi(price, length)
co = crossover(vrsi, overSold)
cu = crossunder(vrsi, overBought)
//longonly
//strategy.risk.allow_entry_in(strategy.direction.long)
trade= co or cu
if(bar_index>=nexT)
if(trade)
if (co)
strategy.entry("RsiLE", strategy.long, comment="RsiLE")
if (cu)
strategy.entry("RsiSE", strategy.short, comment="RsiSE")
nextT= bar_index+gap
回答1:
Use :=
operator for var assign in Pine:
nextT := bar_index+gap
回答2:
Your nexT
variable is an input variable, so it is immutable.
That means that you cannot change it's value through code.
The solution is to create a new variable new_nexT
which is defined with the keyword var
and is initialized with your nexT
input variable.
When you define a variable without the var
keyword, it is re-initialized on every bar, so it's value is volatile.
When you define a variable with the var
keyword, it's only initialized once, on the first bar, and keeps it's value on the subsequent bars, unless you change it's value.
To change a value for a var
variable, you must use :=
instead of =
.
Using my edited code example hereunder, new_nexT
is initialized once (because it's a var
) and assigned the nexT
input variable during that initialization.
A bit further in the code, :=
is used to assign a new value to it.
//@version=4
strategy("RSI Strategy", overlay=true)
//timeframe
FromMonth = input(defval = 9, title = "From Month", minval = 1)
FromDay = input(defval = 9, title = "From Day", minval = 1)
FromYear = input(defval = 2019, title = "From Year", minval = 2000)
ToMonth = input(defval = 11, title = "To Month", minval = 1)
ToDay = input(defval = 23, title = "To Day", minval = 1)
ToYear = input(defval = 2020, title = "To Year", minval = 2014)
nexT = input(defval = 0)
gap = input(defval = 4)
var int new_nexT = nexT
testPeriod() =>
(time > timestamp(FromYear, FromMonth, FromDay, 00, 00)) and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59))
//Indicator
length = input( 14 )
overSold = input( 30 )
overBought = input( 70 )
price = close
vrsi = rsi(price, length)
co = crossover(vrsi, overSold)
cu = crossunder(vrsi, overBought)
//longonly
//strategy.risk.allow_entry_in(strategy.direction.long)
trade = co or cu
if(bar_index >= new_nexT and trade)
if (co)
strategy.entry("RsiLE", strategy.long, comment="RsiLE")
if (cu)
strategy.entry("RsiSE", strategy.short, comment="RsiSE")
new_nexT := bar_index + gap
来源:https://stackoverflow.com/questions/64970980/trying-to-write-a-code-which-gives-the-strategy-a-buffer-time-before-it-takes-th