Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save doppelgunner/388ab9caf35590a8353df7623adcdbc2 to your computer and use it in GitHub Desktop.
Save doppelgunner/388ab9caf35590a8353df7623adcdbc2 to your computer and use it in GitHub Desktop.
Algo Trading: Simple 3-EMA Strategy (youtube: https://www.youtube.com/channel/UC4dgxbwzMoV6aKSyNkf9hhA)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © doppelgunner
//@version=4
strategy("Algo Trading: Simple 3-EMA Strategy", overlay=true, margin_long=100, margin_short=100)
ema_fast_length = input(50, "EMA Fast Length", minval=1, group="EMA Settings")
ema_mid_length = input(100, "EMA Mid Length", minval=1, group="EMA Settings")
ema_slow_length = input(200, "EMA Slow Length", minval=1, group="EMA Settings")
ema_slow = ema(close, ema_slow_length)
ema_mid = ema(close, ema_mid_length)
ema_fast = ema(close, ema_fast_length)
plot(ema_slow, color=color.black, linewidth=3)
plot(ema_mid, color=color.red, linewidth=2)
plot(ema_fast, color=color.blue, linewidth=1)
buy = crossover(ema_fast, ema_mid) and close > ema_slow
sell = crossunder(ema_fast, ema_mid) and close < ema_slow
strategy.entry("long", strategy.long, when=buy)
strategy.exit("long", when=sell)
strategy.entry("short", strategy.short, when=sell)
strategy.exit("short", when=buy)
inLong = strategy.position_size > 0
inShort = strategy.position_size < 0
notInTrade = strategy.position_size == 0
showBGColor = input(true, "Show BG Color?", type=input.bool, group="Color Settings")
bgcolor(notInTrade or (not showBGColor) ? color.white : inLong ? color.green : color.red)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment