繁体   English   中英

Pine script 为什么我不能使用 barsince 作为策略条件?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM