Here I have combined a script to trade the moon phases (that I found - credit to Pascal Simon) with a moving average cross strategy.
I have set the commission to 0.2% and get a fairly good profit factor (up to 7 on some 6 month periods) on different sets of data. An order size of about 70% and trailing stop losses for both long and short trades of about 10.5% seems to be the optimum.
I also used a variable offset to the moon phase days, offsetting this by about 6 days seems to give better results.
The moving averages I chose were 6 and 9 simple daily
The downside is that there are only a couple of trades a month max.
The code and screenshot are below:
Cheers,
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// ---------------------------© paaax----------------------------
// --------------------------------------------------------------
// ---------------- Author: Pascal Simon (paaax) ----------------
// --------------------------------------------------------------
// -------------------- www.pascal-simon.de ---------------------
// --------------------------------------------------------------
// ---------------- www.tradingview.com/u/paaax/-----------------
// --------------------------------------------------------------
// Source: https://gist.github.com/L-A/3497902#file-moonobject-js
// --------------------------------------------------------------
//@version=4
strategy(“My Strategy”, overlay=true, margin_long=100, margin_short=100)
//Test time range
fromDay = input(defval=1, title=“From day”, minval=1)
fromMonth = input(defval=1, title=“From month”, minval=1)
fromYear = input(defval=2018, title=“From year”, minval=2008)
toDay = input(defval=1, title=“To day”, minval=1)
toMonth = input(defval=1, title=“To month”, minval=1)
toYear = input(defval=2018, title=“To year”, minval=2008)
// Moon day offset
dayOffset = input(defval=0, title=“Moon day offset”)
shortSMA = input(defval=14, title=“shortSMA”)
longSMA = input(defval=28, title=“longSMA”)
// Configure trail stop level with input options
longTrailPerc = input(defval=3, title=“Trail Long Loss (%)”, type=input.float, minval=0.0, step=0.1) * 0.01
shortTrailPerc = input(defval=3, title=“Trail Short Loss (%)”, type=input.float, minval=0.0, step=0.1) * 0.01
fillBackground = input(true, “Fill Background?”)
// Moon Phase FUNCTION — {
normalize(_v) =>
x = _v
x := x - floor(x)
if x < 0
x := x + 1
x
calcPhase(_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 + dayOffset)
moon = floor(age)[1] > floor(age) ? 1 : floor(age)[1] < 15 and floor(age) >= 15 ? -1 : na
//} — INIT
//Logic
// Determine long trail stop loss price
longStopPrice = 0.0
longStopPrice := if (strategy.position_size > 0)
stopValue = close * (1 - longTrailPerc)
max(stopValue, longStopPrice[1])
else
0
// Determine trailing short price
shortStopPrice = 0.0
shortStopPrice := if (strategy.position_size < 0)
stopValue = close * (1 + shortTrailPerc)
min(stopValue, shortStopPrice[1])
else
999999
// Timerange
timeInRange = (time > timestamp(fromYear, fromMonth, fromDay, 00, 00)) and (time < timestamp(toYear, toMonth, toDay, 23, 59))
// Submit entry orders if the moon phase is favourable, the time is within the time range and the trend as defined by two SMAs is favourable
longCondition = (moon<=0) and (timeInRange) and (sma(close, shortSMA) > sma(close, longSMA))
if (longCondition)
strategy.entry(“My Long Entry Id”, strategy.long)
shortCondition = (moon>=0) and (timeInRange) and (sma(close, shortSMA) < sma(close, longSMA))
if (shortCondition)
strategy.entry(“My Short Entry Id”, strategy.short)
// Submit exit orders for trail stop loss price
if (strategy.position_size > 0)
strategy.exit(id=“XL TRL STP”, stop=longStopPrice)
if (strategy.position_size < 0)
strategy.exit(id=“XS TRL STP”, stop=shortStopPrice)