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.
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.
Mã nguồn mở
Theo đúng tinh thần TradingView, tác giả của tập lệnh này đã công bố nó dưới dạng mã nguồn mở, để các nhà giao dịch có thể xem xét và xác minh chức năng. Chúc mừng tác giả! Mặc dù bạn có thể sử dụng miễn phí, hãy nhớ rằng việc công bố lại mã phải tuân theo Nội quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.