繁体   English   中英

在 pinescript v5 策略中定义进入/退出的问题

[英]Problem defining entry/exit in a pinescript v5 strategy

我正在制定一个简单的 RSI 背离策略来测试看涨背离的多头,但我的代码没有进行任何交易。 我不知道为什么,请帮忙。

初始代码块来自 Libertus 的完美 RSI 背离指标。

策略代码开始于//strategy code

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rypto_dynasty

//@version=5
strategy("RSI div strategy", overlay=true)

len = input.int(14, minval=1, title='RSI Length')
ob = input.int(defval=70, title='Overbought', minval=0, maxval=100)
os = input.int(defval=30, title='Oversold', minval=0, maxval=100)
i_stop_prcnt = input.float(title='Stop percentage?', defval=0.5)
i_exposure = input.float(title='Exposure?', defval=0.5)
i_period_start = input.time(timestamp("1 Apr 2022 00:00 +0300"), "Period start")
i_period_end = input.time(timestamp("14 Apr 2022 00:00 +0300"), 'Period end')
// RSI code
rsi = ta.rsi(close, len)
band1 = hline(ob)
band0 = hline(os)
plot(rsi, color=rsi > ob or rsi < os ? color.new(color.red, 0) : color.new(color.black, 0))
fill(band1, band0, color=color.new(color.purple, 97))

// DIVS code
piv = input(false, 'Hide pivots?')
shrt = input(false, 'Shorter labels?')
hidel = input(false, 'Hide labels and color background')
xbars = input.int(defval=90, title='Div lookback period (bars)?', minval=1)
hb = math.abs(ta.highestbars(rsi, xbars))  // Finds bar with highest value in last X bars
lb = math.abs(ta.lowestbars(rsi, xbars))  // Finds bar with lowest value in last X bars

// Defining variable values, mandatory in Pine 3
max = float(na)
max_rsi = float(na)
min = float(na)
min_rsi = float(na)
pivoth = bool(na)
pivotl = bool(na)
divbear = bool(na)
divbull = bool(na)

// If bar with lowest / highest is current bar, use it's value
max := hb == 0 ? close : na(max[1]) ? close : max[1]
max_rsi := hb == 0 ? rsi : na(max_rsi[1]) ? rsi : max_rsi[1]
min := lb == 0 ? close : na(min[1]) ? close : min[1]
min_rsi := lb == 0 ? rsi : na(min_rsi[1]) ? rsi : min_rsi[1]

// Compare high of current bar being examined with previous bar's high
// If curr bar high is higher than the max bar high in the lookback window range
if close > max  // we have a new high
    max := close  // change variable "max" to use current bar's high value
    max
if rsi > max_rsi  // we have a new high
    max_rsi := rsi  // change variable "max_rsi" to use current bar's RSI value
    max_rsi
if close < min  // we have a new low
    min := close  // change variable "min" to use current bar's low value
    min
if rsi < min_rsi  // we have a new low
    min_rsi := rsi  // change variable "min_rsi" to use current bar's RSI value
    min_rsi

// Finds pivot point with at least 2 right candles with lower value
pivoth := max_rsi == max_rsi[2] and max_rsi[2] != max_rsi[3] ? true : na
pivotl := min_rsi == min_rsi[2] and min_rsi[2] != min_rsi[3] ? true : na

// Detects divergences between price and indicator with 1 candle delay so it filters out repeating divergences
if max[1] > max[2] and rsi[1] < max_rsi and rsi <= rsi[1]
    divbear := true
    divbear
if min[1] < min[2] and rsi[1] > min_rsi and rsi >= rsi[1]
    divbull := true
    divbull


// Plots divergences and pivots with offest
l = divbear ? label.new(bar_index - 1, rsi[1] + 1, 'BEAR', color=color.red, textcolor=color.white, style=label.style_label_down, yloc=yloc.price, size=size.small) : divbull ? label.new(bar_index - 1, rsi[1] - 1, 'BULL', color=color.green, textcolor=color.white, style=label.style_label_up, yloc=yloc.price, size=size.small) : pivoth ? label.new(bar_index - 2, max_rsi + 1, 'PIVOT', color=color.blue, textcolor=color.white, style=label.style_label_down, yloc=yloc.price, size=size.small) : pivotl ? label.new(bar_index - 2, min_rsi - 1, 'PIVOT', color=color.blue, textcolor=color.white, style=label.style_label_up, yloc=yloc.price, size=size.small) : na

// Shorter labels
if shrt
    label.set_text(l, na)
// Hides pivots or labels
if piv and (pivoth or pivotl) or hidel
    label.delete(l)

//strategy code 


//checking if current time lies in the defined time period 

timeCond = i_period_start < time  and time < i_period_end

//Defining condition for bullish divergence long and  bearish divergence short
buyCondition = (divbull  and strategy.position_size <= 0) ? true : false
sellCondition = (divbear and strategy.position_size <= 0) ? true : false

if buyCondition and timeCond 
    strategy.entry('long', strategy.long, qty=1000)
    
if strategy.position_size > 0
    effective_sl = strategy.position_avg_price * i_stop_prcnt / 100
    effective_exp = strategy.position_avg_price * i_exposure / 100
    strategy.exit(id='long exit', from_entry='long', comment='long closed', stop=effective_sl , profit=effective_exp)

我尝试查看所有 pinescript 资源,但找不到足够的 pinescript v5 策略示例。 如果有 pinescript v5 策略示例的资源,也请指导我。 谢谢你。

更正/解决方案 - 该代码适用于 v5 pinescript。 我输入了时间并忘记编辑其默认值。 我的错。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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