簡體   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