OPEN-SOURCE SCRIPT
Extremum Range MA Crossover Strategy

1. Principle of Work & Strategy Logic ⚙️📈
Main idea: The strategy tries to catch the moment of a breakout from a price consolidation range (flat) and the start of a new trend. It combines two key elements:
Moving Average (MA) 📉: Acts as a dynamic support/resistance level and trend filter.
Range Extremes (Range High/Low) 🔺🔻: Define the borders of the recent price channel or consolidation.
The strategy does not attempt to catch absolute tops and bottoms. Instead, it enters an already formed move after the breakout, expecting continuation.
Type: Trend-following, momentum-based.
Timeframes: Works on different TFs (H1, H4, D), but best suited for H4 and higher, where breakouts are more meaningful.
2. Justification of Indicators & Settings ⚙️
A. Moving Average (MA) 📊
Why used: Core of the strategy. It smooths price fluctuations and helps define the trend. The price (via extremes) must cross the MA → signals a potential trend shift or strengthening.
Parameters:
maLength = 20: Default length (≈ one trading month, 20-21 days). Good balance between sensitivity & smoothing.
Lower TF → reduce (10–14).
Higher TF → increase (50).
maSource: Defines price source (default = Close). Alternatives (HL2, HLC3) → smoother, less noisy MA.
maType: Default = EMA (Exponential MA).
Why EMA? Faster reaction to recent price changes vs SMA → useful for breakout strategies.
Other options:
SMA 🟦 – classic, slowest.
WMA 🟨 – weights recent data stronger.
HMA 🟩 – near-zero lag, but “nervous,” more false signals.
DEMA/TEMA 🟧 – even faster & more sensitive than EMA.
VWMA 🔊 – volume-weighted.
ZLEMA ⏱ – reduced lag.
👉 Choice = tradeoff between speed of reaction & false signals.
B. Range Extremes (Previous High/Low) 📏
Why used: Define borders of recent trading range.
prevHigh = local resistance.
prevLow = local support.
Break of these levels on close = trigger.
Parameters:
lookbackPeriod = 5: Searches for highest high / lowest low of last 5 candles. Very recent range.
Higher value (10–20) → wider, stronger ranges but rarer signals.
3. Entry & Exit Rules 🎯
Long signals (BUY) 🟢📈
Condition (longCondition): Previous Low crosses MA from below upwards.
→ Price bounced from the bottom & strong enough to push range border above MA.
Execution: Auto-close short (if any) → open long.
Short signals (SELL) 🔴📉
Condition (shortCondition): Previous High crosses MA from above downwards.
→ Price rejected from the top, upper border failed above MA.
Execution: Auto-close long (if any) → open short.
Exit conditions 🚪
Exit Long (exitLongCondition): Close below prevLow.
→ Uptrend likely ended, range shifts down.
Exit Short (exitShortCondition): Close above prevHigh.
→ Downtrend likely ended, range shifts up.
⚠️ Important: Exit = only on candle close beyond extremes (not just wick).
4. Trading Settings ⚒️
overlay = true → indicators shown on chart.
initial_capital = 10000 💵.
default_qty_type = strategy.cash, default_qty_value = 100 → trades fixed $100 per order (not lots). Can switch to % of equity.
commission_type = strategy.commission.percent, commission_value = 0.1 → default broker fee = 0.1%. Adjust for your broker!
slippage = 3 → slippage = 3 ticks. Adjust to asset liquidity.
currency = USD.
margin_long = 100, margin_short = 100 → no leverage (100% margin).
5. Visualization on Chart 📊
The strategy draws 3 lines:
🔵 MA line (thickness 2).
🔴 Previous High (last N candles).
🟢 Previous Low (last N candles).
Also: entry/exit arrows & equity curve shown in backtest.
Disclaimer ⚠️📌
Risk Warning: This description & code are for educational purposes only. Not financial advice. Trading (Forex, Stocks, Crypto) carries high risk and may lead to full capital loss. You trade at your own risk.
Testing: Always backtest & demo test first. Past results ≠ future profits.
Responsibility: Author of this strategy & description is not responsible for your trading decisions or losses.
Main idea: The strategy tries to catch the moment of a breakout from a price consolidation range (flat) and the start of a new trend. It combines two key elements:
Moving Average (MA) 📉: Acts as a dynamic support/resistance level and trend filter.
Range Extremes (Range High/Low) 🔺🔻: Define the borders of the recent price channel or consolidation.
The strategy does not attempt to catch absolute tops and bottoms. Instead, it enters an already formed move after the breakout, expecting continuation.
Type: Trend-following, momentum-based.
Timeframes: Works on different TFs (H1, H4, D), but best suited for H4 and higher, where breakouts are more meaningful.
2. Justification of Indicators & Settings ⚙️
A. Moving Average (MA) 📊
Why used: Core of the strategy. It smooths price fluctuations and helps define the trend. The price (via extremes) must cross the MA → signals a potential trend shift or strengthening.
Parameters:
maLength = 20: Default length (≈ one trading month, 20-21 days). Good balance between sensitivity & smoothing.
Lower TF → reduce (10–14).
Higher TF → increase (50).
maSource: Defines price source (default = Close). Alternatives (HL2, HLC3) → smoother, less noisy MA.
maType: Default = EMA (Exponential MA).
Why EMA? Faster reaction to recent price changes vs SMA → useful for breakout strategies.
Other options:
SMA 🟦 – classic, slowest.
WMA 🟨 – weights recent data stronger.
HMA 🟩 – near-zero lag, but “nervous,” more false signals.
DEMA/TEMA 🟧 – even faster & more sensitive than EMA.
VWMA 🔊 – volume-weighted.
ZLEMA ⏱ – reduced lag.
👉 Choice = tradeoff between speed of reaction & false signals.
B. Range Extremes (Previous High/Low) 📏
Why used: Define borders of recent trading range.
prevHigh = local resistance.
prevLow = local support.
Break of these levels on close = trigger.
Parameters:
lookbackPeriod = 5: Searches for highest high / lowest low of last 5 candles. Very recent range.
Higher value (10–20) → wider, stronger ranges but rarer signals.
3. Entry & Exit Rules 🎯
Long signals (BUY) 🟢📈
Condition (longCondition): Previous Low crosses MA from below upwards.
→ Price bounced from the bottom & strong enough to push range border above MA.
Execution: Auto-close short (if any) → open long.
Short signals (SELL) 🔴📉
Condition (shortCondition): Previous High crosses MA from above downwards.
→ Price rejected from the top, upper border failed above MA.
Execution: Auto-close long (if any) → open short.
Exit conditions 🚪
Exit Long (exitLongCondition): Close below prevLow.
→ Uptrend likely ended, range shifts down.
Exit Short (exitShortCondition): Close above prevHigh.
→ Downtrend likely ended, range shifts up.
⚠️ Important: Exit = only on candle close beyond extremes (not just wick).
4. Trading Settings ⚒️
overlay = true → indicators shown on chart.
initial_capital = 10000 💵.
default_qty_type = strategy.cash, default_qty_value = 100 → trades fixed $100 per order (not lots). Can switch to % of equity.
commission_type = strategy.commission.percent, commission_value = 0.1 → default broker fee = 0.1%. Adjust for your broker!
slippage = 3 → slippage = 3 ticks. Adjust to asset liquidity.
currency = USD.
margin_long = 100, margin_short = 100 → no leverage (100% margin).
5. Visualization on Chart 📊
The strategy draws 3 lines:
🔵 MA line (thickness 2).
🔴 Previous High (last N candles).
🟢 Previous Low (last N candles).
Also: entry/exit arrows & equity curve shown in backtest.
Disclaimer ⚠️📌
Risk Warning: This description & code are for educational purposes only. Not financial advice. Trading (Forex, Stocks, Crypto) carries high risk and may lead to full capital loss. You trade at your own risk.
Testing: Always backtest & demo test first. Past results ≠ future profits.
Responsibility: Author of this strategy & description is not responsible for your trading decisions or losses.
开源脚本
本着TradingView的真正精神,此脚本的创建者将其开源,以便交易者可以查看和验证其功能。向作者致敬!虽然您可以免费使用它,但请记住,重新发布代码必须遵守我们的网站规则。
免责声明
这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在使用条款阅读更多信息。
开源脚本
本着TradingView的真正精神,此脚本的创建者将其开源,以便交易者可以查看和验证其功能。向作者致敬!虽然您可以免费使用它,但请记住,重新发布代码必须遵守我们的网站规则。
免责声明
这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在使用条款阅读更多信息。