如何检查交叉后第二天快速移动平均线是否保持在慢速移动平均线上方?
我对 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要习惯 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[0] > Y[0]
X[1] < Y[1]
如果两者都成立,则 X 在当前柱中穿过 Y。
这就是您要检查的内容:
LongEntry[1] = True
)ma1 大于当前柱中的
ma2
:ma1 > 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 ofopen
,close
, yourLongEntry
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 ofSeries
. The closest reference to thisSeries
type in other programming languages is theArray
, just its indexing is backwards in Pine, meaning the last/latest entry is at index 0. ThusLongEntry
is the same asLongEntry[0]
and you can always leave[0]
from a variable.LongEntry[1]
is the previous value ofLongEntry
,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:X[0] > Y[0]
X[1] < Y[1]
If both hold true then X crossed over Y in the current bar.
This is what you want to check:
LongEntry[1] = True
)ma1
is larger thanma2
in the current bar:ma1 > ma2
, same asma1[0] > ma2[0]
Summarized:
LongEntry[1] and ma1 > ma2