Strategy · Pine Script v4 · On the chart

Algo Trading: Simple 3-EMA Strategy

by doppelgunner

This strategy plots three exponential moving averages on the price chart and trades their alignment.

View on GitHub

GitHub stars4on the repository
Pine versionv4older, still runs
Lines of code45
Settings8inputs
LicenceMIT
Last update5 years ago16 Sep 2021

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

45 lines of Pine Script v4 from 0001_Free Profitable TradingView Strategy Script For Ethereum ETHUSDT Binance Using EMA only/EMA Crossover Strategy.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

EMA Crossover Strategy.pine
// 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
initial_capital = 10000
percent_risk = input(2, "% Risk per trade (of equity)", minval=0, type=input.float, group="Risk Management Settings", tooltip="This is important for position sizing") / 100
strategy("Algo Trading: Simple 3-EMA Strategy", overlay=true, initial_capital=initial_capital, commission_type=strategy.commission.percent, commission_value=0.08)
ave_loss_percent = input(2, "% of average loss (backtesting)", minval=0, type=input.float, group="Risk Management Settings", tooltip="This is important for position sizing") / 100

enter_long = input(true, "Enter Long?", type=input.bool, group="Backtest Settings")
enter_short = input(false, "Enter Short?", type=input.bool, group="Backtest Settings")

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, "EMA Slow", color=color.black, linewidth=3)
plot(ema_mid, "EMA Mid", color=color.red, linewidth=2)
plot(ema_fast, "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

risk_money = strategy.equity * percent_risk
stop_risk = abs(close - (close * ave_loss_percent))
diffSR = abs(close - stop_risk)

position_size = risk_money / diffSR

strategy.entry("long", strategy.long, when=buy and enter_long, qty=position_size)
strategy.close("long", when=sell)

strategy.entry("short", strategy.short, when=sell and enter_short, qty=position_size)
strategy.close("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)

Overview

Algo Trading: Simple 3-EMA Strategy enters long when the fast EMA crosses above the mid EMA while price is above the slow EMA, and closes that long on the opposite cross with price below the slow EMA. Short entries use the matching reverse cross and stay off by default. Size comes from a percent of equity and an assumed average-loss percent, but no stop-loss order is placed. Users change the three EMA lengths and can toggle background coloring by position.

Details

Published by

doppelgunner on GitHub

Type

Strategy, Pine Script v4

Draws

On the price chart

Category

Trend, Moving averages

Markets

Crypto

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
% Risk per trade (of equity)float2percent_risk
% of average loss (backtesting)float2ave_loss_percent
Enter Long?booltrueenter_long
Enter Short?boolfalseenter_short
EMA Fast Lengthint50ema_fast_length
EMA Mid Lengthint100ema_mid_length
EMA Slow Lengthint200ema_slow_length
Show BG Color?booltrueshowBGColor
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Doge SMA

by hackingthemarkets
474 stars

Doge SMA is a long-only moving-average crossover strategy drawn on the price chart. It calculates a fast and a slow simple moving average of close and enters a long position when the fast average crosses above the slow average, but only inside a user-defined s

Strategy v4 TrendMoving averages
Updated 4 years ago · None stated GitHub repository of Doge SMA

This strategy calculates and plots a fast Exponential Moving Average (EMA) and a slow EMA directly on the main price chart. It enters long trades when the fast EMA crosses above the slow EMA and exits when the fast EMA crosses below the slow EMA. Inputs allow

Strategy v6 TrendMoving averages
Updated today · AGPL-3.0 GitHub repository of Forven S01508 — Simple EMA20/50 Cross
330 stars

2 EMA/SMA + RSI Strategy [Alorse] combines three configurable moving averages with a short-period Relative Strength Index filter. It plots fast, slow, and trend moving averages on the price chart. Long entries trigger when the fast moving average is above the

Strategy v4 TrendOscillators & momentum
Updated 2 months ago · None stated GitHub repository of 2 EMA/SMA + RSI Strategy [Alorse]

Acrypto - Weighted Strategy

by AlbertoCuadra
318 stars

This strategy plots on the price chart and opens long or short positions when a weighted score from MACD, Stochastic RSI, RSI, Supertrend and a moving-average crossover exceeds a user-set trigger. Each component has its own weight and candle delay. Entries can

Strategy v5 TrendOscillators & momentum
Updated 3 years ago · Custom licence GitHub repository of Acrypto - Weighted Strategy
257 stars

This strategy calculates dynamic support and resistance levels using pivot highs and lows alongside a custom smoothed trend filter and a 3-period EMA. It identifies entry opportunities when price breaks out relative to the trend filter and short-term EMA cross

Strategy v5 TrendSupport & resistance Repaints
Updated 6 weeks ago · None stated GitHub repository of 30s - ES 1m Strategy Candle Bar

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers