This strategy plots an exponential moving average on the price chart and opens a long position when the close crosses above that average, but only when no position is already open.
1Copy the scriptPress Copy script above, or the copy button on the code below.
2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
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
28 lines of Pine Script v6 from scripts/first-strategy.pine.txt, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.
first-strategy.pine.txt
//@version=6
strategy("My First Strategy", overlay = true, initial_capital = 10000,
default_qty_type = strategy.percent_of_equity, default_qty_value = 10,
commission_type = strategy.commission.percent, commission_value = 0.05,
slippage = 2)
// 1. Settings you can change later
emaLen = input.int(50, "Trend average length")
stopPct = input.float(2.0, "Stop loss %", step = 0.5)
takePct = input.float(4.0, "Take profit %", step = 0.5)
// 2. The trend line
trend = ta.ema(close, emaLen)
plot(trend, "Trend", color.orange, 2)
// 3. The entry rule: price closes back above the trend line
buySignal = ta.crossover(close, trend)
// 4. Place the order, but only if we are not already in a trade
if buySignal and strategy.position_size == 0
strategy.entry("Long", strategy.long)
// 5. Stop loss and take profit, measured from the entry price
entryPrice = strategy.position_avg_price
stopPrice = entryPrice * (1 - stopPct / 100)
takePrice = entryPrice * (1 + takePct / 100)
if strategy.position_size > 0
strategy.exit("Exit", "Long", stop = stopPrice, limit = takePrice)
Overview
My First Strategy exits with a percentage stop loss and a percentage take profit measured from the average entry price. The average length, stop percentage, and take-profit percentage are user inputs. Position size defaults to a share of equity, and the strategy includes commission and slippage. It does not open short trades.
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
Trend average length
int
50
emaLen
Stop loss %
float
2.0
stopPct
Take profit %
float
4.0
takePct
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.
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
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
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
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
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
EMA Crossover Strategy plots a fast and a slow exponential moving average on the price chart. It treats a confirmed fast-over-slow cross as a long condition and a confirmed cross under as a short condition. Position size is a percentage of equity. Optional per