Example input
[market]: Cryptocurrency
[target audience]: advanced traders
[trading approach]: algorithmic trading
[key elements]: entry/exit signals, risk management, and backtesting
Example output
Here's a custom Pine Script trading strategy for advanced cryptocurrency traders. This strategy focuses on algorithmic trading, including essential components such as entry/exit signals, risk management, and backtesting.
```pinescript
//@version=5
strategy("Advanced Crypto Algorithmic Trading Strategy", overlay=true)
// --- Input Parameters ---
length = input.int(14, title="RSI Length")
rsiOverbought = input.int(70, title="RSI Overbought Level")
rsiOversold = input.int(30, title="RSI Oversold Level")
smaLength = input.int(50, title="SMA Length")
stopLossPerc = input.float(2.0, title="Stop Loss Percentage", step=0.1)
takeProfitPerc = input.float(4.0, title="Take Profit Percentage", step=0.1)
// --- Indicator Calculations ---
rsi = ta.rsi(close, length)
sma = ta.sma(close, smaLength)
// --- Entry Conditions ---
longCondition = ta.crossover(rsi, rsiOversold) and close > sma
shortCondition = ta.crossunder(rsi, rsiOverbought) and close < sma
// --- Exit Conditions ---
stopLoss = close * (1 - stopLossPerc / 100)
takeProfit = close * (1 + takeProfitPerc / 100)
// --- Trading Logic ---
if (longCondition)
strategy.entry("Long", strategy.long, stop=stopLoss, limit=takeProfit)
if (shortCondition)
strategy.entry("Short", strategy.short, stop=stopLoss, limit=takeProfit)
// --- Plotting ---
plot(rsi, title="RSI", color=color.blue)
hline(rsiOverbought, "Overbought", color=color.red)
hline(rsiOversold, "Oversold", color=color.green)
plot(sma, title="SMA", color=color.orange)
// --- Backtesting ---
strategy.exit("Take Profit/Stop Loss", from_entry="Long", loss=stopLoss, profit=takeProfit)
strategy.exit("Take Profit/Stop Loss", from_entry="Short", loss=stopLoss, profit=takeProfit)
// --- Risk Management ---
risk = input.float(1.0, title="Risk Percentage", step=0.1)
capital = input.float(10000, title="Initial Capital", step=100)
riskAmount = capital * (risk / 100)
size = riskAmount / (stopLossPerc / 100 * close)
// --- Position Sizing ---
strategy.risk.allow_entry_in(strategy.direction.long)
strategy.risk.max_contracts(size)
```
### Explanation:
1. **Inputs:**
- `length`: Length for RSI calculation.
- `rsiOverbought`: RSI level indicating overbought conditions.
- `rsiOversold`: RSI level indicating oversold conditions.
- `smaLength`: Length for Simple Moving Average (SMA) calculation.
- `stopLossPerc`: Stop loss percentage.
- `takeProfitPerc`: Take profit percentage.
- `risk`: Percentage of capital to risk per trade.
- `capital`: Initial trading capital.
2. **Indicators:**
- `rsi`: Calculates the Relative Strength Index (RSI).
- `sma`: Calculates the Simple Moving Average (SMA).
3. **Entry Conditions:**
- Long entry: RSI crosses above the oversold level and price is above the SMA.
- Short entry: RSI crosses below the overbought level and price is below the SMA.
4. **Exit Conditions:**
- Stop loss and take profit are calculated as percentages of the entry price.
5. **Trading Logic:**
- Long and short entries are triggered based on the defined conditions, with stop loss and take profit levels.
6. **Plotting:**
- RSI and SMA are plotted for visualization.
- Overbought and oversold levels are indicated with horizontal lines.
7. **Backtesting:**
- The strategy includes logic for backtesting with defined exit conditions.
8. **Risk Management:**
- Position sizing is based on the percentage of capital to risk per trade.
This script provides a robust framework for advanced algorithmic trading in cryptocurrencies. Adjust the parameters and conditions based on specific market conditions and backtest thoroughly before using it in live trading.