OPEN-SOURCE SCRIPT
EMA + VWMA + ATR Smoothed BuySell (merged) - TOM ZENG 202509

Logic and Functionality Analysis
The script is divided into three main logical sections: EMA trend analysis, ATR-based signal generation, and VWMA smoothing.
1. EMA Trend Analysis (EMA Fan) 📈
This section uses a series of Exponential Moving Averages (EMAs) to identify trends. You've wisely chosen a set of EMA lengths (8, 21, 50, 200) that are commonly used in trading. These numbers are often derived from the Fibonacci sequence and are believed to offer a good balance of sensitivity to recent price action while still reflecting the underlying trend.
Purpose: The EMAs serve as dynamic support and resistance levels. When the price is above the EMAs and they are fanned out in ascending order (short-term EMA above long-term EMA), it indicates a strong uptrend. Conversely, a descending order indicates a downtrend.
Customization: The code allows you to easily adjust the EMA lengths in the inputs section, giving you control over the sensitivity of your trend analysis.
2. ATR Trailing Stop (Buy/Sell Signals) 🎯
This is the core of the indicator's signal-generating capability. It uses the Average True Range (ATR) to create a dynamic trailing stop line. The ATR measures volatility, so the stop line adjusts automatically to wider price swings.
Logic: The script uses a var float variable xATRTrailingStop to store the value of the stop line from the previous bar. The code then determines the current bar's stop line by comparing the current price to the previous bar's stop line and using math.max and math.min to smoothly move the line along with the trend.
Signal Generation: The pos variable tracks whether the trend is long (pos = 1) or short (pos = -1). The isLong and isShort variables act as a state machine, ensuring that the "Buy" and "Sell" signals are only triggered once at the exact point of a crossover, rather than on every subsequent bar.
Visuals & Alerts: The plotshape functions create labels directly on the chart, and the barcolor function changes the color of the candlesticks, providing a clear visual representation of the current trend state. The alertcondition functions are crucial for automation, allowing you to set up notifications for when a signal occurs.
3. VWMA and Combined Average 🌊
This section introduces a Volume-Weighted Moving Average (VWMA), which gives more weight to periods of high trading volume. This makes the VWMA more responsive to significant moves that are backed by strong institutional buying or selling.
Combined Logic: The avg1 variable creates a new line by averaging the VWMA and the xATRTrailingStop line. This is an innovative approach to blend two different types of analysis—volume-based trend and volatility-based risk management—into a single, smoothed line. It can act as an additional filter or a unique trading signal on its own.
Summary
Your code is a very effective and clean example of a multi-faceted indicator. It correctly implements a robust ATR trailing stop for signals while also providing valuable trend context through EMAs and volume analysis through VWMA. The combination of these elements makes it a powerful tool for a trader looking for a comprehensive view of the market.
The script is divided into three main logical sections: EMA trend analysis, ATR-based signal generation, and VWMA smoothing.
1. EMA Trend Analysis (EMA Fan) 📈
This section uses a series of Exponential Moving Averages (EMAs) to identify trends. You've wisely chosen a set of EMA lengths (8, 21, 50, 200) that are commonly used in trading. These numbers are often derived from the Fibonacci sequence and are believed to offer a good balance of sensitivity to recent price action while still reflecting the underlying trend.
Purpose: The EMAs serve as dynamic support and resistance levels. When the price is above the EMAs and they are fanned out in ascending order (short-term EMA above long-term EMA), it indicates a strong uptrend. Conversely, a descending order indicates a downtrend.
Customization: The code allows you to easily adjust the EMA lengths in the inputs section, giving you control over the sensitivity of your trend analysis.
2. ATR Trailing Stop (Buy/Sell Signals) 🎯
This is the core of the indicator's signal-generating capability. It uses the Average True Range (ATR) to create a dynamic trailing stop line. The ATR measures volatility, so the stop line adjusts automatically to wider price swings.
Logic: The script uses a var float variable xATRTrailingStop to store the value of the stop line from the previous bar. The code then determines the current bar's stop line by comparing the current price to the previous bar's stop line and using math.max and math.min to smoothly move the line along with the trend.
Signal Generation: The pos variable tracks whether the trend is long (pos = 1) or short (pos = -1). The isLong and isShort variables act as a state machine, ensuring that the "Buy" and "Sell" signals are only triggered once at the exact point of a crossover, rather than on every subsequent bar.
Visuals & Alerts: The plotshape functions create labels directly on the chart, and the barcolor function changes the color of the candlesticks, providing a clear visual representation of the current trend state. The alertcondition functions are crucial for automation, allowing you to set up notifications for when a signal occurs.
3. VWMA and Combined Average 🌊
This section introduces a Volume-Weighted Moving Average (VWMA), which gives more weight to periods of high trading volume. This makes the VWMA more responsive to significant moves that are backed by strong institutional buying or selling.
Combined Logic: The avg1 variable creates a new line by averaging the VWMA and the xATRTrailingStop line. This is an innovative approach to blend two different types of analysis—volume-based trend and volatility-based risk management—into a single, smoothed line. It can act as an additional filter or a unique trading signal on its own.
Summary
Your code is a very effective and clean example of a multi-faceted indicator. It correctly implements a robust ATR trailing stop for signals while also providing valuable trend context through EMAs and volume analysis through VWMA. The combination of these elements makes it a powerful tool for a trader looking for a comprehensive view of the market.
סקריפט קוד פתוח
ברוח TradingView אמיתית, היוצר של הסקריפט הזה הפך אותו לקוד פתוח, כך שסוחרים יכולים לבדוק ולאמת את הפונקציונליות שלו. כל הכבוד למחבר! למרות שאתה יכול להשתמש בו בחינם, זכור שפרסום מחדש של הקוד כפוף לכללי הבית שלנו.
כתב ויתור
המידע והפרסומים אינם אמורים להיות, ואינם מהווים, עצות פיננסיות, השקעות, מסחר או סוגים אחרים של עצות או המלצות שסופקו או מאושרים על ידי TradingView. קרא עוד בתנאים וההגבלות.
סקריפט קוד פתוח
ברוח TradingView אמיתית, היוצר של הסקריפט הזה הפך אותו לקוד פתוח, כך שסוחרים יכולים לבדוק ולאמת את הפונקציונליות שלו. כל הכבוד למחבר! למרות שאתה יכול להשתמש בו בחינם, זכור שפרסום מחדש של הקוד כפוף לכללי הבית שלנו.
כתב ויתור
המידע והפרסומים אינם אמורים להיות, ואינם מהווים, עצות פיננסיות, השקעות, מסחר או סוגים אחרים של עצות או המלצות שסופקו או מאושרים על ידי TradingView. קרא עוד בתנאים וההגבלות.