OPEN-SOURCE SCRIPT

RSI Trading Strategy with EMA and ATR Profit __ahmad__razavi__


### Code Breakdown

1. **Strategy Declaration**:
```pinescript
//version=6
strategy("RSI Trading Strategy with EMA and ATR Stop Loss/Take Profit", overlay=true)
```
- `//version=6`: This specifies that the script uses version 6 of Pine Script.
- `strategy(...)`: This function defines the properties of the trading strategy, including its title and whether it overlays on the price chart (`overlay=true`).

2. **Input Parameters**:
```pinescript
length = input.int(14, minval=1, title="RSI Length")
src = input(close, title="Source")
rsi = ta.rsi(src, length)
```
- `length`: Input for the RSI length, defaulting to 14.
- `src`: Input for the source data (default is the closing price).
- `rsi`: Calculation of the RSI based on the specified source and length.

3. **Smoothing the RSI**:
```pinescript
smoothingLength = input.int(14, minval=1, title="Smoothing Length")
smoothedRsi = ta.ema(rsi, smoothingLength) // Using EMA to smooth RSI
```
- This section defines a smoothing length and calculates a smoothed version of the RSI using an Exponential Moving Average (EMA).

4. **ATR Calculation**:
```pinescript
atrLength = input.int(14, title="ATR Length")
atrMultiplier = input.float(1, title="ATR Multiplier")
atrValue = ta.atr(atrLength) // Calculate ATR
```
- `atrLength`: Input for the ATR calculation period.
- `atrMultiplier`: Input to set how many times the ATR will be used for stop-loss and take-profit levels.
- `atrValue`: Calculation of ATR based on the specified length.

5. **Trading Conditions**:
The following conditions define when to enter long or short positions based on RSI levels:

- **Long Entry Condition**:
```pinescript
if (ta.crossover(smoothedRsi, level2))
strategy.entry("Long", strategy.long)
strategy.exit("Take Profit/Stop Loss", "Long", stop=close - atrMultiplier * atrValue, limit=close + atrMultiplier * atrValue, comment="")
crossCount := crossCount + 1
crossPrice := close
```
- A long position is entered when the smoothed RSI crosses above level 70 (`level2`).
- The exit conditions set a stop-loss and take-profit based on ATR.

- **Short Entry Condition**:
```pinescript
if (ta.crossunder(smoothedRsi, level2))
strategy.entry("Short", strategy.short)
strategy.exit("Take Profit/Stop Loss", "Short", stop=close + atrMultiplier * atrValue, limit=close - atrMultiplier * atrValue, comment="")
crossCount := crossCount + 1
crossPrice := close
```
- A short position is entered when the smoothed RSI crosses below level 70.
- Similar exit conditions apply as in long entries.

- **Additional Long and Short Conditions**:
The script also includes conditions for entering long positions when crossing above level 30 (`level1`) and short positions when crossing below level 30.

6. **Cross Count Table**:
```pinescript
if (not na(crossPrice))
table.cell(crossingTable, 0, crossCount % 5, text=str.tostring(crossCount), bgcolor=color.green)
table.cell(crossingTable, 1, crossCount % 5, text=str.tostring(crossPrice), bgcolor=color.green)
```
- A table is created to display the count of crosses and their prices.
- The table updates every time a crossover occurs.

7. **Plotting RSI and Levels**:
```pinescript
plot(smoothedRsi, title="Smoothed RSI", color=color.blue)
hline(level1, "Level 30", color=color.red)
hline(level2, "Level 70", color=color.green)
```
- The smoothed RSI is plotted in blue.
- Horizontal lines are drawn at levels 30 and 70 to indicate overbought and oversold conditions.

### Summary

This script provides a comprehensive trading strategy that utilizes both RSI and ATR to manage trades effectively. It allows traders to enter positions based on RSI movements while using ATR to set dynamic stop-loss and take-profit levels. Additionally, it keeps track of how many times the RSI has crossed specified levels and displays this information in a table on the chart.
educational

نص برمجي مفتوح المصدر

قام مؤلف هذا النص البرمجي بنشره وجعله مفتوح المصدر، بحيث يمكن للمتداولين فهمه والتحقق منه، وهو الأمر الذي يدخل ضمن قيم TradingView. تحياتنا للمؤلف! يمكنك استخدامه مجانًا، ولكن إعادة استخدام هذا الرمز في المنشور يخضع لقواعد‎‎قوانين الموقع. يمكنك جعله مفضلاً لاستخدامه على الرسم البياني.

هل تريد استخدام هذا النص البرمجي على الرسم البياني؟

إخلاء المسؤولية