
This strategy utilizes the Stochastic Oscillator to identify overbought and oversold market conditions, triggering trades with predefined risk and reward parameters to capitalize on price fluctuations within a volatile trading range. The main idea behind this strategy is to buy at the low end of the trading range and sell at the high end, while strictly controlling risk.
The volatility range trading strategy based on the Stochastic Oscillator attempts to capitalize on the oscillator’s overbought and oversold signals within a predefined trading range. The strategy controls risk through strict risk management and trade intervals. While the strategy has certain advantages, its success largely depends on correctly identifying the trading range. Future optimization directions include combining other technical indicators, introducing dynamic stop-loss and take-profit levels, using more advanced range identification techniques, and adding a trend filter. When applying the strategy in practice, be sure to adjust the parameters and risk management rules according to personal preferences and risk tolerance.
/*backtest
start: 2023-06-11 00:00:00
end: 2024-06-16 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Range Trading with Stochastic", overlay=true)
// Input Parameters
overboughtLevel = input.int(80, title="Overbought Level", minval=1, maxval=100)
oversoldLevel = input.int(20, title="Oversold Level", minval=1, maxval=100)
stochLength = input.int(14, title="Stochastic Length", minval=1)
riskPerTrade = input.float(0.01, title="Risk per Trade (%)", minval=0.01, maxval=100, step=0.01)
barsBetweenTrades = input.int(20, title="Bars Between Trades", minval=1)
// Calculate Stochastic Oscillator
k = ta.sma(ta.stoch(close, high, low, stochLength), 3)
d = ta.sma(k, 3)
// Variables to Track Time Since Last Trade
var lastTradeBar = 0
barsSinceLastTrade = bar_index - lastTradeBar
// Risk Management
atr = ta.atr(14)
stopLoss = 2 * atr
takeProfit = 2 * atr
riskAmount = strategy.equity * riskPerTrade / 100
positionSize = 1
// Entry Conditions
longCondition = k < oversoldLevel and strategy.position_size == 0 and barsSinceLastTrade >= barsBetweenTrades
shortCondition = k > overboughtLevel and strategy.position_size == 0 and barsSinceLastTrade >= barsBetweenTrades
// Entry/Exit Orders
if longCondition
strategy.entry("Long", strategy.long, qty=positionSize)
strategy.exit("Long Exit", "Long", stop=close - stopLoss, limit=close + takeProfit)
lastTradeBar := bar_index // Update last trade bar
if shortCondition
strategy.entry("Short", strategy.short, qty=positionSize)
strategy.exit("Short Exit", "Short", stop=close + stopLoss, limit=close - takeProfit)
lastTradeBar := bar_index // Update last trade bar
// Plot Stochastic
plot(k, color=color.blue, title="%K")
plot(d, color=color.orange, title="%D")
hline(overboughtLevel, color=color.red, title="Overbought")
hline(oversoldLevel, color=color.green, title="Oversold")