Pine Script v4 · Strategy
AlignedMA and Cumulative HighLow Strategy
A Pine Script v4 strategy built on SMA or EMA or HMA or RMA or VWMA or WMA (source, length) and Supertrend (2, 22). It has 4 order rules, 2 entries among them, and trades long and short with the default settings. Exits wait for a signal; there is no stop loss. Each trade commits 100% of equity and pays 0.01% commission. 10 settings; defaults include Moving Average Type hma, High Low Period 22 and Lookback Period 10.
Generated from the script's code by a template: every clause is a fact read from the source. Not written by a person or a model, and not taken from the author's description.
- Does AlignedMA and Cumulative HighLow Strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 4 of 4 trades.
- How did AlignedMA and Cumulative HighLow Strategy do on BTCUSDT 1D with default settings?
- +32.44%
- From 26 Jan 2024 to 21 Oct 2026, over 22 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can AlignedMA and Cumulative HighLow Strategy 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 AlignedMA and Cumulative HighLow Strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does AlignedMA and Cumulative HighLow Strategy run in Zeal Terminal?
- Yes
- It compiles in 30 ms and runs 1,000 bars in 113 ms, in the browser.
BTCUSDT · 1D · default settings
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +32.44%
- $324.36
- Buy and hold
- +102.5%
- same period
- Closed trades
- 22
- 9 won · 13 lost
- Profitable
- 40.9%
- of closed trades
- Profit factor
- 1.36
- gross profit ÷ gross loss
- Max drawdown
- 34.11%
- −$517.74
One run with the author's default settings, $1,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.
-
Check
No stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
6 settings have no label
In the settings dialog includePartiallyAligned (line 6), HighLowPeriod (line 7), LookbackPeriod (line 8), considerYearlyHighLow (line 9), supertrendMult (line 10), supertrendLength (line 11) 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 86, 87, 88, 89. The upgraded version below moves each call inside an if block.
Upgrade and export
Written in Pine Script v4. The upgrade to v5 takes 1 step, one per version, each applied to the result of the one before. A last clean-v5 pass then types the settings and moves when = into if blocks, which v6 requires. Every step runs before and after on the same candles in Zeal Terminal's engine; a step whose results differ is shown but not offered as a download. Upgrades preserve behaviour: bugs are fixed in the Review, never silently here.
- Upgrading to v5 renames sma → ta.sma, ema → ta.ema, hma → ta.hma, rma → ta.rma, vwma → ta.vwma, wma → ta.wma, highest → ta.highest, lowest → ta.lowest, security → request.security, max → math.max, min → math.min, sum → math.sum, supertrend → ta.supertrend.
- 4 strategy calls use the when = argument (line 86, 87, 88, 89). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 22 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 13: input(type = input.time) → input.time()
- Line 14: input(type = input.time) → input.time()
- Line 17: sma → ta.sma
- Line 19: ema → ta.ema
- Line 21: hma → ta.hma
16 more edits
- Line 23: rma → ta.rma
- Line 25: vwma → ta.vwma
- Line 27: wma → ta.wma
- Line 51: highest → ta.highest
- Line 52: lowest → ta.lowest
- Line 59: security → request.security
- Line 60: security → request.security
- Line 66: max → math.max
- Line 66: max → math.max
- Line 66: min → math.min
- Line 68: min → math.min
- Line 68: min → math.min
- Line 68: max → math.max
- Line 74: sum → math.sum
- Line 77: sum → math.sum
- Line 80: supertrend → ta.supertrend
⋯// © HeWhoMustNotBeNamed-//@version=4+//@version=5strategy("AlignedMA and Cumulative HighLow Strategy", overlay=true, initial_capital = 1000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, pyramiding = 1, commission_value = 0.01)⋯tradeDirection = input(title="Trade Direction", defval=strategy.direction.long, options=[strategy.direction.all, strategy.direction.long, strategy.direction.short])-i_startTime = input(defval = timestamp("01 Jan 2010 00:00 +0000"), title = "Backtest Start Time", type = input.time)+i_startTime = input.time(defval = timestamp("01 Jan 2010 00:00 +0000"), title = "Backtest Start Time")-i_endTime = input(defval = timestamp("01 Jan 2099 00:00 +0000"), title = "Backtest End Time", type = input.time)+i_endTime = input.time(defval = timestamp("01 Jan 2099 00:00 +0000"), title = "Backtest End Time")inDateRange = time >= i_startTime and time <= i_endTimef_getMovingAverage(source, MAType, length)=>- ma = sma(source, length)+ ma = ta.sma(source, length) if(MAType == "ema")- ma := ema(source,length)+ ma := ta.ema(source,length) if(MAType == "hma")- ma := hma(source,length)+ ma := ta.hma(source,length) if(MAType == "rma")- ma := rma(source,length)+ ma := ta.rma(source,length) if(MAType == "vwma")- ma := vwma(source,length)+ ma := ta.vwma(source,length) if(MAType == "wma")- ma := wma(source,length)+ ma := ta.wma(source,length) ma⋯f_getHighLowValue(HighLowPeriod)=>- currentHigh = highest(high,HighLowPeriod) == high+ currentHigh = ta.highest(high,HighLowPeriod) == high- currentLow = lowest(low,HighLowPeriod) == low+ currentLow = ta.lowest(low,HighLowPeriod) == low currentHigh?1:currentLow?-1:0⋯f_getYearlyHighLowCondition(considerYearlyHighLow)=>- yhigh = security(syminfo.tickerid, '12M', high[1]) + yhigh = request.security(syminfo.tickerid, '12M', high[1]) - ylow = security(syminfo.tickerid, '12M', low[1]) + ylow = request.security(syminfo.tickerid, '12M', low[1]) yhighlast = yhigh[365]⋯ - yearlyTrendUp = na(yhigh)? true : na(yhighlast)? close > yhigh : na(yhighllast)? close > max(yhigh,yhighlast) : close > max(yhigh, min(yhighlast, yhighllast))+ yearlyTrendUp = na(yhigh)? true : na(yhighlast)? close > yhigh : na(yhighllast)? close > math.max(yhigh,yhighlast) : close > math.max(yhigh, math.min(yhighlast, yhighllast)) yearlyHighCondition = ( (na(yhigh) or na(yhighlast) ? true : (yhigh > yhighlast) ) and ( na(yhigh) or na(yhighllast) ? true : (yhigh > yhighllast))) or yearlyTrendUp or not considerYearlyHighLow- yearlyTrendDown = na(ylow)? true : na(ylowlast)? close < ylow : na(ylowllast)? close < min(ylow,ylowlast) : close < min(ylow, max(ylowlast, ylowllast))+ yearlyTrendDown = na(ylow)? true : na(ylowlast)? close < ylow : na(ylowllast)? close < math.min(ylow,ylowlast) : close < math.min(ylow, math.max(ylowlast, ylowllast)) yearlyLowCondition = ( (na(ylow) or na(ylowlast) ? true : (ylow < ylowlast) ) and ( na(ylow) or na(ylowllast) ? true : (ylow < ylowllast))) or yearlyTrendDown or not considerYearlyHighLow⋯maAlignment = f_getMaAlignment(MAType,includePartiallyAligned)-alignedMaIndex = sum(maAlignment,LookbackPeriod)+alignedMaIndex = math.sum(maAlignment,LookbackPeriod)maAlignmentDirection=f_getDirection(alignedMaIndex)highLowIndex = f_getHighLowValue(HighLowPeriod)-cumulativeHighLowIndex = sum(highLowIndex,LookbackPeriod)+cumulativeHighLowIndex = math.sum(highLowIndex,LookbackPeriod)hlDirection = f_getDirection(cumulativeHighLowIndex)[yearlyHighCondition,yearlyLowCondition] = f_getYearlyHighLowCondition(considerYearlyHighLow)-[superTrend, dir] = supertrend(supertrendMult, supertrendLength)+[superTrend, dir] = ta.supertrend(supertrendMult, supertrendLength)buyEntry = (dir == -1 and maAlignmentDirection == 1 and hlDirection == 1 and yearlyHighCondition)⋯Same 22 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 5: input() → input.string()
- Line 6: input() → input.bool()
- Line 7: input() → input.int()
- Line 8: input() → input.int()
- Line 9: input() → input.bool()
- Line 10: input() → input.int()
5 more edits
- Line 11: input() → input.int()
- Line 86: when = barColor == color.lime and inDateRange → if block
- Line 87: when = dir == 1 → if block
- Line 88: when = barColor == color.orange and inDateRange → if block
- Line 89: when = dir == -1 → if block
⋯strategy("AlignedMA and Cumulative HighLow Strategy", overlay=true, initial_capital = 1000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, pyramiding = 1, commission_value = 0.01)-MAType = input(title="Moving Average Type", defval="hma", options=["ema", "sma", "hma", "rma", "vwma", "wma"])+MAType = input.string(title="Moving Average Type", defval="hma", options=["ema", "sma", "hma", "rma", "vwma", "wma"])-includePartiallyAligned = input(true)+includePartiallyAligned = input.bool(true)-HighLowPeriod = input(22, minval=1,step=1)+HighLowPeriod = input.int(22, minval=1,step=1)-LookbackPeriod = input(10, minval=1,step=1)+LookbackPeriod = input.int(10, minval=1,step=1)-considerYearlyHighLow = input(false)+considerYearlyHighLow = input.bool(false)-supertrendMult = input(2, minval=1, maxval=10, step=0.5)+supertrendMult = input.int(2, minval=1, maxval=10, step=0.5)-supertrendLength = input(22, minval=1)+supertrendLength = input.int(22, minval=1)tradeDirection = input(title="Trade Direction", defval=strategy.direction.long, options=[strategy.direction.all, strategy.direction.long, strategy.direction.short])⋯strategy.risk.allow_entry_in(tradeDirection)-strategy.entry("Buy", strategy.long, when=barColor == color.lime and inDateRange, oca_name="oca_buy", oca_type=strategy.oca.none)+if barColor == color.lime and inDateRange+ strategy.entry("Buy", strategy.long, oca_name="oca_buy", oca_type=strategy.oca.none)-strategy.close("Buy", when=dir == 1)+if dir == 1+ strategy.close("Buy")-strategy.entry("Sell", strategy.short, when=barColor == color.orange and inDateRange, oca_name="oca_sell", oca_type=strategy.oca.none)+if barColor == color.orange and inDateRange+ strategy.entry("Sell", strategy.short, oca_name="oca_sell", oca_type=strategy.oca.none)-strategy.close("Sell", when=dir == -1)+if dir == -1+ strategy.close("Sell")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: 22 before, 34 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.
⋯// © HeWhoMustNotBeNamed-//@version=5+//@version=6strategy("AlignedMA and Cumulative HighLow Strategy", overlay=true, initial_capital = 1000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, pyramiding = 1, commission_value = 0.01)⋯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 |
|---|---|---|---|
| Moving Average Type | hma | string · options ["ema", "sma", "hma", "rma", "vwma", "wma"] | ma5 ma10 ma20 ma30 ma50 ma100 ma200 maAlignment line 18 line 20 line 22 line 24 line 26 |
| Include Partially Aligned (no label in the script) | true | bool | maAlignment line 49 |
| High Low Period (no label in the script) | 22 | int · min 1 · step 1 | currentHigh currentLow highLowIndex |
| Lookback Period (no label in the script) | 10 | int · min 1 · step 1 | alignedMaIndex cumulativeHighLowIndex |
| Consider Yearly High Low (no label in the script) | false | bool | yearlyHighCondition yearlyLowCondition |
| Supertrend Mult (no label in the script) | 2 | int · min 1 · max 10 · step 0.5 | superTrend dir |
| Supertrend Length (no label in the script) | 22 | int · min 1 | superTrend dir |
| Trade Direction | strategy.direction.long | value · options [strategy.direction.all, strategy.direction.long, strategy.direction.short] | line 85 |
| Backtest Start Time | timestamp("01 Jan 2010 00:00 +0000") | time | inDateRange |
| Backtest End Time | timestamp("01 Jan 2099 00:00 +0000") | time | inDateRange |
What it calculates
- SMA / EMA / HMA / RMA / VWMA / WMA (
sma,ta.smain v5)source, lengthma · line 17 - Supertrend (
supertrend,ta.supertrendin v5)Supertrend Mult (2), Supertrend Length (22)superTrend · line 80 - Supertrend (
supertrend,ta.supertrendin v5)Supertrend Mult (2), Supertrend Length (22)dir · line 80
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 Buy long | barColor == color.lime and inDateRange | On | 86 |
| Close Buy | dir == 1 | On | 87 |
| Open Sell short | barColor == color.orange and inDateRange | On | 88 |
| Close Sell | dir == -1 | On | 89 |
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 data2 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
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 | +32.4% | +102.5% | 22 | 41% | 1.36 | 34.1% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +10.3% | +5.5% | 17 | 35% | 1.47 | 15.3% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −14.8% | +9.8% | 20 | 30% | 0.88 | 51.5% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +26.4% | +8.4% | 15 | 33% | 2.02 | 9.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −33.8% | +16.7% | 22 | 32% | 0.73 | 63.6% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +18.0% | +21.5% | 17 | 29% | 1.48 | 10.5% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +24.8% | +148.3% | 21 | 43% | 1.28 | 34.7% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +1.6% | +17.1% | 22 | 36% | 1.06 | 9.4% | 8 May 2026 – 21 Oct 2026 |
Higher-timeframe data for request.security(): BINANCE:BTCUSDT 12M (aggregated from Binance BTCUSDT 1M), fetched from Binance's public market data for each market.
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 | 8 | 38% | $263.58 |
| Sideways market | 10 | 50% | $210.79 |
| Falling market | 4 | 25% | −$150.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
39.9%
The 95th percentile of the worst drawdown when the same 22 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 18.8% to 39.9%; the tick is what actually happened, 31.4%. Order luck alone moves it this much.
All 22 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 27 Feb 2024 54,476.50 | 17 Mar 2024 65,300.60 | 19 | $198.46 |
| long | 12 Apr 2024 70,006.20 | 14 Apr 2024 63,924.50 | 2 | −$104.33 |
| long | 16 May 2024 66,206.50 | 15 Jun 2024 66,044.00 | 30 | −$2.90 |
| long | 15 Jul 2024 60,797.90 | 3 Aug 2024 61,498.30 | 19 | $12.35 |
| long | 15 Aug 2024 58,683.40 | 2 Sep 2024 57,301.80 | 18 | −$26.20 |
| long | 15 Sep 2024 59,993.00 | 2 Oct 2024 60,804.90 | 17 | $14.36 |
| long | 15 Oct 2024 66,084.00 | 5 Nov 2024 67,850.00 | 21 | $28.95 |
| long | 7 Nov 2024 75,572.00 | 20 Dec 2024 97,461.90 | 43 | $324.32 |
| long | 18 Jan 2025 104,077.00 | 6 Feb 2025 96,612.40 | 19 | −$103.91 |
| long | 9 Mar 2025 86,222.50 | 10 Mar 2025 80,734.50 | 1 | −$85.61 |
| long | 22 Apr 2025 87,516.20 | 31 May 2025 103,985.00 | 39 | $235.97 |
| long | 16 Jun 2025 105,594.00 | 21 Jun 2025 103,298.00 | 5 | −$32.72 |
| long | 3 Jul 2025 108,850.00 | 2 Aug 2025 113,298.00 | 30 | $59.31 |
| long | 12 Aug 2025 118,686.00 | 20 Aug 2025 112,873.00 | 8 | −$74.64 |
| long | 12 Sep 2025 115,483.00 | 26 Sep 2025 108,994.00 | 14 | −$81.37 |
| long | 3 Oct 2025 120,529.00 | 11 Oct 2025 112,774.00 | 8 | −$87.89 |
| long | 27 Oct 2025 114,559.00 | 4 Nov 2025 106,583.00 | 8 | −$88.95 |
| long | 4 Dec 2025 93,429.90 | 21 Jan 2026 88,427.70 | 48 | −$63.68 |
| long | 5 Mar 2026 72,666.80 | 23 Mar 2026 67,859.00 | 18 | −$74.41 |
| long | 8 Apr 2026 71,924.20 | 19 May 2026 77,001.90 | 41 | $73.70 |
| long | 15 Jun 2026 65,746.40 | 25 Jun 2026 61,078.00 | 10 | −$79.79 |
| long | 7 Jul 2026 64,042.90 | 18 Oct 2026 81,491.00 | 103 | $283.36 |
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 |
|---|---|---|---|---|---|
| AlignedMA and Cumulative HighLow Strategy this page | — | v4 strategy | +32.4% | 22 | 34.1% |
| Ultra Moving Average Rating Trend Strategy by exlux99 | ema wma highest lowest sma rma vwma hma | v5 strategy | +2.8% | 5 | 2.4% |
| VCP pivot buy by perrycc007 | sma ema rma vwma wma lowest highest | v4 strategy | does not run in Zeal Terminal | ||
| MACD Willy Strategy by PtGambler | rma sma ema highest lowest wma | v5 strategy | No trades on this market | ||
| Multi Trend Cross Strategy Template by TradeAutomation | ema hma rma sma vwma wma highest lowest | v5 strategy | +33.0% | 3 | 7.3% |
| Multi MA Trend Following Strategy Template by TradeAutomation | ema hma rma sma vwma wma highest lowest | v5 strategy | +12.3% | 3 | 26.9% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 30 ms | With a workaround 1,000 bars in 113 ms | Not measured |
| PineTS | No v5 and v6 only | — | 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. 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/// © HeWhoMustNotBeNamed//@version=4strategy("AlignedMA and Cumulative HighLow Strategy", overlay=true, initial_capital = 1000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, pyramiding = 1, commission_value = 0.01)MAType = input(title="Moving Average Type", defval="hma", options=["ema", "sma", "hma", "rma", "vwma", "wma"])includePartiallyAligned = input(true)HighLowPeriod = input(22, minval=1,step=1)LookbackPeriod = input(10, minval=1,step=1)considerYearlyHighLow = input(false)supertrendMult = input(2, minval=1, maxval=10, step=0.5)supertrendLength = input(22, minval=1)tradeDirection = input(title="Trade Direction", defval=strategy.direction.long, options=[strategy.direction.all, strategy.direction.long, strategy.direction.short])i_startTime = input(defval = timestamp("01 Jan 2010 00:00 +0000"), title = "Backtest Start Time", type = input.time)i_endTime = input(defval = timestamp("01 Jan 2099 00:00 +0000"), title = "Backtest End Time", type = input.time)inDateRange = time >= i_startTime and time <= i_endTimef_getMovingAverage(source, MAType, length)=> ma = sma(source, length) if(MAType == "ema") ma := ema(source,length) if(MAType == "hma") ma := hma(source,length) if(MAType == "rma") ma := rma(source,length) if(MAType == "vwma") ma := vwma(source,length) if(MAType == "wma") ma := wma(source,length) ma f_getMaAlignment(MAType, includePartiallyAligned)=> ma5 = f_getMovingAverage(close,MAType,5) ma10 = f_getMovingAverage(close,MAType,10) ma20 = f_getMovingAverage(close,MAType,20) ma30 = f_getMovingAverage(close,MAType,30) ma50 = f_getMovingAverage(close,MAType,50) ma100 = f_getMovingAverage(close,MAType,100) ma200 = f_getMovingAverage(close,MAType,200) upwardScore = 0 upwardScore := close > ma5? upwardScore+1:upwardScore upwardScore := ma5 > ma10? upwardScore+1:upwardScore upwardScore := ma10 > ma20? upwardScore+1:upwardScore upwardScore := ma20 > ma30? upwardScore+1:upwardScore upwardScore := ma30 > ma50? upwardScore+1:upwardScore upwardScore := ma50 > ma100? upwardScore+1:upwardScore upwardScore := ma100 > ma200? upwardScore+1:upwardScore upwards = close > ma5 and ma5 > ma10 and ma10 > ma20 and ma20 > ma30 and ma30 > ma50 and ma50 > ma100 and ma100 > ma200 downwards = close < ma5 and ma5 < ma10 and ma10 < ma20 and ma20 < ma30 and ma30 < ma50 and ma50 < ma100 and ma100 < ma200 upwards?1:downwards?-1:includePartiallyAligned ? (upwardScore > 5? 0.5: upwardScore < 2?-0.5:upwardScore>3?0.25:-0.25) : 0f_getHighLowValue(HighLowPeriod)=> currentHigh = highest(high,HighLowPeriod) == high currentLow = lowest(low,HighLowPeriod) == low currentHigh?1:currentLow?-1:0f_getDirection(Series)=> direction = Series > Series[1] ? 1 : Series < Series[1] ? -1 : 0 direction := direction == 0? nz(direction[1],0):direction directionf_getYearlyHighLowCondition(considerYearlyHighLow)=> yhigh = security(syminfo.tickerid, '12M', high[1]) ylow = security(syminfo.tickerid, '12M', low[1]) yhighlast = yhigh[365] ylowlast = ylow[365] yhighllast = yhigh[2 * 365] ylowllast = ylow[2 * 365] yearlyTrendUp = na(yhigh)? true : na(yhighlast)? close > yhigh : na(yhighllast)? close > max(yhigh,yhighlast) : close > max(yhigh, min(yhighlast, yhighllast)) yearlyHighCondition = ( (na(yhigh) or na(yhighlast) ? true : (yhigh > yhighlast) ) and ( na(yhigh) or na(yhighllast) ? true : (yhigh > yhighllast))) or yearlyTrendUp or not considerYearlyHighLow yearlyTrendDown = na(ylow)? true : na(ylowlast)? close < ylow : na(ylowllast)? close < min(ylow,ylowlast) : close < min(ylow, max(ylowlast, ylowllast)) yearlyLowCondition = ( (na(ylow) or na(ylowlast) ? true : (ylow < ylowlast) ) and ( na(ylow) or na(ylowllast) ? true : (ylow < ylowllast))) or yearlyTrendDown or not considerYearlyHighLow label_x = time+(60*60*24*1000*1) [yearlyHighCondition,yearlyLowCondition]maAlignment = f_getMaAlignment(MAType,includePartiallyAligned)alignedMaIndex = sum(maAlignment,LookbackPeriod)maAlignmentDirection=f_getDirection(alignedMaIndex)highLowIndex = f_getHighLowValue(HighLowPeriod)cumulativeHighLowIndex = sum(highLowIndex,LookbackPeriod)hlDirection = f_getDirection(cumulativeHighLowIndex)[yearlyHighCondition,yearlyLowCondition] = f_getYearlyHighLowCondition(considerYearlyHighLow)[superTrend, dir] = supertrend(supertrendMult, supertrendLength)buyEntry = (dir == -1 and maAlignmentDirection == 1 and hlDirection == 1 and yearlyHighCondition)sellEntry = (dir == 1 and maAlignmentDirection == -1 and hlDirection == -1 and yearlyLowCondition)barColor = buyEntry?color.lime:sellEntry?color.orange:color.graybarcolor(barColor)strategy.risk.allow_entry_in(tradeDirection)strategy.entry("Buy", strategy.long, when=barColor == color.lime and inDateRange, oca_name="oca_buy", oca_type=strategy.oca.none)strategy.close("Buy", when=dir == 1)strategy.entry("Sell", strategy.short, when=barColor == color.orange and inDateRange, oca_name="oca_sell", oca_type=strategy.oca.none)strategy.close("Sell", when=dir == -1)