Pine 脚本为什么我不能使用 barssince 作为策略条件?
Pine script Why can't I use barssince as the strategy condition?
我的策略是这样的:
//@version=4
strategy("Simple RSI Buy/Sell at a level", shorttitle="Simple RSI Strategy", default_qty_type=strategy.cash, default_qty_value=1000, currency=currency.USD, initial_capital=10000,commission_type=strategy.commission.percent, commission_value=0.075, pyramiding=20)
FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromYear = input(defval = 2017, title = "From Year", minval = 2017)
ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear = input(defval = 2022, title = "To Year", minval = 2017)
start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
window() => time >= start and time <= finish ? true : false // create function "within window of time"
rsi1 = rsi(close, 14) > input(70)
rsi2 = rsi(close, 14) < input(28)
barcolor(rsi1 ? color.black : na)
barcolor(rsi2 ? color.blue : na)
stopPer = input(50.0, title='Stop Loss %', type=input.float) / 100
takePer = input(80.0, title='Take Profit %', type=input.float) / 100
buy_signal_cnt = 0
buy_signal = (window() and rsi2)
buy_signal_cnt := barssince(buy_signal)
plot(buy_signal_cnt)
if (buy_signal_cnt > 1)
strategy.entry("Buy",strategy.long, stop=longStop, qty=500, when = buy_signal)
if ((strategy.position_avg_price - close) / close > 0.5)
strategy.close_all(comment="Sell All", when=window())
//strategy.entry("Sell",strategy.short, stop=shortStop, qty=strategy.position_size * 0.5, when = window() and rsi1 and strategy.position_size > 0)
strategy.close("Buy", when = window() and rsi1 and strategy.position_size > 0, qty_percent = 60, comment = "close 60%")
问题是,如果有条件if (buy_signal_cnt > 1)
,它根本不会触发任何买入信号,如果没有这个条件就好了。
buy_signal_cnt的图和预想的一样,是满足上次信号条件的柱数,但是为什么我不能用它作为条件?
每次您的 buy_signal
变量变为真时,barssince()
将 return 归零,所以现在我们使用前一个柱的柱计数。
我们为您的 longStop
变量添加了一些内容,因为它丢失了。请记住,这是停止购买级别,以及最后的调试图:
//@version=4
strategy("Simple RSI Buy/Sell at a level", shorttitle="Simple RSI Strategy", default_qty_type=strategy.cash, default_qty_value=1000, currency=currency.USD, initial_capital=10000,commission_type=strategy.commission.percent, commission_value=0.075, pyramiding=20)
FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromYear = input(defval = 2017, title = "From Year", minval = 2017)
ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear = input(defval = 2022, title = "To Year", minval = 2017)
start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
window() => time >= start and time <= finish ? true : false // create function "within window of time"
rsi1 = rsi(close, 14) > input(70)
rsi2 = rsi(close, 14) < input(28)
barcolor(rsi1 ? color.black : na)
barcolor(rsi2 ? color.blue : na)
stopPer = input(50.0, title='Stop Loss %', type=input.float) / 100
takePer = input(80.0, title='Take Profit %', type=input.float) / 100
buy_signal_cnt = 0
buy_signal = (window() and rsi2)
// We are using the bar count from the preceding bar, because whenever `buy_signal` is true, `barssince()` will return 0.
buy_signal_cnt := barssince(buy_signal)[1]
plot(buy_signal_cnt)
// This is the stop-buy limit.
longStop = low
if (buy_signal_cnt > 1)
strategy.entry("Buy",strategy.long, stop=longStop, qty=500, when = buy_signal)
if ((strategy.position_avg_price - close) / close > 0.5)
strategy.close_all(comment="Sell All", when=window())
//strategy.entry("Sell",strategy.short, stop=shortStop, qty=strategy.position_size * 0.5, when = window() and rsi1 and strategy.position_size > 0)
strategy.close("Buy", when = window() and rsi1 and strategy.position_size > 0, qty_percent = 60, comment = "close 60%")
// Debugging.
plotchar(buy_signal, "buy_signal", "•", location.top, size = size.tiny)
bgcolor(buy_signal_cnt > 1 and buy_signal ? color.silver : na)
我的策略是这样的:
//@version=4
strategy("Simple RSI Buy/Sell at a level", shorttitle="Simple RSI Strategy", default_qty_type=strategy.cash, default_qty_value=1000, currency=currency.USD, initial_capital=10000,commission_type=strategy.commission.percent, commission_value=0.075, pyramiding=20)
FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromYear = input(defval = 2017, title = "From Year", minval = 2017)
ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear = input(defval = 2022, title = "To Year", minval = 2017)
start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
window() => time >= start and time <= finish ? true : false // create function "within window of time"
rsi1 = rsi(close, 14) > input(70)
rsi2 = rsi(close, 14) < input(28)
barcolor(rsi1 ? color.black : na)
barcolor(rsi2 ? color.blue : na)
stopPer = input(50.0, title='Stop Loss %', type=input.float) / 100
takePer = input(80.0, title='Take Profit %', type=input.float) / 100
buy_signal_cnt = 0
buy_signal = (window() and rsi2)
buy_signal_cnt := barssince(buy_signal)
plot(buy_signal_cnt)
if (buy_signal_cnt > 1)
strategy.entry("Buy",strategy.long, stop=longStop, qty=500, when = buy_signal)
if ((strategy.position_avg_price - close) / close > 0.5)
strategy.close_all(comment="Sell All", when=window())
//strategy.entry("Sell",strategy.short, stop=shortStop, qty=strategy.position_size * 0.5, when = window() and rsi1 and strategy.position_size > 0)
strategy.close("Buy", when = window() and rsi1 and strategy.position_size > 0, qty_percent = 60, comment = "close 60%")
问题是,如果有条件if (buy_signal_cnt > 1)
,它根本不会触发任何买入信号,如果没有这个条件就好了。
buy_signal_cnt的图和预想的一样,是满足上次信号条件的柱数,但是为什么我不能用它作为条件?
每次您的 buy_signal
变量变为真时,barssince()
将 return 归零,所以现在我们使用前一个柱的柱计数。
我们为您的 longStop
变量添加了一些内容,因为它丢失了。请记住,这是停止购买级别,以及最后的调试图:
//@version=4
strategy("Simple RSI Buy/Sell at a level", shorttitle="Simple RSI Strategy", default_qty_type=strategy.cash, default_qty_value=1000, currency=currency.USD, initial_capital=10000,commission_type=strategy.commission.percent, commission_value=0.075, pyramiding=20)
FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromYear = input(defval = 2017, title = "From Year", minval = 2017)
ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear = input(defval = 2022, title = "To Year", minval = 2017)
start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
window() => time >= start and time <= finish ? true : false // create function "within window of time"
rsi1 = rsi(close, 14) > input(70)
rsi2 = rsi(close, 14) < input(28)
barcolor(rsi1 ? color.black : na)
barcolor(rsi2 ? color.blue : na)
stopPer = input(50.0, title='Stop Loss %', type=input.float) / 100
takePer = input(80.0, title='Take Profit %', type=input.float) / 100
buy_signal_cnt = 0
buy_signal = (window() and rsi2)
// We are using the bar count from the preceding bar, because whenever `buy_signal` is true, `barssince()` will return 0.
buy_signal_cnt := barssince(buy_signal)[1]
plot(buy_signal_cnt)
// This is the stop-buy limit.
longStop = low
if (buy_signal_cnt > 1)
strategy.entry("Buy",strategy.long, stop=longStop, qty=500, when = buy_signal)
if ((strategy.position_avg_price - close) / close > 0.5)
strategy.close_all(comment="Sell All", when=window())
//strategy.entry("Sell",strategy.short, stop=shortStop, qty=strategy.position_size * 0.5, when = window() and rsi1 and strategy.position_size > 0)
strategy.close("Buy", when = window() and rsi1 and strategy.position_size > 0, qty_percent = 60, comment = "close 60%")
// Debugging.
plotchar(buy_signal, "buy_signal", "•", location.top, size = size.tiny)
bgcolor(buy_signal_cnt > 1 and buy_signal ? color.silver : na)