[英]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 的plot看起來和預期一樣,這是因為它滿足最后一個信號條件的柱數,但為什么我不能將它用作條件?
每次您的buy_signal
變量變為真時, barssince()
都會返回零,所以現在我們使用前一根柱的柱數。
我們為您的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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.