如何检查交叉后第二天快速移动平均线是否保持在慢速移动平均线上方?

发布于 2025-01-15 12:38:39 字数 614 浏览 2 评论 0原文

我对 Pine Script 非常陌生,需要一些帮助来编写 MA 交叉策略来进行回测。

场景:50 日移动平均线穿过 100 日移动平均线,如果 50 日移动平均线在下一个交易日仍然高于 100 日移动平均线,我的策略如何考虑?

到目前为止我的脚本:

start = timestamp (2012,1,1,0,0)
end = timestamp (2022,3,16,0,0)
ma1 = ta.sma(close, 50)
ma2 = ta.sma(close, 100)
plot (sma1, title = "SMA50", color=color.green)
plot (sma2, title = "SMA100", color=color.red)

//strategy
LongEntry = ta.crossover (sma1, sma2)
LongExit = ta.crossover (sma2, sma1)

if time >= start and time < end
    strategy.entry ("Long", strategy.long,1, when = LongEntry)
    strategy.close ("Long", when = LongExit)

I am very new to Pine Script and need some help writing a MA crossover strategy to backtest.

Scenario: The 50 day SMA crosses over the 100 day SMA, how do I have my strategy also consider if the 50 day SMA remains above the 100 day SMA the following session?

My script so far:

start = timestamp (2012,1,1,0,0)
end = timestamp (2022,3,16,0,0)
ma1 = ta.sma(close, 50)
ma2 = ta.sma(close, 100)
plot (sma1, title = "SMA50", color=color.green)
plot (sma2, title = "SMA100", color=color.red)

//strategy
LongEntry = ta.crossover (sma1, sma2)
LongExit = ta.crossover (sma2, sma1)

if time >= start and time < end
    strategy.entry ("Long", strategy.long,1, when = LongEntry)
    strategy.close ("Long", when = LongExit)

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

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

发布评论

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

评论(1

等风来 2025-01-22 12:38:39

您需要习惯 Pine 主要数据结构是 Series。您的脚本将在每个柱上运行(或者有时在每个价格变动时)运行,每次运行时都会有一个新版本的开盘收盘、您的LongEntry< /code> 已计算(以及每个变量)。

这些变量的奇异值(也称为计算结果) - 在脚本末尾 - 将被提交到同名的Series

因此,您的 LongEntry 可能看起来像一个简单的变量,但其数据类型为 Series。在其他编程语言中,与此 Series 类型最接近的引用是 Array,只是它的索引在 Pine 中是向后的,这意味着最后/最新条目位于索引 0 处。因此 < code>LongEntry 与 LongEntry[0] 相同,并且您始终可以从变量中保留 [0]LongEntry[1]LongEntry 的前一个值,LongEntry[2] 是第二个前一个值。

本质上内置变量的工作原理是相同的,您也有 open, open[1], open[2]

检查交叉与同时检查这些相同:

  • 当前柱中变量X大于变量Y:X[0] > Y[0]
  • 变量 X 小于前一柱中的变量 Y:X[1] < Y[1]

如果两者都成立,则 X 在当前柱中穿过 Y。

这就是您要检查的内容:

  • 上一个柱中发生了交叉(您有交叉结果,即在 LongEntry 中,所以这是:LongEntry[1] = True
  • ma1 大于当前柱中的 ma2ma1 > ma2,同ma1[0]> ma2[0]

总结:LongEntry[1] 和 ma1 > ma2

You need get used to that Pine primary data structure is Series. Your script will be run on every bar (or sometimes on ever tick) and on every run there will be a new version of open, close, your LongEntry calculated (and every variable as well).

The singular values (aka. calculation results) of these variables - at the end of your script - will be committed to a Series of the same name.

Thus, your LongEntry might look like a simple variable, but that has a data type of Series. The closest reference to this Series type in other programming languages is the Array, just its indexing is backwards in Pine, meaning the last/latest entry is at index 0. Thus LongEntry is the same as LongEntry[0] and you can always leave [0] from a variable. LongEntry[1] is the previous value of LongEntry, LongEntry[2] is the second previous value.

Essentially built-in variables work the same, you have open, open[1], open[2] as well.

Checking a crossover is the same as checking these at the same time:

  • variable X is larger than variable Y in the current bar: X[0] > Y[0]
  • variable X was smaller than variable Y in the previous bar: X[1] < Y[1]

If both hold true then X crossed over Y in the current bar.

This is what you want to check:

  • a crossover happened in the previous bar (you have your crossover results ie. in LongEntry so this is: LongEntry[1] = True)
  • and ma1 is larger than ma2 in the current bar: ma1 > ma2, same as ma1[0] > ma2[0]

Summarized: LongEntry[1] and ma1 > ma2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文