首页 > 解决方案 > RSI 背离 - 更高的时间范围

问题描述

我的目标是在 m15 或 m5 中表示 H1 rsi 分歧。我不能resolution=""study()函数中使用参数,因为我需要将此脚本作为策略的一部分插入。

所以我认为我需要使用security()函数,但我不知道我必须把它们放在哪里。

这是我用于 rsi 分歧的代码:

//@version=4
study("rsi divergence", overlay=false)

//-----Input: divergence-----------------------------------------------------------\\
rsi_len      = input(title="rsi len",     type=input.integer, defval=14)    
pivot_right  = input(title="Pivot right", type=input.integer, defval=5)     
pivot_left   = input(title="Pivot left",  type=input.integer, defval=5)     
max_range    = input(title="Max range",   type=input.integer, defval=50)   
min_range    = input(title="Min range",   type=input.integer, defval=5)    
rsi_ob       = input(title="RSI OB",      type=input.integer, defval=70)   
rsi_os       = input(title="RSI OS",      type=input.integer, defval=30)    

//-----Declaration: rsi-------------------------------------------------------------\\
rsi = rsi(close, rsi_len)

//-----Plot: rsi--------------------------------------------------------------------\\
plot(rsi, title="RSI", color=color.yellow)
hline(50, linestyle=hline.style_dotted)
ob_line = hline(rsi_ob, linestyle=hline.style_dotted, color=color.white)
os_line = hline(rsi_os, linestyle=hline.style_dotted, color=color.white)
fill(ob_line, os_line, color=color.fuchsia)

//-----Check: pivot low-------------------------------------------------------------\\
pivot_low  = na(pivotlow(rsi, pivot_left, pivot_right))  ? false : true 
pivot_high = na(pivothigh(rsi, pivot_left, pivot_right)) ? false : true 

//-----Declaration: range rsi-------------------------------------------------------------\\
confirm_range(x) =>
    bars = barssince(x == true) 
    min_range <= bars and bars <= max_range 

//-----Declaration: divergences-------------------------------------------------------------\\
//long
rsi_check_long   = rsi[pivot_right] > valuewhen(pivot_low, rsi[pivot_right], 1) and confirm_range(pivot_low[1])
price_check_long = low[pivot_right] < valuewhen(pivot_low, low[pivot_right], 1)

//short
rsi_check_short   = rsi[pivot_right]  < valuewhen(pivot_high,  rsi[pivot_right], 1) and confirm_range(pivot_high[1])
price_check_short = high[pivot_right] > valuewhen(pivot_high, high[pivot_right], 1)

//-----Conditions-------------------------------------------------------------\\
div_bullCond = price_check_long  and rsi_check_long  and pivot_low
div_bearCond = price_check_short and rsi_check_short and pivot_high

//-----Plot: divergences-------------------------------------------------------------\\
//long
plot(pivot_low ? rsi[pivot_right] : na, offset=-pivot_right, linewidth=3, 
     color=(div_bullCond ? color.green : color.new(color.white, 100)))
plotshape(div_bullCond   ? rsi[pivot_right] : na, offset=-pivot_right, text=" LONG ",
     style=shape.labelup, location=location.absolute, color=color.green, textcolor=color.white)
     
//short
plot(pivot_high ? rsi[pivot_right] : na, offset=-pivot_right, linewidth=3, 
     color=(div_bearCond ? color.red : color.new(color.white, 100)))
plotshape(div_bearCond   ? rsi[pivot_right] : na, offset=-pivot_right, text=" SHORT ",
     style=shape.labelup, location=location.absolute, color=color.red, textcolor=color.white)

标签: pine-script

解决方案


尝试这个

rsi = security(syminfo.tickerid,'60',rsi(close, rsi_len))


推荐阅读