I am trying to create pinescript version 5 code that combines three different trading strategies: trend following, mean reversion, and breakout.
The trend following strategy uses a simple moving average (SMA) with a length of 20 and a multiplier of 2 to calculate upper and lower bands. The current trend is determined based on whether the price is above or below these bands.
The mean reversion strategy uses two SMAs with lengths of 10 and 30 to determine whether the current price is overbought or oversold. A long position is taken when the short SMA crosses above the long SMA, and a short position is taken when the short SMA crosses below the long SMA.
The breakout strategy uses an exponential moving average (EMA) with a lookback period of 20 and an ATR multiple of 1.5 to calculate upper and lower bands. A long position is taken when the price crosses above the upper band, and a short position is taken when the price crosses below the lower band.
The entry condition for this strategy is a combination of the trend following and breakout strategies. A long position is taken when the current trend is up and there is a breakout above the upper band or a mean reversion buy signal. A short position is taken when the current trend is down and there is a breakout below the lower band or a mean reversion sell signal.
The exit condition for a long position is a mean reversion sell signal or a trend following sell signal. The exit condition for a short position is a mean reversion buy signal or a trend following buy signal.
The maximum allowed drawdown is set to 10%, and if the drawdown exceeds this value, all positions are closed.
The strategy plots the SMA and upper/lower bands for the trend following strategy, the two SMAs for the mean reversion strategy, and the EMA and upper/lower bands for the breakout strategy. The close price is also plotted.
However the code does not compile as I keep getting this "Undeclared identifier 'strategy'" error. Any help would be greatly appreciated. The code is attached below.
//@version=5
strategy(title="Combined Strategies", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=80, currency=currency.NONE, initial_capital=100)
// Set the maximum allowed drawdown to 10%
max_drawdown = 10
// Calculate the current equity and maximum equity
var equity = strategy.equity
var max_equity = math.max(strategy.closedtrades.profit + equity, ta.max(nz(max_equity[1], 0)))
// Calculate the current drawdown as a percentage of the maximum equity
var drawdown = 100 * (max_equity - equity) / max_equity
// Stop trading if the drawdown exceeds the maximum allowed drawdown
if drawdown >= max_drawdown
strategy.close_all()
// Trend Following Strategy
var len = input(20, title="Length")
var mult = input(2.0, title="Multiplier")
var basis = ta.sma(close, len)
var dev = mult * ta.stdev(close, len)
var upper = basis + dev
var lower = basis - dev
var trendUp = close > upper
var trendDown = close < lower
var trend = 0
trend := nz(trend[1], trend)
trend := trendUp ? 1 : trendDown ? -1 : trend
// Mean Reversion Strategy
var shortLen = input(10, title="Short Length")
var longLen = input(30, title="Long Length")
var maShort = ta.sma(close, shortLen)
var maLong = ta.sma(close, longLen)
var longCondition = ta.crossover(maShort, maLong)
var shortCondition = ta.crossunder(maShort, maLong)
// Breakout Strategy
var lookback = input.int(title="Lookback Period", defval=20)
var nATR = input.float(title="ATR Multiple", step=0.1, defval=1.5)
var atr = ta.atr(lookback)
var upperBand = ta.ema(close, lookback) + nATR * atr
var lowerBand = ta.ema(close, lookback) - nATR * atr
var breakout = close > upperBand or close < lowerBand
// Entry and Exit conditions
var longCondition2 = trend > 0 and longCondition
var shortCondition2 = trend < 0 and shortCondition
var entryCondition = breakout and (longCondition2 or shortCondition2)
var longExit = trend < 0 and shortCondition
var shortExit = trend > 0 and longCondition
// Trading logic
plot(basis, color=color.blue, title="Basis")
var upperPlot = plot(upper, color=color.red, title="Upper")
var lowerPlot = plot(lower, color=color.lime, title="Lower")
fill(upperPlot, lowerPlot, color=color.new(color.purple, 80), title="Background")
plot(maShort, color=color.orange, title="MA Short")
plot(maLong, color=color.fuchsia, title="MA Long")
plot(upperBand, color=color.green, title="Upper Band")
plot(lowerBand, color=color.red, title="Lower Band")
plot(close, color=color.blue, title="Close")
I am expecting the code to compile with no errors and plot the three strategies on a single tradingview graph so I can test the strategy on historical data before trying it in the live market.
Your error comes from the use of strategy.closedtrades.profit which must have the trade number as variable.
strategy.closedtrades.profit(trade_num)
Then you have another error in the same line as you try to define for the first time your variable max_equity and reference from it at the same time with max_equity[1].
You should first declare your variable, then reference to it after.
You can change your code with this working part :
// Calculate the current equity and maximum equity
var equity = strategy.equity
var max_equity = 0.0
max_equity := math.max(strategy.closedtrades.profit(strategy.closedtrades) + equity, ta.max(nz(max_equity[1], 0)))