Skip to content

Instantly share code, notes, and snippets.

@sherwind
Last active April 21, 2018 11:18
Show Gist options
  • Save sherwind/e65c4cd766c3fbe6aa164c4b45b907ac to your computer and use it in GitHub Desktop.
Save sherwind/e65c4cd766c3fbe6aa164c4b45b907ac to your computer and use it in GitHub Desktop.
USDJPY Assumption v1
//@version=3
//
// Based on the "logical trading" post of Charles Cornley (thanks!).
//
// Indicator States:
//
// Very Bullish (Lime) = USD trend rising and JPY trend falling and Gold trend falling and US 10Y Bond trend falling and
// Dow Jones trend rising and Nasdaq trend rising and Russell 2000 trend rising and
// S&P 500 trend rising and Nikkei 225 trend rising
//
// Very Bearish (Red) = USD trend falling and JPY trend rising and Gold trend rising and US 10Y Bond trend rising and
// Dow Jones trend falling and Nasdaq trend falling and Russell 2000 trend falling and
// S&P 500 trend falling and Nikkei 225 trend falling
//
// Bullish (Green) = USD trend rising and JPY trend falling
// Bearish (Red) = USD trend falling and JPY trend rising
//
//
// -----------------------------------------------------------------------------
// Copyright 2018 sherwind
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// The GNU General Public License can be found here
// <http://www.gnu.org/licenses/>.
//
// -----------------------------------------------------------------------------
//
study("USDJPY Assumption v1")
trend_src = input(close, title="Trend Source", type=source)
trend_filter_type = input("EMA", title="Trend MA Type: ", options=["SMA", "EMA", "LowPass", "Hull MA", "Zero-Lag MA", "ALMA", "Smooth"], type=string)
trend_filter_len = input(10, title="Trend MA Period", minval=1)
is_rising(src) => rising(src, 1)
is_falling(src) => falling(src, 1)
// A second order lowpass filter by Johann C. Lotter
lowpass(src, len) =>
a = 2.0 / (1 + len)
a2 = a * a
lp = 0.0
lp := (a - 0.25 * a2) * src + 0.5 * a2 * src[1] - (a - 0.75 * a2) * src[2] + 2 * (1.0 - a) * nz(lp[1]) - (1.0 - a) * (1.0 - a) * nz(lp[2])
lp
// John Ehlers' "Super Smoother", a 2-pole Butterworth filter combined with a 2-bar SMA that suppresses the Nyquist frequency
smooth(src, len) =>
f = (1.414 * 3.14159) / len
a = exp(-f)
c2 = 2 * a * cos(f)
c3 = (-a) * a
c1 = 1 - c2 - c3
s = 0.0
s := c1 * (src + nz(src[1])) * 0.5 + c2 * nz(s[1]) + c3 * nz(s[2])
s
// Ehlers' Zero-Lag Moving Average, an EMA with a correction term for removing lag
zma(src, len) =>
zma = 0.0
a = 2.0 / (1 + len)
//ema_ = ema(src, len)
ema_ = 0.0
ema_ := a * src + (1 - a) * nz(ema_[1])
least_error = 1000000.0
gain_limit = 5.0
best_gain = 0.0
for gain = -gain_limit to gain_limit - 0.1 by 0.1
zma := a * (ema_ + gain * (src - nz(zma[1]))) + (1 - a) * nz(zma[1])
error = src - zma
if (abs(error) < least_error)
least_error := abs(error)
best_gain := gain
zma := a * (ema_ + best_gain * (src - nz(zma[1]))) + (1 - a) * nz(zma[1])
zma
filter(type, src, len) =>
type == "EMA" ? ema(src, len) :
type == "LowPass" ? lowpass(src, len) :
type == "Hull MA" ? wma(2 * wma(src, ceil(len / 2)) - wma(src, len), ceil(sqrt(len))) :
type == "Zero-Lag MA" ? zma(src, len) :
type == "ALMA" ? alma(src, len, 0.85, 6) :
type == "Smooth" ? smooth(src, len) :
sma(src, len)
eur = 1/security('OANDA:EURJPY', period, trend_src)
gbp = 1/security('OANDA:GBPJPY', period, trend_src)
aud = 1/security('OANDA:AUDJPY', period, trend_src)
usd = 1/security('OANDA:USDJPY', period, trend_src)
chf = 1/security('OANDA:CHFJPY', period, trend_src)
nzd = 1/security('OANDA:NZDJPY', period, trend_src)
cad = 1/security('OANDA:CADJPY', period, trend_src)
e = 0.142857142857143
jpy_trend = filter(trend_filter_type, pow(eur, e) * pow(gbp, e) * pow(aud, e) * pow(usd, e) * pow(chf, e) * pow(nzd, e) * pow(cad, e), trend_filter_len)
usd_trend = filter(trend_filter_type, security('TVC:DXY', period, trend_src), trend_filter_len)
gold_trend = filter(trend_filter_type, security('TVC:GOLD', period, trend_src), trend_filter_len)
dowjones_trend = filter(trend_filter_type, security('DJ:DJI', period, trend_src), trend_filter_len)
sp500_trend = filter(trend_filter_type, security('SP:SPX', period, trend_src), trend_filter_len)
nasdaq_trend = filter(trend_filter_type, security('TVC:NDX', period, trend_src), trend_filter_len)
russell2000_trend = filter(trend_filter_type, security('TVC:RUT', period, trend_src), trend_filter_len)
nikkei225_trend = filter(trend_filter_type, security('TVC:NI225', period, trend_src), trend_filter_len)
us10ybond_trend = filter(trend_filter_type, security('TVC:US10', period, trend_src), trend_filter_len)
is_very_bullish = is_rising(usd_trend) and
is_falling(us10ybond_trend) and
is_falling(gold_trend) and
is_falling(jpy_trend) and
is_rising(dowjones_trend) and
is_rising(nasdaq_trend) and
is_rising(russell2000_trend) and
is_rising(sp500_trend) and
is_rising(nikkei225_trend)
is_very_bearish = is_falling(usd_trend) and
is_rising(us10ybond_trend) and
is_rising(gold_trend) and
is_rising(jpy_trend) and
is_falling(dowjones_trend) and
is_falling(nasdaq_trend) and
is_falling(russell2000_trend) and
is_falling(sp500_trend) and
is_falling(nikkei225_trend)
is_bullish = is_rising(usd_trend) and is_falling(jpy_trend)
is_bearish = is_falling(usd_trend) and is_rising(jpy_trend)
usdjpy_assumption = is_very_bullish ? lime : (is_bullish ? green : (is_very_bearish ? red : (is_bearish ? maroon : gray)))
plot(1, color=usdjpy_assumption, style=columns)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment