Strategy · Pine Script v4 · On the chart

Simple Long Term Strategy

by hirawatt· code credited to yesgoods

Simple Long Term Strategy plots five exponential moving averages on the price chart and trades crossovers between selected averages.

View on GitHub

GitHub stars11on the repository
Pine versionv4older, still runs
Lines of code60
Settings13inputs
LicenceMIT
Last update2 years ago11 Dec 2023

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

60 lines of Pine Script v4 from strategy/simpleEMAStrategy.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

simpleEMAStrategy.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © yesgoods

//@version=4
strategy("Simple Long Term Strategy", overlay=true, margin_long=100, margin_short=100)
// Dependency 5MA Study on the chart
// Variables {
src = input(title="Source", type=input.source, defval=hlc3)

ema1Len = input(title="MA 1 Length", type=input.integer, defval=1)
ema2Len = input(title="MA 2 Length", type=input.integer, defval=5)
ema3Len = input(title="MA 3 Length", type=input.integer, defval=10)
ema4Len = input(title="MA 4 Length", type=input.integer, defval=21)
ema5Len = input(title="MA 5 Length", type=input.integer, defval=55)

ema1 = ema(src, ema1Len)
ema2 = ema(src, ema2Len)
ema3 = ema(src, ema3Len)
ema4 = ema(src, ema4Len)
ema5 = ema(src, ema5Len)

start = input(title="Start Date", type=input.time, defval=timestamp("20 Feb 2001 00:00 +0300"))
end = input(title="End Date", type=input.time, defval=timestamp("20 Feb 2022 00:00 +0300"))


crossema1 = input(title="Crossover MA1", defval=ema1, options=[ema1, ema2, ema3, ema3, ema4, ema5])
crossema2 = input(title="Crossover MA2", defval=ema2, options=[ema1, ema2, ema3, ema3, ema4, ema5])
crossema3 = input(title="Crossover MA3", defval=ema3, options=[ema1, ema2, ema3, ema3, ema4, ema5])
crossema4 = input(title="Crossover MA4", defval=ema4, options=[ema1, ema2, ema3, ema3, ema4, ema5])
crossema5 = input(title="Crossover MA5", defval=ema5, options=[ema1, ema2, ema3, ema3, ema4, ema5])

//}

long = crossover(crossema1, crossema5)
short = crossunder(crossema1, crossema5)

longCondition = long
shortCondition = short

closeLong = crossunder(crossema1, crossema3)
closeShort = crossover(crossema1, crossema3)

if time >= start and time <= end
    strategy.entry("Long", strategy.long, when = longCondition)
    strategy.entry("Short", strategy.short, when =shortCondition)

strategy.exit("exit", "", profit = 500, loss = 50)
strategy.close("Long", when = closeShort)
strategy.close("Short", when = closeLong)


// Design {
p_1 = plot(ema1, title="1", color=color.blue)
p_2 = plot(ema2, title="2", color=color.green)
p_3 = plot(ema3, title="3", color=color.yellow)
p_4 = plot(ema4, title="4", color=color.orange)
p_5 = plot(ema5, title="5", color=color.red)
fill(p_1, p_2, color= color.green)
daily = timeframe.isdaily
//}

Overview

Simple Long Term Strategy enters long when the first selected average crosses above the fifth and enters short on the opposite cross, only inside the chosen start and end dates. It closes longs and shorts on crosses with the third average and also exits with a fixed profit target and stop loss. Lengths, source, and which averages form the crosses are adjustable. No market or timeframe is specified.

Details

Published by

hirawatt on GitHub

Code credit

yesgoods, as written in the script's header

Type

Strategy, Pine Script v4

Draws

On the price chart

Category

Trend, Moving averages

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

pinescriptpinescript-indicatorspinescript-strategiestradingview

Settings

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

SettingTypeDefaultVariable
Sourcesourcehlc3src
MA 1 Lengthint1ema1Len
MA 2 Lengthint5ema2Len
MA 3 Lengthint10ema3Len
MA 4 Lengthint21ema4Len
MA 5 Lengthint55ema5Len
Start Datetimetimestamp("20 Feb 2001 00:00 +0300")start
End Datetimetimestamp("20 Feb 2022 00:00 +0300")end
Crossover MA1inputema1crossema1
Crossover MA2inputema2crossema2
Crossover MA3inputema3crossema3
Crossover MA4inputema4crossema4
Show all 13 settings
SettingTypeDefaultVariable
Crossover MA5inputema5crossema5
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