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
177 lines of Pine Script v5 from templates/strategy.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.
strategy.pine
//@version=5
// Developed by ProfitRobots: https://github.com/sibvic/pinescript-templates
strategy("Strategy", overlay = true, pyramiding = 100)
// Trading module v 1.11
FromMonth = input.int(defval = 1, title = "From Month", minval = 1)
FromDay = input.int(defval = 1, title = "From Day", minval = 1)
FromYear = input.int(defval = 2017, title = "From Year", minval = 2014)
ToMonth = input.int(defval = 1, title = "To Month", minval = 1)
ToDay = input.int(defval = 1, title = "To Day", minval = 1)
ToYear = input.int(defval = 9999, title = "To Year", minval = 2014)
in_testing_range = (time > timestamp(FromYear, FromMonth, FromDay, 00, 00)) and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59))
last_signal = 0
last_signal_prev = na(last_signal[1]) ? 0 : last_signal[1]
in_buy = last_signal_prev > 0
in_sell = last_signal_prev < 0
// ==========
buy_signal = TODO ? 1 : 0
sell_signal = TODO ? 1 : 0
close_buy_signal = in_buy and TODO
close_sell_signal = in_sell and TODO
// ==========
max_positions = input(1, "Max positions")
Buy = ta.change(buy_signal) > 0 and (not in_buy or last_signal_prev < max_positions) and in_testing_range
Sell = ta.change(sell_signal) > 0 and (not in_sell or last_signal_prev > -max_positions) and in_testing_range
last_buy_price = ta.valuewhen(Buy, close, 0)
last_sell_price = ta.valuewhen(Sell, close, 0)
BuyBreakevenHit(be_type, last_price, trigger) =>
hit = false
if (be_type == "Pips")
hit := (close - last_price) >= trigger * 10 * syminfo.mintick
else
hit := (100 * (close - last_price)) / last_price >= trigger
hit
SellBreakevenHit(be_type, last_price, trigger) =>
hit = false
if (be_type == "Pips")
hit := (last_price - close) >= trigger * 10 * syminfo.mintick
else
hit := (100 * (last_price - close)) / last_price >= trigger
hit
GetBreakevenLevel(by_type, isBuy, last_price, target) =>
level = 0.0
if (isBuy)
if (by_type == "Pips")
level := last_price + target * 10 * syminfo.mintick
else
level := last_price + last_price * target / 100
else
if (by_type == "Pips")
level := last_price - target * 10 * syminfo.mintick
else
level := last_price - last_price * target / 100
level
FixedStopLevel(is_buy, type, value, value_bars) =>
float stop = na
mult = is_buy ? -1 : 1
if (type == "Fixed rate shift")
stop := close + value * mult
if (type == "Pips")
stop := close + value * 10 * syminfo.mintick * mult
if (type == "%")
stop := close + close * value / 100 * mult
if (type == "Min/max of x candles")
if (is_buy)
stop := ta.lowest(low, value_bars)
else
stop := ta.highest(high, value_bars)
stop
FixedLimitLevel(is_buy, type, value, value_bars) =>
float limit = na
mult = is_buy ? -1 : 1
if (type == "Fixed rate shift")
limit := close - value * mult
if (type == "Pips")
limit := close - value * 10 * syminfo.mintick * mult
if (type == "%")
limit := close - close * value / 100 * mult
if (type == "Min/max of x candles")
if (is_buy)
limit := ta.highest(high, value_bars)
else
limit := ta.lowest(low, value_bars)
limit
stop_type = input.string("Do not use", "Stop loss type", options = ["Do not use", "%", "Fixed rate shift", "ATR", "Pips", "Min/max of x candles"])
stop = input.float(1, "Stop loss")
stop_bars = input.int(10, "Stop loss, bars (min/max)")
stop_atr_length = input.int(5, "Stop ATR Length")
stop_atr_mult = input.float(1.5, "Stop ATR Multiplier")
trailing_sl_type = input.string("Do not use", "SL/TP Trailing Type", options = ["Do not use", "%", "Pips"])
trailing_sl_trigger = input.float(10, "Trailing start after")
trailing_sl_target = input.float(10, "Trailing distance, pips")
breakeven_use = input.string("Do not use", "Use breakeven", options = ["Do not use", "Pips", "%"])
breakeven_trigger = input.float(10, "Move stop on profit")
breakeven_target = input.float(0, "Move stop to")
buy_fixed_stop_level = ta.valuewhen(Buy, FixedStopLevel(true, stop_type, stop, stop_bars), 0)
buy_breakeven_level = ta.valuewhen(Buy or in_buy and BuyBreakevenHit(breakeven_use, last_buy_price, breakeven_trigger), Buy ? na : GetBreakevenLevel(breakeven_use, true, last_buy_price, breakeven_target), 0)
sell_fixed_stop_level = ta.valuewhen(Sell, FixedStopLevel(false, stop_type, stop, stop_bars), 0)
sell_breakeven_level = ta.valuewhen(Sell or in_sell and SellBreakevenHit(breakeven_use, last_sell_price, breakeven_trigger), Sell ? na : GetBreakevenLevel(breakeven_use, false, last_sell_price, breakeven_target), 0)
limit_type = input.string("Do not use", "Take profit type", options = ["Do not use", "%", "Fixed rate shift", "ATR", "Pips", "Min/max of x candles"])
limit = input.float(1, "Take profit")
limit_bars = input.int(10, "Take profit, bars (min/max)")
limit_atr_length = input.int(5, "Take profit ATR Length")
limit_atr_mult = input.float(1.5, "Take profit ATR Multiplier")
buy_fixed_limit_level = ta.valuewhen(Buy, FixedLimitLevel(true, limit_type, limit, limit_bars), 0)
sell_fixed_limit_level = ta.valuewhen(Sell, FixedLimitLevel(false, limit_type, limit, limit_bars), 0)
buy_stop = stop_type == "Do not use" ? na : na(buy_fixed_stop_level) ? last_buy_price - ta.atr(stop_atr_length) * stop_atr_mult : buy_fixed_stop_level
sell_stop = stop_type == "Do not use" ? na : na(sell_fixed_stop_level) ? last_sell_price + ta.atr(stop_atr_length) * stop_atr_mult : sell_fixed_stop_level
buy_limit = limit_type == "Do not use" ? na : na(buy_fixed_limit_level) ? last_buy_price + ta.atr(limit_atr_length) * limit_atr_mult : buy_fixed_limit_level
sell_limit = limit_type == "Do not use" ? na : na(sell_fixed_limit_level) ? last_sell_price - ta.atr(limit_atr_length) * limit_atr_mult : sell_fixed_limit_level
buy_stop_be = na(buy_breakeven_level) ? buy_stop : math.max(buy_breakeven_level, buy_stop)
sell_stop_be = na(sell_breakeven_level) ? sell_stop : math.min(sell_breakeven_level, sell_stop)
buy_stop_hit = in_buy and not na(buy_stop_be) and low <= buy_stop_be
sell_stop_hit = in_sell and not na(sell_stop_be) and high >= sell_stop_be
buy_limit_hit = in_buy and not na(buy_limit) and high >= buy_limit
sell_limit_hit = in_sell and not na(sell_limit) and low <= sell_limit
GetPositionsCount(buy, sell, count) =>
c = count
if (buy)
if (count < 0)
c := 1
else
c := count + 1
if (sell)
if (count > 0)
c := -1
else
c := count - 1
c
GetTrailingPrice(isBuy, lastPrice, triggerType, triggerValue) =>
float res = na
if (triggerType == "Pips")
if (isBuy)
res := lastPrice + triggerValue * 10 * syminfo.mintick
else
res := lastPrice - triggerValue * 10 * syminfo.mintick
if (triggerType == "%")
if (isBuy)
res := lastPrice + lastPrice * triggerValue / 100
else
res := lastPrice - lastPrice * triggerValue / 100
res
close_buy = close_buy_signal and not Sell
close_sell = close_sell_signal and not Buy
last_signal := close_buy or close_sell or buy_stop_hit or sell_stop_hit or buy_limit_hit or sell_limit_hit ? (Buy ? 1 : (Sell ? -1 : 0)) : GetPositionsCount(Buy, Sell, last_signal_prev)
strategy.close_all(not in_testing_range)
strategy.entry("Long", strategy.long, when = Buy)
strategy.entry("Short", strategy.short, when = Sell)
strategy.close("Long", close_buy)
strategy.close("Short", close_sell)
b_stop_val = na(buy_breakeven_level) ? buy_stop : math.max(buy_breakeven_level, buy_stop)
s_stop_val = na(sell_breakeven_level) ? sell_stop : math.min(sell_breakeven_level, sell_stop)
b_trail_price = GetTrailingPrice(true, last_buy_price, trailing_sl_type, trailing_sl_trigger)
s_trail_price = GetTrailingPrice(true, last_sell_price, trailing_sl_type, trailing_sl_trigger)
b_trail_offset = trailing_sl_type != "Do not use" ? trailing_sl_target * 10 : na
s_trail_offset = trailing_sl_type != "Do not use" ? trailing_sl_target * 10 : na
strategy.exit("exit long", "Long", stop = b_stop_val, limit = buy_limit, trail_price = b_trail_price, trail_offset = b_trail_offset)
strategy.exit("exit short", "Short", stop = s_stop_val, limit = sell_limit, trail_price = s_trail_price, trail_offset = s_trail_offset)
Overview
Strategy includes placeholder buy and sell signals intended to be completed by the user. The template provides customizable risk management features including date range filtering, pyramiding controls, trailing stop-loss, breakeven triggers, and stop-loss or take-profit orders based on fixed pips, percentages, ATR, or recent candle extremes. It overlays directly on the main price chart. By default, both stop-loss and take-profit mechanisms are disabled.
Date range filter for backtesting windows
Configurable stop-loss via pips, percentage, ATR, or bar extremes
Breakeven and trailing stop-loss automation
Take-profit targets using fixed offsets, ATR, or candle extremes
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
From Month
int
1
FromMonth
From Day
int
1
FromDay
From Year
int
2017
FromYear
To Month
int
1
ToMonth
To Day
int
1
ToDay
To Year
int
9999
ToYear
Max positions
int
1
max_positions
Stop loss type
string
Do not use
stop_type
Stop loss
float
1
stop
Stop loss, bars (min/max)
int
10
stop_bars
Stop ATR Length
int
5
stop_atr_length
Stop ATR Multiplier
float
1.5
stop_atr_mult
Show all 23 settings
Setting
Type
Default
Variable
SL/TP Trailing Type
string
Do not use
trailing_sl_type
Trailing start after
float
10
trailing_sl_trigger
Trailing distance, pips
float
10
trailing_sl_target
Use breakeven
string
Do not use
breakeven_use
Move stop on profit
float
10
breakeven_trigger
Move stop to
float
0
breakeven_target
Take profit type
string
Do not use
limit_type
Take profit
float
1
limit
Take profit, bars (min/max)
int
10
limit_bars
Take profit ATR Length
int
5
limit_atr_length
Take profit ATR Multiplier
float
1.5
limit_atr_mult
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 script is an empty strategy template for Pine Script developers. It declares a version 4 strategy named Basic Strategy Boilerplate with the short title Strategy Boilerplate and sets overlay to true so any future plots would appear on the price chart. It d
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 overlay strategy offers two selectable entry modes: a Bollinger squeeze on typical price and a simple moving-average cross. It filters trades by weekday, session, date range, and an optional news window, and can restrict direction or invert the side. Risk