Pine Script v4 · Strategy
[cache_that_pass] 1m 15m Function - Weighted Standard Deviation
A Pine Script v4 strategy with no technical-analysis calculation. It has 4 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 commits 20% of equity and pays 0.075% commission. 3 settings; defaults include Len 20, Stop Loss % 3.11 and Take Profit % 7.50.
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 [cache_that_pass] 1m 15m Function - Weighted Standard Deviation repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 30 of 30 trades.
- How did [cache_that_pass] 1m 15m Function - Weighted Standard Deviation do on BTCUSDT 1D with default settings?
- −5.25%
- From 26 Jan 2024 to 21 Oct 2026, over 100 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can [cache_that_pass] 1m 15m Function - Weighted Standard Deviation be converted to Pine Script v5?
- Yes
- The upgrade takes 1 step, from v4; each was run before and after on the same candles with identical results, and the v5 script can be downloaded below.
- Does [cache_that_pass] 1m 15m Function - Weighted Standard Deviation use a stop loss?
- Yes
- strategy.exit sets a stop on line 100, 103.
- Does [cache_that_pass] 1m 15m Function - Weighted Standard Deviation run in Zeal Terminal?
- Yes
- It compiles in 66 ms and runs 1,000 bars in 242 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Plot
- Plot
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −5.25%
- −$524.79
- Buy and hold
- +102.5%
- same period
- Closed trades
- 100
- 34 won · 66 lost
- Profitable
- 34.0%
- of closed trades
- Profit factor
- 0.89
- gross profit ÷ gross loss
- Max drawdown
- 10.64%
- −$1,063.83
One run with the author's default settings, $10,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.
-
Note
1 setting has no label
In the settings dialog len (line 56) shows 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.
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 sum → math.sum, pow → math.pow, sqrt → math.sqrt, tostring → str.tostring.
v4 → v5
VerifiedSame 100 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 12: sum → math.sum
- Line 13: sum → math.sum
- Line 17: pow → math.pow
- Line 20: sqrt → math.sqrt
- Line 22: sqrt → math.sqrt
15 more edits
- Line 46: pow → math.pow
- Line 48: sqrt → math.sqrt
- Line 50: sqrt → math.sqrt
- Line 81: tostring → str.tostring
- Line 81: tostring → str.tostring
- Line 81: tostring → str.tostring
- Line 81: tostring → str.tostring
- Line 81: tostring → str.tostring
- Line 82: tostring → str.tostring
- Line 82: tostring → str.tostring
- Line 82: tostring → str.tostring
- Line 82: tostring → str.tostring
- Line 82: tostring → str.tostring
- Line 92: input(type = input.float) → input.float()
- Line 93: input(type = input.float) → input.float()
⋯// © rumpypumpydumpy © cache_that_pass-//@version=4+//@version=5strategy("[cache_that_pass] 1m 15m Function - Weighted Standard Deviation", overlay=true, pyramiding=0, default_qty_type=strategy.percent_of_equity, default_qty_value=20, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.075)⋯ _xw = _src * _weight- _sum_weight = sum(_weight, _n)+ _sum_weight = math.sum(_weight, _n)- _mean = sum(_xw, _n) / _sum_weight+ _mean = math.sum(_xw, _n) / _sum_weight float _sqerror_sum = 0⋯ for _i = 0 to _n - 1- _sqerror_sum := _sqerror_sum + pow(_mean - _src[_i], 2) * _weight[_i]+ _sqerror_sum := _sqerror_sum + math.pow(_mean - _src[_i], 2) * _weight[_i] _nonzero_n := _weight[_i] != 0 ? _nonzero_n + 1 : _nonzero_n _variance = _sqerror_sum / ((_nonzero_n - 1) * _sum_weight / _nonzero_n)- _dev = sqrt(_variance)+ _dev = math.sqrt(_variance) _mse = _sqerror_sum / _sum_weight- _rmse = sqrt(_mse)+ _rmse = math.sqrt(_mse) [_mean, _variance, _dev, _mse, _rmse]⋯ for _j = 0 to _n - 1- _sqerror_sum := _sqerror_sum + pow(_mean - array.get(_a_src, _j), 2) * array.get(_a_weight, _j)+ _sqerror_sum := _sqerror_sum + math.pow(_mean - array.get(_a_src, _j), 2) * array.get(_a_weight, _j) _variance := _sqerror_sum / ((_nonzero_n - 1) * _sum_weight / _nonzero_n)- _dev := sqrt(_variance)+ _dev := math.sqrt(_variance) _mse := _sqerror_sum / _sum_weight- _rmse := sqrt(_mse)+ _rmse := math.sqrt(_mse) [_mean, _variance, _dev, _mse, _rmse]⋯// ------------------------------------------------------------------------------series_text = "Mean : " + tostring(m) + "\nVariance : " + tostring(v) + "\nSD : " + tostring(d) + "\nMSE : " + tostring(mse) + "\nRMSE : " + tostring(rmse)+series_text = "Mean : " + str.tostring(m) + "\nVariance : " + str.tostring(v) + "\nSD : " + str.tostring(d) + "\nMSE : " + str.tostring(mse) + "\nRMSE : " + str.tostring(rmse)-array_text = "Mean : " + tostring(a_m) + "\nVariance : " + tostring(a_v) + "\nSD : " + tostring(a_d) + "\nMSE : " + tostring(a_mse) + "\nRMSE : " + tostring(a_rmse)+array_text = "Mean : " + str.tostring(a_m) + "\nVariance : " + str.tostring(a_v) + "\nSD : " + str.tostring(a_d) + "\nMSE : " + str.tostring(a_mse) + "\nRMSE : " + str.tostring(a_rmse)debug_text = "Volume weighted from time series : \n" + series_text + "\n\nLinearly weighted from arrays : \n" + array_text⋯// User Options to Change Inputs (%)-stopPer = input(3.11, title='Stop Loss %', type=input.float) / 100+stopPer = input.float(3.11, title='Stop Loss %') / 100-takePer = input(7.50, title='Take Profit %', type=input.float) / 100+takePer = input.float(7.50, title='Take Profit %') / 100// Determine where you've entered and in what direction⋯Same 100 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 56: input() → input.int()
⋯// ------------------------------------------------------------------------------len = input(20)+len = input.int(20)// -----------------------------------------------------------------------------⋯Does it run in Pine Script v6?
Yes
Same 100 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. It differs from the v5 version by 1 edit.
⋯// © rumpypumpydumpy © cache_that_pass-//@version=5+//@version=6strategy("[cache_that_pass] 1m 15m Function - Weighted Standard Deviation", overlay=true, pyramiding=0, default_qty_type=strategy.percent_of_equity, default_qty_value=20, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.075)⋯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 |
|---|---|---|---|
| Len (no label in the script) | 20 | int | m v d mse rmse a_m a_v a_d a_mse a_rmse line 71 line 74 |
| Stop Loss % | 3.11 | float | longStop shortStop |
| Take Profit % | 7.50 | float | shortTake longTake |
What it calculates
No technical-analysis calculation: the script works on prices and settings directly.
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 LE long | low <= (m - d * 2) | On | 88 |
| Open SE short | high >= (m + d * 2) | On | 90 |
| Exit Close Long stop and limit | strategy.position_size > 0 | On | 100 |
| Exit Close Short stop and limit | strategy.position_size < 0 | On | 103 |
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, and the live replay matched history.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- 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
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 | −5.2% | +102.5% | 100 | 34% | 0.89 | 10.6% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +2.6% | +5.5% | 58 | 64% | 1.23 | 2.4% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −7.7% | +9.8% | 105 | 32% | 0.86 | 11.9% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.4% | +8.4% | 57 | 54% | 0.97 | 5.2% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −15.4% | +16.7% | 123 | 28% | 0.75 | 20.9% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −2.6% | +21.5% | 74 | 46% | 0.90 | 4.9% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +2.8% | +148.3% | 101 | 38% | 1.06 | 8.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.4% | +17.1% | 57 | 56% | 0.97 | 3.8% | 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 | 36 | 33% | −$332.04 |
| Sideways market | 39 | 28% | −$590.47 |
| Falling market | 25 | 44% | $399.14 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
17.2%
The 95th percentile of the worst drawdown when the same 100 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 8.5% to 17.2%; the tick is what actually happened, 10.5%. Order luck alone moves it this much.
All 100 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 28 Feb 2024 57,037.30 | 29 Feb 2024 62,432.10 | 1 | −$192.16 |
| short | 1 Mar 2024 61,131.00 | 3 Mar 2024 63,032.20 | 2 | −$63.95 |
| short | 9 Apr 2024 71,620.00 | 12 Apr 2024 66,248.50 | 3 | $143.24 |
| long | 13 Apr 2024 67,116.50 | 14 Apr 2024 63,924.50 | 1 | −$96.87 |
| long | 15 Apr 2024 65,661.90 | 16 Apr 2024 63,420.00 | 1 | −$69.69 |
| long | 17 Apr 2024 63,793.40 | 18 Apr 2024 61,277.40 | 1 | −$79.48 |
| long | 20 Apr 2024 63,818.00 | 29 Apr 2024 61,833.30 | 9 | −$62.77 |
| long | 1 May 2024 60,672.00 | 2 May 2024 58,365.00 | 1 | −$75.61 |
| long | 3 May 2024 59,060.60 | 4 May 2024 63,490.20 | 1 | $139.48 |
| short | 16 May 2024 66,206.50 | 20 May 2024 68,265.50 | 4 | −$62.87 |
| short | 21 May 2024 71,446.60 | 11 Jun 2024 66,088.10 | 21 | $140.82 |
| long | 12 Jun 2024 67,314.20 | 14 Jun 2024 65,220.80 | 2 | −$63.28 |
| long | 15 Jun 2024 66,044.00 | 21 Jun 2024 63,990.00 | 6 | −$62.87 |
| long | 25 Jun 2024 60,293.30 | 4 Jul 2024 58,418.20 | 9 | −$62.46 |
| long | 6 Jul 2024 56,628.80 | 8 Jul 2024 54,867.60 | 2 | −$62.05 |
| short | 16 Jul 2024 64,724.10 | 19 Jul 2024 66,737.00 | 3 | −$61.74 |
| short | 20 Jul 2024 66,660.00 | 27 Jul 2024 68,733.10 | 7 | −$61.34 |
| long | 3 Aug 2024 61,498.30 | 4 Aug 2024 59,585.70 | 1 | −$60.85 |
| long | 5 Aug 2024 58,161.00 | 6 Aug 2024 54,018.80 | 1 | −$134.85 |
| short | 24 Aug 2024 64,037.20 | 27 Aug 2024 59,234.40 | 3 | $134.52 |
| long | 5 Sep 2024 57,970.90 | 6 Sep 2024 56,168.00 | 1 | −$60.45 |
| long | 7 Sep 2024 53,963.00 | 9 Sep 2024 58,010.20 | 2 | $135.40 |
| short | 20 Sep 2024 62,948.00 | 26 Sep 2024 64,905.70 | 6 | −$61.03 |
| long | 11 Oct 2024 60,326.40 | 14 Oct 2024 64,850.90 | 3 | $136.49 |
| short | 16 Oct 2024 67,074.10 | 20 Oct 2024 69,160.10 | 4 | −$61.52 |
| short | 30 Oct 2024 72,736.40 | 4 Nov 2024 67,281.20 | 5 | $137.80 |
| short | 11 Nov 2024 80,370.00 | 12 Nov 2024 88,648.00 | 1 | −$198.79 |
| short | 6 Dec 2024 96,945.60 | 7 Dec 2024 99,960.60 | 1 | −$60.72 |
| short | 10 Dec 2024 97,276.50 | 11 Dec 2024 100,302.00 | 1 | −$60.32 |
| short | 12 Dec 2024 101,125.00 | 15 Dec 2024 104,270.00 | 3 | −$59.93 |
| short | 16 Dec 2024 104,464.00 | 17 Dec 2024 107,713.00 | 1 | −$59.54 |
| short | 18 Dec 2024 106,134.00 | 19 Dec 2024 98,173.70 | 1 | $133.37 |
| long | 21 Dec 2024 97,805.40 | 22 Dec 2024 94,763.70 | 1 | −$59.94 |
| long | 24 Dec 2024 94,881.50 | 30 Dec 2024 91,930.60 | 6 | −$59.55 |
| short | 7 Jan 2025 102,236.00 | 8 Jan 2025 94,567.90 | 1 | $133.57 |
| long | 14 Jan 2025 94,536.10 | 17 Jan 2025 101,626.00 | 3 | $135.33 |
| short | 18 Jan 2025 104,077.00 | 20 Jan 2025 107,314.00 | 2 | −$61.00 |
| short | 21 Jan 2025 102,260.00 | 22 Jan 2025 106,144.00 | 1 | −$73.39 |
| long | 3 Feb 2025 97,700.60 | 12 Feb 2025 94,662.10 | 9 | −$60.03 |
| long | 24 Feb 2025 96,258.00 | 25 Feb 2025 91,552.90 | 1 | −$92.17 |
| long | 26 Feb 2025 88,680.40 | 27 Feb 2025 84,250.10 | 1 | −$93.19 |
| long | 1 Mar 2025 84,349.90 | 2 Mar 2025 90,676.20 | 1 | $131.74 |
| long | 11 Mar 2025 78,595.90 | 12 Mar 2025 84,490.60 | 1 | $133.68 |
| short | 3 Apr 2025 82,516.30 | 7 Apr 2025 78,430.00 | 4 | $88.75 |
| long | 7 Apr 2025 78,430.00 | 9 Apr 2025 75,990.80 | 2 | −$60.75 |
| long | 10 Apr 2025 82,615.20 | 11 Apr 2025 79,607.30 | 1 | −$70.17 |
| short | 22 Apr 2025 87,516.20 | 23 Apr 2025 93,443.00 | 1 | −$127.35 |
| short | 24 Apr 2025 93,691.10 | 1 May 2025 96,604.90 | 7 | −$59.14 |
| short | 9 May 2025 103,262.00 | 18 May 2025 106,473.00 | 9 | −$58.76 |
| long | 6 Jun 2025 101,509.00 | 9 Jun 2025 109,122.00 | 3 | $131.42 |
| long | 23 Jun 2025 100,964.00 | 30 Jun 2025 108,536.00 | 7 | $133.35 |
| short | 3 Jul 2025 108,850.00 | 10 Jul 2025 112,235.00 | 7 | −$60.10 |
| short | 11 Jul 2025 116,010.00 | 14 Jul 2025 119,618.00 | 3 | −$59.71 |
| short | 15 Jul 2025 119,841.00 | 31 Jul 2025 117,840.00 | 16 | $27.65 |
| long | 31 Jul 2025 117,840.00 | 1 Aug 2025 114,175.00 | 1 | −$59.42 |
| long | 2 Aug 2025 113,298.00 | 11 Aug 2025 121,795.00 | 9 | $133.08 |
| short | 12 Aug 2025 118,686.00 | 13 Aug 2025 122,377.00 | 1 | −$59.98 |
| short | 14 Aug 2025 123,306.00 | 19 Aug 2025 114,058.00 | 5 | $134.36 |
| long | 25 Aug 2025 113,494.00 | 26 Aug 2025 109,964.00 | 1 | −$60.38 |
| long | 27 Aug 2025 111,763.00 | 29 Aug 2025 108,287.00 | 2 | −$59.99 |
| short | 12 Sep 2025 115,483.00 | 26 Sep 2025 108,994.00 | 14 | $100.11 |
| long | 26 Sep 2025 108,994.00 | 1 Oct 2025 117,169.00 | 5 | $135.85 |
| short | 4 Oct 2025 122,232.00 | 6 Oct 2025 126,034.00 | 2 | −$61.22 |
| long | 11 Oct 2025 112,774.00 | 16 Oct 2025 109,267.00 | 5 | −$60.73 |
| long | 18 Oct 2025 106,432.00 | 26 Oct 2025 114,414.00 | 8 | $136.03 |
| long | 5 Nov 2025 101,497.00 | 13 Nov 2025 98,340.70 | 8 | −$61.22 |
| long | 19 Nov 2025 92,960.80 | 20 Nov 2025 90,069.70 | 1 | −$60.83 |
| long | 22 Nov 2025 85,129.40 | 27 Nov 2025 91,514.10 | 5 | $136.24 |
| short | 10 Dec 2025 92,678.80 | 15 Dec 2025 85,727.90 | 5 | $138.45 |
| long | 16 Dec 2025 86,432.10 | 3 Jan 2026 89,995.10 | 18 | $75.81 |
| short | 3 Jan 2026 89,995.10 | 5 Jan 2026 92,794.00 | 2 | −$62.81 |
| short | 6 Jan 2026 93,859.70 | 14 Jan 2026 96,778.80 | 8 | −$62.39 |
| short | 15 Jan 2026 96,951.80 | 20 Jan 2026 89,680.40 | 5 | $139.76 |
| long | 22 Jan 2026 89,454.70 | 25 Jan 2026 86,672.70 | 3 | −$62.81 |
| long | 31 Jan 2026 84,260.50 | 1 Feb 2026 78,741.10 | 1 | −$128.25 |
| long | 26 Feb 2026 67,988.00 | 27 Feb 2026 65,873.60 | 1 | −$61.56 |
| long | 1 Mar 2026 66,973.30 | 4 Mar 2026 71,996.30 | 3 | $137.89 |
| short | 5 Mar 2026 72,666.80 | 7 Mar 2026 67,216.80 | 2 | $140.13 |
| short | 14 Mar 2026 70,930.00 | 15 Mar 2026 73,135.90 | 1 | −$63.06 |
| short | 17 Mar 2026 74,884.70 | 19 Mar 2026 69,268.30 | 2 | $141.26 |
| long | 28 Mar 2026 66,407.30 | 7 Apr 2026 71,387.80 | 10 | $143.12 |
| short | 8 Apr 2026 71,924.20 | 13 Apr 2026 74,161.10 | 5 | −$64.50 |
| short | 14 Apr 2026 74,418.00 | 17 Apr 2026 76,732.40 | 3 | −$64.08 |
| short | 18 Apr 2026 77,072.00 | 22 Apr 2026 79,468.90 | 4 | −$63.67 |
| short | 23 Apr 2026 78,178.20 | 4 May 2026 80,609.60 | 11 | −$63.25 |
| short | 5 May 2026 79,861.00 | 6 May 2026 82,344.70 | 1 | −$62.84 |
| short | 7 May 2026 81,447.00 | 20 May 2026 76,834.40 | 13 | $105.59 |
| long | 20 May 2026 76,834.40 | 23 May 2026 74,444.80 | 3 | −$63.04 |
| long | 24 May 2026 76,752.00 | 27 May 2026 74,365.00 | 3 | −$62.62 |
| long | 29 May 2026 73,617.50 | 1 Jun 2026 71,328.00 | 3 | −$62.21 |
| long | 2 Jun 2026 71,408.90 | 3 Jun 2026 66,760.80 | 1 | −$126.24 |
| long | 4 Jun 2026 64,142.80 | 5 Jun 2026 62,147.90 | 1 | −$60.98 |
| long | 25 Jun 2026 61,078.00 | 26 Jun 2026 59,178.50 | 1 | −$60.59 |
| long | 27 Jun 2026 60,097.30 | 30 Jun 2026 58,228.20 | 3 | −$60.19 |
| short | 22 Jul 2026 66,556.10 | 4 Aug 2026 63,520.00 | 13 | $81.05 |
| long | 4 Aug 2026 63,520.00 | 19 Aug 2026 68,284.00 | 15 | $136.03 |
| short | 20 Aug 2026 69,334.80 | 21 Aug 2026 73,027.00 | 1 | −$102.95 |
| short | 22 Aug 2026 78,338.00 | 25 Aug 2026 80,774.40 | 3 | −$60.63 |
| long | 12 Sep 2026 77,225.70 | 21 Sep 2026 83,017.60 | 9 | $135.60 |
| short | 22 Sep 2026 86,620.00 | 9 Oct 2026 81,754.50 | 17 | $102.50 |
Similar scripts
No other indicator or strategy here shares enough of its calculations.
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 66 ms | Yes 1,000 bars in 242 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/// © rumpypumpydumpy © cache_that_pass//@version=4strategy("[cache_that_pass] 1m 15m Function - Weighted Standard Deviation", overlay=true, pyramiding=0, default_qty_type=strategy.percent_of_equity, default_qty_value=20, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.075)f_weighted_sd_from_series(_src, _weight, _n) => //{// @function: Calculates weighted mean, variance, standard deviation, MSE and RMSE from time series variables// @parameters:// _src: time series variable of sample values// _weight: time series of corresponding weight values.// _n : number of samples _xw = _src * _weight _sum_weight = sum(_weight, _n) _mean = sum(_xw, _n) / _sum_weight float _sqerror_sum = 0 int _nonzero_n = 0 for _i = 0 to _n - 1 _sqerror_sum := _sqerror_sum + pow(_mean - _src[_i], 2) * _weight[_i] _nonzero_n := _weight[_i] != 0 ? _nonzero_n + 1 : _nonzero_n _variance = _sqerror_sum / ((_nonzero_n - 1) * _sum_weight / _nonzero_n) _dev = sqrt(_variance) _mse = _sqerror_sum / _sum_weight _rmse = sqrt(_mse) [_mean, _variance, _dev, _mse, _rmse]//}// -----------------------------------------------------------------------------f_weighted_sd_from_arrays(_a_src, _a_weight, _n) => //{// @function: Calculates weighted mean, variance, standard deviation, MSE and RMSE from arrays// Expects index 0 of the arrays to be the most recent sample and weight values!// @parameters:// _a_src: array of sample values// _a_weight: array of corresponding weight values.// _n : number of samples float _mean = na, float _variance = na, float _dev = na, float _mse = na float _rmse = na, float _sqerror_sum = na, float _sum_weight = na float[] _a_xw = array.new_float(_n) int _nonzero_n = 0 if array.size(_a_src) >= _n _sum_weight := 0 _sqerror_sum := 0 for _i = 0 to _n - 1 array.set(_a_xw, _i, array.get(_a_src, _i) * array.get(_a_weight, _i)) _sum_weight := _sum_weight + array.get(_a_weight, _i) _nonzero_n := array.get(_a_weight, _i) != 0 ? _nonzero_n + 1 : _nonzero_n _mean := array.sum(_a_xw) / _sum_weight for _j = 0 to _n - 1 _sqerror_sum := _sqerror_sum + pow(_mean - array.get(_a_src, _j), 2) * array.get(_a_weight, _j) _variance := _sqerror_sum / ((_nonzero_n - 1) * _sum_weight / _nonzero_n) _dev := sqrt(_variance) _mse := _sqerror_sum / _sum_weight _rmse := sqrt(_mse) [_mean, _variance, _dev, _mse, _rmse]//}// -----------------------------------------------------------------------------// Example usage : // -----------------------------------------------------------------------------len = input(20)// -----------------------------------------------------------------------------// From series :// -----------------------------------------------------------------------------[m, v, d, mse, rmse] = f_weighted_sd_from_series(close, volume, len)plot(m, color = color.blue)plot(m + d * 2, color = color.blue)plot(m - d * 2, color = color.blue)// -----------------------------------------------------------------------------// -----------------------------------------------------------------------------// From arrays : // -----------------------------------------------------------------------------var float[] a_src = array.new_float()var float[] a_weight = array.new_float()if barstate.isfirst for i = 1 to len array.unshift(a_weight, i)array.unshift(a_src, close)if array.size(a_src) > len array.pop(a_src)[a_m, a_v, a_d, a_mse, a_rmse] = f_weighted_sd_from_arrays(a_src, a_weight, len)plot(a_m, color = color.orange)plot(a_m + a_d * 2, color = color.orange)plot(a_m - a_d * 2, color = color.orange)// -----------------------------------------------------------------------------series_text = "Mean : " + tostring(m) + "\nVariance : " + tostring(v) + "\nSD : " + tostring(d) + "\nMSE : " + tostring(mse) + "\nRMSE : " + tostring(rmse)array_text = "Mean : " + tostring(a_m) + "\nVariance : " + tostring(a_v) + "\nSD : " + tostring(a_d) + "\nMSE : " + tostring(a_mse) + "\nRMSE : " + tostring(a_rmse)debug_text = "Volume weighted from time series : \n" + series_text + "\n\nLinearly weighted from arrays : \n" + array_text//debug = label.new(x = bar_index, y = close, text = debug_text, style = label.style_label_left)//.delete(debug[1])//test strategyif low <= (m - d * 2) strategy.entry("LE", strategy.long)if high >= (m + d * 2) strategy.entry("SE", strategy.short)// User Options to Change Inputs (%)stopPer = input(3.11, title='Stop Loss %', type=input.float) / 100takePer = input(7.50, title='Take Profit %', type=input.float) / 100// Determine where you've entered and in what directionlongStop = strategy.position_avg_price * (1 - stopPer)shortStop = strategy.position_avg_price * (1 + stopPer)shortTake = strategy.position_avg_price * (1 - takePer)longTake = strategy.position_avg_price * (1 + takePer)if strategy.position_size > 0 strategy.exit(id="Close Long", stop=longStop, limit=longTake)// strategy.close("LE", when = (longStop) or (longTake), qty_percent = 100)if strategy.position_size < 0 strategy.exit(id="Close Short", stop=shortStop, limit=shortTake)// strategy.close("SE", when = (shortStop) or (shortTake), qty_percent = 100)