Trading View Guide to Your First Backtest
Sep 2, 2018

The Trading View Guide to Your First Backtest tool provides a fundamental framework for automated strategy execution using exponential moving average crossovers to identify and trade trend shifts.
Usage
The strategy is primarily used to demonstrate how technical indicators can be converted into automated trading signals. By observing the interaction between a fast and slow moving average, traders can visualize potential entry and exit points on a price chart.
Bullish Crossover (Long Entry)
A long position is initiated when the 20-period Exponential Moving Average (EMA) crosses above the 50-period EMA. This indicates an upward shift in momentum. When this condition is met, the script closes any existing short positions and enters a long trade with a predefined quantity.
Bearish Crossover (Short Entry)
A short position is initiated when the 20-period EMA crosses below the 50-period EMA. This signifies a downward shift in momentum. The script will automatically close any active long positions and open a short trade.
Details
The script utilizes the strategy() function in Pine Script to allow for historical backtesting and real-time performance tracking. It calculates two distinct Exponential Moving Averages (EMAs):
- Fast EMA (20): Reacts more quickly to recent price changes.
- Slow EMA (50): Provides a broader view of the trend direction.
The execution logic is built on a continuous "always-in-the-market" approach, where the strategy flips between long and short states based on the relative position of the two EMAs. Alerts are optimized to trigger at the close of the bar once a crossover is confirmed, preventing "repainting" or false signals during active candle formation. This script was refactored and visually updated by the LuxAlgo team, based on original concepts by @dennisivanov.
Settings
As a foundational guide, this specific version uses hardcoded values to ensure simplicity for first-time users.
- EMA 20: The fast-period exponential moving average used for signal generation.
- EMA 50: The slow-period exponential moving average used as a trend baseline.
- Quantity: The order size is fixed at 10,000 units per trade for consistent backtesting results.
FAQ
What is the advantage of using EMA over SMA in this strategy?
The Exponential Moving Average (EMA) places a higher weight on recent price data compared to a Simple Moving Average (SMA). This allows the strategy to react faster to price reversals, though it may result in more frequent signals in ranging markets.
Can this strategy be used on any timeframe?
Yes, the crossover logic is universal and can be applied to any timeframe. However, shorter timeframes (like the 1-minute or 5-minute) may produce more "noise" and false signals compared to higher timeframes like the daily or 4-hour charts.
How can I access the Trading View Guide to Your First Backtest?
You can get access on the LuxAlgo Library for charting platforms like TradingView, MetaTrader (MT4/MT5), and NinjaTrader for free.
Unlock Unlimited Access to the LuxAlgo Library
Upgrade your plan to get all indicators, strategies, charts, and full access to Quant, our AI agent.
Trading & investing are risky and many will lose money in connection with trading and investing activities. All content on this site is not intended to, and should not be, construed as financial advice. Decisions to buy, sell, hold or trade in securities, commodities and other investments involve risk and are best made based on the advice of qualified financial professionals. Past performance does not guarantee future results.
Hypothetical or Simulated performance results have certain limitations. Unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, including, but not limited to, lack of liquidity. Simulated trading programs in general are designed with the benefit of hindsight, and are based on historical information. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.
Testimonials appearing on this website may not be representative of other clients or customers and is not a guarantee of future performance or success.
As a provider of technical analysis tools and strategies, we do not have access to the personal trading accounts or brokerage statements of our customers. As a result, we have no reason to believe our customers perform better or worse than traders as a whole based on any content, tool, or platform feature we provide.
Charts used on this site are by TradingView in which the majority of our technical indicators are built on. TradingView® is a registered trademark of TradingView, Inc. www.TradingView.com TradingView® has no affiliation with the owner, developer, or provider of the Services described herein.
Market data is provided by CBOE, CME Group, BarChart, Massive, CoinAPI. Select U.S. equities data is provided through Massive. CBOE BZX real-time U.S. equities data is licensed from CBOE and provided through BarChart. Real-time futures data is licensed from CME Group and provided through BarChart. Select cryptocurrency data, including major coins, is provided through CoinAPI. All data is provided “as is” and should be verified independently for trading purposes.
This does not represent our full Disclaimer. Please read our full disclaimer.
© 2026 LuxAlgo Global, LLC.

