Sure! To create a custom trading indicator based on the strategy you mentioned, where you buy when the EMA (Exponential Moving Average) 9 crosses above EMA 21 and sell when it crosses below, here's a simple script that can be used in Pine Script (for TradingView).
```pinescript //version=5 indicator("EMA 9 and EMA 21 Crossover", overlay=true)
// Define the EMAs ema9 = ta.ema(close, 9) ema21 = ta.ema(close, 21)
// Plot the EMAs plot(ema9, color=color.blue, linewidth=2, title="EMA 9") plot(ema21, color=color.orange, linewidth=2, title="EMA 21")
// Buy condition: EMA 9 crosses above EMA 21 buyCondition = ta.crossover(ema9, ema21)
// Sell condition: EMA 9 crosses below EMA 21 sellCondition = ta.crossunder(ema9, ema21)
### Explanation: 1. **EMA 9 and EMA 21**: This script calculates the Exponential Moving Averages for the periods 9 and 21. 2. **Buy Signal**: The script will detect when the EMA 9 crosses above the EMA 21 and mark it with a green "BUY" label below the price bar. 3. **Sell Signal**: The script will detect when the EMA 9 crosses below the EMA 21 and mark it with a red "SELL" label above the price bar. 4. **Alert Conditions**: Alerts are also set up for when these crossovers happen, so you can get notified.
You can copy this script into TradingView's Pine Script editor, and it will display the signals on your chart.
In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.
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.