Pine Script v4 · Strategy
2nd Grade Strategy
A Pine Script v4 strategy built on SMA or Stoch or EMA or RMA or WMA or VWMA (1, 1), SMA or EMA or RMA or WMA or VWMA (1, 1) and Crossover (K, D). It has 5 order rules, 2 entries among them, and trades long and short with the default settings. Exits wait for a signal; there is no stop loss. Each trade commits 10% of equity and pays no commission. 24 settings; defaults include Source close, Moving Average Type SMA and %K Length 10.
Generated from the script's code by a template: every clause is a fact read from the source. Not written by a person or a model, and not taken from the author's description.
- Does 2nd Grade Strategy repaint?
- Yes
- At least one pattern in the source can repaint: intrabar recalculation.
- How did 2nd Grade Strategy do on BTCUSDT 1D with default settings?
- Not measured
- It closed no trades there from 26 Jan 2024 to 21 Oct 2026 with the default settings, so there is no result to report. Its code limits trading to a date window, which can leave this period out. It closed no trades on the other markets either. A backtest, not investment advice.
- Can 2nd Grade Strategy be converted to Pine Script v5?
- No
- The automatic step from v4 to v5 gives a script that does not run in v5 (Pine Script v5 Error at @L7:C51: No value assigned to the `transp` parameter in color.new()), so no v5 download is offered; the edits are shown below.
- Does 2nd Grade Strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does 2nd Grade Strategy run in Zeal Terminal?
- Yes
- It compiles in 116 ms and runs 1,000 bars in 154 ms, in the browser.
BTCUSDT · 1D · default settings
- Overbought
- Centerline
- Oversold
- K
- D
- RSI
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- 0.00%
- $0.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 0
- 0 won · 0 lost
- Profitable
- 0.0%
- of closed trades
- Profit factor
- —
- gross profit ÷ gross loss
- Max drawdown
- 0.00%
- $0.00
One run with the author's default settings, $100,000 starting capital, commission as the script sets it. A single backtest says little about future results and is not a recommendation. Backtests are simulations on historical data and are not investment advice.
Review
What a careful reader of this script would point out, found by checking the source and the backtest.
-
Likely bug
sma(1, 1) ignores the settings line 19
Line 19 hard-codes 1 where line 22 uses “%K Smoothing”. Changing those settings moves one band but not this one.
sma(1,1)sma(stoch(source, high, low, Length), SmoothK) -
Likely bug
sma(1, 1) ignores the settings line 20
Line 20 hard-codes 1 where line 22 uses “%K Smoothing”. Changing those settings moves one band but not this one.
sma(1,1)sma(stoch(source, high, low, Length), SmoothK) -
Check
No stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses an argument v6 removed
when = on line 146, 147, 148, 149. The upgraded version below moves each call inside an if block.
-
Note
Uses iff(), which v5 removed line 129
iff() on line 129, 130. v5 writes it as the ternary c ? a : b.
-
Note
Uses transp =, which v5 removed line 7
transp = on line 7, 8, 9, 39, 40, 41, 42, 49, 97, 97, 97, 97, 118, 120, 122, 124, 126, 152, 153. v5 folds the transparency into the colour with color.new(c, transp).
Upgrade and export
Written in Pine Script v4. The upgrade to v5 takes 1 step, one per version, each applied to the result of the one before. A last clean-v5 pass then types the settings and moves when = into if blocks, which v6 requires. Every step runs before and after on the same candles in Zeal Terminal's engine; a step whose results differ is shown but not offered as a download. Upgrades preserve behaviour: bugs are fixed in the Review, never silently here.
- Upgrading to v5 renames sma → ta.sma, stoch → ta.stoch, ema → ta.ema, rma → ta.rma, wma → ta.wma, vwma → ta.vwma, crossover → ta.crossover, crossunder → ta.crossunder, rsi → ta.rsi, highest → ta.highest, floor → math.floor.
- 4 strategy calls use the when = argument (line 146, 147, 148, 149). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
DiffersThe v5 version does not run in Zeal Terminal's engine: Pine Script v5 Error at @L7:C51: No value assigned to the `transp` parameter in color.new()
- Line 3: //@version=4 → //@version=5
- Line 7: transp = 50 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 8: transp = 50 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 9: transp = 50 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 19: sma → ta.sma
- Line 20: sma → ta.sma
70 more edits
- Line 22: sma → ta.sma
- Line 22: stoch → ta.stoch
- Line 23: sma → ta.sma
- Line 25: ema → ta.ema
- Line 25: stoch → ta.stoch
- Line 26: ema → ta.ema
- Line 28: rma → ta.rma
- Line 28: stoch → ta.stoch
- Line 29: rma → ta.rma
- Line 31: wma → ta.wma
- Line 31: stoch → ta.stoch
- Line 32: wma → ta.wma
- Line 34: vwma → ta.vwma
- Line 34: stoch → ta.stoch
- Line 35: vwma → ta.vwma
- Line 36: crossover → ta.crossover
- Line 37: crossunder → ta.crossunder
- Line 39: transp = 50 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 40: transp = 50 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 41: transp = 50 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 42: transp = 50 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 47: input(type = input.integer) → input.int()
- Line 48: rsi → ta.rsi
- Line 49: transp = 50 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 59: sma → ta.sma
- Line 60: sma → ta.sma
- Line 61: sma → ta.sma
- Line 63: sma → ta.sma
- Line 64: sma → ta.sma
- Line 65: sma → ta.sma
- Line 67: ema → ta.ema
- Line 68: ema → ta.ema
- Line 69: ema → ta.ema
- Line 71: rma → ta.rma
- Line 72: rma → ta.rma
- Line 73: rma → ta.rma
- Line 75: wma → ta.wma
- Line 76: wma → ta.wma
- Line 77: wma → ta.wma
- Line 79: vwma → ta.vwma
- Line 80: vwma → ta.vwma
- Line 81: vwma → ta.vwma
- Line 83: sma → ta.sma
- Line 85: sma → ta.sma
- Line 87: ema → ta.ema
- Line 89: rma → ta.rma
- Line 91: wma → ta.wma
- Line 93: vwma → ta.vwma
- Line 95: highest → ta.highest
- Line 97: transp = 80 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 97: transp = 80 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 97: transp = 80 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 97: transp = 80 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 118: transp = 0 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 120: transp = 50 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 122: transp = 50 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 124: transp = 50 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 126: transp = 50 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 129: iff(c, a, b) → (c ? a : b)
- Line 130: iff(c, a, b) → (c ? a : b)
- Line 134: input(type = input.integer) → input.int()
- Line 135: input(type = input.integer) → input.int()
- Line 136: input(type = input.integer) → input.int()
- Line 137: input(type = input.integer) → input.int()
- Line 138: input(type = input.integer) → input.int()
- Line 139: input(type = input.integer) → input.int()
- Line 146: floor → math.floor
- Line 147: floor → math.floor
- Line 152: transp = 100 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 153: transp = 100 removed: the call sets no colour to fold it into, so its default colour loses the transparency
⋯// © cyrule-//@version=4+//@version=5strategy("2nd Grade Strategy", overlay=false, shorttitle="2GTS", max_lines_count = 500, max_labels_count = 500, calc_on_every_tick = true, calc_on_order_fills = true, pyramiding = 1, default_qty_type = strategy.percent_of_equity, default_qty_value = 10)⋯maType = input(title = "Moving Average Type", defval="SMA", options=["SMA", "EMA", "RMA", "WMA", "VWMA"])-hline( 30, linestyle = hline.style_dotted, color = color.new(color.gray, transp = 50), linewidth = 2, title = "Overbought")+hline( 30, linestyle = hline.style_dotted, color = color.new(color.gray), linewidth = 2, title = "Overbought")-hline( 0, linestyle = hline.style_solid , color = color.new(color.black, transp = 50), linewidth = 1, title = "Centerline")+hline( 0, linestyle = hline.style_solid , color = color.new(color.black), linewidth = 1, title = "Centerline")-hline(-30, linestyle = hline.style_dotted, color = color.new(color.gray, transp = 50), linewidth = 2, title = "Oversold")+hline(-30, linestyle = hline.style_dotted, color = color.new(color.gray), linewidth = 2, title = "Oversold")// *******************⋯// Calculate Stochastic-K = sma(1,1)+K = ta.sma(1,1)-D = sma(1,1)+D = ta.sma(1,1)if (maType == "SMA")- K := sma(stoch(source, high, low, Length), SmoothK)+ K := ta.sma(ta.stoch(source, high, low, Length), SmoothK)- D := sma(K, SmoothD)+ D := ta.sma(K, SmoothD)if (maType == "EMA")- K := ema(stoch(source, high, low, Length), SmoothK)+ K := ta.ema(ta.stoch(source, high, low, Length), SmoothK)- D := ema(K, SmoothD)+ D := ta.ema(K, SmoothD)if (maType == "RMA")- K := rma(stoch(source, high, low, Length), SmoothK)+ K := ta.rma(ta.stoch(source, high, low, Length), SmoothK)- D := rma(K, SmoothD)+ D := ta.rma(K, SmoothD)if (maType == "WMA")- K := wma(stoch(source, high, low, Length), SmoothK)+ K := ta.wma(ta.stoch(source, high, low, Length), SmoothK)- D := wma(K, SmoothD)+ D := ta.wma(K, SmoothD)if (maType == "VWMA")- K := vwma(stoch(source, high, low, Length), SmoothK)+ K := ta.vwma(ta.stoch(source, high, low, Length), SmoothK)- D := vwma(K, SmoothD)+ D := ta.vwma(K, SmoothD)-StochasticCrossOver = crossover(K, D)+StochasticCrossOver = ta.crossover(K, D)-StochasticCrossUnder = crossunder(K, D)+StochasticCrossUnder = ta.crossunder(K, D)// Lukis SS-plotshape(showSSC and StochasticCrossOver and K <= 20 ? K : na, text = "Golden\nCrossover", color = color.new(color.green, transp = 50), location = location.top, size = size.tiny)+plotshape(showSSC and StochasticCrossOver and K <= 20 ? K : na, text = "Golden\nCrossover", color = color.new(color.green), location = location.top, size = size.tiny)-plotshape(showSSC and StochasticCrossUnder and K >= 80 ? D : na, text = "Deadly\nCrossover", color = color.new(color.red, transp = 50) , location = location.top, size = size.tiny)+plotshape(showSSC and StochasticCrossUnder and K >= 80 ? D : na, text = "Deadly\nCrossover", color = color.new(color.red) , location = location.top, size = size.tiny)-plot(series = showSS ? K - 50 : na, color = color.new(#0072E8, transp = 50), linewidth = 2, title = "K")+plot(series = showSS ? K - 50 : na, color = color.new(#0072E8), linewidth = 2, title = "K")-plot(series = showSS ? D - 50 : na, color = color.new(#9F1C24, transp = 50), linewidth = 2, title = "D")+plot(series = showSS ? D - 50 : na, color = color.new(#9F1C24), linewidth = 2, title = "D")// ***************************⋯showRSI = input(true, title = "Show RSI?", group = "Relative Strength Index")-rsiLength = input(title = "RSI Length", type = input.integer, defval = 14, group = "Relative Strength Index")+rsiLength = input.int(title = "RSI Length", defval = 14, group = "Relative Strength Index")-rsiValue = rsi(source, rsiLength)+rsiValue = ta.rsi(source, rsiLength)-plot(showRSI ? rsiValue - 50 : na, color = color.new(color.fuchsia, transp = 50), linewidth = 2, title = "RSI")+plot(showRSI ? rsiValue - 50 : na, color = color.new(color.fuchsia), linewidth = 2, title = "RSI")// ********⋯// MACD Calculation-fastMA = sma(source, fastMACDLength)+fastMA = ta.sma(source, fastMACDLength)-slowMA = sma(source, slowMACDLength)+slowMA = ta.sma(source, slowMACDLength)-filterMA = sma(source, signalMACDLength)+filterMA = ta.sma(source, signalMACDLength)if (maType == "SMA")- fastMA := sma(source, fastMACDLength)+ fastMA := ta.sma(source, fastMACDLength)- slowMA := sma(source, slowMACDLength)+ slowMA := ta.sma(source, slowMACDLength)- filterMA := sma(source, signalMACDLength) + filterMA := ta.sma(source, signalMACDLength) if (maType == "EMA")- fastMA := ema(source, fastMACDLength)+ fastMA := ta.ema(source, fastMACDLength)- slowMA := ema(source, slowMACDLength)+ slowMA := ta.ema(source, slowMACDLength)- filterMA := ema(source, signalMACDLength) + filterMA := ta.ema(source, signalMACDLength) if (maType == "RMA")- fastMA := rma(source, fastMACDLength)+ fastMA := ta.rma(source, fastMACDLength)- slowMA := rma(source, slowMACDLength)+ slowMA := ta.rma(source, slowMACDLength)- filterMA := rma(source, signalMACDLength) + filterMA := ta.rma(source, signalMACDLength) if (maType == "WMA")- fastMA := wma(source, fastMACDLength)+ fastMA := ta.wma(source, fastMACDLength)- slowMA := wma(source, slowMACDLength)+ slowMA := ta.wma(source, slowMACDLength)- filterMA := wma(source, signalMACDLength) + filterMA := ta.wma(source, signalMACDLength) if (maType == "VWMA")- fastMA := vwma(source, fastMACDLength)+ fastMA := ta.vwma(source, fastMACDLength)- slowMA := vwma(source, slowMACDLength)+ slowMA := ta.vwma(source, slowMACDLength)- filterMA := vwma(source, signalMACDLength) + filterMA := ta.vwma(source, signalMACDLength) macd = fastMA - slowMA-macdSignal = sma(macd, signalMACDLength)+macdSignal = ta.sma(macd, signalMACDLength)if (maType == "SMA")- macdSignal := sma (macd, signalMACDLength)+ macdSignal := ta.sma (macd, signalMACDLength)if (maType == "EMA")- macdSignal := ema (macd, signalMACDLength)+ macdSignal := ta.ema (macd, signalMACDLength)if (maType == "RMA")- macdSignal := rma (macd, signalMACDLength)+ macdSignal := ta.rma (macd, signalMACDLength)if (maType == "WMA")- macdSignal := wma (macd, signalMACDLength)+ macdSignal := ta.wma (macd, signalMACDLength)if (maType == "VWMA")- macdSignal := vwma(macd, signalMACDLength)+ macdSignal := ta.vwma(macd, signalMACDLength)hist = macd - macdSignal-maxHist = highest(hist, 252) // Look back 252 bars to be used for normalization factor+maxHist = ta.highest(hist, 252) // Look back 252 bars to be used for normalization factorhist := hist / maxHist * 100 / normalizeMACD-histColor = hist < 0 ? hist < hist[1] ? color.new(color.maroon, transp = 80) : color.new(color.red, transp = 80) : hist < hist[1] ? color.new(color.olive, transp = 80) : color.new(color.green, transp = 80)+histColor = hist < 0 ? hist < hist[1] ? color.new(color.maroon) : color.new(color.red) : hist < hist[1] ? color.new(color.olive) : color.new(color.green)plot(showMACD ? hist : na, style = plot.style_columns, color = histColor)⋯ bearishCriteria := bearishCriteria + 1-signal = color.new(color.white, transp = 0)+signal = color.new(color.white)if bearishCriteria == 2- signal := color.new(color.orange, transp = 50)+ signal := color.new(color.orange)if bearishCriteria == 3- signal := color.new(color.red, transp = 50)+ signal := color.new(color.red)if bullishCriteria == 2- signal := color.new(color.aqua, transp = 50)+ signal := color.new(color.aqua)if bullishCriteria == 3- signal := color.new(color.green, transp = 50)+ signal := color.new(color.green)bullishCriteria := showBull ? bullishCriteria : 0bearishCriteria := showBear ? bearishCriteria : 0-bgcolor(iff(bullishCriteria > 1, signal, na), title = 'Bullish Signal')+bgcolor((bullishCriteria > 1 ? signal : na), title = 'Bullish Signal')-bgcolor(iff(bearishCriteria > 1, signal, na), title = 'Bearish Signal')+bgcolor((bearishCriteria > 1 ? signal : na), title = 'Bearish Signal')// ***********⋯// ***********-startDate = input(title="Start Date", type=input.integer, defval=1, minval=1, maxval=31, group = "Trading")+startDate = input.int(title="Start Date", defval=1, minval=1, maxval=31, group = "Trading")-startMonth = input(title="Start Month", type=input.integer, defval=1, minval=1, maxval=12, group = "Trading")+startMonth = input.int(title="Start Month", defval=1, minval=1, maxval=12, group = "Trading")-startYear = input(title="Start Year", type=input.integer, defval=2019, minval=1900, maxval=2100, group = "Trading")+startYear = input.int(title="Start Year", defval=2019, minval=1900, maxval=2100, group = "Trading")-endDate = input(title="End Date", type=input.integer, defval=31, minval=1, maxval=31, group = "Trading")+endDate = input.int(title="End Date", defval=31, minval=1, maxval=31, group = "Trading")-endMonth = input(title="End Month", type=input.integer, defval=12, minval=1, maxval=12, group = "Trading")+endMonth = input.int(title="End Month", defval=12, minval=1, maxval=12, group = "Trading")-endYear = input(title="End Year", type=input.integer, defval=2019, minval=1800, maxval=2100, group = "Trading")+endYear = input.int(title="End Year", defval=2019, minval=1800, maxval=2100, group = "Trading")longTPPerc = input(title = "Take Profit Threshold (%)" , minval = 0.0, step = 0.5, defval = 2.5, group = "Trading") / 100⋯inDateRange = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) and (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))-strategy.entry("Long" , strategy.long , floor(strategy.initial_capital*.1/close), stop = strategy.position_avg_price * 1.25, when = inDateRange and bullishCriteria > 1)+strategy.entry("Long" , strategy.long , math.floor(strategy.initial_capital*.1/close), stop = strategy.position_avg_price * 1.25, when = inDateRange and bullishCriteria > 1)-strategy.entry("Short", strategy.short, floor(strategy.initial_capital*.1/close), stop = strategy.position_avg_price * 1.25, when = inDateRange and bearishCriteria > 1)+strategy.entry("Short", strategy.short, math.floor(strategy.initial_capital*.1/close), stop = strategy.position_avg_price * 1.25, when = inDateRange and bearishCriteria > 1)strategy.close("Long" , when = (open >= takeProfit) or (open <= stopLoss) or (high >= takeProfit) or (low <= stopLoss))⋯ strategy.close_all()-plotshape(bullishCriteria, location = location.top, color = color.new(color.black, transp = 100))+plotshape(bullishCriteria, location = location.top, color = color.new(color.black))-plotshape(bearishCriteria, location = location.top, color = color.new(color.black, transp = 100))+plotshape(bearishCriteria, location = location.top, color = color.new(color.black))Clean v5
Not verifiedThe v5 script does not run in Zeal Terminal's engine (Pine Script v5 Error at @L7:C51: No value assigned to the `transp` parameter in color.new()), so this step could not be checked.
- Line 5: input() → input.source()
- Line 6: input() → input.string()
- Line 13: input() → input.bool()
- Line 14: input() → input.bool()
- Line 15: input() → input.int()
- Line 16: input() → input.int()
15 more edits
- Line 17: input() → input.int()
- Line 46: input() → input.bool()
- Line 53: input() → input.bool()
- Line 54: input() → input.int()
- Line 55: input() → input.int()
- Line 56: input() → input.int()
- Line 57: input() → input.int()
- Line 102: input() → input.bool()
- Line 103: input() → input.bool()
- Line 140: input() → input.float()
- Line 141: input() → input.float()
- Line 146: when = inDateRange and bullishCriteria > 1 → if block
- Line 147: when = inDateRange and bearishCriteria > 1 → if block
- Line 148: when = (open >= takeProfit) or (open <= stopLoss) or (high >= takeProfit) or (low <= stopLoss) → if block
- Line 149: when = (open >= takeProfit) or (open <= stopLoss) or (high >= takeProfit) or (low <= stopLoss) → if block
⋯strategy("2nd Grade Strategy", overlay=false, shorttitle="2GTS", max_lines_count = 500, max_labels_count = 500, calc_on_every_tick = true, calc_on_order_fills = true, pyramiding = 1, default_qty_type = strategy.percent_of_equity, default_qty_value = 10)-source = input(close, title = "Source")+source = input.source(close, title = "Source")-maType = input(title = "Moving Average Type", defval="SMA", options=["SMA", "EMA", "RMA", "WMA", "VWMA"])+maType = input.string(title = "Moving Average Type", defval="SMA", options=["SMA", "EMA", "RMA", "WMA", "VWMA"])hline( 30, linestyle = hline.style_dotted, color = color.new(color.gray), linewidth = 2, title = "Overbought")⋯// *******************-showSS = input(true , title = "Show Stochastic?" , group = "Stochastic")+showSS = input.bool(true , title = "Show Stochastic?" , group = "Stochastic")-showSSC = input(true , title = "Show Stochastic Crossovers?", group = "Stochastic")+showSSC = input.bool(true , title = "Show Stochastic Crossovers?", group = "Stochastic")-Length = input(10, minval = 1, title = "%K Length" , group = "Stochastic")+Length = input.int(10, minval = 1, title = "%K Length" , group = "Stochastic")-SmoothK = input(3, minval = 1, title = "%K Smoothing" , group = "Stochastic")+SmoothK = input.int(3, minval = 1, title = "%K Smoothing" , group = "Stochastic")-SmoothD = input(3, minval = 1, title = "%D Smoothing" , group = "Stochastic")+SmoothD = input.int(3, minval = 1, title = "%D Smoothing" , group = "Stochastic")// Calculate Stochastic⋯// ***************************-showRSI = input(true, title = "Show RSI?", group = "Relative Strength Index")+showRSI = input.bool(true, title = "Show RSI?", group = "Relative Strength Index")rsiLength = input.int(title = "RSI Length", defval = 14, group = "Relative Strength Index")⋯// ********-showMACD = input(true, title = "Show MACD?", group = "MACD")+showMACD = input.bool(true, title = "Show MACD?", group = "MACD")-fastMACDLength = input(12, minval = 1, title = "MACD fast moving average", group = "MACD")+fastMACDLength = input.int(12, minval = 1, title = "MACD fast moving average", group = "MACD")-slowMACDLength = input(26,minval = 1, title = "MACD slow moving average", group = "MACD")+slowMACDLength = input.int(26,minval = 1, title = "MACD slow moving average", group = "MACD")-signalMACDLength = input(9,minval = 1, title = "MACD signal line moving average", group = "MACD")+signalMACDLength = input.int(9,minval = 1, title = "MACD signal line moving average", group = "MACD")-normalizeMACD = input(3, minval = 0.25, step = 0.25, title = "Normalization factor", group = "MACD")+normalizeMACD = input.int(3, minval = 0.25, step = 0.25, title = "Normalization factor", group = "MACD")// MACD Calculation⋯// **************-showBull = input(false, title = "Show Bullish Signal?", group = "Signal")+showBull = input.bool(false, title = "Show Bullish Signal?", group = "Signal")-showBear = input(false, title = "Show Bearish Signal?", group = "Signal")+showBear = input.bool(false, title = "Show Bearish Signal?", group = "Signal")bullishCriteria = 0⋯endYear = input.int(title="End Year", defval=2019, minval=1800, maxval=2100, group = "Trading")-longTPPerc = input(title = "Take Profit Threshold (%)" , minval = 0.0, step = 0.5, defval = 2.5, group = "Trading") / 100+longTPPerc = input.float(title = "Take Profit Threshold (%)" , minval = 0.0, step = 0.5, defval = 2.5, group = "Trading") / 100-profitRatio = input(title = "Profit-to-Loss ratio (risk tolerance)", minval = 1.0, step = 0.1, defval = 1.4, group = "Trading")+profitRatio = input.float(title = "Profit-to-Loss ratio (risk tolerance)", minval = 1.0, step = 0.1, defval = 1.4, group = "Trading")longSLPerc = longTPPerc / profitRatio⋯inDateRange = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) and (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))-strategy.entry("Long" , strategy.long , math.floor(strategy.initial_capital*.1/close), stop = strategy.position_avg_price * 1.25, when = inDateRange and bullishCriteria > 1)+if inDateRange and bullishCriteria > 1+ strategy.entry("Long" , strategy.long , math.floor(strategy.initial_capital*.1/close), stop = strategy.position_avg_price * 1.25)-strategy.entry("Short", strategy.short, math.floor(strategy.initial_capital*.1/close), stop = strategy.position_avg_price * 1.25, when = inDateRange and bearishCriteria > 1)+if inDateRange and bearishCriteria > 1+ strategy.entry("Short", strategy.short, math.floor(strategy.initial_capital*.1/close), stop = strategy.position_avg_price * 1.25)-strategy.close("Long" , when = (open >= takeProfit) or (open <= stopLoss) or (high >= takeProfit) or (low <= stopLoss))+if (open >= takeProfit) or (open <= stopLoss) or (high >= takeProfit) or (low <= stopLoss)+ strategy.close("Long" )-strategy.close("Short", when = (open >= takeProfit) or (open <= stopLoss) or (high >= takeProfit) or (low <= stopLoss))+if (open >= takeProfit) or (open <= stopLoss) or (high >= takeProfit) or (low <= stopLoss)+ strategy.close("Short")if (not inDateRange)⋯Does it run in Pine Script v6?
Not tested
The v5 script does not run in Zeal Terminal's engine (Pine Script v5 Error at @L7:C51: No value assigned to the `transp` parameter in color.new()), so this step could not be checked. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © cyrule-//@version=5+//@version=6strategy("2nd Grade Strategy", overlay=false, shorttitle="2GTS", max_lines_count = 500, max_labels_count = 500, calc_on_every_tick = true, calc_on_order_fills = true, pyramiding = 1, default_qty_type = strategy.percent_of_equity, default_qty_value = 10)⋯Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Source | close | source | K rsiValue fastMA slowMA filterMA |
| Moving Average Type | SMA | string · options ["SMA", "EMA", "RMA", "WMA", "VWMA"] | line 21 line 24 line 27 line 30 line 33 line 62 line 66 line 70 line 74 line 78 line 84 line 86 line 88 line 90 line 92 |
| Stochastic | |||
| Show Stochastic? | true | bool | K D |
| Show Stochastic Crossovers? | true | bool | plotshape |
| %K Length | 10 | int · min 1 | K |
| %K Smoothing | 3 | int · min 1 | K |
| %D Smoothing | 3 | int · min 1 | D |
| Relative Strength Index | |||
| Show RSI? | true | bool | RSI |
| RSI Length | 14 | int (written input.integer) | rsiValue |
| MACD | |||
| Show MACD? | true | bool | plot |
| MACD fast moving average | 12 | int · min 1 | fastMA |
| MACD slow moving average | 26 | int · min 1 | slowMA |
| MACD signal line moving average | 9 | int · min 1 | filterMA macdSignal |
| Normalization factor | 3 | int · min 0.25 · step 0.25 | hist |
| Signal | |||
| Show Bullish Signal? | false | bool | bullishCriteria |
| Show Bearish Signal? | false | bool | bearishCriteria |
| Trading | |||
| Start Date | 1 | int (written input.integer) · min 1 · max 31 | inDateRange |
| Start Month | 1 | int (written input.integer) · min 1 · max 12 | inDateRange |
| Start Year | 2019 | int (written input.integer) · min 1900 · max 2100 | inDateRange |
| End Date | 31 | int (written input.integer) · min 1 · max 31 | inDateRange |
| End Month | 12 | int (written input.integer) · min 1 · max 12 | inDateRange |
| End Year | 2019 | int (written input.integer) · min 1800 · max 2100 | inDateRange |
| Take Profit Threshold (%) | 2.5 | float · min 0.0 · step 0.5 | longSLPerc takeProfit |
| Profit-to-Loss ratio (risk tolerance) | 1.4 | float · min 1.0 · step 0.1 | longSLPerc |
What it calculates
- SMA / Stoch / EMA / RMA / WMA / VWMA (
sma,ta.smain v5)1, 1K · line 19 - SMA / EMA / RMA / WMA / VWMA (
sma,ta.smain v5)1, 1D · line 20 - Crossover (
crossover,ta.crossoverin v5)K, DStochasticCrossOver · line 36 - Crossunder (
crossunder,ta.crossunderin v5)K, DStochasticCrossUnder · line 37 - RSI (
rsi,ta.rsiin v5)Source (close), RSI Length (14)rsiValue · line 48 - SMA / EMA / RMA / WMA / VWMA (
sma,ta.smain v5)Source (close), MACD fast moving average (12)fastMA · line 59 - SMA / EMA / RMA / WMA / VWMA (
sma,ta.smain v5)Source (close), MACD slow moving average (26)slowMA · line 60 - SMA / EMA / RMA / WMA / VWMA (
sma,ta.smain v5)Source (close), MACD signal line moving average (9)filterMA · line 61 - SMA / EMA / RMA / WMA / VWMA (
sma,ta.smain v5)macd, MACD signal line moving average (9)macdSignal · line 83 - Highest (
highest,ta.highestin v5)hist, 252maxHist · line 95
Signal flow
How price data and settings reach the orders. Each box is a name in the script, or several computed the same way; each arrow is a value one of them reads. Hover a box for its members.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Long long | inDateRange and bullishCriteria > 1 | On | 146 |
| Open Short short | inDateRange and bearishCriteria > 1 | On | 147 |
| Close Long | (open >= takeProfit) or (open <= stopLoss) or (high >= takeProfit) or (low <= stopLoss) | On | 148 |
| Close Short | (open >= takeProfit) or (open <= stopLoss) or (high >= takeProfit) or (low <= stopLoss) | On | 149 |
| Close | (not inDateRange) | On | 151 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
Yes
At least one pattern in the source can repaint; it is marked Check below.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- CheckIntrabar recalculationcalc_on_every_tick = true. Live values can differ from history.
- ClearOrder timingOrders fill at the next bar's open, after the signal bar has closed.
The checks read the source; the replay runs it. Together they catch the common causes, not every possible one.
Other markets
The same script with default settings on 8 Binance markets. Each row is a separate backtest over the most recent 1,000 bars.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | 0.0% | +102.5% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | 0.0% | +5.5% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | 0.0% | +9.8% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | 0.0% | +8.4% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | 0.0% | +16.7% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | 0.0% | +21.5% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | 0.0% | +148.3% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | 0.0% | +17.1% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
Similar scripts
Indicators and strategies built from the same calculations, run on BTCUSDT 1D with their own defaults so they can be compared directly.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| 2nd Grade Strategy this page | — | v4 strategy | 0.0% | 0 | 0.0% |
| Bear & Bull Zone Signal Strategy by Solutions1978 | rsi ema highest stoch sma vwma | v4 strategy | No trades on this market | ||
| VCP pivot buy by perrycc007 | sma ema rma vwma wma highest | v4 strategy | does not run in Zeal Terminal | ||
| All in One Strategy no RSI Label - For higher dollar crypto by Solutions1978 | rsi ema highest stoch sma vwma | v4 strategy | No trades on this market | ||
| All in One Strategy by Solutions1978 | rsi ema highest stoch sma vwma | v4 strategy | No trades on this market | ||
| MACD Willy Strategy by PtGambler | rma sma ema highest wma | v5 strategy | No trades on this market | ||
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 116 ms | Yes 1,000 bars in 154 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
“Matches TradingView” needs this script's own output exported from TradingView and compared bar by bar, which has not been done for this script. The engine is built on OpenPineScript, whose conformance results are in its conformance report. PineTS runs Pine Script v5 and v6 only.
Source
Copied verbatim from the author's published script, with its licence header intact. The author may have updated the original since this snapshot.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © cyrule//@version=4strategy("2nd Grade Strategy", overlay=false, shorttitle="2GTS", max_lines_count = 500, max_labels_count = 500, calc_on_every_tick = true, calc_on_order_fills = true, pyramiding = 1, default_qty_type = strategy.percent_of_equity, default_qty_value = 10)source = input(close, title = "Source")maType = input(title = "Moving Average Type", defval="SMA", options=["SMA", "EMA", "RMA", "WMA", "VWMA"])hline( 30, linestyle = hline.style_dotted, color = color.new(color.gray, transp = 50), linewidth = 2, title = "Overbought")hline( 0, linestyle = hline.style_solid , color = color.new(color.black, transp = 50), linewidth = 1, title = "Centerline")hline(-30, linestyle = hline.style_dotted, color = color.new(color.gray, transp = 50), linewidth = 2, title = "Oversold")// *******************// * Stochastic *// *******************showSS = input(true , title = "Show Stochastic?" , group = "Stochastic")showSSC = input(true , title = "Show Stochastic Crossovers?", group = "Stochastic")Length = input(10, minval = 1, title = "%K Length" , group = "Stochastic")SmoothK = input(3, minval = 1, title = "%K Smoothing" , group = "Stochastic")SmoothD = input(3, minval = 1, title = "%D Smoothing" , group = "Stochastic")// Calculate StochasticK = sma(1,1)D = sma(1,1)if (maType == "SMA") K := sma(stoch(source, high, low, Length), SmoothK) D := sma(K, SmoothD)if (maType == "EMA") K := ema(stoch(source, high, low, Length), SmoothK) D := ema(K, SmoothD)if (maType == "RMA") K := rma(stoch(source, high, low, Length), SmoothK) D := rma(K, SmoothD)if (maType == "WMA") K := wma(stoch(source, high, low, Length), SmoothK) D := wma(K, SmoothD)if (maType == "VWMA") K := vwma(stoch(source, high, low, Length), SmoothK) D := vwma(K, SmoothD)StochasticCrossOver = crossover(K, D)StochasticCrossUnder = crossunder(K, D)// Lukis SSplotshape(showSSC and StochasticCrossOver and K <= 20 ? K : na, text = "Golden\nCrossover", color = color.new(color.green, transp = 50), location = location.top, size = size.tiny)plotshape(showSSC and StochasticCrossUnder and K >= 80 ? D : na, text = "Deadly\nCrossover", color = color.new(color.red, transp = 50) , location = location.top, size = size.tiny)plot(series = showSS ? K - 50 : na, color = color.new(#0072E8, transp = 50), linewidth = 2, title = "K")plot(series = showSS ? D - 50 : na, color = color.new(#9F1C24, transp = 50), linewidth = 2, title = "D")// ***************************// * Relative Strength Index *// ***************************showRSI = input(true, title = "Show RSI?", group = "Relative Strength Index")rsiLength = input(title = "RSI Length", type = input.integer, defval = 14, group = "Relative Strength Index")rsiValue = rsi(source, rsiLength)plot(showRSI ? rsiValue - 50 : na, color = color.new(color.fuchsia, transp = 50), linewidth = 2, title = "RSI")// ********// * MACD *// ********showMACD = input(true, title = "Show MACD?", group = "MACD")fastMACDLength = input(12, minval = 1, title = "MACD fast moving average", group = "MACD")slowMACDLength = input(26,minval = 1, title = "MACD slow moving average", group = "MACD")signalMACDLength = input(9,minval = 1, title = "MACD signal line moving average", group = "MACD")normalizeMACD = input(3, minval = 0.25, step = 0.25, title = "Normalization factor", group = "MACD")// MACD CalculationfastMA = sma(source, fastMACDLength)slowMA = sma(source, slowMACDLength)filterMA = sma(source, signalMACDLength)if (maType == "SMA") fastMA := sma(source, fastMACDLength) slowMA := sma(source, slowMACDLength) filterMA := sma(source, signalMACDLength) if (maType == "EMA") fastMA := ema(source, fastMACDLength) slowMA := ema(source, slowMACDLength) filterMA := ema(source, signalMACDLength) if (maType == "RMA") fastMA := rma(source, fastMACDLength) slowMA := rma(source, slowMACDLength) filterMA := rma(source, signalMACDLength) if (maType == "WMA") fastMA := wma(source, fastMACDLength) slowMA := wma(source, slowMACDLength) filterMA := wma(source, signalMACDLength) if (maType == "VWMA") fastMA := vwma(source, fastMACDLength) slowMA := vwma(source, slowMACDLength) filterMA := vwma(source, signalMACDLength) macd = fastMA - slowMAmacdSignal = sma(macd, signalMACDLength)if (maType == "SMA") macdSignal := sma (macd, signalMACDLength)if (maType == "EMA") macdSignal := ema (macd, signalMACDLength)if (maType == "RMA") macdSignal := rma (macd, signalMACDLength)if (maType == "WMA") macdSignal := wma (macd, signalMACDLength)if (maType == "VWMA") macdSignal := vwma(macd, signalMACDLength)hist = macd - macdSignalmaxHist = highest(hist, 252) // Look back 252 bars to be used for normalization factorhist := hist / maxHist * 100 / normalizeMACDhistColor = hist < 0 ? hist < hist[1] ? color.new(color.maroon, transp = 80) : color.new(color.red, transp = 80) : hist < hist[1] ? color.new(color.olive, transp = 80) : color.new(color.green, transp = 80)plot(showMACD ? hist : na, style = plot.style_columns, color = histColor)// **************// * Signal *// **************showBull = input(false, title = "Show Bullish Signal?", group = "Signal")showBear = input(false, title = "Show Bearish Signal?", group = "Signal")bullishCriteria = 0if (K > D) and (K > K[1]) and (D > D[1]) bullishCriteria := bullishCriteria + 1if rsiValue > 50 bullishCriteria := bullishCriteria + 1if (hist > 0) and (hist > hist[1]) bullishCriteria := bullishCriteria + 1bearishCriteria = 0if (K < D) and (K < K[1]) and (D < D[1]) bearishCriteria := bearishCriteria + 1if rsiValue < 50 bearishCriteria := bearishCriteria + 1if (hist < 0) and (hist < hist[1]) bearishCriteria := bearishCriteria + 1signal = color.new(color.white, transp = 0)if bearishCriteria == 2 signal := color.new(color.orange, transp = 50)if bearishCriteria == 3 signal := color.new(color.red, transp = 50)if bullishCriteria == 2 signal := color.new(color.aqua, transp = 50)if bullishCriteria == 3 signal := color.new(color.green, transp = 50)bullishCriteria := showBull ? bullishCriteria : 0bearishCriteria := showBear ? bearishCriteria : 0bgcolor(iff(bullishCriteria > 1, signal, na), title = 'Bullish Signal')bgcolor(iff(bearishCriteria > 1, signal, na), title = 'Bearish Signal')// ***********// * Trading *// ***********startDate = input(title="Start Date", type=input.integer, defval=1, minval=1, maxval=31, group = "Trading")startMonth = input(title="Start Month", type=input.integer, defval=1, minval=1, maxval=12, group = "Trading")startYear = input(title="Start Year", type=input.integer, defval=2019, minval=1900, maxval=2100, group = "Trading")endDate = input(title="End Date", type=input.integer, defval=31, minval=1, maxval=31, group = "Trading")endMonth = input(title="End Month", type=input.integer, defval=12, minval=1, maxval=12, group = "Trading")endYear = input(title="End Year", type=input.integer, defval=2019, minval=1800, maxval=2100, group = "Trading")longTPPerc = input(title = "Take Profit Threshold (%)" , minval = 0.0, step = 0.5, defval = 2.5, group = "Trading") / 100profitRatio = input(title = "Profit-to-Loss ratio (risk tolerance)", minval = 1.0, step = 0.1, defval = 1.4, group = "Trading")longSLPerc = longTPPerc / profitRatiotakeProfit = strategy.position_avg_price * (1 + longTPPerc)stopLoss = strategy.position_avg_price * (1 - longSLPerc)inDateRange = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) and (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))strategy.entry("Long" , strategy.long , floor(strategy.initial_capital*.1/close), stop = strategy.position_avg_price * 1.25, when = inDateRange and bullishCriteria > 1)strategy.entry("Short", strategy.short, floor(strategy.initial_capital*.1/close), stop = strategy.position_avg_price * 1.25, when = inDateRange and bearishCriteria > 1)strategy.close("Long" , when = (open >= takeProfit) or (open <= stopLoss) or (high >= takeProfit) or (low <= stopLoss))strategy.close("Short", when = (open >= takeProfit) or (open <= stopLoss) or (high >= takeProfit) or (low <= stopLoss))if (not inDateRange) strategy.close_all()plotshape(bullishCriteria, location = location.top, color = color.new(color.black, transp = 100))plotshape(bearishCriteria, location = location.top, color = color.new(color.black, transp = 100))