Pine Script v5 · Strategy
Top 40 High Low Strategy for SPY, 5min
A Pine Script v5 strategy built on RMA (ta.tr, 14), SMA or EMA (src, 12) and Cross (macd, signal). It has 14 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss and a profit target. Each trade is one contract and pays 0.09 commission. 76 settings; defaults include Start Year 2011, Start Month 1 and Start Day 1.
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 Top 40 High Low Strategy for SPY, 5min repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did Top 40 High Low Strategy for SPY, 5min 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.
- Does Top 40 High Low Strategy for SPY, 5min run in Pine Script v6?
- Not tested
- Neither version placed a trade on any of the 8 markets, so there was nothing to compare. The v6 version is shown for reference and is not offered as a download.
- Does Top 40 High Low Strategy for SPY, 5min use a stop loss?
- Yes
- strategy.exit sets a stop on line 326, 327, 335, 337, 341, 343.
- Does Top 40 High Low Strategy for SPY, 5min run in Zeal Terminal?
- Yes
- It compiles in 128 ms and runs 1,000 bars in 556 ms, in the browser.
BTCUSDT · 1D · default settings
- Char
- Winning trade
- Losing trade
- Equity
- Buy and hold
Needs AAPL 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs MSFT 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs AMZN 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs GOOG 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs FB 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs TSLA 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs NVDA 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs BRK.B 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs JPM 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs UNH 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs JNJ 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs HD 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs PG 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs V 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs PFE 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs BAC 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs MA 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs ADBE 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs DIS 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs NFLX 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs TMO 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs XOM 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs CRM 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs CSCO 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs COST 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs ABT 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs PEP 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs ABBV 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs KO 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs PYPL 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs CVX 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs CMCSA 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs LLY 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs QCOM 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs NKE 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs VZ 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs WMT 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs INTC 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs ACN 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Needs AVGO 5 data, not available: a lower timeframe than the chart; the engine does not evaluate intrabar data Values that depend on it are empty on this chart.
- 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, $5,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.
-
Note
40 settings have no label
In the settings dialog symbol1 (line 127), symbol2 (line 128), symbol3 (line 129), symbol4 (line 130), symbol5 (line 131), symbol6 (line 132), symbol7 (line 133), symbol8 (line 134), symbol9 (line 135), symbol10 (line 136), symbol11 (line 137), symbol12 (line 138), symbol13 (line 139), symbol14 (line 140), symbol15 (line 141), symbol16 (line 142), symbol17 (line 143), symbol18 (line 144), symbol19 (line 145), symbol20 (line 146), symbol21 (line 147), symbol22 (line 148), symbol23 (line 149), symbol24 (line 150), symbol25 (line 151), symbol26 (line 152), symbol27 (line 153), symbol28 (line 154), symbol29 (line 155), symbol30 (line 156), symbol31 (line 157), symbol32 (line 158), symbol33 (line 159), symbol34 (line 160), symbol35 (line 161), symbol36 (line 162), symbol37 (line 163), symbol38 (line 164), symbol39 (line 165), symbol40 (line 166) show an empty name. The labels on this page come from the variable names.
-
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 323, 324, 329, 330, 335, 341, 351. The upgraded version below moves each call inside an if block.
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 |
|---|---|---|---|
| Trading window | |||
| Start Year | 2011 | int | timestart |
| Start Month | 1 | int · min 1 · max 12 | timestart |
| Start Day | 1 | int · min 1 · max 31 | timestart |
| Finish Year | 2050 | int | timefinish |
| Finish Month | 12 | int · min 1 · max 12 | timefinish |
| Finish Day | 31 | int · min 1 · max 31 | timefinish |
| Trading Options | |||
| Options trading only | true | bool | line 316 |
| # of options per trade | 10 | int | trade_amount |
| Reinvest profit? | false | bool | line 317 |
| Trade Trailing SL after PT | true | bool | exit Exit exit Exit exit Exit exit Exit exit Exit exit Exit line 325 line 332 line 336 line 338 line 342 |
| Take Profit early based on Top 40 overbought/oversold | true | bool | close Long close Short line 328 |
| Trade same/1 day expiry, exits EOD Mon/Wed/Fri; exits 1/2 Tues/Thurs | true | bool | line 345 |
| Reward to Risk Ratio | 2.0 | float · min 1 · step 0.1 | long_PT short_PT long_CT_PT short_CT_PT |
| % Stop loss reduction(+ve)/increase(-ve) | 15 | int · min -100 · max 100 | long_SL short_SL |
| No entry between 10:05 - 10:20 (Statistically bad trades) | true | bool | line 296 |
| Multiplier for trading options (adjust to approx. options P&L) | 0.007 | float · step 0.001 | trade_amount |
| ATR | |||
| ATR period | 14 | int · min 1 | atr |
| % ATR to use for SL / PT | 130 | float · min 0 · step 10 | percentage |
| MACD | |||
| Fast Length | 12 | int | fast_ma |
| Slow Length | 26 | int | slow_ma |
| Signal Smoothing | 9 | int · min 1 · max 50 | signal |
| Oscillator MA Type | SMA | string · options ["SMA", "EMA"] | fast_ma slow_ma |
| Signal Line MA Type | EMA | string · options ["SMA", "EMA"] | signal |
| Linear Regression | |||
| Linear Regression Period | 48 | int · min 1 | lrc |
| LR Slope Period | 5 | int · min 1 | slrs |
| LR Signal Period | 14 | int · min 1 | alrs |
| LR Threshold for Ranging vs Trending | 18.8 | float · min 0 · step 0.1 | LR_ranging LR_trending |
| Stochastic RSI | |||
| Stoch K | 3 | int · min 1 | k |
| Stoch D | 3 | int · min 1 | d |
| RSI Length | 14 | int · min 1 | rsi |
| Stochastic Length | 14 | int · min 1 | k |
| High Low Index | |||
| Time Frame | 5 | timeframe | highlow line 184 line 185 line 186 line 187 line 188 line 189 line 190 line 191 line 192 line 193 line 194 line 195 line 196 line 197 line 198 line 199 line 200 line 201 line 202 line 203 line 204 line 205 line 206 line 207 line 208 line 209 line 210 line 211 line 212 line 213 line 214 line 215 line 216 line 217 line 218 line 219 line 220 line 221 line 222 line 223 |
| Length | 15 | int · min 1 | highest_ lowest_ highlow line 184 line 185 line 186 line 187 line 188 line 189 line 190 line 191 line 192 line 193 line 194 line 195 line 196 line 197 line 198 line 199 line 200 line 201 line 202 line 203 line 204 line 205 line 206 line 207 line 208 line 209 line 210 line 211 line 212 line 213 line 214 line 215 line 216 line 217 line 218 line 219 line 220 line 221 line 222 line 223 |
| Source | High/Low | string · options ["High/Low", "Close"] | h_ l_ |
| Smoothing Length | 10 | int · min 1 | HighLowIndex |
| EMA Length | 9 | int · min 1 | HLIma |
| Symbols | |||
| Symbol1 (no label in the script) | AAPL | symbol | line 184 |
| Symbol2 (no label in the script) | MSFT | symbol | line 185 |
| Symbol3 (no label in the script) | AMZN | symbol | line 186 |
| Symbol4 (no label in the script) | GOOG | symbol | line 187 |
| Symbol5 (no label in the script) | FB | symbol | line 188 |
| Symbol6 (no label in the script) | TSLA | symbol | line 189 |
| Symbol7 (no label in the script) | NVDA | symbol | line 190 |
| Symbol8 (no label in the script) | BRK.B | symbol | line 191 |
| Symbol9 (no label in the script) | JPM | symbol | line 192 |
| Symbol10 (no label in the script) | UNH | symbol | line 193 |
| Symbol11 (no label in the script) | JNJ | symbol | line 194 |
| Symbol12 (no label in the script) | HD | symbol | line 195 |
| Symbol13 (no label in the script) | PG | symbol | line 196 |
| Symbol14 (no label in the script) | V | symbol | line 197 |
| Symbol15 (no label in the script) | PFE | symbol | line 198 |
| Symbol16 (no label in the script) | BAC | symbol | line 199 |
| Symbol17 (no label in the script) | MA | symbol | line 200 |
| Symbol18 (no label in the script) | ADBE | symbol | line 201 |
| Symbol19 (no label in the script) | DIS | symbol | line 202 |
| Symbol20 (no label in the script) | NFLX | symbol | line 203 |
| Symbol21 (no label in the script) | TMO | symbol | line 204 |
| Symbol22 (no label in the script) | XOM | symbol | line 205 |
| Symbol23 (no label in the script) | CRM | symbol | line 206 |
| Symbol24 (no label in the script) | CSCO | symbol | line 207 |
| Symbol25 (no label in the script) | COST | symbol | line 208 |
| Symbol26 (no label in the script) | ABT | symbol | line 209 |
| Symbol27 (no label in the script) | PEP | symbol | line 210 |
| Symbol28 (no label in the script) | ABBV | symbol | line 211 |
| Symbol29 (no label in the script) | KO | symbol | line 212 |
| Symbol30 (no label in the script) | PYPL | symbol | line 213 |
| Symbol31 (no label in the script) | CVX | symbol | line 214 |
| Symbol32 (no label in the script) | CMCSA | symbol | line 215 |
| Symbol33 (no label in the script) | LLY | symbol | line 216 |
| Symbol34 (no label in the script) | QCOM | symbol | line 217 |
| Symbol35 (no label in the script) | NKE | symbol | line 218 |
| Symbol36 (no label in the script) | VZ | symbol | line 219 |
| Symbol37 (no label in the script) | WMT | symbol | line 220 |
| Symbol38 (no label in the script) | INTC | symbol | line 221 |
| Symbol39 (no label in the script) | ACN | symbol | line 222 |
| Symbol40 (no label in the script) | AVGO | symbol | line 223 |
What it calculates
- RMAta.tr, ATR period (14)
atr · line 37 - SMA / EMAsrc, Fast Length (12) · chosen by Oscillator MA Type
fast_ma · line 57 - SMA / EMAsrc, Slow Length (26) · chosen by Oscillator MA Type
slow_ma · line 58 - SMA / EMAmacd, Signal Smoothing (9) · chosen by Signal Line MA Type
signal · line 60 - Crossmacd, signal
macd_cross · line 66 - Linregsrc, Linear Regression Period (48), 0
lrc · line 75 - EMAlrs, LR Slope Period (5)
slrs · line 79 - SMAslrs, LR Signal Period (14)
alrs · line 81 - RSIsrc, RSI Length (14)
rsi · line 102 - SMA / Stochta.stoch(rsi, rsi, rsi, lengthStoch), Stoch K (3)
k · line 103 - SMAk, Stoch D (3)
d · line 104 - HighestLength (15)
highest_ · line 169 - LowestLength (15)
lowest_ · line 170 - SMARecordHighPercent, Smoothing Length (10)
HighLowIndex · line 232 - EMAHighLowIndex, EMA Length (9)
HLIma · line 241 - CrossHighLowIndex, HLIma
HL_cross · line 250
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.
The script has 110 named inputs, calculations, conditions and outputs, too many to draw legibly here; the tables above list them.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Long long | L_entry1 and window | On | 323 |
| Open Short short | S_entry1 and window | On | 324 |
| Exit Exit stop and limit | not(trade_trailing) and window | On | 326 |
| Exit Exit stop and limit | not(trade_trailing) and window | On | 327 |
| Close Long | L_exit1 or L_exit2 | On | 329 |
| Close Short | S_exit1 or S_exit2 | On | 330 |
| Exit Exit stop | exit0 and window2 | On | 335 |
| Exit Exit stop | trade_trailing and not(trailing_stop) and window | On | 337 |
| Exit Exit stop | exit0 and window2 | On | 341 |
| Exit Exit stop | trade_trailing and not(trailing_stop) and window | On | 343 |
| Close | time(timeframe.period, "1550-1555:246") | On | 347 |
| Close Long | time(timeframe.period, "1550-1555:35") | On | 349 |
| Close Short | time(timeframe.period, "1550-1555:35") | On | 350 |
| cancel_all | time(timeframe.period, "1555-1600:23456") | On | 351 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
No
No repainting patterns in the source.
- ClearHigher-timeframe data1 request.security call(s), none with lookahead on.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- ClearIntrabar recalculationNo varip, calc_on_every_tick or realtime-only state. Orders are decided once per closed bar.
- 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
Not run on other markets: the script requests AAPL 5, MSFT 5, AMZN 5, GOOG 5, FB 5, TSLA 5, NVDA 5, BRK.B 5, JPM 5, UNH 5, JNJ 5, HD 5, PG 5, V 5, PFE 5, BAC 5, MA 5, ADBE 5, DIS 5, NFLX 5, TMO 5, XOM 5, CRM 5, CSCO 5, COST 5, ABT 5, PEP 5, ABBV 5, KO 5, PYPL 5, CVX 5, CMCSA 5, LLY 5, QCOM 5, NKE 5, VZ 5, WMT 5, INTC 5, ACN 5, AVGO 5, and that data is not available here (a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data; a lower timeframe than the chart; the engine does not evaluate intrabar data). Its results would rest on empty values.
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 |
|---|---|---|---|---|---|
| Top 40 High Low Strategy for SPY, 5min this page | — | v5 strategy | 0.0% | 0 | 0.0% |
| VuManChu Cipher B + Divergences Strategy by nattichai001 | ema cross lowest highest rsi sma rma | v4 strategy | +76.6% | 175 | 18.4% |
| Bear & Bull Zone Signal Strategy by Solutions1978 | rsi ema highest lowest stoch sma | v4 strategy | No trades on this market | ||
| PB Trend Scalper by PatrickGwynBuckley | ema rma stoch highest lowest | v5 strategy | −16.9% | 10 | 68.3% |
| Bagheri IG Ether by Bagheri_IG | ema highest lowest rma sma | v4 strategy | +0.0% | 3 | 0.0% |
| Bagheri IG Ether v2 by Bagheri_IG | ema highest lowest rma sma | v4 strategy | −0.0% | 2 | 0.0% |
Upgrade and export
Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.
- 7 strategy calls use the when = argument (line 323, 324, 329, 330, 335, 341, 351). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- 4 settings use the untyped input() (line 9, 12, 44, 45). It still compiles in v6; input.int, input.float or input.source state the type.
- No v6-only features. v6 is not needed to run this script.
Clean v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 9: input() → input.int()
- Line 12: input() → input.int()
- Line 44: input() → input.int()
- Line 45: input() → input.int()
- Line 323: when = L_entry1 and window → if block
- Line 324: when = S_entry1 and window → if block
5 more edits
- Line 329: when = L_exit1 or L_exit2 → if block
- Line 330: when = S_exit1 or S_exit2 → if block
- Line 335: when = exit0 and window2 → if block
- Line 341: when = exit0 and window2 → if block
- Line 351: when = time(timeframe.period, "1555-1600:23456") → if block
⋯// ******************** Period **************************************-startY = input(title='Start Year', defval=2011, group = "Trading window")+startY = input.int(title='Start Year', defval=2011, group = "Trading window")startM = input.int(title='Start Month', defval=1, minval=1, maxval=12, group = "Trading window")startD = input.int(title='Start Day', defval=1, minval=1, maxval=31, group = "Trading window")-finishY = input(title='Finish Year', defval=2050, group = "Trading window")+finishY = input.int(title='Finish Year', defval=2050, group = "Trading window")finishM = input.int(title='Finish Month', defval=12, minval=1, maxval=12, group = "Trading window")⋯// Getting inputs-fast_length = input(title="Fast Length", defval=12, group = "MACD")+fast_length = input.int(title="Fast Length", defval=12, group = "MACD")-slow_length = input(title="Slow Length", defval=26, group = "MACD")+slow_length = input.int(title="Slow Length", defval=26, group = "MACD")signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 50, defval = 9, group = "MACD")⋯if not(trade_pause) and not(trade_pause2) and strategy.opentrades == 0- strategy.entry("Long", strategy.long, trade_amount, when = L_entry1 and window, comment="Long")+ if L_entry1 and window+ strategy.entry("Long", strategy.long, trade_amount, comment="Long")- strategy.entry("Short", strategy.short, trade_amount, when = S_entry1 and window, comment = "Short")+ if S_entry1 and window+ strategy.entry("Short", strategy.short, trade_amount, comment = "Short")if not(trade_trailing) and window⋯if strategy.opentrades.profit(strategy.opentrades-1) > 0 and trade_earlyExit- strategy.close("Long", when = L_exit1 or L_exit2, comment = "Exit - Take profit")+ if L_exit1 or L_exit2+ strategy.close("Long", comment = "Exit - Take profit")- strategy.close("Short", when = S_exit1 or S_exit2, comment = "Exit - Take profit")+ if S_exit1 or S_exit2+ strategy.close("Short", comment = "Exit - Take profit")// *************** Trailing SL *******************************************⋯ trailing_stop := true- strategy.exit("Exit", "Long", when = exit0 and window2, stop= long_SL, comment = "Exit Long Trailing SL")+ if exit0 and window2+ strategy.exit("Exit", "Long", stop= long_SL, comment = "Exit Long Trailing SL")if trade_trailing and not(trailing_stop) and window⋯ trailing_stop := true- strategy.exit("Exit", "Short", when= exit0 and window2, stop= short_SL, comment = "Exit Short Trailing SL")+ if exit0 and window2+ strategy.exit("Exit", "Short", stop= short_SL, comment = "Exit Short Trailing SL")if trade_trailing and not(trailing_stop) and window⋯ strategy.close("Short", qty_percent = 50, comment="Close 1/2 EOD")-strategy.cancel_all(when=time(timeframe.period, "1555-1600:23456"))+if time(timeframe.period, "1555-1600:23456")+ strategy.cancel_all()// plot(trailing_stop? 1:0, "Trailing stop ON")⋯Does it run in Pine Script v6?
Not tested
Neither version placed a trade on any of the 8 markets, so there was nothing to compare. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯ -//@version=5+//@version=6strategy("Top 40 High Low Strategy", overlay=true, pyramiding=0, initial_capital=5000, commission_type=strategy.commission.cash_per_contract, commission_value = 0.09) ⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 128 ms | With a workaround 1,000 bars in 556 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.
“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.
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/// © platsn//// ***************************************************************************************************************************************************************************************************** //@version=5strategy("Top 40 High Low Strategy", overlay=true, pyramiding=0, initial_capital=5000, commission_type=strategy.commission.cash_per_contract, commission_value = 0.09) // ******************** Period **************************************startY = input(title='Start Year', defval=2011, group = "Trading window")startM = input.int(title='Start Month', defval=1, minval=1, maxval=12, group = "Trading window")startD = input.int(title='Start Day', defval=1, minval=1, maxval=31, group = "Trading window")finishY = input(title='Finish Year', defval=2050, group = "Trading window")finishM = input.int(title='Finish Month', defval=12, minval=1, maxval=12, group = "Trading window")finishD = input.int(title='Finish Day', defval=31, minval=1, maxval=31, group = "Trading window")timestart = timestamp(startY, startM, startD, 00, 00)timefinish = timestamp(finishY, finishM, finishD, 23, 59)t1 = time(timeframe.period, "0930-1500:23456")t2 = time(timeframe.period, "0930-1600:23456")window = time >= timestart and time <= timefinish and t1 ? true : false window2 = time >= timestart and time <= timefinish and t2 ? true : false // *****************************************************isSPY = input.bool(defval=true,title="Options trading only", group = "Trading Options")SPY_option = input.int(defval=10,title="# of options per trade", group = "Trading Options")reinvest = input.bool(defval=false,title="Reinvest profit?", group = "Trading Options")trade_trailing = input.bool(defval=true, title = "Trade Trailing SL after PT", group = "Trading Options")trailing_stop = falseTS = 0trade_earlyExit = input.bool(defval=true, title= "Take Profit early based on Top 40 overbought/oversold", group="Trading Options")trade_expiry = input.bool(defval=true, title="Trade same/1 day expiry, exits EOD Mon/Wed/Fri; exits 1/2 Tues/Thurs", group="Trading Options")src = close// ***************************************************************************************************** Daily ATR *****************************************************// // Inputsatrlen = input.int(14, minval=1, title="ATR period", group = "ATR")iPercent = input.float(130, minval=0, step=10, title="% ATR to use for SL / PT", group = "ATR")// // Logicpercentage = iPercent * 0.01atr = ta.rma(ta.tr, atrlen)datrp = atr * percentage// plot(datr,"Daily ATR")// plot(atr, "ATR")// plot(datrp, "ATR for SL/PT")// ***************************************************************************************************************** MACD ************************// Getting inputsfast_length = input(title="Fast Length", defval=12, group = "MACD")slow_length = input(title="Slow Length", defval=26, group = "MACD")signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 50, defval = 9, group = "MACD")sma_source = input.string(title="Oscillator MA Type", defval="SMA", options=["SMA", "EMA"], group = "MACD")sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"], group = "MACD")// Plot colors// col_macd = input(#2962FF, "MACD Line ", group="Color Settings", inline="MACD")// col_signal = input(#FF6D00, "Signal Line ", group="Color Settings", inline="Signal")// col_grow_above = input(#26A69A, "Above Grow", group="Histogram", inline="Above")// col_fall_above = input(#B2DFDB, "Fall", group="Histogram", inline="Above")// col_grow_below = input(#FFCDD2, "Below Grow", group="Histogram", inline="Below")// col_fall_below = input(#FF5252, "Fall", group="Histogram", inline="Below")// Calculatingfast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)macd = fast_ma - slow_masignal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)hist = macd - signalmacd_bull = hist > hist[1]macd_bear = hist < hist[1]// plot(macd_bull?1:0, "MACD bull")// plot(macd_bear?1:0, "MACD bear")macd_cross = ta.cross(macd,signal)// **************************************************************************************************************** Linear Regression *************************//Inputclen = input.int(defval = 48, minval = 1, title = "Linear Regression Period", group = "Linear Regression")slen = input.int(defval=5, minval=1, title="LR Slope Period" , group = "Linear Regression")glen = input.int(defval=14, minval=1, title="LR Signal Period", group = "Linear Regression")LR_thres = input.float(18.8, minval=0, step=0.1, title="LR Threshold for Ranging vs Trending" , group = "Linear Regression") //Linear Regression Curvelrc = ta.linreg(src, clen, 0)//Linear Regression Slopelrs = (lrc-lrc[1])/1//Smooth Linear Regression Slopeslrs = ta.ema(lrs, slen)//Signal Linear Regression Slopealrs = ta.sma(slrs, glen)up_accel = lrs > alrs and lrs > 0 down_accel = lrs < alrs and lrs < 0 zero_accel = not(up_accel) and not(down_accel)LR_upTrend = slrs > 0LR_downTrend = slrs < 0LR_ranging = math.abs(slrs)*100 <= LR_thresLR_trending = math.abs(slrs)*100 > LR_thresLR_bull = slrs > slrs[1]LR_bear = slrs < slrs[1]// plot(slrs*100, "LR slope")// plot(LR_trending?1:0, "LR Trending")plotchar(not(LR_trending[1]) and LR_trending and LR_upTrend, char="⇗", color=color.green, location = location.belowbar, size = size.small)plotchar(not(LR_trending[1]) and LR_trending and LR_downTrend, char="⇘", color=color.red, location = location.abovebar, size = size.small)plotchar(LR_trending[1] and not(LR_trending), char="⇒", color=color.gray, location = location.abovebar, size = size.small)// *************************************************************************************************************************** Stochastic RSI ********************************// Stochastic Main ParameterssmoothK = input.int(3, minval=1, title='Stoch K', group = "Stochastic RSI")smoothD = input.int(3, minval=1, title='Stoch D', group = "Stochastic RSI")lengthRSI = input.int(14, "RSI Length", minval=1, group = "Stochastic RSI")lengthStoch = input.int(14, "Stochastic Length", minval=1, group = "Stochastic RSI")rsi = ta.rsi(src, lengthRSI)k = ta.sma(ta.stoch(rsi, rsi, rsi, lengthStoch), smoothK)d = ta.sma(k, smoothD)stoch_long = k < 10 stoch_short = k > 90stoch_bottom = k[2] >= k[1] and k > k[1]stoch_top = k[2] <= k[1] and k < k[1] // *************************************************************************************************************************** RSI **********************rsi_bull = rsi > rsi[1]rsi_bear = rsi < rsi[1]// plot(rsi,"RSI")// plot(rsi_bull? 1:0, "RSI Bull")// plot(rsi_bear?1:0, "RSI Bear")// ********************************************************************************************************** High Low Index *****************************************************// original author --> © LonesomeTheBlue// changed list of 40 tickers to top 40 SPY holdingstimeFrame = input.timeframe(defval = '5', title = "Time Frame", group = "High Low Index")length = input.int(defval = 15, title = "Length", minval = 1, group = "High Low Index")source = input.string(defval = "High/Low", title = "Source", options = ["High/Low", "Close"], group = "High Low Index")smoothLength = input.int(defval = 10, title = "Smoothing Length", minval = 1, group = "High Low Index")// showRecordHighPercent = input.bool(defval = true, title = "Record High Percent", group = "High Low Index")// showTable= input.bool(defval = true, title = "Show Table", inline = "table", group = "High Low Index")// textsize = input.string(defval = size.tiny, title = " | Text Size", options = [size.tiny, size.small, size.normal], inline = "table", group = "High Low Index")// showEma = input.bool(defval = true, title = "EMA", inline = "ema", group = "High Low Index")emaLength = input.int(defval = 9, title = "EMA Length", minval = 1, group = "High Low Index")symbol1 = input.symbol(defval = "AAPL", group = "Symbols")symbol2 = input.symbol(defval = "MSFT", group = "Symbols")symbol3 = input.symbol(defval = "AMZN", group = "Symbols")symbol4 = input.symbol(defval = "GOOG", group = "Symbols")symbol5 = input.symbol(defval = "FB", group = "Symbols")symbol6 = input.symbol(defval = "TSLA", group = "Symbols")symbol7 = input.symbol(defval = "NVDA", group = "Symbols")symbol8 = input.symbol(defval = "BRK.B", group = "Symbols")symbol9 = input.symbol(defval = "JPM", group = "Symbols")symbol10 = input.symbol(defval = "UNH", group = "Symbols")symbol11 = input.symbol(defval = "JNJ", group = "Symbols")symbol12 = input.symbol(defval = "HD", group = "Symbols")symbol13 = input.symbol(defval = "PG", group = "Symbols")symbol14 = input.symbol(defval = "V", group = "Symbols")symbol15 = input.symbol(defval = "PFE", group = "Symbols")symbol16 = input.symbol(defval = "BAC", group = "Symbols")symbol17 = input.symbol(defval = "MA", group = "Symbols")symbol18 = input.symbol(defval = "ADBE", group = "Symbols")symbol19 = input.symbol(defval = "DIS", group = "Symbols")symbol20 = input.symbol(defval = "NFLX", group = "Symbols")symbol21 = input.symbol(defval = "TMO", group = "Symbols")symbol22 = input.symbol(defval = "XOM", group = "Symbols")symbol23 = input.symbol(defval = "CRM", group = "Symbols")symbol24 = input.symbol(defval = "CSCO", group = "Symbols")symbol25 = input.symbol(defval = "COST", group = "Symbols")symbol26 = input.symbol(defval = "ABT", group = "Symbols")symbol27 = input.symbol(defval = "PEP", group = "Symbols")symbol28 = input.symbol(defval = "ABBV", group = "Symbols")symbol29 = input.symbol(defval = "KO", group = "Symbols")symbol30 = input.symbol(defval = "PYPL", group = "Symbols")symbol31 = input.symbol(defval = "CVX", group = "Symbols")symbol32 = input.symbol(defval = "CMCSA", group = "Symbols")symbol33 = input.symbol(defval = "LLY", group = "Symbols")symbol34 = input.symbol(defval = "QCOM", group = "Symbols")symbol35 = input.symbol(defval = "NKE", group = "Symbols")symbol36 = input.symbol(defval = "VZ", group = "Symbols")symbol37 = input.symbol(defval = "WMT", group = "Symbols")symbol38 = input.symbol(defval = "INTC", group = "Symbols")symbol39 = input.symbol(defval = "ACN", group = "Symbols")symbol40 = input.symbol(defval = "AVGO", group = "Symbols")HighsLows = array.new_int(40, 0)getHighLow(length)=> highest_ = ta.highest(length)[1] lowest_ = ta.lowest(length)[1] int ret = 0 if na(highest_) ret := 2 else bool h_ = ((source == "High/Low" ? high : close) > highest_) bool l_ = ((source == "High/Low" ? low : close) < lowest_) ret := h_ ? 1 : l_ ? -1 : 0 ret getHLindexForTheSymbol(symbol, timeFrame, length, index)=> highlow = request.security(symbol, timeFrame, getHighLow(length)) array.set(HighsLows, index, na(highlow) ? 2 : highlow) getHLindexForTheSymbol(symbol1, timeFrame, length, 0)getHLindexForTheSymbol(symbol2, timeFrame, length, 1)getHLindexForTheSymbol(symbol3, timeFrame, length, 2)getHLindexForTheSymbol(symbol4, timeFrame, length, 3)getHLindexForTheSymbol(symbol5, timeFrame, length, 4)getHLindexForTheSymbol(symbol6, timeFrame, length, 5)getHLindexForTheSymbol(symbol7, timeFrame, length, 6)getHLindexForTheSymbol(symbol8, timeFrame, length, 7)getHLindexForTheSymbol(symbol9, timeFrame, length, 8)getHLindexForTheSymbol(symbol10, timeFrame, length, 9)getHLindexForTheSymbol(symbol11, timeFrame, length, 10)getHLindexForTheSymbol(symbol12, timeFrame, length, 11)getHLindexForTheSymbol(symbol13, timeFrame, length, 12)getHLindexForTheSymbol(symbol14, timeFrame, length, 13)getHLindexForTheSymbol(symbol15, timeFrame, length, 14)getHLindexForTheSymbol(symbol16, timeFrame, length, 15)getHLindexForTheSymbol(symbol17, timeFrame, length, 16)getHLindexForTheSymbol(symbol18, timeFrame, length, 17)getHLindexForTheSymbol(symbol19, timeFrame, length, 18)getHLindexForTheSymbol(symbol20, timeFrame, length, 19)getHLindexForTheSymbol(symbol21, timeFrame, length, 20)getHLindexForTheSymbol(symbol22, timeFrame, length, 21)getHLindexForTheSymbol(symbol23, timeFrame, length, 22)getHLindexForTheSymbol(symbol24, timeFrame, length, 23) getHLindexForTheSymbol(symbol25, timeFrame, length, 24)getHLindexForTheSymbol(symbol26, timeFrame, length, 25)getHLindexForTheSymbol(symbol27, timeFrame, length, 26)getHLindexForTheSymbol(symbol28, timeFrame, length, 27)getHLindexForTheSymbol(symbol29, timeFrame, length, 28)getHLindexForTheSymbol(symbol30, timeFrame, length, 29)getHLindexForTheSymbol(symbol31, timeFrame, length, 30)getHLindexForTheSymbol(symbol32, timeFrame, length, 31)getHLindexForTheSymbol(symbol33, timeFrame, length, 32)getHLindexForTheSymbol(symbol34, timeFrame, length, 33)getHLindexForTheSymbol(symbol35, timeFrame, length, 34)getHLindexForTheSymbol(symbol36, timeFrame, length, 35)getHLindexForTheSymbol(symbol37, timeFrame, length, 36)getHLindexForTheSymbol(symbol38, timeFrame, length, 37)getHLindexForTheSymbol(symbol39, timeFrame, length, 38)getHLindexForTheSymbol(symbol40, timeFrame, length, 39)highs = 0total = 0for x = 0 to 39 highs += (array.get(HighsLows, x) == 1 ? 1 : 0) total += (array.get(HighsLows, x) == 1 or array.get(HighsLows, x) == -1 ? 1 : 0)// calculate & show Record High Percent / High Low Index / EMAvar float RecordHighPercent = 50RecordHighPercent := total == 0 ? RecordHighPercent : 100 * highs / totalHighLowIndex = ta.sma(RecordHighPercent, smoothLength)// hline0 = hline(0, linestyle = hline.style_dotted)// hline50 = hline(50, linestyle = hline.style_dotted)// hline100 = hline(100, linestyle = hline.style_dotted)// fill(hline0, hline50, color = color.rgb(255, 0, 0, 75))// fill(hline50, hline100, color = color.rgb(0, 255, 0, 75))// plot(showRecordHighPercent ? RecordHighPercent : na, color = showRecordHighPercent ? #9598a1 : na)// plot(showEma ? ta.ema(HighLowIndex, emaLength) : na, color = showEma ? color.red : na, title = "EMA line")// plot(HighLowIndex, color = color.blue, linewidth = 2, title = "High Low Index Line")HLIma = ta.ema(HighLowIndex, emaLength)peak = HighLowIndex[2] <= HighLowIndex[1] and HighLowIndex < HighLowIndex[1]dip = HighLowIndex[2] >= HighLowIndex[1] and HighLowIndex > HighLowIndex[1]HL_bull = HighLowIndex > HighLowIndex[1]HL_bear = HighLowIndex < HighLowIndex[1]HLma_bull = ta.crossover(HLIma, 50) and HL_bullHLma_bear = ta.crossunder(HLIma,50) and HL_bear// plot(HL_bull?1:0, "HL Bull")// plot(HL_bear?1:0, "HL Bear")HL_cross = ta.cross(HighLowIndex,HLIma)// ***************************************************************************************************************************************** SL & PT ***********************************RR = input.float(2.0, minval = 1, step = 0.1, title="Reward to Risk Ratio", group = "Trading Options")SL_offset = input.int(15, minval=-100, maxval = 100, title ="% Stop loss reduction(+ve)/increase(-ve)", group = "Trading Options")barsSinceLastEntry()=> strategy.opentrades > 0 ? (bar_index - strategy.opentrades.entry_bar_index(strategy.opentrades-1)) : na// plot(barsSinceLastEntry(),"BSLE")inLong = falseinShort = falselast_high = high[1]last_low = low[1]long_SL = last_low - datrpshort_SL = last_high + datrplong_PT = last_high + math.abs(high[1]-low[1])short_PT = last_low - math.abs(high[1]-low[1])long_CT_PT = long_PTshort_CT_PT = short_PTlast_entry = strategy.opentrades.entry_price(strategy.opentrades-1)L_risk = last_entry - long_SLS_risk = short_SL - last_entryif strategy.opentrades > 0 and not(trailing_stop) long_SL := math.min(long_SL[barsSinceLastEntry()], last_low) short_SL := math.max(short_SL[barsSinceLastEntry()], last_high) L_risk := last_entry - long_SL S_risk := short_SL - last_entry long_PT := last_entry + (last_entry - long_SL) * RR short_PT := last_entry - (short_SL - last_entry) * RR long_CT_PT := last_entry + (last_entry - long_SL) * (RR/3) short_CT_PT := last_entry - (short_SL - last_entry) * (RR/3) long_SL := long_SL + (L_risk * SL_offset * 0.01) short_SL := short_SL - (S_risk * SL_offset * 0.01) if strategy.opentrades.size(0) > 0 and barsSinceLastEntry() > 0 inLong := true else if strategy.opentrades.size(0) < 0 and barsSinceLastEntry() > 0 inShort := trueelse long_PT := last_high + math.abs(high-long_SL) short_PT := last_low - math.abs(short_SL-low)// **************************************************************************************************************************************** Trade Pauses ****************************************bool trade_pause = falsebool trade_pause2 = falseno_longat10 = input.bool(true, title="No entry between 10:05 - 10:20 (Statistically bad trades)", group = "Trading Options")// if math.abs(close - open) > atr// trade_pause := true// else// trade_pause := false if no_longat10 and time(timeframe.period, "1000-1030:23456") trade_pause2 := trueelse trade_pause2 := false // ************************************************************************************************************************************ Entry conditions **************************L_entry1 = macd_bull and HL_bull and LR_trending and up_accel and HighLowIndex < 96 S_entry1 = macd_bear and HL_bear and LR_trending and down_accel and HighLowIndex > 4// ************************************************************************************************************************************** Exit Conditions ********************************exit0 = true L_exit1 = (HighLowIndex[1] > 99 and HighLowIndex < 99) S_exit1 = (HighLowIndex[1] < 1 and HighLowIndex > 1)L_exit2 = S_entry1S_exit2 = L_entry1// ************************************************************************************************************************************ Entry and Exit orders ***************************** option_multiplier = input.float(0.007, title="Multiplier for trading options (adjust to approx. options P&L)", step=0.001, group = "Trading Options")profit = strategy.netprofittrade_amount = math.floor(strategy.initial_capital / close) if isSPY if strategy.netprofit > 0 and reinvest and strategy.closedtrades.profit(strategy.closedtrades-1) > 0 trade_amount := math.floor(strategy.initial_capital * option_multiplier) * (SPY_option + math.floor((profit/strategy.initial_capital)*10)) else trade_amount := math.floor(strategy.initial_capital * option_multiplier) * SPY_option if not(trade_pause) and not(trade_pause2) and strategy.opentrades == 0 strategy.entry("Long", strategy.long, trade_amount, when = L_entry1 and window, comment="Long") strategy.entry("Short", strategy.short, trade_amount, when = S_entry1 and window, comment = "Short")if not(trade_trailing) and window strategy.exit("Exit", "Long", stop= long_SL, limit = long_PT, comment = "Exit Long SL/PT hit") strategy.exit("Exit", "Short", stop= short_SL, limit = short_PT, comment = "Exit Short SL/PT hit")if strategy.opentrades.profit(strategy.opentrades-1) > 0 and trade_earlyExit strategy.close("Long", when = L_exit1 or L_exit2, comment = "Exit - Take profit") strategy.close("Short", when = S_exit1 or S_exit2, comment = "Exit - Take profit")// *************** Trailing SL *******************************************if trade_trailing and (high > long_PT or TS[1] == 1) and window2 long_SL := math.max(long_SL[1], low - datrp) trailing_stop := true strategy.exit("Exit", "Long", when = exit0 and window2, stop= long_SL, comment = "Exit Long Trailing SL")if trade_trailing and not(trailing_stop) and window strategy.exit("Exit", "Long", stop= long_SL, comment = "Exit Long SL/PT hit")if trade_trailing and (low < short_PT or TS[1] == 1) and window2 short_SL := math.min(short_SL[1], high + datrp) trailing_stop := true strategy.exit("Exit", "Short", when= exit0 and window2, stop= short_SL, comment = "Exit Short Trailing SL")if trade_trailing and not(trailing_stop) and window strategy.exit("Exit", "Short", stop= short_SL, comment = "Exit Short SL/PT hit") if trade_expiry if time(timeframe.period, "1550-1555:246") strategy.close_all() if time(timeframe.period, "1550-1555:35") strategy.close("Long", qty_percent = 50, comment="Close 1/2 EOD") strategy.close("Short", qty_percent = 50, comment="Close 1/2 EOD")strategy.cancel_all(when=time(timeframe.period, "1555-1600:23456"))// plot(trailing_stop? 1:0, "Trailing stop ON")if strategy.opentrades == 0 trailing_stop := falseTS := trailing_stop? 1:0SSL = plot(short_SL,title = "Short SL", color= inShort ? color.red: color.new(color.red,100), linewidth = 3)LSL = plot(long_SL,title = "Long SL", color= inLong ? color.red: color.new(color.red,100), linewidth = 3)LPT = plot(long_PT,title = "Long PT", color= inLong ? color.green: color.new(color.green,100), linewidth = 3)SPT = plot(short_PT,title = "Short PT", color= inShort ? color.green: color.new(color.green,100), linewidth = 3)LE = plot(last_entry, title = "Last entry", color = strategy.opentrades > 0 ? color.gray : color.new(color.gray,100))// plot(L_risk, title = "Risk Long")// plot(S_risk, title = "Risk Short")fill(LPT, LE, color = strategy.opentrades.size(0) > 0 ? color.new(color.green,90) : na)fill(LE, LSL, color = strategy.opentrades.size(0) > 0 ? color.new(color.red,90): na)fill(SPT, LE, color = strategy.opentrades.size(0) < 0 ? color.new(color.green,90) : na)fill(LE, SSL, color = strategy.opentrades.size(0) < 0 ? color.new(color.red,90) : na)