OPEN-SOURCE SCRIPT
SuperTrend Strategy with Trend-Based Exits

**SuperTrend Strategy with Trend-Based Exits & Qty Rounding**
**Overview**
This automated strategy uses the SuperTrend indicator to detect trend reversals and manage both entries and exits. Position size is calculated dynamically based on a fixed risk-per-trade in USD and the ATR-based stop-loss distance, then rounded down to a configurable lot step to guarantee valid order sizes on any exchange.
**Key Features**
* **Dynamic Position Sizing**
```
raw_qty = risk_per_trade / |close – SuperTrend|
qty = floor(raw_qty / lot_step) * lot_step
```
* **Trend-Based Entries & Exits**
* On SuperTrend flip: close existing position, then open new in the opposite direction.
* Uses one strategy.close() + one strategy.entry() per reversal—no duplicate signals.
* **Trailing Stop**
* A built-in stop follows the SuperTrend line, locking in profits.
* **Qty Rounding**
* Ensures orders are always integer (or custom-step) sizes:
– Default `lot_step = 1` for whole contracts
– Use `lot_step = 100` for coins like PEPE
– Use `lot_step = 0.01` for ETH/BTC
**Inputs**
| Input | Description | Default |
| -------------------- | -------------------------------------------------------- | :-----: |
| Risk per trade (USD) | Maximum dollar risk per trade | 150 |
| ATR Length | Period for ATR used in SuperTrend | 50 |
| SuperTrend Factor | Multiplier applied to ATR for the SuperTrend calculation | 3.5 |
| Qty step | Minimum tradeable lot size (integer or decimal) | 1.0 |
**Usage & Setup**
1. **Manual Sync (one-time)**
* Check the rounded `qty` and trend direction on your chart.
* Open a matching market order on your exchange to align your real position with the strategy’s virtual position.
2. **Create TradingView Alert**
* Condition: **“Order fills”**
* Message: use your exchange’s required JSON template (e.g. Bybit’s).
* Webhook URL: point to your execution endpoint.
3. **First Reversal**
* The initial `close` signal will sync without sending a webhook.
* Subsequent `close` + `entry` signals will each fire exactly one valid webhook.
4. **Live Trading**
* Strategy will continuously close and reverse positions based on SuperTrend flips, with a SuperTrend-based trailing stop.
**Tips**
* Adjust **ATR Length** & **Factor** per asset volatility:
* Low vol: ATR 30–50, Factor 3.0–4.0
* Mid vol: ATR 50–75, Factor 4.0–4.5
* High vol: ATR 75–120, Factor 4.5–5.0
* If you trade a new coin, screen it quickly with 3–4 proven parameter sets, then refine around the best performer (3×3 grid search).
Add to your Pine Editor, publish as a strategy, set your alert and enjoy automated, volatility-adapted trading!
**Overview**
This automated strategy uses the SuperTrend indicator to detect trend reversals and manage both entries and exits. Position size is calculated dynamically based on a fixed risk-per-trade in USD and the ATR-based stop-loss distance, then rounded down to a configurable lot step to guarantee valid order sizes on any exchange.
**Key Features**
* **Dynamic Position Sizing**
```
raw_qty = risk_per_trade / |close – SuperTrend|
qty = floor(raw_qty / lot_step) * lot_step
```
* **Trend-Based Entries & Exits**
* On SuperTrend flip: close existing position, then open new in the opposite direction.
* Uses one strategy.close() + one strategy.entry() per reversal—no duplicate signals.
* **Trailing Stop**
* A built-in stop follows the SuperTrend line, locking in profits.
* **Qty Rounding**
* Ensures orders are always integer (or custom-step) sizes:
– Default `lot_step = 1` for whole contracts
– Use `lot_step = 100` for coins like PEPE
– Use `lot_step = 0.01` for ETH/BTC
**Inputs**
| Input | Description | Default |
| -------------------- | -------------------------------------------------------- | :-----: |
| Risk per trade (USD) | Maximum dollar risk per trade | 150 |
| ATR Length | Period for ATR used in SuperTrend | 50 |
| SuperTrend Factor | Multiplier applied to ATR for the SuperTrend calculation | 3.5 |
| Qty step | Minimum tradeable lot size (integer or decimal) | 1.0 |
**Usage & Setup**
1. **Manual Sync (one-time)**
* Check the rounded `qty` and trend direction on your chart.
* Open a matching market order on your exchange to align your real position with the strategy’s virtual position.
2. **Create TradingView Alert**
* Condition: **“Order fills”**
* Message: use your exchange’s required JSON template (e.g. Bybit’s).
* Webhook URL: point to your execution endpoint.
3. **First Reversal**
* The initial `close` signal will sync without sending a webhook.
* Subsequent `close` + `entry` signals will each fire exactly one valid webhook.
4. **Live Trading**
* Strategy will continuously close and reverse positions based on SuperTrend flips, with a SuperTrend-based trailing stop.
**Tips**
* Adjust **ATR Length** & **Factor** per asset volatility:
* Low vol: ATR 30–50, Factor 3.0–4.0
* Mid vol: ATR 50–75, Factor 4.0–4.5
* High vol: ATR 75–120, Factor 4.5–5.0
* If you trade a new coin, screen it quickly with 3–4 proven parameter sets, then refine around the best performer (3×3 grid search).
Add to your Pine Editor, publish as a strategy, set your alert and enjoy automated, volatility-adapted trading!
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
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.
Skrip sumber terbuka
Dalam semangat sebenar TradingView, pencipta skrip ini telah menjadikannya sumber terbuka supaya pedagang dapat menilai dan mengesahkan kefungsiannya. Terima kasih kepada penulis! Walaupun anda boleh menggunakannya secara percuma, ingat bahawa menerbitkan semula kod ini adalah tertakluk kepada Peraturan Dalaman kami.
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.