Skip to content

Instantly share code, notes, and snippets.

@bongkook
Last active November 27, 2021 20:16
Show Gist options
  • Save bongkook/a4a6fa180717272a7c21bae8f8e60674 to your computer and use it in GitHub Desktop.
Save bongkook/a4a6fa180717272a7c21bae8f8e60674 to your computer and use it in GitHub Desktop.
TradingView Strategies
// NOTE: Add this strategy on a usual candles chart, NOT on a HeikinAshi chart.
//@version=3
strategy("Heikin Ashi Strategy Example", overlay=true)
haTicker = heikinashi(tickerid)
haOpen = security(haTicker, period, open)
haClose = security(haTicker, period, close)
longCondition = haClose > haOpen and haOpen > haClose[1]
exitCondition = haClose < haOpen
if (time > timestamp(2018,5,11,0,0)) and time < timestamp(2018,5,15,0,0)
strategy.entry("Long", strategy.long, 100, when = longCondition)
strategy.close_all(when = exitCondition)
//@version=2
strategy("Heikin-Ashi Strategy", overlay=true)
// Plots Color Of Heikin-Ashi Bars while Viewing Candlestics or Bars
//Works on Candlesticks and OHLC Bars - Does now work on Heikin-Ashi bars - But I have verified its accuracy
// Created By User ChrisMoody 1-30-2014 with help from Alex in Tech Support
// === BACKTEST RANGE ===
FromMonth = input(defval = 1, title = "From Month", minval = 1)
FromDay = input(defval = 1, title = "From Day", minval = 1)
FromYear = input(defval = 2017, title = "From Year", minval = 1998)
ToMonth = input(defval = 1, title = "To Month", minval = 1)
ToDay = input(defval = 1, title = "To Day", minval = 1)
ToYear = input(defval = 9999, title = "To Year", minval = 1998)
haclose = ((open + high + low + close)/4)//[smoothing]
haopen = na(haopen[1]) ? (open + close)/2 : (haopen[1] + haclose[1]) / 2
heikUpColor() => haclose > haopen
heikDownColor() => haclose <= haopen
barcolor(heikUpColor() ? aqua: heikDownColor() ? red : na)
if (heikUpColor() and (time > timestamp(FromYear, FromMonth, FromDay, 00, 00)) and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59)))
strategy.entry("LONG", strategy.long, comment="LONG")
if (heikDownColor() and (time > timestamp(FromYear, FromMonth, FromDay, 00, 00)) and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59)))
strategy.entry("SHORT", strategy.short, comment="SHORT")
//plot(pos, title="pos", style=line, linewidth=1, color=red )
//@version=3
//Heiken-Ashi Strategy V3 by wziel
strategy("Heiken-Ashi Strategy V3",shorttitle="WZIV3",overlay=true,default_qty_value=10000,initial_capital=10000,currency=currency.USD)
res = input(title="Heikin Ashi Candle Time Frame", type=resolution, defval="60")
hshift = input(1,title="Heikin Ashi Candle Time Frame Shift")
res1 = input(title="Heikin Ashi EMA Time Frame", type=resolution, defval="180")
mhshift = input(0,title="Heikin Ashi EMA Time Frame Shift")
fama = input(1,"Heikin Ashi EMA Period")
test = input(1,"Heikin Ashi EMA Shift")
sloma = input(30,"Slow EMA Period")
slomas = input(1,"Slow EMA Shift")
macdf = input(false,title="With MACD filter")
res2 = input(title="MACD Time Frame", type=resolution, defval="15")
macds = input(1,title="MACD Shift")
//Heikin Ashi Open/Close Price
ha_t = heikinashi(tickerid)
ha_open = security(ha_t, res, open[hshift])
ha_close = security(ha_t, res, close[hshift])
mha_close = security(ha_t, res1, close[mhshift])
//macd
[macdLine, signalLine, histLine] = macd(close, 12, 26, 9)
macdl = security(ha_t,res2,macdLine[macds])
macdsl= security(ha_t,res2,signalLine[macds])
//Moving Average
fma = ema(mha_close[test],fama)
sma = ema(ha_close[slomas],sloma)
plot(fma,title="MA",color=lime,linewidth=2,style=line)
plot(sma,title="SMA",color=red,linewidth=2,style=line)
//Strategy
golong = crossover(fma,sma) and (macdl > macdsl or macdf == false )
goshort = crossunder(fma,sma) and (macdl < macdsl or macdf == false )
strategy.entry("Buy",strategy.long,when = golong)
strategy.entry("Sell",strategy.short,when = goshort)
//@version=3
strategy("RSI Strategy", overlay=true)
length = input(14)
lower_threshold = input(30)
upper_threshold = input(70)
price = close
vrsi = rsi(price, length)
long_condition = crossover(vrsi, lower_threshold)
if (long_condition)
strategy.entry("Buy", strategy.long)
short_condition = crossunder(vrsi, upper_threshold)
if (short_condition)
strategy.entry("Sell", strategy.short)
//@Version=3
strategy("MACD戦略", overlay=true)
[,_,hist] = macd(close, 12, 26, 9)
strategy.entry("BUY", strategy.long, when=(hist>0))
strategy.close("BUY", when=(hist<=0))
//@Version=3
strategy(title="MACD example strategy", overlay=false, default_qty_value=3,
initial_capital=10000, currency=currency.EUR)
// Create inputs
fastLen = input(title="Fast Length", type=integer, defval=12)
slowLen = input(title="Slow Length", type=integer, defval=26)
sigLen = input(title="Signal Length", type=integer, defval=9)
// Get MACD values
[macdLine, signalLine, _] = macd(close, fastLen, slowLen, sigLen)
// Plot MACD values and line
plot(series=macdLine, color=#6495ED, linewidth=2)
plot(series=signalLine, color=orange, linewidth=2)
hline(price=0)
// Determine long and short conditions
longCondition = crossover(macdLine, signalLine)
shortCondition = crossunder(macdLine, signalLine)
// Submit orders
strategy.entry(id="Long Entry", long=true, when=longCondition)
strategy.entry(id="Short Entry", long=false, when=shortCondition)
//@Version=3
strategy("Heikin-Ashi Color change Strategy ",shorttitle="HAS color change ",overlay=true)
/////////////////////////////////////////////////////////////////////////////
res = input(title="Resolution", type=resolution, defval="60")
//Heikin ashi taken from trading view's funciton and resolusion added
ha_t = heikinashi(tickerid)
ha_open = security(ha_t, res, open)
ha_close = security(ha_t, res, close)
golong = ha_close > ha_open
goshort = ha_close < ha_open
////////////////////////////////////////////////////////////////////////////
strategy.entry("Buy",strategy.long,when = golong and year > 2015)
strategy.entry("Sell",strategy.short,when = goshort and year > 2015)
//@version=3
strategy("bt_heikinashi", overlay=true)
h_a = heikinashi(tickerid)
vp = input(3, "Validation Period")
c = security(h_a, period, close)
o = security(h_a, period, open)
lt = true
st = true
for i = 0 to vp-1
lt:= lt and c[i] > o[i]
st:= st and c[i] < o[i]
l = lt and c[vp] < o[vp]
s = st and c[vp] > o[vp]
strategy.entry("long", strategy.long, when=l)
strategy.entry("short", strategy.short, when=s)
@bongkook
Copy link
Author

bongkook commented Dec 5, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment