Pine Script v4 · Strategy
[astropark] Moon Phases [strategy]
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 wait for a signal; there is no stop loss. Each trade commits 100% of equity and pays 0.1 commission. 14 settings; defaults include New Moon Color color.black, Full Moon Color color.white and New Moon Background Color #fffff0aa.
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 [astropark] Moon Phases [strategy] repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 19 of 19 trades.
- How did [astropark] Moon Phases [strategy] do on BTCUSDT 1D with default settings?
- −87.05%
- From 26 Jan 2024 to 21 Oct 2026, over 66 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can [astropark] Moon Phases [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 [astropark] Moon Phases [strategy] use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does [astropark] Moon Phases [strategy] run in Zeal Terminal?
- Yes
- It compiles in 30 ms and runs 1,000 bars in 65 ms, in the browser.
BTCUSDT · 1D · default settings
- Full Moon
- New Moon
- Buy Label
- Sell Label
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −87.05%
- −$87,052.50
- Buy and hold
- +102.5%
- same period
- Closed trades
- 66
- 22 won · 44 lost
- Profitable
- 33.3%
- of closed trades
- Profit factor
- 0.45
- gross profit ÷ gross loss
- Max drawdown
- 90.15%
- −$113,985.00
One run with the author's default settings, $100,000 starting capital, commission as the script sets it. A short 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
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
1 setting has no label
In the settings dialog strategy (line 73) 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.
-
Note
Uses an argument v6 removed
when = on line 93, 94, 95, 96. The upgraded version below moves each call inside an if block.
-
Note
Uses transp =, which v5 removed line 70
transp = on line 70, 91, 92. v5 folds the transparency into the colour with color.new(c, transp).
Upgrade and export
Written in Pine Script v4. The upgrade to v5 takes 1 step, one per version, each applied to the result of the one before. A last clean-v5 pass then types the settings and moves when = into if blocks, which v6 requires. Every step runs before and after on the same candles in Zeal Terminal's engine; a step whose results differ is shown but not offered as a download. Upgrades preserve behaviour: bugs are fixed in the Review, never silently here.
- Upgrading to v5 renames floor → math.floor.
- 4 strategy calls use the when = argument (line 93, 94, 95, 96). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 66 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 2: //@version=4 → //@version=5
- Line 14: floor → math.floor
- Line 26: floor → math.floor
- Line 31: floor → math.floor
- Line 32: floor → math.floor
- Line 33: floor → math.floor
8 more edits
- Line 33: floor → math.floor
- Line 45: floor → math.floor
- Line 45: floor → math.floor
- Line 46: floor → math.floor
- Line 46: floor → math.floor
- Line 70: transp = 10 → color.new(col, 10)
- Line 91: transp = 0 → color.new(color.green, 0)
- Line 92: transp = 0 → color.new(color.red, 0)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/-// @version=4+// @version=5strategy(title="[astropark] Moon Phases [strategy]", overlay=true, pyramiding = 10, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, initial_capital = 100000, currency = currency.USD, commission_value = 0.1)⋯ x = _v- x := x - floor(x)+ x := x - math.floor(x) if x < 0⋯ float ip = na- y := _year - floor((12 - _month) / 10) + y := _year - math.floor((12 - _month) / 10) m := _month + 9⋯ - k1 := floor(365.25 * (y + 4712))+ k1 := math.floor(365.25 * (y + 4712))- k2 := floor(30.6 * m + 0.5)+ k2 := math.floor(30.6 * m + 0.5)- k3 := floor(floor((y / 100) + 49) * 0.75) - 38+ k3 := math.floor(math.floor((y / 100) + 49) * 0.75) - 38 ⋯moon = - floor(age)[1] > floor(age) ? 1 : + math.floor(age)[1] > math.floor(age) ? 1 : - floor(age)[1] < 15 and floor(age) >= 15 ? -1 : na+ math.floor(age)[1] < 15 and math.floor(age) >= 15 ? -1 : na//} --- INIT⋯ col := newMoonBackgroundColor-bgcolor(col, title="Moon Phase", transp=10)+bgcolor(color.new(col, 10), title="Moon Phase")//} --- PLOT⋯analyzeFromTimestamp = timestamp(analysisStartYear, analysisStartMonth, analysisStartDay, analysisStartHour, analysisStartMinute)-plotshape(showBuySellSignals and buySignal, title="Buy Label", text="Buy", location=location.belowbar, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)+plotshape(showBuySellSignals and buySignal, title="Buy Label", text="Buy", location=location.belowbar, style=shape.labelup, size=size.tiny, color=color.new(color.green, 0), textcolor=color.white)-plotshape(showBuySellSignals and sellSignal, title="Sell Label", text="Sell", location=location.abovebar, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)+plotshape(showBuySellSignals and sellSignal, title="Sell Label", text="Sell", location=location.abovebar, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 0), textcolor=color.white)strategy.entry("long", strategy.long, when = time > analyzeFromTimestamp and buySignal and longEnabled)⋯Same 66 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 5: input() → input.color()
- Line 6: input() → input.color()
- Line 7: input() → input.bool()
- Line 8: input() → input.color()
- Line 9: input() → input.color()
- Line 73: input() → input.string()
12 more edits
- Line 82: input() → input.bool()
- Line 83: input() → input.bool()
- Line 84: input() → input.bool()
- Line 85: input() → input.int()
- Line 86: input() → input.int()
- Line 87: input() → input.int()
- Line 88: input() → input.int()
- Line 89: input() → input.int()
- Line 93: when = time > analyzeFromTimestamp and buySignal and longEnabled → if block
- Line 94: when = time > analyzeFromTimestamp and sellSignal and shortEnabled → if block
- Line 95: when = sellSignal → if block
- Line 96: when = buySignal → if block
⋯// INPUT --- {-newMoonColor = input(color.black, "New Moon Color")+newMoonColor = input.color(color.black, "New Moon Color")-fullMoonColor = input(color.white, "Full Moon Color")+fullMoonColor = input.color(color.white, "Full Moon Color")-fillBackground = input(true, "Fill Background?")+fillBackground = input.bool(true, "Fill Background?")-newMoonBackgroundColor = input(#fffff0aa, "New Moon Background Color")+newMoonBackgroundColor = input.color(#fffff0aa, "New Moon Background Color")-fullMoonBackgroundColor = input(#aaaaaaaa, "Full Moon Background Color")+fullMoonBackgroundColor = input.color(#aaaaaaaa, "Full Moon Background Color")//} --- INPUT⋯// STRATEGY --- {-strategy = input("buy on new moon, sell on full moon", options=["buy on new moon, sell on full moon","sell on new moon, buy on full moon"])+strategy = input.string("buy on new moon, sell on full moon", options=["buy on new moon, sell on full moon","sell on new moon, buy on full moon"])longCond = strategy == "buy on new moon, sell on full moon" ? moon == -1 : moon == 1⋯sellSignal = shortCond and weAreInShortTrade[1] == false-showBuySellSignals = input(defval=true, title = "Show Buy/Sell Signals")+showBuySellSignals = input.bool(defval=true, title = "Show Buy/Sell Signals")-longEnabled = input(true, title="Long enabled")+longEnabled = input.bool(true, title="Long enabled")-shortEnabled = input(true, title="Short enabled")+shortEnabled = input.bool(true, title="Short enabled")-analysisStartYear = input(2017, "Backtesting From Year", minval=1980)+analysisStartYear = input.int(2017, "Backtesting From Year", minval=1980)-analysisStartMonth = input(1, "And Month", minval=1, maxval=12)+analysisStartMonth = input.int(1, "And Month", minval=1, maxval=12)-analysisStartDay = input(1, "And Day", minval=1, maxval=31)+analysisStartDay = input.int(1, "And Day", minval=1, maxval=31)-analysisStartHour = input(0, "And Hour", minval=0, maxval=23)+analysisStartHour = input.int(0, "And Hour", minval=0, maxval=23)-analysisStartMinute = input(0, "And Minute", minval=0, maxval=59)+analysisStartMinute = input.int(0, "And Minute", minval=0, maxval=59)analyzeFromTimestamp = timestamp(analysisStartYear, analysisStartMonth, analysisStartDay, analysisStartHour, analysisStartMinute)⋯plotshape(showBuySellSignals and sellSignal, title="Sell Label", text="Sell", location=location.abovebar, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 0), textcolor=color.white)-strategy.entry("long", strategy.long, when = time > analyzeFromTimestamp and buySignal and longEnabled)+if time > analyzeFromTimestamp and buySignal and longEnabled+ strategy.entry("long", strategy.long)-strategy.entry("short", strategy.short, when = time > analyzeFromTimestamp and sellSignal and shortEnabled)+if time > analyzeFromTimestamp and sellSignal and shortEnabled+ strategy.entry("short", strategy.short)-strategy.close("long", when = sellSignal)+if sellSignal+ strategy.close("long")-strategy.close("short", when = buySignal)+if buySignal+ strategy.close("short")//} --- STRATEGYDoes 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: 66 before, 89 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.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/-// @version=5+// @version=6strategy(title="[astropark] Moon Phases [strategy]", overlay=true, pyramiding = 10, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, initial_capital = 100000, currency = currency.USD, commission_value = 0.1)⋯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 |
|---|---|---|---|
| New Moon Color | color.black | value | plotshape |
| Full Moon Color | color.white | value | plotshape |
| Fill Background? | true | bool | line 66 line 68 |
| New Moon Background Color | #fffff0aa | value | col |
| Full Moon Background Color | #aaaaaaaa | value | col |
| Strategy (no label in the script) | buy on new moon, sell on full moon | string · options ["buy on new moon, sell on full moon","sell on new moon, buy on full moon"] | longCond shortCond |
| Show Buy/Sell Signals | true | bool | Buy Label Sell Label |
| Long enabled | true | bool | entry long |
| Short enabled | true | bool | entry short |
| Backtesting From Year | 2017 | int · min 1980 | analyzeFromTimestamp |
| And Month | 1 | int · min 1 · max 12 | analyzeFromTimestamp |
| And Day | 1 | int · min 1 · max 31 | analyzeFromTimestamp |
| And Hour | 0 | int · min 0 · max 23 | analyzeFromTimestamp |
| And Minute | 0 | int · min 0 · max 59 | analyzeFromTimestamp |
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 long long | time > analyzeFromTimestamp and buySignal and longEnabled | On | 93 |
| Open short short | time > analyzeFromTimestamp and sellSignal and shortEnabled | On | 94 |
| Close long | sellSignalshortCond and weAreInShortTrade[1] == false | On | 95 |
| Close short | buySignallongCond and weAreInLongTrade[1] == false | On | 96 |
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 | −87.1% | +102.5% | 66 | 33% | 0.45 | 90.2% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −29.3% | +5.5% | 10 | 40% | 0.45 | 38.7% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −94.2% | +9.8% | 66 | 36% | 0.40 | 94.9% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −34.0% | +8.4% | 10 | 40% | 0.49 | 45.5% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −93.5% | +16.7% | 66 | 47% | 0.54 | 95.8% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −59.6% | +21.5% | 10 | 40% | 0.21 | 62.8% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −88.0% | +148.3% | 66 | 35% | 0.46 | 89.8% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −21.0% | +17.1% | 10 | 50% | 0.51 | 27.3% | 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 | 23 | 35% | −$15,799.30 |
| Sideways market | 27 | 41% | −$39,946.10 |
| Falling market | 16 | 19% | −$31,294.10 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
92.0%
The 95th percentile of the worst drawdown when the same 66 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 87.0% to 92.0%; the tick is what actually happened, 89.7%. Order luck alone moves it this much.
All 66 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 11 Feb 2024 47,751.10 | 26 Feb 2024 51,728.80 | 15 | −$8,530.02 |
| long | 26 Feb 2024 51,728.80 | 11 Mar 2024 68,955.90 | 14 | $30,254.00 |
| short | 11 Mar 2024 68,955.90 | 26 Mar 2024 69,880.00 | 15 | −$1,876.38 |
| long | 26 Mar 2024 69,880.00 | 10 Apr 2024 69,146.00 | 15 | −$1,497.33 |
| short | 10 Apr 2024 69,146.00 | 25 Apr 2024 64,289.60 | 15 | $8,083.87 |
| long | 25 Apr 2024 64,289.60 | 9 May 2024 61,193.00 | 14 | −$6,335.74 |
| short | 9 May 2024 61,193.00 | 24 May 2024 67,969.70 | 15 | −$13,553.40 |
| long | 24 May 2024 67,969.70 | 8 Jun 2024 69,355.60 | 15 | $1,957.74 |
| short | 8 Jun 2024 69,355.60 | 23 Jun 2024 64,262.00 | 15 | $7,759.60 |
| long | 23 Jun 2024 64,262.00 | 7 Jul 2024 58,230.10 | 14 | −$11,132.90 |
| short | 7 Jul 2024 58,230.10 | 22 Jul 2024 68,165.40 | 15 | −$18,165.40 |
| long | 22 Jul 2024 68,165.40 | 6 Aug 2024 54,018.80 | 15 | −$18,211.30 |
| short | 6 Aug 2024 54,018.80 | 21 Aug 2024 59,013.80 | 15 | −$6,501.26 |
| long | 21 Aug 2024 59,013.80 | 4 Sep 2024 57,487.70 | 14 | −$1,733.03 |
| short | 4 Sep 2024 57,487.70 | 19 Sep 2024 61,760.00 | 15 | −$4,623.01 |
| long | 19 Sep 2024 61,760.00 | 4 Oct 2024 60,752.70 | 15 | −$1,022.66 |
| short | 4 Oct 2024 60,752.70 | 19 Oct 2024 68,428.00 | 15 | −$7,049.10 |
| long | 19 Oct 2024 68,428.00 | 2 Nov 2024 69,496.00 | 14 | $650.22 |
| short | 2 Nov 2024 69,496.00 | 17 Nov 2024 90,588.00 | 15 | −$14,823.50 |
| long | 17 Nov 2024 90,588.00 | 2 Dec 2024 97,185.20 | 15 | $2,383.05 |
| short | 2 Dec 2024 97,185.20 | 17 Dec 2024 106,059.00 | 15 | −$3,365.39 |
| long | 17 Dec 2024 106,059.00 | 1 Jan 2025 93,576.00 | 15 | −$3,907.17 |
| short | 1 Jan 2025 93,576.00 | 16 Jan 2025 100,497.00 | 15 | −$2,186.95 |
| long | 16 Jan 2025 100,497.00 | 30 Jan 2025 103,733.00 | 14 | $801.78 |
| short | 30 Jan 2025 103,733.00 | 14 Feb 2025 96,608.10 | 15 | $1,827.48 |
| long | 14 Feb 2025 96,608.10 | 1 Mar 2025 84,349.90 | 15 | −$3,759.68 |
| short | 1 Mar 2025 84,349.90 | 16 Mar 2025 84,338.40 | 15 | −$47.41 |
| long | 16 Mar 2025 84,338.40 | 30 Mar 2025 82,648.50 | 14 | −$559.15 |
| short | 30 Mar 2025 82,648.50 | 14 Apr 2025 83,760.00 | 15 | −$384.02 |
| long | 14 Apr 2025 83,760.00 | 29 Apr 2025 95,011.20 | 15 | $3,232.57 |
| short | 29 Apr 2025 95,011.20 | 14 May 2025 104,104.00 | 15 | −$2,707.52 |
| long | 14 May 2025 104,104.00 | 28 May 2025 108,938.00 | 14 | $1,109.07 |
| short | 28 May 2025 108,938.00 | 12 Jun 2025 108,645.00 | 15 | $18.07 |
| long | 12 Jun 2025 108,645.00 | 27 Jun 2025 106,947.00 | 15 | −$459.81 |
| short | 27 Jun 2025 106,947.00 | 12 Jul 2025 117,528.00 | 15 | −$2,591.00 |
| long | 12 Jul 2025 117,528.00 | 26 Jul 2025 117,614.00 | 14 | −$29.13 |
| short | 26 Jul 2025 117,614.00 | 10 Aug 2025 116,462.00 | 15 | $179.71 |
| long | 10 Aug 2025 116,462.00 | 25 Aug 2025 113,494.00 | 15 | −$637.31 |
| short | 25 Aug 2025 113,494.00 | 9 Sep 2025 112,065.00 | 15 | $239.17 |
| long | 9 Sep 2025 112,065.00 | 23 Sep 2025 112,651.00 | 14 | $73.48 |
| short | 23 Sep 2025 112,651.00 | 8 Oct 2025 121,333.00 | 15 | −$1,810.90 |
| long | 8 Oct 2025 121,333.00 | 23 Oct 2025 107,567.00 | 15 | −$2,430.52 |
| short | 23 Oct 2025 107,567.00 | 7 Nov 2025 101,346.00 | 15 | $1,041.82 |
| long | 7 Nov 2025 101,346.00 | 21 Nov 2025 86,637.20 | 14 | −$2,892.53 |
| short | 21 Nov 2025 86,637.20 | 6 Dec 2025 89,330.00 | 15 | −$555.90 |
| long | 6 Dec 2025 89,330.00 | 21 Dec 2025 88,360.90 | 15 | −$208.40 |
| short | 21 Dec 2025 88,360.90 | 5 Jan 2026 91,529.70 | 15 | −$607.27 |
| long | 5 Jan 2026 91,529.70 | 19 Jan 2026 93,673.10 | 14 | $329.85 |
| short | 19 Jan 2026 93,673.10 | 3 Feb 2026 78,738.60 | 15 | $2,481.50 |
| long | 3 Feb 2026 78,738.60 | 18 Feb 2026 67,503.50 | 15 | −$2,634.03 |
| short | 18 Feb 2026 67,503.50 | 5 Mar 2026 72,666.80 | 15 | −$1,225.13 |
| long | 5 Mar 2026 72,666.80 | 20 Mar 2026 69,930.00 | 15 | −$569.44 |
| short | 20 Mar 2026 69,930.00 | 4 Apr 2026 66,964.30 | 15 | $558.21 |
| long | 4 Apr 2026 66,964.30 | 18 Apr 2026 77,072.00 | 14 | $2,136.09 |
| short | 18 Apr 2026 77,072.00 | 3 May 2026 78,686.80 | 15 | −$378.91 |
| long | 3 May 2026 78,686.80 | 18 May 2026 77,457.70 | 15 | −$283.72 |
| short | 18 May 2026 77,457.70 | 2 Jun 2026 71,408.90 | 15 | $1,205.83 |
| long | 2 Jun 2026 71,408.90 | 16 Jun 2026 66,328.70 | 14 | −$1,244.71 |
| short | 16 Jun 2026 66,328.70 | 1 Jul 2026 58,624.70 | 15 | $1,804.49 |
| long | 1 Jul 2026 58,624.70 | 16 Jul 2026 64,756.30 | 15 | $1,803.02 |
| short | 16 Jul 2026 64,756.30 | 31 Jul 2026 64,780.00 | 15 | −$45.92 |
| long | 31 Jul 2026 64,780.00 | 14 Aug 2026 63,490.90 | 14 | −$423.47 |
| short | 14 Aug 2026 63,490.90 | 29 Aug 2026 77,845.90 | 15 | −$4,322.17 |
| long | 29 Aug 2026 77,845.90 | 13 Sep 2026 77,278.70 | 15 | −$135.62 |
| short | 13 Sep 2026 77,278.70 | 28 Sep 2026 84,472.00 | 15 | −$1,377.41 |
| long | 28 Sep 2026 84,472.00 | 12 Oct 2026 83,773.10 | 14 | −$134.45 |
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 30 ms | Yes 1,000 bars in 65 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/// @version=4strategy(title="[astropark] Moon Phases [strategy]", overlay=true, pyramiding = 10, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, initial_capital = 100000, currency = currency.USD, commission_value = 0.1)// INPUT --- {newMoonColor = input(color.black, "New Moon Color")fullMoonColor = input(color.white, "Full Moon Color")fillBackground = input(true, "Fill Background?")newMoonBackgroundColor = input(#fffff0aa, "New Moon Background Color")fullMoonBackgroundColor = input(#aaaaaaaa, "Full Moon Background Color")//} --- INPUT// FUNCTION --- {normalize(_v) => x = _v x := x - floor(x) if x < 0 x := x + 1 xcalcPhase(_year, _month, _day) => int y = na int m = na float k1 = na float k2 = na float k3 = na float jd = na float ip = na y := _year - floor((12 - _month) / 10) m := _month + 9 if m >= 12 m := m - 12 k1 := floor(365.25 * (y + 4712)) k2 := floor(30.6 * m + 0.5) k3 := floor(floor((y / 100) + 49) * 0.75) - 38 jd := k1 + k2 + _day + 59 if jd > 2299160 jd := jd - k3 ip := normalize((jd - 2451550.1) / 29.530588853) age = ip * 29.53//} --- FUNCTION// INIT --- {age = calcPhase(year, month, dayofmonth)moon = floor(age)[1] > floor(age) ? 1 : floor(age)[1] < 15 and floor(age) >= 15 ? -1 : na//} --- INIT// PLOT --- {plotshape( moon==1, "Full Moon", shape.circle, location.top, color.new(newMoonColor, 20), size=size.normal ) plotshape( moon==-1, "New Moon", shape.circle, location.bottom, color.new(fullMoonColor, 20), size=size.normal ) var color col = naif moon == 1 and fillBackground col := fullMoonBackgroundColorif moon == -1 and fillBackground col := newMoonBackgroundColorbgcolor(col, title="Moon Phase", transp=10)//} --- PLOT// STRATEGY --- {strategy = input("buy on new moon, sell on full moon", options=["buy on new moon, sell on full moon","sell on new moon, buy on full moon"])longCond = strategy == "buy on new moon, sell on full moon" ? moon == -1 : moon == 1shortCond = strategy == "buy on new moon, sell on full moon" ? moon == 1 : moon == -1weAreInLongTrade = falseweAreInShortTrade = falseweAreInLongTrade := (longCond or weAreInLongTrade[1]) and shortCond == falseweAreInShortTrade := (shortCond or weAreInShortTrade[1]) and longCond == falsebuySignal = longCond and weAreInLongTrade[1] == falsesellSignal = shortCond and weAreInShortTrade[1] == falseshowBuySellSignals = input(defval=true, title = "Show Buy/Sell Signals")longEnabled = input(true, title="Long enabled")shortEnabled = input(true, title="Short enabled")analysisStartYear = input(2017, "Backtesting From Year", minval=1980)analysisStartMonth = input(1, "And Month", minval=1, maxval=12)analysisStartDay = input(1, "And Day", minval=1, maxval=31)analysisStartHour = input(0, "And Hour", minval=0, maxval=23)analysisStartMinute = input(0, "And Minute", minval=0, maxval=59)analyzeFromTimestamp = timestamp(analysisStartYear, analysisStartMonth, analysisStartDay, analysisStartHour, analysisStartMinute)plotshape(showBuySellSignals and buySignal, title="Buy Label", text="Buy", location=location.belowbar, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)plotshape(showBuySellSignals and sellSignal, title="Sell Label", text="Sell", location=location.abovebar, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)strategy.entry("long", strategy.long, when = time > analyzeFromTimestamp and buySignal and longEnabled)strategy.entry("short", strategy.short, when = time > analyzeFromTimestamp and sellSignal and shortEnabled)strategy.close("long", when = sellSignal)strategy.close("short", when = buySignal)//} --- STRATEGY