Skip to content

Instantly share code, notes, and snippets.

@mattzab
Created February 6, 2018 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattzab/de13c032e202c4d23495e3ab4eadf80d to your computer and use it in GitHub Desktop.
Save mattzab/de13c032e202c4d23495e3ab4eadf80d to your computer and use it in GitHub Desktop.
//@version=3
//Advanced Buy/Sell Signals with Bollinger Band with Alerts
strategy(shorttitle="Advanced Buy/Sell Signals", title="Advanced Buy/Sell Signals", overlay=true, calc_on_order_fills=true, pyramiding=0)
//Define MACD Variables
fast = 10, slow = 20
fastMACD = ema(hlc3, fast)
slowMACD = ema(hlc3, slow)
macd = fastMACD - slowMACD
signal = sma(macd, 5)
hist = macd - signal
currMacd = hist[0]
prevMacd = hist[1]
currPrice = hl2[0]
prevPrice = hl2[1]
buy = currPrice > prevPrice and currMacd > prevMacd
sell = currPrice < prevPrice and currMacd < prevMacd
neutral = (currPrice < prevPrice and currMacd > prevMacd) or (currPrice > prevPrice and currMacd < prevMacd)
//Plot Arrows
timetobuy = buy==1 and (sell[1]==1 or (neutral[1]==1 and sell[2]==1) or (neutral[1]==1 and neutral[2]==1 and sell[3]==1) or (neutral[1]==1 and neutral[2]==1 and neutral[3]==1 and sell[4]==1) or (neutral[1]==1 and neutral[2]==1 and neutral[3]==1 and neutral[4]==1 and sell[5]==1) or (neutral[1]==1 and neutral[2]==1 and neutral[3]==1 and neutral[4]==1 and neutral[5]==1 and sell[6]==1))
timetosell = sell==1 and (buy[1]==1 or (neutral[1]==1 and buy[2]==1) or (neutral[1]==1 and neutral[2]==1 and buy[3]==1) or (neutral[1]==1 and neutral[2]==1 and neutral[3]==1 and buy[4]==1) or (neutral[1]==1 and neutral[2]==1 and neutral[3]==1 and neutral[4]==1 and buy[5]==1) or (neutral[1]==1 and neutral[2]==1 and neutral[3]==1 and neutral[4]==1 and neutral[5]==1 and buy[6]==1))
plotshape(timetobuy, color=blue, location=location.belowbar, style=shape.arrowup)
plotshape(timetosell, color=red, location=location.abovebar, style=shape.arrowdown)
//plotshape(neutral, color=black, location=location.belowbar, style=shape.circle)
//Test Strategy
// strategy.entry("long", true, 1, when = timetobuy and time > timestamp(2017, 01, 01, 01, 01)) // buy by market if current open great then previous high
// strategy.close("long", when = timetosell and time > timestamp(2017, 01, 01, 01, 01))
strategy.order("buy", true, 1000, when=timetobuy==1 and time > timestamp(2017, 01, 01, 01, 01))
strategy.order("sell", false, 1000, when=timetosell==1 and time > timestamp(2017, 01, 01, 01, 01))
// strategy.entry(id = "Short", long = false, when = enterShort())
// strategy.close(id = "Short", when = exitShort())
//strategy.entry("long", true, 1, when = open > high[1]) // enter long by market if current open great then previous high
// strategy.exit("exit", "long", profit = 10, loss = 5) // ge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment