PROTECTED SOURCE SCRIPT

Box with RSI Div(Dynamic Adjustment + MA)

180
Script Description: Dynamic Box Range with RSI Divergence and MA Ribbon Strategy
This script is a comprehensive trading strategy combining RSI divergence, a box range breakout system, and a moving average ribbon strategy. It is designed to generate dynamic entries and exits for both long and short positions while enabling integration with OKX trading bots for automated signal execution. Below is a detailed breakdown of the script's components:

1. General Overview
Name: Box Range with RSI Divergence and Dynamic Adjustment + MA Ribbon Strategy
Purpose:
To identify trade opportunities using a combination of RSI divergence, price action within a box range, and moving average conditions.
To dynamically adjust position sizes and integrate with OKX trading bots for automated trade execution.
Features:
Dynamic position sizing for entries and exits.
Customizable parameters for box range length, RSI settings, and moving averages.
Alert generation for integration with OKX bots.
2. Key Components
A. Box Range Logic
Definition:
The box range is defined by the highest high and lowest low of the price over a customizable lookback period (boxLength).
Used to detect potential breakout or breakdown levels.
Components:
highestHigh: The highest high over the box range period.
lowestLow: The lowest low over the box range period.
Visualization:
The box range's upper and lower bounds are plotted on the chart with green (upper) and red (lower) lines.
B. RSI Divergence Detection
RSI Calculation:
The Relative Strength Index (RSI) is calculated using a customizable period (rsiLength).
Divergence Conditions:
Bullish Divergence:
Price forms lower lows, but RSI forms higher lows.
Bearish Divergence:
Price forms higher highs, but RSI forms lower highs.
Purpose:
Divergences indicate potential trend reversals and are used as trade entry signals.
C. Moving Average Ribbon Strategy
Moving Averages:
Includes 4 moving averages: MA20, MA50, MA100, and MA200.
Users can customize the type (SMA, EMA, etc.) and period of these moving averages.
The moving averages are calculated using a customizable timeframe (e.g., 4H).
Trend Conditions:
Uptrend:
MA20 > MA50 > MA100 > MA200, and price is above MA20.
Downtrend:
MA20 < MA50 < MA100 < MA200, and price is below MA20.
D. Dynamic Position Sizing
Dynamic Adjustment:
The script dynamically adjusts position size (entry and exit percentages) based on the difference between the current price and the average entry price (avgPrice).
Long Position:
Adds to the position if the price drops below the average price.
Reduces the position if the price rises significantly above the average price.
Short Position:
Closes part of the position if the price falls significantly below the average price.
Adds to the position if the price rises above the average price.
E. OKX Bot Integration
Signal Configuration:
The script generates alerts compatible with OKX trading bots.
Signals include parameters such as:
action: Indicates whether to enter or exit a position.
instrument: The trading pair (symbol).
orderType: Market or limit orders.
investmentType: Specifies how the trade size is calculated (e.g., percentage of balance or equity).
amount: The size of the trade.
Alerts are sent via alert() for automated trading.
3. Signal Logic
A. Long Signals
Conditions:
Price is at or below the lower box range (lowestLow).
A bullish RSI divergence is detected.
An uptrend is confirmed by the moving averages (MA20 > MA50 > MA100 > MA200).
Actions:
Enter a long position with dynamically calculated size.
Display a "BUY" label on the chart.
Trigger an alert for OKX integration.
B. Short Signals
Conditions:
Price is at or above the upper box range (highestHigh).
A bearish RSI divergence is detected.
A downtrend is confirmed by the moving averages (MA20 < MA50 < MA100 < MA200).
Actions:
Enter a short position with dynamically calculated size.
Display a "SELL" label on the chart.
Trigger an alert for OKX integration.
C. Position Adjustments
Long Adjustments:
Increase position size if the price drops below the average entry price.
Reduce position size if the price rises above the average entry price.
Short Adjustments:
Increase position size if the price rises above the average entry price.
Reduce position size if the price falls below the average entry price.
4. Visualization
Box Range:
Green line for the upper range (resistance).
Red line for the lower range (support).
Moving Averages:
MA20 (yellow), MA50 (orange), MA100 (red), MA200 (maroon).
Signal Labels:
"BUY" label for long signals (green).
"SELL" label for short signals (red).
Dynamic Position Info:
Displays entry/exit percentages and price levels.
5. Customizable Parameters
Box Range:
boxLength: The lookback period for the box range.
RSI:
rsiLength: The calculation period for RSI.
divergenceLookback: The number of bars to check for divergence.
Moving Averages:
Type (SMA, EMA, etc.).
Periods for MA20, MA50, MA100, and MA200.
Timeframe for moving average calculations (e.g., 4H).
Position Sizing:
Minimum and maximum percentage limits for entry and exit adjustments.
6. Alerts
Alerts are generated for:
Long entry signals.
Short entry signals.
Long and short position exits.
Alert messages are formatted for OKX bot compatibility, including all necessary parameters.
7. Use Cases
This script is ideal for:

Automated Trading:
Integrates easily with OKX bots for hands-free trading.
Trend and Range Trading:
Combines breakout strategies with trend confirmation.
Dynamic Risk Management:
Adjusts position sizes dynamically based on market conditions.
This script provides a powerful combination of indicators and logic, enabling traders to capitalize on both trend-following and counter-trend opportunities while dynamically managing positions.

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.