EMA & MA Crossover Strategy

HPotter · strategy · 33 行 · 点赞 441 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
////////////////////////////////////////////////////////////
//  Copyright by HPotter v1.0 12/04/2025
// The Moving Average Crossover trading strategy is possibly the most popular
// trading strategy in the world of trading. First of them were written in the
// middle of XX century, when commodities trading strategies became popular.
// This strategy is a good example of so-called traditional strategies. 
// Traditional strategies are always long or short. That means they are never 
// out of the market. The concept of having a strategy that is always long or 
// short may be scary, particularly in today’s market where you don’t know what 
// is going to happen as far as risk on any one market. But a lot of traders 
// believe that the concept is still valid, especially for those of traders who 
// do their own research or their own discretionary trading. 
// This version uses crossover of moving average and its exponential moving average. 
////////////////////////////////////////////////////////////
strategy(title="EMA & MA Crossover Strategy", shorttitle="EMA&MA", overlay = true)
LengthMA = input.int(10, minval=1)
LengthEMA = input.int(10,minval=1)
xMA = ta.sma(close[1], LengthMA)
xEMA = ta.ema(xMA[1], LengthEMA)
pos = 0
pos := xEMA < xMA ? 1 : xEMA > xMA ? -1 : nz(pos[1], 0)
barcolor(pos == -1 ? color.red: pos == 1 ? color.green : color.blue)
 
if pos == 1
    strategy.entry('Long', strategy.long)
if pos == -1
    strategy.entry('Short', strategy.short)
if pos == 0
    strategy.close_all(comment = 'Exit')
 
plot(xMA, color=color.red, title="MA")
plot(xEMA, color=color.blue, title="EMA")

← 返回列表