OPEN-SOURCE SCRIPT
Dual Best MA Strategy Analyzer

Dual Best MA Strategy Analyzer (Lookback Window)
What it does
This indicator scans a range of moving-average lengths and finds the single best MA for long crossovers and the single best MA for short crossunders over a fixed lookback window. It then plots those two “winner” MAs on your chart:
Best Long MA (green): The MA length that would have made the highest total profit using a simple “price crosses above MA → long; exit on cross back below” logic.
Best Short MA (red): The MA length that would have made the highest total profit using “price crosses below MA → short; exit on cross back above.”
You can switch between SMA and EMA, set the min/max length, choose a step size, and define the lookback window used for evaluation.
How it works (brief)
For each candidate MA length between Min MA Length and Max MA Length (stepping by Step Size), the script:
Builds the MA (SMA or EMA).
Simulates a naïve crossover strategy over the last Lookback Window candles:
Long model: enter on crossover, exit on crossunder.
Short model: enter on crossunder, exit on crossover.
Sums simple P&L in price units (no compounding, no fees/slippage).
Picks the best long and best short lengths by total P&L and plots those two MAs.
Note: Long and short are evaluated independently. The script plots MAs only; it doesn’t open positions.
Inputs
Min MA Length / Max MA Length – Bounds for MA search.
Step Size – Spacing between tested lengths (e.g., 10 tests 10, 20, 30…).
Use EMA instead of SMA – Toggle average type.
Lookback Window (candles) – Number of bars used to score each MA. Needs enough history to be meaningful.
What the plots mean
Best Long MA (green): If price crosses above this line (historically), that MA length produced the best long-side results over the lookback.
Best Short MA (red): If price crosses below this line (historically), that MA length produced the best short-side results.
These lines can change over time as new bars enter the lookback window. Think of them as adaptive “what worked best recently” guides, not fixed signals.
Practical tips
Timeframe matters: Run it on the timeframe you trade; the “best” length on 1h won’t match 1m or 1D.
Step size trade-off: Smaller steps = more precision but heavier compute. Larger steps = faster scans, coarser choices.
Use with confirmation: Combine with structure, volume, or volatility filters. This is a single-factor tester.
Normalization: P&L is in raw price units. For cross-symbol comparison, consider using one symbol at a time (or adapt the script to percent P&L).
Limitations & assumptions
No fees, funding, slippage, or position sizing.
Simple “in/out” on the next crossover; no stops/targets/filters.
Results rely on lookback choice and will repaint historically as the “best” length is re-selected with new data (the plot is adaptive, not forward-fixed).
The script tests up to ~101 candidates internally (bounded by your min/max/step).
Good uses
Quickly discover a recently effective MA length for trend following.
Compare SMA vs EMA performance on your market/timeframe.
Build a playbook: note which lengths tend to win in certain regimes (trending vs choppy).
Not included (by design)
Alerts, entries/exits, or a full strategy report. It’s an analyzer/overlay.
If you want alerts, you can add simple conditions like:
ta.crossover(close, plotLongMA) for potential long interest
ta.crossunder(close, plotShortMA) for potential short interest
Changelog / Notes
v1: Initial release. Array-based scanner, SMA/EMA toggle, adaptive long/short best MA plots, user-set lookback.
Disclaimer
This is educational tooling, not financial advice. Test thoroughly and use proper risk management.
What it does
This indicator scans a range of moving-average lengths and finds the single best MA for long crossovers and the single best MA for short crossunders over a fixed lookback window. It then plots those two “winner” MAs on your chart:
Best Long MA (green): The MA length that would have made the highest total profit using a simple “price crosses above MA → long; exit on cross back below” logic.
Best Short MA (red): The MA length that would have made the highest total profit using “price crosses below MA → short; exit on cross back above.”
You can switch between SMA and EMA, set the min/max length, choose a step size, and define the lookback window used for evaluation.
How it works (brief)
For each candidate MA length between Min MA Length and Max MA Length (stepping by Step Size), the script:
Builds the MA (SMA or EMA).
Simulates a naïve crossover strategy over the last Lookback Window candles:
Long model: enter on crossover, exit on crossunder.
Short model: enter on crossunder, exit on crossover.
Sums simple P&L in price units (no compounding, no fees/slippage).
Picks the best long and best short lengths by total P&L and plots those two MAs.
Note: Long and short are evaluated independently. The script plots MAs only; it doesn’t open positions.
Inputs
Min MA Length / Max MA Length – Bounds for MA search.
Step Size – Spacing between tested lengths (e.g., 10 tests 10, 20, 30…).
Use EMA instead of SMA – Toggle average type.
Lookback Window (candles) – Number of bars used to score each MA. Needs enough history to be meaningful.
What the plots mean
Best Long MA (green): If price crosses above this line (historically), that MA length produced the best long-side results over the lookback.
Best Short MA (red): If price crosses below this line (historically), that MA length produced the best short-side results.
These lines can change over time as new bars enter the lookback window. Think of them as adaptive “what worked best recently” guides, not fixed signals.
Practical tips
Timeframe matters: Run it on the timeframe you trade; the “best” length on 1h won’t match 1m or 1D.
Step size trade-off: Smaller steps = more precision but heavier compute. Larger steps = faster scans, coarser choices.
Use with confirmation: Combine with structure, volume, or volatility filters. This is a single-factor tester.
Normalization: P&L is in raw price units. For cross-symbol comparison, consider using one symbol at a time (or adapt the script to percent P&L).
Limitations & assumptions
No fees, funding, slippage, or position sizing.
Simple “in/out” on the next crossover; no stops/targets/filters.
Results rely on lookback choice and will repaint historically as the “best” length is re-selected with new data (the plot is adaptive, not forward-fixed).
The script tests up to ~101 candidates internally (bounded by your min/max/step).
Good uses
Quickly discover a recently effective MA length for trend following.
Compare SMA vs EMA performance on your market/timeframe.
Build a playbook: note which lengths tend to win in certain regimes (trending vs choppy).
Not included (by design)
Alerts, entries/exits, or a full strategy report. It’s an analyzer/overlay.
If you want alerts, you can add simple conditions like:
ta.crossover(close, plotLongMA) for potential long interest
ta.crossunder(close, plotShortMA) for potential short interest
Changelog / Notes
v1: Initial release. Array-based scanner, SMA/EMA toggle, adaptive long/short best MA plots, user-set lookback.
Disclaimer
This is educational tooling, not financial advice. Test thoroughly and use proper risk management.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.