Pine Script v5 · Strategy
Strategy Backtesting Template [MYN]
A Pine Script v5 strategy built on RSI (close, 10), Crossover (rsiEntry, 80) and Crossunder (rsiEntry, 20). It has 10 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a profit target and no stop loss. Each trade commits 100.0% of equity and pays 0.075 commission. 30 settings; defaults include EMA Length 200, EMA Source close and ADX Variant ORIGINAL.
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 Strategy Backtesting Template [MYN] repaint?
- Yes
- At least one pattern in the source can repaint: intrabar recalculation.
- How did Strategy Backtesting Template [MYN] do on BTCUSDT 1D with default settings?
- −33.10%
- From 26 Jan 2024 to 21 Oct 2026, over 6 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- What alerts does Strategy Backtesting Template [MYN] have?
- 2
- alertSyntaxBase + 'side:long'; alertSyntaxBase + 'side:short'.
- Does Strategy Backtesting Template [MYN] run in Pine Script v6?
- No
- The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 6 before, 8 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download.
- Does Strategy Backtesting Template [MYN] use a stop loss?
- No
- Its exits set a profit target but no stop.
BTCUSDT · 1D · default settings
- Winning trade
- Losing trade
- Equity
- Buy and hold
| Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec | Year | |
| 2024 | 29.62 | 14.26 | -12.9 | 15.73 | -14.83 | -3 | 11.57 | -9.54 | -15.89 | -5.8 | -3.72 | -1.13 | |
| 2025 | 8.3 | -16.54 | 4.27 | -10.83 | -8.43 | 1.43 | 9.56 | -4.47 | 4.41 | -7.59 | -27.58 | -1.62 | -45.16 |
| 2026 | 12.42 | 10.73 | -2.4 | -8.07 | 4.96 | 12.95 | -2.6 | 6.34 | 8 | -2.18 | 48.32 |
- Net profit
- −33.10%
- −$6,620.34
- Buy and hold
- +102.5%
- same period
- Closed trades
- 6
- 3 won · 3 lost
- Profitable
- 50.0%
- of closed trades
- Profit factor
- 0.56
- gross profit ÷ gross loss
- Max drawdown
- 62.44%
- −$15,953.40
One run with the author's default settings, $20,000 starting capital, commission as the script sets it. A long position was still open at the last bar. 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.
-
Check
No stop loss
strategy.exit is used without a stop, loss or trailing stop.
-
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
2 settings have no label
In the settings dialog i_startPeriodTime (line 60), i_endPeriodTime (line 62) 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 133, 134, 136, 137. 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 |
|---|---|---|---|
| EMA Filter | |||
| Enable EMA FilterEnable if you would like to conditionally have entries incorporate EMA as a filter where source is above/below the EMA line | false | bool | emaValue isEMAFilterEnabledAndCloseAboveMA isEMAFilterEnabledAndCloseBelowMA |
| EMA Length | 200 | int · min 1 | emaValue |
| EMA Source | close | source | emaValue isEMAFilterEnabledAndCloseAboveMA isEMAFilterEnabledAndCloseBelowMA |
| ADX Filter | |||
| Enable ADX FilterEnable if you would like to conditionally have entries incorporate ADX as a filter | false | bool | adx isADXFilterEnabledAndAboveThreshold |
| ADX Variant | ORIGINAL | string · options ['ORIGINAL', 'MASANAKAMURA'] | adx |
| ADX Smoothing | 14 | int | DIPlusO DIMinusO ADXO |
| DI Length | 14 | int | DIPlusO DIMinusO ADXO DIPlusM DIMinusM ADXM |
| ADX Threshold | 25 | float · step .5 | isADXFilterEnabledAndAboveThreshold |
| Date Range | |||
| Start | true | bool | isStartPeriodEnabledAndInRange |
| I start Period Time (no label in the script) | timestamp('1 Jan 2019') | time | isStartPeriodEnabledAndInRange |
| End | true | bool | isEndPeriodEnabledAndInRange |
| I end Period Time (no label in the script) | timestamp('31 Dec 2030') | time | isEndPeriodEnabledAndInRange |
| Time Window | |||
| Enable Time-Of-Day WindowLimit the time of day for trade execution | false | bool | isTimeFilterEnabledAndInRange |
| Select Local Time Zone | GMT-5 | string · options ["GMT-8","GMT-7", "GMT-6", "GMT-5", "GMT-4", "GMT-3", "GMT-2", "GMT-1", "GMT", "GMT+1", "GMT+2", "GMT+3","GMT+4","GMT+5","GMT+6","GMT+7","GMT+8","GMT+9","GMT+10","GMT+11","GMT+12","GMT+13"] | currentTimeIsWithinWindowOfTime |
| Time Filter | 0700-0900 | session | isTimeFilterEnabledAndInRange |
| Trade Direction | |||
| Trade Direction | Long and Short | string · options ['Long and Short', 'Long Only', 'Short Only'] | entry Long entry Short line 120 line 123 |
| Take Profit | |||
| Take Profit 1 - Target % | 100 | float · min 0.0 · step 0.5 | exit TP1 |
| % Of Position | 100 | int · min 0 | exit TP1 |
| Take Profit 2 - Target % | 100 | float · min 0.0 · step 0.5 | exit TP2 |
| % Of Position | 100 | int · min 0 | exit TP2 |
| Take Profit 3 - Target % | 100 | float · min 0.0 · step 0.5 | exit TP3 |
| % Of Position | 100 | int · min 0 | exit TP3 |
| Take Profit 4 - Target % | 100 | float · min 0.0 · step 0.5 | exit i_takeProfitTargetPercent4 |
| Stop Loss | |||
| Stop Loss (%) | 999 | float · min 0.01 · step 0.5 | slLongClose slShortClose |
| Leverage | |||
| Leverage | 1 | float · step .5 | contracts |
| % of Equity to Stake Per Trade | 100 | float · min 0.01 · max 100 · step 5 | contracts |
| ProfitView Alert Syntax | |||
| Alert Syntax Prefix | CRYPTANEX_99FTX_Strategy-Name-Here | string | alertSyntaxBase |
| Dashboards | |||
| Dashboard Size | Small | string · options ["Auto", "Huge", "Large", "Normal", "Small", "Tiny"] | tableTextSize |
| Performance Summary | true | bool | line 159 |
| Monthly Performance | true | bool | line 202 |
What it calculates
- RSIclose, 10
rsiEntry · line 9 - RSIclose, 100
rsiExit · line 10 - CrossoverrsiEntry, 80
longConditionEntry · line 11 - CrossunderrsiEntry, 20
shortConditionEntry · line 12 - CrossunderrsiExit, 40
longConditionExit · line 13 - CrossoverrsiExit, 60
shortConditionExit · line 14 - EMAEMA Source (close), EMA Length (200)
emaValue · line 28 - DMIDI Length (14), ADX Smoothing (14)
DIPlusO · line 53 - DMIDI Length (14), ADX Smoothing (14)
DIMinusO · line 53 - DMIDI Length (14), ADX Smoothing (14)
ADXO · line 53
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 82 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 | longConditionCalc and i_tradeDirection != 'Short Only' and isInLongPosition == false | On | 121 |
| Open Short short | shortConditionCalc and i_tradeDirection != 'Long Only' and isInShortPosition == false | On | 124 |
| Exit TP1 limit | isStartEndPeriodsAndTimeInRange | On | 128 |
| Exit TP2 limit | isStartEndPeriodsAndTimeInRange | On | 129 |
| Exit TP3 limit | isStartEndPeriodsAndTimeInRange | On | 130 |
| Exit i_takeProfitTargetPercent4 limit | isStartEndPeriodsAndTimeInRange | On | 131 |
| Close Long | slLongCloseclose < strategy.position_avg_price * (1 - i_stopLossPercent) | On | 133 |
| Close Short | slShortCloseclose > strategy.position_avg_price * (1 + i_stopLossPercent) | On | 134 |
| Close Long | closeLongCondition | On | 136 |
| Close Short | closeShortCondition | On | 137 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Alerts
| Alert | Call | Message | Line |
|---|---|---|---|
| (no title) | alert | alertSyntaxBase + 'side:long' | 122 |
| (no title) | alert | alertSyntaxBase + 'side:short' | 125 |
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 recalculationuses varip. 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 | −33.1% | +102.5% | 6 | 50% | 0.56 | 62.4% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +24.6% | +5.5% | 3 | 67% | 3.21 | 12.6% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −80.0% | +9.8% | 7 | 43% | 0.26 | 83.1% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +7.5% | +8.4% | 5 | 40% | 1.20 | 28.9% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −56.0% | +16.7% | 5 | 20% | 0.64 | 78.6% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +15.9% | +21.5% | 3 | 33% | 1.69 | 34.4% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +48.1% | +148.3% | 5 | 60% | 1.26 | 71.1% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +11.7% | +17.1% | 3 | 67% | 2.20 | 14.7% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
The trades
By market direction
| At entry, last 90 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 1 | 100% | $795.39 |
| Sideways market | 4 | 25% | −$9,489.52 |
| Falling market | 1 | 100% | $2,083.84 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
57.7%
The 95th percentile of the worst drawdown when the same 6 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 35.2% to 57.7%; the tick is what actually happened, 55.7%. Order luck alone moves it this much.
All 6 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 10 Feb 2024 47,132.80 | 25 Jun 2024 60,293.30 | 136 | $5,550.26 |
| short | 25 Jun 2024 60,293.30 | 11 Nov 2024 80,370.00 | 139 | −$8,558.96 |
| long | 11 Nov 2024 80,370.00 | 27 Feb 2025 84,250.10 | 108 | $795.39 |
| short | 27 Feb 2025 84,250.10 | 9 May 2025 103,262.00 | 71 | −$4,046.40 |
| long | 9 May 2025 103,262.00 | 22 Nov 2025 85,129.40 | 197 | −$2,434.42 |
| short | 22 Nov 2025 85,129.40 | 20 Aug 2026 69,334.80 | 271 | $2,083.84 |
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 |
|---|---|---|---|---|---|
| Strategy Backtesting Template [MYN] this page | — | v5 strategy | −33.1% | 6 | 62.4% |
| RSI Cross [xaurr] by livetrend | rsi ema | v4 strategy | +24.4% | 53 | 35.3% |
| Bitpanda Coinrule Template by joshuajcoop01 | rsi ema | v5 strategy | −2.8% | 4 | 4.6% |
| RSI with Slow and Fast MA Crossing Strategy (by Coinrule) by Coinrule | rsi ema | v5 strategy | −2.8% | 4 | 4.6% |
| RSI of VWAP by mohanee | rsi ema | v4 strategy | No trades on this market | ||
| EMA SCALPEUR + RSi - SHORT by YukalMoon | rsi ema | v5 strategy | No trades on this market | ||
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.
- 4 strategy calls use the when = argument (line 133, 134, 136, 137). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- No v6-only features. v6 is not needed to run this script.
Same 6 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 133: when = slLongClose → if block
- Line 134: when = slShortClose → if block
- Line 136: when = closeLongCondition → if block
- Line 137: when = closeShortCondition → if block
⋯ // Stop Loss- strategy.close('Long', qty_percent=100, comment='SL Long', when=slLongClose)+ if slLongClose+ strategy.close('Long', qty_percent=100, comment='SL Long')- strategy.close('Short', qty_percent=100, comment='SL Short', when=slShortClose)+ if slShortClose+ strategy.close('Short', qty_percent=100, comment='SL Short') // Conditional Closes- strategy.close('Long', qty_percent=100, comment='Close Long', when=closeLongCondition)+ if closeLongCondition+ strategy.close('Long', qty_percent=100, comment='Close Long')- strategy.close('Short', qty_percent=100, comment='Close Short', when=closeShortCondition)+ if closeShortCondition+ strategy.close('Short', qty_percent=100, comment='Close Short')// Global Dashboard Variables⋯Does it run in Pine Script v6?
No
The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 6 before, 8 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © myn-//@version=5+//@version=6strategy('Strategy Backtesting Template [MYN]', max_bars_back=5000, overlay=true, pyramiding=0, initial_capital=20000, currency='NONE', default_qty_type=strategy.percent_of_equity, default_qty_value=100.0, commission_value=0.075, use_bar_magnifier = false)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 177 ms | With a workaround 1,000 bars in 174 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/// © myn//@version=5strategy('Strategy Backtesting Template [MYN]', max_bars_back=5000, overlay=true, pyramiding=0, initial_capital=20000, currency='NONE', default_qty_type=strategy.percent_of_equity, default_qty_value=100.0, commission_value=0.075, use_bar_magnifier = false)///////////////////////////////////////* Put your strategy logic below *///////////////////////////////////////// Below is a very simple example to demonstrate the trading templatersiEntry = ta.rsi(close, 10)rsiExit = ta.rsi(close,100)longConditionEntry = ta.crossover(rsiEntry, 80)shortConditionEntry = ta.crossunder(rsiEntry, 20) longConditionExit = ta.crossunder(rsiExit,40) shortConditionExit = ta.crossover(rsiExit, 60)////////////////////////////////////////* Put your strategy rules below *///////////////////////////////////////longCondition = longConditionEntry // default: falseshortCondition = shortConditionEntry // default: false//define as 0 if do not want to have a conditional closecloseLongCondition = longConditionExit // default: 0closeShortCondition = shortConditionExit // default: 0// EMA Filter//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░i_emaFilterEnabled = input.bool(defval = false , title = "Enable EMA Filter", tooltip = "Enable if you would like to conditionally have entries incorporate EMA as a filter where source is above/below the EMA line", group ="EMA Filter" ) i_emaLength = input.int(200, title="EMA Length", minval=1, group ="EMA Filter")i_emaSource = input.source(close,"EMA Source" , group ="EMA Filter")emaValue = i_emaFilterEnabled ? ta.ema(i_emaSource, i_emaLength) : nabool isEMAFilterEnabledAndCloseAboveMA = i_emaFilterEnabled ? i_emaSource > emaValue : truebool isEMAFilterEnabledAndCloseBelowMA = i_emaFilterEnabled ? i_emaSource < emaValue : true// ADX Filter//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░i_adxFilterEnabled = input.bool(defval = false , title = "Enable ADX Filter", tooltip = "Enable if you would like to conditionally have entries incorporate ADX as a filter", group ="ADX Filter" ) i_adxVariant = input.string('ORIGINAL', title='ADX Variant', options=['ORIGINAL', 'MASANAKAMURA'], group ="ADX Filter" )i_adxSmoothing = input.int(14, title="ADX Smoothing", group="ADX Filter")i_adxDILength = input.int(14, title="DI Length", group="ADX Filter")i_adxLowerThreshold = input.float(25, title="ADX Threshold", step=.5, group="ADX Filter")calcADX_Masanakamura(int _len) => _smoothedTrueRange = 0.0 _smoothedDirectionalMovementPlus = 0.0 _smoothed_directionalMovementMinus = 0.0 _trueRange = math.max(math.max(high - low, math.abs(high - nz(close[1]))), math.abs(low - nz(close[1]))) _directionalMovementPlus = high - nz(high[1]) > nz(low[1]) - low ? math.max(high - nz(high[1]), 0) : 0 _directionalMovementMinus = nz(low[1]) - low > high - nz(high[1]) ? math.max(nz(low[1]) - low, 0) : 0 _smoothedTrueRange := nz(_smoothedTrueRange[1]) - nz(_smoothedTrueRange[1]) / _len + _trueRange _smoothedDirectionalMovementPlus := nz(_smoothedDirectionalMovementPlus[1]) - nz(_smoothedDirectionalMovementPlus[1]) / _len + _directionalMovementPlus _smoothed_directionalMovementMinus := nz(_smoothed_directionalMovementMinus[1]) - nz(_smoothed_directionalMovementMinus[1]) / _len + _directionalMovementMinus DIP = _smoothedDirectionalMovementPlus / _smoothedTrueRange * 100 DIM = _smoothed_directionalMovementMinus / _smoothedTrueRange * 100 _DX = math.abs(DIP - DIM) / (DIP + DIM) * 100 adx = ta.sma(_DX, _len) [DIP, DIM, adx][DIPlusO, DIMinusO, ADXO] = ta.dmi(i_adxDILength, i_adxSmoothing)[DIPlusM, DIMinusM, ADXM] = calcADX_Masanakamura(i_adxDILength)adx = i_adxFilterEnabled and i_adxVariant == "ORIGINAL" ? ADXO : ADXMbool isADXFilterEnabledAndAboveThreshold = i_adxFilterEnabled ? adx > i_adxLowerThreshold : true///Start / End Time Periods//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░i_startPeriodEnabled = input.bool(true, 'Start', group='Date Range', inline='Start Period')i_startPeriodTime = input.time(timestamp('1 Jan 2019'), '', group='Date Range', inline='Start Period')i_endPeriodEnabled = input.bool(true, 'End', group='Date Range', inline='End Period')i_endPeriodTime = input.time(timestamp('31 Dec 2030'), '', group='Date Range', inline='End Period')isStartPeriodEnabledAndInRange = i_startPeriodEnabled ? i_startPeriodTime <= time : trueisEndPeriodEnabledAndInRange = i_endPeriodEnabled ? i_endPeriodTime >= time : true// Time-Of-Day Window// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░// Inspired from https://www.tradingview.com/script/3BmID7aW-Highlight-Trading-Window-Simple-Hours-Time-of-Day-Filter/i_timeFilterEnabled = input.bool(defval = false , title = "Enable Time-Of-Day Window", tooltip = "Limit the time of day for trade execution", group ="Time Window" ) i_timeZone = input.string(title="Select Local Time Zone", defval="GMT-5", options=["GMT-8","GMT-7", "GMT-6", "GMT-5", "GMT-4", "GMT-3", "GMT-2", "GMT-1", "GMT", "GMT+1", "GMT+2", "GMT+3","GMT+4","GMT+5","GMT+6","GMT+7","GMT+8","GMT+9","GMT+10","GMT+11","GMT+12","GMT+13"], group="Time Window")i_betweenTime = input.session('0700-0900', title = "Time Filter", group="Time Window") // '0000-0000' is anytime to enterisWithinWindowOfTime(_position) => currentTimeIsWithinWindowOfTime = not na(time(timeframe.period, _position + ':1234567', i_timeZone))bool isTimeFilterEnabledAndInRange = i_timeFilterEnabled ? isWithinWindowOfTime(i_betweenTime) : trueisStartEndPeriodsAndTimeInRange = isStartPeriodEnabledAndInRange and isEndPeriodEnabledAndInRange and isTimeFilterEnabledAndInRange// Trade Direction // ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░i_tradeDirection = input.string('Long and Short', title='Trade Direction', options=['Long and Short', 'Long Only', 'Short Only'], group='Trade Direction')// Percent as Points// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░per(pcnt) => strategy.position_size != 0 ? math.round(pcnt / 100 * strategy.position_avg_price / syminfo.mintick) : float(na)// Take profit 1// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░i_takeProfitTargetPercent1 = input.float(title='Take Profit 1 - Target %', defval=100, minval=0.0, step=0.5, group='Take Profit', inline='Take Profit 1')i_takeProfitQuantityPercent1 = input.int(title='% Of Position', defval=100, minval=0, group='Take Profit', inline='Take Profit 1')// Take profit 2// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░i_takeProfitTargetPercent2 = input.float(title='Take Profit 2 - Target %', defval=100, minval=0.0, step=0.5, group='Take Profit', inline='Take Profit 2')i_takeProfitQuantityPercent2 = input.int(title='% Of Position', defval=100, minval=0, group='Take Profit', inline='Take Profit 2')// Take profit 3// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░i_takeProfitTargetPercent3 = input.float(title='Take Profit 3 - Target %', defval=100, minval=0.0, step=0.5, group='Take Profit', inline='Take Profit 3')i_takeProfitQuantityPercent3 = input.int(title='% Of Position', defval=100, minval=0, group='Take Profit', inline='Take Profit 3')// Take profit 4// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░i_takeProfitTargetPercent4 = input.float(title='Take Profit 4 - Target %', defval=100, minval=0.0, step=0.5, group='Take Profit')/// Stop Loss// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░i_stopLossPercent = input.float(title='Stop Loss (%)', defval=999, minval=0.01, step=0.5, group='Stop Loss') * 0.01slLongClose = close < strategy.position_avg_price * (1 - i_stopLossPercent)slShortClose = close > strategy.position_avg_price * (1 + i_stopLossPercent)/// Leverage// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░i_leverage = input.float(1, 'Leverage', step=.5, group='Leverage')i_percentOfEquityToTrade = input.float(100, "% of Equity to Stake Per Trade", minval=0.01, maxval=100, step=5, group='Leverage') * .01contracts = (i_percentOfEquityToTrade * strategy.equity / close * i_leverage)/// Trade State Management// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░isInLongPosition = strategy.position_size > 0isInShortPosition = strategy.position_size < 0/// ProfitView Alert Syntax String Generation// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░i_alertSyntaxPrefix = input.string(defval='CRYPTANEX_99FTX_Strategy-Name-Here', title='Alert Syntax Prefix', group='ProfitView Alert Syntax')alertSyntaxBase = i_alertSyntaxPrefix + '\n#' + str.tostring(open) + ',' + str.tostring(high) + ',' + str.tostring(low) + ',' + str.tostring(close) + ',' + str.tostring(volume) + ','/// Trade Execution// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░longConditionCalc = (longCondition and isADXFilterEnabledAndAboveThreshold and isEMAFilterEnabledAndCloseAboveMA)shortConditionCalc = (shortCondition and isADXFilterEnabledAndAboveThreshold and isEMAFilterEnabledAndCloseBelowMA)if isStartEndPeriodsAndTimeInRange if longConditionCalc and i_tradeDirection != 'Short Only' and isInLongPosition == false strategy.entry('Long', strategy.long, qty=contracts) alert(message=alertSyntaxBase + 'side:long', freq=alert.freq_once_per_bar_close) if shortConditionCalc and i_tradeDirection != 'Long Only' and isInShortPosition == false strategy.entry('Short', strategy.short, qty=contracts) alert(message=alertSyntaxBase + 'side:short', freq=alert.freq_once_per_bar_close) //Inspired from Multiple %% profit exits example by adolgo https://www.tradingview.com/script/kHhCik9f-Multiple-profit-exits-example/ strategy.exit('TP1', qty_percent=i_takeProfitQuantityPercent1, profit=per(i_takeProfitTargetPercent1)) strategy.exit('TP2', qty_percent=i_takeProfitQuantityPercent2, profit=per(i_takeProfitTargetPercent2)) strategy.exit('TP3', qty_percent=i_takeProfitQuantityPercent3, profit=per(i_takeProfitTargetPercent3)) strategy.exit('i_takeProfitTargetPercent4', profit=per(i_takeProfitTargetPercent4)) // Stop Loss strategy.close('Long', qty_percent=100, comment='SL Long', when=slLongClose) strategy.close('Short', qty_percent=100, comment='SL Short', when=slShortClose) // Conditional Closes strategy.close('Long', qty_percent=100, comment='Close Long', when=closeLongCondition) strategy.close('Short', qty_percent=100, comment='Close Short', when=closeShortCondition)// Global Dashboard Variables// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░// Dashboard Table Text Sizei_tableTextSize = input.string(title="Dashboard Size", defval="Small", options=["Auto", "Huge", "Large", "Normal", "Small", "Tiny"], group="Dashboards")table_text_size(s) => switch s "Auto" => size.auto "Huge" => size.huge "Large" => size.large "Normal" => size.normal "Small" => size.small => size.tinytableTextSize = table_text_size(i_tableTextSize)/// Performance Summary Dashboard// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░// Inspired by https://www.tradingview.com/script/uWqKX6A2/ - Thanks VertMTi_showDashboard = input.bool(title="Performance Summary", defval=true, group="Dashboards", inline="Show Dashboards")f_fillCell(_table, _column, _row, _title, _value, _bgcolor, _txtcolor) => _cellText = _title + "\n" + _value table.cell(_table, _column, _row, _cellText, bgcolor=_bgcolor, text_color=_txtcolor, text_size=tableTextSize)// Draw dashboard tableif i_showDashboard var bgcolor = color.new(color.black,0) // Keep track of Wins/Losses streaks newWin = (strategy.wintrades > strategy.wintrades[1]) and (strategy.losstrades == strategy.losstrades[1]) and (strategy.eventrades == strategy.eventrades[1]) newLoss = (strategy.wintrades == strategy.wintrades[1]) and (strategy.losstrades > strategy.losstrades[1]) and (strategy.eventrades == strategy.eventrades[1]) varip int winRow = 0 varip int lossRow = 0 varip int maxWinRow = 0 varip int maxLossRow = 0 if newWin lossRow := 0 winRow := winRow + 1 if winRow > maxWinRow maxWinRow := winRow if newLoss winRow := 0 lossRow := lossRow + 1 if lossRow > maxLossRow maxLossRow := lossRow // Prepare stats table var table dashTable = table.new(position.top_right, 1, 15, border_width=1) if barstate.islastconfirmedhistory // Update table dollarReturn = strategy.netprofit f_fillCell(dashTable, 0, 0, "Start:", str.format("{0,date,long}", strategy.closedtrades.entry_time(0)) , bgcolor, color.white) // + str.format(" {0,time,HH:mm}", strategy.closedtrades.entry_time(0)) f_fillCell(dashTable, 0, 1, "End:", str.format("{0,date,long}", strategy.opentrades.entry_time(0)) , bgcolor, color.white) // + str.format(" {0,time,HH:mm}", strategy.opentrades.entry_time(0)) _profit = (strategy.netprofit / strategy.initial_capital) * 100 f_fillCell(dashTable, 0, 2, "Net Profit:", str.tostring(_profit, '##.##') + "%", _profit > 0 ? color.teal : color.maroon, color.white) _numOfDaysInStrategy = (strategy.opentrades.entry_time(0) - strategy.closedtrades.entry_time(0)) / (1000 * 3600 * 24) f_fillCell(dashTable, 0, 3, "Percent Per Day", str.tostring(_profit / _numOfDaysInStrategy, '#########################.#####')+"%", _profit > 0 ? color.teal : color.maroon, color.white) _winRate = ( strategy.wintrades / strategy.closedtrades ) * 100 f_fillCell(dashTable, 0, 4, "Percent Profitable:", str.tostring(_winRate, '##.##') + "%", _winRate < 50 ? color.maroon : _winRate < 75 ? #999900 : color.teal, color.white) f_fillCell(dashTable, 0, 5, "Profit Factor:", str.tostring(strategy.grossprofit / strategy.grossloss, '##.###'), strategy.grossprofit > strategy.grossloss ? color.teal : color.maroon, color.white) f_fillCell(dashTable, 0, 6, "Total Trades:", str.tostring(strategy.closedtrades), bgcolor, color.white) f_fillCell(dashTable, 0, 8, "Max Wins In A Row:", str.tostring(maxWinRow, '######') , bgcolor, color.white) f_fillCell(dashTable, 0, 9, "Max Losses In A Row:", str.tostring(maxLossRow, '######') , bgcolor, color.white)// Monthly Table Performance Dashboard By @QuantNomad// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░i_showMonthlyPerformance = input.bool(true, 'Monthly Performance', group='Dashboards', inline="Show Dashboards")i_monthlyReturnPercision = 2if i_showMonthlyPerformance new_month = month(time) != month(time[1]) new_year = year(time) != year(time[1]) eq = strategy.equity bar_pnl = eq / eq[1] - 1 cur_month_pnl = 0.0 cur_year_pnl = 0.0 // Current Monthly P&L cur_month_pnl := new_month ? 0.0 : (1 + cur_month_pnl[1]) * (1 + bar_pnl) - 1 // Current Yearly P&L cur_year_pnl := new_year ? 0.0 : (1 + cur_year_pnl[1]) * (1 + bar_pnl) - 1 // Arrays to store Yearly and Monthly P&Ls var month_pnl = array.new_float(0) var month_time = array.new_int(0) var year_pnl = array.new_float(0) var year_time = array.new_int(0) last_computed = false if (not na(cur_month_pnl[1]) and (new_month or barstate.islastconfirmedhistory)) if (last_computed[1]) array.pop(month_pnl) array.pop(month_time) array.push(month_pnl , cur_month_pnl[1]) array.push(month_time, time[1]) if (not na(cur_year_pnl[1]) and (new_year or barstate.islastconfirmedhistory)) if (last_computed[1]) array.pop(year_pnl) array.pop(year_time) array.push(year_pnl , cur_year_pnl[1]) array.push(year_time, time[1]) last_computed := barstate.islastconfirmedhistory ? true : nz(last_computed[1]) // Monthly P&L Table var monthly_table = table(na) if (barstate.islastconfirmedhistory) monthly_table := table.new(position.bottom_right, columns = 14, rows = array.size(year_pnl) + 1, border_width = 1) table.cell(monthly_table, 0, 0, "", bgcolor = #cccccc, text_size=tableTextSize) table.cell(monthly_table, 1, 0, "Jan", bgcolor = #cccccc, text_size=tableTextSize) table.cell(monthly_table, 2, 0, "Feb", bgcolor = #cccccc, text_size=tableTextSize) table.cell(monthly_table, 3, 0, "Mar", bgcolor = #cccccc, text_size=tableTextSize) table.cell(monthly_table, 4, 0, "Apr", bgcolor = #cccccc, text_size=tableTextSize) table.cell(monthly_table, 5, 0, "May", bgcolor = #cccccc, text_size=tableTextSize) table.cell(monthly_table, 6, 0, "Jun", bgcolor = #cccccc, text_size=tableTextSize) table.cell(monthly_table, 7, 0, "Jul", bgcolor = #cccccc, text_size=tableTextSize) table.cell(monthly_table, 8, 0, "Aug", bgcolor = #cccccc, text_size=tableTextSize) table.cell(monthly_table, 9, 0, "Sep", bgcolor = #cccccc, text_size=tableTextSize) table.cell(monthly_table, 10, 0, "Oct", bgcolor = #cccccc, text_size=tableTextSize) table.cell(monthly_table, 11, 0, "Nov", bgcolor = #cccccc, text_size=tableTextSize) table.cell(monthly_table, 12, 0, "Dec", bgcolor = #cccccc, text_size=tableTextSize) table.cell(monthly_table, 13, 0, "Year", bgcolor = #999999, text_size=tableTextSize) for yi = 0 to array.size(year_pnl) - 1 table.cell(monthly_table, 0, yi + 1, str.tostring(year(array.get(year_time, yi))), bgcolor = #cccccc, text_size=tableTextSize) y_color = array.get(year_pnl, yi) > 0 ? color.new(color.teal, transp = 40) : color.new(color.gray, transp = 40) table.cell(monthly_table, 13, yi + 1, str.tostring(math.round(array.get(year_pnl, yi) * 100, i_monthlyReturnPercision)), bgcolor = y_color, text_color=color.new(color.white, 0),text_size=tableTextSize) for mi = 0 to array.size(month_time) - 1 m_row = year(array.get(month_time, mi)) - year(array.get(year_time, 0)) + 1 m_col = month(array.get(month_time, mi)) m_color = array.get(month_pnl, mi) > 0 ? color.new(color.teal, transp = 40) : color.new(color.maroon, transp = 40) table.cell(monthly_table, m_col, m_row, str.tostring(math.round(array.get(month_pnl, mi) * 100, i_monthlyReturnPercision)), bgcolor = m_color, text_color=color.new(color.white, 0), text_size=tableTextSize)