在不同的时间范围内更改 RSI 的时间范围

发布于 2025-01-14 02:19:42 字数 2031 浏览 0 评论 0原文

大家好,谁能帮我为 RSI 策略设置一个不同的时间框架。例如,我想在 1 小时图表上查看 4 小时时间范围 RSI。只是想知道这是否可能,以及您是否可以帮我设置它。代码如下,如果您能告诉我需要添加什么才能使其工作,那将会很有帮助。提前致谢。

输入图片此处描述

//@version=5
strategy(title="Relative Strength Index", shorttitle="RSI-SMA(f)", overlay=false, initial_capital=100, default_qty_type=strategy.cash, default_qty_value=50, currency=currency.USD, commission_type=strategy.commission.percent, commission_value=0.07)

// Choose MA Type
ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

// RSI and MA inputs
rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
maLengthInput = input.int(14, title="MA Length", group="MA Settings")

up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)

// Plotting RSI and SMA
plot(rsi, "RSI", color=color.blue)
plot(rsiMA, "RSI-based MA", color=color.yellow)


// Conditions and Entries
longCondition= ta.crossover(rsi,rsiMA)
if (longCondition)
    strategy.entry('Long', strategy.long)
shortCondition= ta.crossunder(rsi,rsiMA)
if (shortCondition)
    strategy.entry('short', strategy.short)

// Exit Position
strategy.close('Long', when= shortCondition, comment= 'Exit Long')
strategy.close('short', when= longCondition, comment= 'Exit Short')

Hi guys can anyone help me set up a different time frame for the RSI strategy. For example I would like to view a 4hr timeframe rsi on a 1hr chart. Just wondering is that is possible and if you could help me set it up. Code is below and it would be helpful if you could tell me what i would have to add to make it work. Thanks in advance.

enter image description here

//@version=5
strategy(title="Relative Strength Index", shorttitle="RSI-SMA(f)", overlay=false, initial_capital=100, default_qty_type=strategy.cash, default_qty_value=50, currency=currency.USD, commission_type=strategy.commission.percent, commission_value=0.07)

// Choose MA Type
ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

// RSI and MA inputs
rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
maLengthInput = input.int(14, title="MA Length", group="MA Settings")

up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)

// Plotting RSI and SMA
plot(rsi, "RSI", color=color.blue)
plot(rsiMA, "RSI-based MA", color=color.yellow)


// Conditions and Entries
longCondition= ta.crossover(rsi,rsiMA)
if (longCondition)
    strategy.entry('Long', strategy.long)
shortCondition= ta.crossunder(rsi,rsiMA)
if (shortCondition)
    strategy.entry('short', strategy.short)

// Exit Position
strategy.close('Long', when= shortCondition, comment= 'Exit Long')
strategy.close('short', when= longCondition, comment= 'Exit Short')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

碍人泪离人颜 2025-01-21 02:19:42

我制作了一个每日移动平均线,可以在较低的时间范围内查看。

您可以创建一个指标,并将其放在 D4h 上( 240) 在你的情况下。
就像你展示的图片一样。但如果你想将其与其他逻辑混合,它就行不通,因为一切都将基于该时间范围,并且仅基于该时间范围。

我使用/滥用了 request.security 函数为了这。

以下是显示 4 小时收盘价的指标示例。如果这是您想要的行为,您可以在 2 小时图上查看。

//@version=5
indicator("My script", overlay=true)
plot(request.security(syminfo.ticker, "240", close, barmerge.gaps_off, barmerge.lookahead_off))

I made a daily moving average that could be viewed on lower timeframes.

You can create for example a indicator and put that on D or 4h (240) in your case.
Just like in the picture your showing. But if you want to mix this with other logic it would not work because everything would be based on that timeframe and that timeframe alone.

I used/abused the request.security function for this.

Here is a example of a indicator that will show you the close of the 4h. You can check it on the 2h chart, if this is the behaviour you want.

//@version=5
indicator("My script", overlay=true)
plot(request.security(syminfo.ticker, "240", close, barmerge.gaps_off, barmerge.lookahead_off))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文