OPEN-SOURCE SCRIPT

Multi-MA Strategy Analyzer with Backtest

120
Multi-MA Strategy Analyzer with Backtest

This TradingView Pine Script indicator is designed to analyze multiple moving averages (SMA or EMA) dynamically and identify the most profitable one based on historical performance.

Features

Dynamic MA Range:

Specify a minLength, maxLength, and step size.

Automatically calculates up to 20 MAs.

Custom MA Calculation:

Uses custom SMA and EMA implementations to support dynamic length values.

Buy/Sell Logic:

Buy when price crosses above a MA.

Sell when price crosses below.

Supports both long and short trades.

Performance Tracking:

Tracks PnL, number of trades, win rate, average profit, and drawdown.

Maintains individual stats for each MA.

Best MA Detection:

Automatically highlights the best-performing MA.

Optional showBestOnly toggle to focus only on the best line and its stats.

Visualization:

Up to 20 plot() calls (static) for MAs.

Green highlight for the best MA.

Color-coded result table and chart.

Table View

When showBestOnly = false, the table displays all MAs with stats.

When showBestOnly = true, the table displays only the best MA with a summary row.

Includes:
Best MA length
Total PnL
Number of trades
Win rate
Avg PnL per trade
Max Drawdown

Configuration

minLength (default: 10)
maxLength (default: 200)
step (default: 10)
useEMA: Toggle between EMA and SMA
showBestOnly: Focus on best-performing MA only

Notes

MA plotting is static, limited to 20 total.
Table supports highlighting and is optimized for performance.
Script is structured to run efficiently using arrays and simple int where required.

Potential Extensions

Add visual buy/sell arrows

Export stats to CSV

Strategy tester conversion

Custom date range filtering for backtesting

Author: Muhammad Wasim
Version: 1.0

Penafian

Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.