OPEN-SOURCE SCRIPT
20 / 200 SMA Strategy -

This Pine Script strategy is a 20 SMA / 200 SMA trend-following strategy designed for back testing and potential bot automation.
The main idea is to identify market direction using the relationship between the 20 simple moving average and the 200 simple moving average. When the 20 SMA is above the 200 SMA, the strategy looks for buy opportunities. When the 20 SMA is below the 200 SMA, it looks for sell opportunities.
The script includes three entry modes:
Original Cross Entry waits for the 20 SMA to cross the 200 SMA, then looks for price to close on the correct side of the 20 SMA.
Trend Pullback Entry allows entries after the market is already trending. For buys, the 20 SMA must be above the 200 SMA and price must cross back above the 20 SMA. For sells, the 20 SMA must be below the 200 SMA and price must cross back below the 20 SMA.
Relaxed Testing Entry is a more flexible testing mode that enters when price is on the correct side of the 20 SMA while the 20 SMA is on the correct side of the 200 SMA. This mode is useful for confirming that the strategy is working and producing trades.
The default risk management uses a 100-point take profit. The default stop loss is placed behind the last opposing candle beyond the 200 SMA. For a buy setup, the stop is placed below the last bearish candle below the 200 SMA. For a sell setup, the stop is placed above the last bullish candle above the 200 SMA. The user can also choose a fixed-point stop loss or recent swing stop.
The strategy includes optional filters such as a 200 SMA slope filter, candle-color confirmation, and a distance filter to avoid entries that are too far from the 20 SMA. It also plots the 20 SMA, 200 SMA, buy/sell labels, debug markers, and stop-loss/take-profit lines on the chart.
Overall, this script is built to help traders test a simple moving-average trend strategy while giving them flexibility to adjust entries, stops, profit targets, and filters based on the market or timeframe they are trading.
The main idea is to identify market direction using the relationship between the 20 simple moving average and the 200 simple moving average. When the 20 SMA is above the 200 SMA, the strategy looks for buy opportunities. When the 20 SMA is below the 200 SMA, it looks for sell opportunities.
The script includes three entry modes:
Original Cross Entry waits for the 20 SMA to cross the 200 SMA, then looks for price to close on the correct side of the 20 SMA.
Trend Pullback Entry allows entries after the market is already trending. For buys, the 20 SMA must be above the 200 SMA and price must cross back above the 20 SMA. For sells, the 20 SMA must be below the 200 SMA and price must cross back below the 20 SMA.
Relaxed Testing Entry is a more flexible testing mode that enters when price is on the correct side of the 20 SMA while the 20 SMA is on the correct side of the 200 SMA. This mode is useful for confirming that the strategy is working and producing trades.
The default risk management uses a 100-point take profit. The default stop loss is placed behind the last opposing candle beyond the 200 SMA. For a buy setup, the stop is placed below the last bearish candle below the 200 SMA. For a sell setup, the stop is placed above the last bullish candle above the 200 SMA. The user can also choose a fixed-point stop loss or recent swing stop.
The strategy includes optional filters such as a 200 SMA slope filter, candle-color confirmation, and a distance filter to avoid entries that are too far from the 20 SMA. It also plots the 20 SMA, 200 SMA, buy/sell labels, debug markers, and stop-loss/take-profit lines on the chart.
Overall, this script is built to help traders test a simple moving-average trend strategy while giving them flexibility to adjust entries, stops, profit targets, and filters based on the market or timeframe they are trading.
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。