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 indicator, select all of the template text and paste. Press Save and give it a name.
4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.
Source Code
104 lines of Pine Script v5 from signal-ext/ao-signal.pine, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.
ao-signal.pine
//@version=5
//@author=LucF, for PineCoders
// Signal for Backtesting-Trading Engine [PineCoders]
// v1.3, 2019.06.21 02:58
// PineCoders, Tools and ideas for all Pine coders.
// This script is referenced from the PineCoders FAQ & Code here: http://www.pinecoders.com/faq_and_code/#how-can-i-use-one-scripts-output-as-an-input-into-another
// Documentation: https://www.tradingview.com/script/y4CvTwRo-Signal-for-Backtesting-Trading-Engine-PineCoders/
// This script is an example of how to configure a signal for the PineCoders Backtesting-Trading Engine
// which you will find here: https://www.tradingview.com/script/dYqL95JB-Backtesting-Trading-Engine-PineCoders/
// We used the built-in MACD indicator as a template. The script's signal adheres to the following protocol:
// EXTERNAL SIGNAL PROTOCOL
// Only one external indicator can be connected to a script; in order to leverage its use to the fullest,
// the engine provides options to use it as either and entry signal, an entry/exit signal or a filter.
// When used as an entry signal, you can also use the signal to provide the entry stop. Here’s how this works.
// - For filter state: supply +1.0 for bull (long entries allowed), -1.0 for bear (short entries allowed).
// - For entry signals: supply +2.0 for long, -2.0 for short.
// - For exit signals: supply +3.0 for exit from long, -3.0 for exit from short.
// - To send an entry stop level with entry signal: Send positive stop level for long entry
// (e.g. 103.33 to enter a long with a stop at 103.33), negative stop level for short entry
// (e.g. -103.33 to enter a short with a stop at 103.33). If you use this feature,
// your indicator will have to check for exact stop levels of 1.0, 2.0 or 3.0 and their negative counterparts,
// and fudge them with a tick in order to avoid confusion with other signals in the protocol.
// Note that each use must be confirmed in the corresponding section of the script's Settings/Inputs.
// CONFIGURATION OF ALERTS
// Entry and Event alerts will generally be configured to trigger Once Per Bar Close.
// Exit alerts can be configured to trigger Once Per Bar Close or Once Per Bar, depending on how fast
// you want them. Once Per Bar means the alert will trigger the moment the condition is first encountered
// during the realtime bar, instead of at the bar's close.
// Entry and Exit alerts trigger at the same bar where the relevant marker will appear on the chart.
// Event alerts trigger on the bar following the appearance of their respective marker on the chart.
indicator('Signal for Backtesting-Trading Engine [PineCoders]', timeframe="1")
// Allow selection of signal content.
IncludeFilter = input(true, 'Include Filter')
IncludeEntries = input(true, 'Include Entries')
IncludeStops = input(true, 'Include Stops with Entries')
IncludeExits = input(true, 'Include Exits')
// ——————————————————————————————————————————————————————————————————————————————————————
// ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
// This part is the section where you would put your own indicator and define conditions.
// ————— TV built-in MACD code.
ao = ta.sma(close,5) - ta.sma(close,34)
diff = ao - ao[1]
plot(ao, color = diff <= 0 ? #F44336 : #009688, style=plot.style_columns)
// ————— Filter.
FilterLong = ao > 0
FilterShort = ao < 0
// ————— Entries.
EnterLong = ta.crossover(diff, 0)
EnterShort = ta.crossunder(diff, 0)
// ————— Stops.
Atr = ta.atr(14)
StopLong = math.min(ta.lowest(5), math.min(close, open) - Atr * 1.5)
StopShort = math.max(ta.highest(5), math.max(close, open) + Atr * 1.5)
// ————— Exits.
ExitLong = ta.crossunder(diff, ao) and diff > 0
ExitShort = ta.crossover(diff, ao) and diff < 0
// ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
// ——————————————————————————————————————————————————————————————————————————————————————
// ————— This function ensures that no stop value equal to one of the protocol's reserved values is sent, so it isn't misinterpreted as a signal.
// The fudge is in the direction where the trade's risk is decreased by a tick, so added for longs and subtracted for shorts.
FudgeStop(_stop, _fudgedir) =>
_stop == 1.0 or _stop == 2.0 or _stop == 3.0 ? _stop + syminfo.mintick * _fudgedir : _stop
// ————— Build required values corresponding to states.
Filter = FilterLong ? 1 : FilterShort ? -1 : 0
Entries = EnterLong ? 2 : EnterShort ? -2 : 0
FudgeStop__1 = FudgeStop(StopLong, 1)
FudgeStop__2 = FudgeStop(StopShort, -1)
Stops = EnterLong ? FudgeStop__1 : EnterShort ? -FudgeStop__2 : 0
Exits = ExitLong ? 3 : ExitShort ? -3 : 0
// ————— We must decide which value will be sent in case more than one is different than 0, since only one value can be sent at each bar.
// Priority is given to exits, with filter states coming last.
Signal = IncludeExits and Exits != 0 ? Exits : IncludeStops and Stops != 0 ? Stops : IncludeEntries and Entries != 0 ? Entries : IncludeFilter and Filter != 0 ? Filter : na
// ————— Plot signal which is to be connected to the Engine through the External Indicator field at the very bottom of the Engine's Settings/Inputs.
plot(Signal, 'Signal')
// ————— Plots markers.
plotshape(IncludeEntries and EnterLong, 'Enter Long', style=shape.triangleup, location=location.bottom, color=color.new(color.green, 0), size=size.small, text='Enter\nLong')
plotshape(IncludeEntries and EnterShort, 'Enter Short', style=shape.triangledown, location=location.top, color=color.new(color.red, 0), size=size.small, text='Enter\nShort')
plotshape(IncludeExits and ExitLong, 'Exit Long', style=shape.triangledown, location=location.top, color=color.new(color.green, 0), size=size.tiny, text='Exit\nLong')
plotshape(IncludeExits and ExitShort, 'Exit Short', style=shape.triangleup, location=location.bottom, color=color.new(color.red, 0), size=size.tiny, text='Exit\nShort')
bgcolor(color=IncludeFilter and FilterLong ? color.lime : IncludeFilter and FilterShort ? color.red : na, title='Filter Background', transp=90)
Overview
Comments state that a built-in MACD setup is the template. It encodes filter state, long and short entries, exits, and optional stop prices as distinct numeric values so the engine can read them. Four inputs choose whether filters, entries, stops, and exits are included. The header notes that entry alerts are normally set to fire once per bar close, while exit alerts may fire during the live bar.
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
Include Filter
bool
true
IncludeFilter
Include Entries
bool
true
IncludeEntries
Include Stops with Entries
bool
true
IncludeStops
Include Exits
bool
true
IncludeExits
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.
Asymmetrical RSI is an indicator that computes a custom relative strength index from the rate of change of a user-selected source. It plots the resulting ARSI line in a separate pane with color coding that turns green above the overbought level and red below t
This indicator calculates momentum setups based on the Relative Strength Index (RSI) and plots in a separate pane. It offers multiple scanning modes such as Continuous Break, Continuous Retracement, Flip Break, Flip Retracement, and both regular and hidden div
This indicator calculates a modified MACD oscillator and signal line alongside a trend EMA filter, plotting the output as a stylized column histogram in a separate pane. Instead of plotting true MACD distance, it quantifies positive and negative histogram mome
This indicator plots the Average Directional Index in a separate pane and colors the line teal or maroon according to whether plus DI is above minus DI. A horizontal key level and a silver background mark periods when ADX exceeds that threshold. With labels en
This indicator demonstrates an experimental graph-style scatter visualization within a separate pane using Pine Script arrays. It tracks two features across recent bars: the single-bar price change and a scaled 14-period Relative Strength Index. Values are loa
This script calculates and plots a Stochastic Oscillator (KD indicator) in a separate chart pane. It determines the fast stochastic value over a 9-period lookback, then applies a 3-period Simple Moving Average (SMA) to create the %K line, and a second 3-period