Skip to content

Instantly share code, notes, and snippets.

@sooch
Created August 16, 2020 11:40
Show Gist options
  • Save sooch/c360ea293852fc65c990f584c3b01b91 to your computer and use it in GitHub Desktop.
Save sooch/c360ea293852fc65c990f584c3b01b91 to your computer and use it in GitHub Desktop.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sooch
//
// TradingViewのMACDストラテジーに期間指定パラメータを追加したPineScriptです.
//
//@version=4
strategy("MACD Strategy", overlay=true)
// Input parameters
start_y = input(2020, minval=1900, maxval=2100, title="Start Year")
start_m = input(01, minval=01, maxval=12, title="Start Month")
start_d = input(01, minval=01, maxval=31, title="Start day")
end_y = input(2020, minval=1900, maxval=2100, title="End Year")
end_m = input(12, minval=01, maxval=12, title="End Month")
end_d = input(31, minval=01, maxval=31, title="End day")
ema1 = input(12, title="EMA1 length")
ema2 = input(26, title="EMA2 length")
signal = input(9, title="Signal length")
src = input(close, "source")
start_time = timestamp("Asia/Tokyo", start_y, start_m, start_d, 00, 00)
end_time = timestamp("Asia/Tokyo", end_y, end_m, end_d, 00, 00)
is_testing = time >= start_time and time <= end_time
bgcolor(is_testing ? color.black : color.white)
[macd, sig, hist] = macd(src, ema1, ema2, signal)
goldencross = crossover(macd, sig)
deadcross = crossunder(macd, sig)
if is_testing and goldencross
strategy.entry("MacdLE", strategy.long)
if is_testing and deadcross
strategy.entry("MacdSE", strategy.short)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment