【问题标题】:Why does pine script enter at the next candle open even when I am using a market order?为什么即使我使用市价单,pine 脚本也会在下一个蜡烛打开时进入?
【发布时间】:2021-10-21 19:43:15
【问题描述】:

我正在尝试在 Pine Script 中实施基于 2 周期 RSI 的策略回测。 这个想法很简单

目标

  • 2 周期 RSI 低于 10,当 200 EMA 低于最近收盘价时,我在下一根蜡烛上做多,市场订单设置为比之前的蜡烛收盘价低 2%。
  • 2 周期 RSI 超过 90,或自进入以来已超过 10 个柱(以先出现的情况为准)我退出交易。

问题

因此,显然 pine 脚本默认在下一根蜡烛开盘时采取多头/空头头寸。 但是,尽管通过指定限制属性下了市价单,但多头头寸仍以下一根蜡烛的开盘价输入。

更多细节

时间范围 - 1 天

代码链接 - https://in.tradingview.com/chart/GDSsFCKq/#(代码 - SBILIFE (NSE INDIA))

我不确定我在这里做错了什么。请帮忙。

代码

//@version=4
strategy("2RSI Strategy by Larry Connor", overlay=true)
rsi_length = input(title="RSI Length", defval=2) 
buying_rsi_value = input(title="Buy at RSI Value", defval=5)
selling_rsi_value = input(title="Sell at RSI Value", defval= 40)
price = close
rsi = rsi(price, rsi_length)
buy = crossunder(rsi, buying_rsi_value)
sell = crossover(rsi, selling_rsi_value)

date = tostring(dayofmonth) + '-' + tostring(month) + '-' + tostring(year)
disable_date_ranges = input(title="Disable Date Ranges", defval=true)

start_date = input(title="Start Date", type=input.time, defval=timestamp("19 Oct 2020 
00:00 +0530"))
end_date = input(title="End Date", type=input.time, defval=timestamp("18 Oct 2021 
00:00 +0530"))
in_date_range = time >= start_date and time < end_date
    
ema_len = input(200, minval=1, title="EMA Length")
ema_src = input(close, title="EMA Source")
ema_200 = ema(ema_src, ema_len)

entry_condition= buy and ema_200 < price 

exit_condition = sell or entry_condition[10]

previous_day_close = close[1]
two_percent_of_prev_day_close = previous_day_close * 0.02
entry_price = previous_day_close - two_percent_of_prev_day_close

// plotchar(entry_condition, "debug", "", location.bottom)

capital_invested = input(title="Invested capital", defval=100000)
initial_capital = strategy.initial_capital
capital_to_be_invested = capital_invested
if(na(capital_invested) or capital_invested == 0)
    capital_to_be_invested = initial_capital

if (not na(rsi) and (in_date_range or disable_date_ranges))
    strategy.entry("buy", when=entry_condition and low < entry_price, limit= 
entry_price, long= true, qty = capital_to_be_invested/entry_price, comment="Long")
        
if (exit_condition)
    strategy.close("buy", true)

【问题讨论】:

    标签: pine-script algorithmic-trading back-testing


    【解决方案1】:

    解决这个问题是要了解历史数据的处理,这用于回溯测试。历史数据是每根蜡烛 4 个数据点 (OHLC)。指标的计算通常使用收盘价进行,而且我们没有足够的关于柱内价格变动的信息来做出警报发生的地点或时间的假设。因此,我们必须依靠给定蜡烛的收盘条件来在历史柱上建立可变状态。在实时,我们会遇到类似的问题,只是我们必须等待关闭确认信号,否则我们会遭受重绘的影响。因此,两种数据类型(历史数据和实时数据)作为一个程序对齐 - 蜡烛收盘是一个确认且可操作的信号。要确定收盘价,蜡烛将耗尽该期间的最后一个刻度。这意味着我们的下一个可操作的销售是下一个可用的销售,它发生在以下柱的第一个滴答声中。这就是为什么在给定变量的状态变化后的回测中使用开盘价。如果蜡烛关闭,我们将如何执行订单?如上所述,我们可以实时放弃这一点,但这样做是将策略的 2 个不同行为分开,这有效地使策略独一无二,而不是我们在历史数据上测试过的策略。只是战略建设的许多警告中的一小部分:)

    干杯!

    【讨论】:

    • 感谢 Bjorgum 的回答。这样可以缓解我的焦虑。我已经花了好几天想知道我的代码是否有问题。事实证明,我只是忽略了这样一个事实,即在历史回溯测试中以确切的给定价格点执行限价单的前景非常不确定,除非蜡烛以限价开盘。如果我对您的答案的解释有误,请纠正我。
    • 是的,在向上跳空时限价可能不会执行,但限价单或市价单仍然只能在蜡烛已经收盘的信号后对柱执行操作。
    【解决方案2】:

    虽然我同意仅使用最高价、最低价、开盘价和收盘价的答案,而不是盘中的其余部分,但有办法解决这个问题。

    如果您使用“安全”功能,您可以从不同的图表中加载数据。 这可以用于不同的股票,也可以用于不同的时间范围。

    因此,如果您在日线图上进行交易,您可以使用类似:

    15minClose = security(systeminfo.tickerid, "15", close)
    

    在这种情况下,您会在 1 天图表中获得当前交易品种、15 分钟蜡烛的收盘数据。

    因此,如果您想在中午进行交易,例如,您可以在满足其他要求的情况下检查 1500 万收盘价?

    有关更多信息,您可以在 pine 参考中查找安全功能。 https://www.tradingview.com/pine-script-reference/v4/#fun_security

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      • 2023-02-17
      • 2022-07-22
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      相关资源
      最近更新 更多