Signal for Backtesting-Trading Engine [PineCoders]

This is a companion script to the PineCoders Backtesting-Trading Engine. It illustrates how to build a signal plot in another script, which can in turn be fed in the Engine to provide entry, exit, filter and stop information.
Connection to the Engine is done through its “External Indicator” input field at the very bottom of the Engine’s Settings/Inputs.
The Engine must be operating in study mode to be able to connect an external indicator to it.
The way this script builds the signal is straightforward, as you will see in the code. Two aspects are worth mentioning:
The “FudgeStop()” function used to fudge the stop value by one tick for the rare cases where it will match one of the protocol reserved values, i.e. 1, 2 or 3.
The priority and exclusivity given to the different types of signals in the signal-building “Signal = …” line. No two signals can be sent simultaneously through the signal plot, except for the entry and stop combination.
You can determine in this script’s Settings/Inputs the type of signals that will go through the signal plot.

This script respects the following 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 an 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’s stop. Here’s how this works:
For filter state: supply +1 for bull (long entries allowed), -1 for bear (short entries allowed).
For entry signals: supply +2 for long, -2 for short.
For exit signals: supply +3 for exit from long, -3 for exit from short.
To send an entry stop level with an 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.
Remember that mere generation of the values by your indicator will have no effect until you explicitly allow their use in the appropriate sections of the Engine’s Settings/Inputs.
Release Notes: Updated links in comments.
Release Notes: This is the Pinescripters Backtesting-Trading Engine for which this type of signal is meant to interact with.

Release Notes: Cleaned up code. The chart shows the Signal in the pane below the chart, with only the entry signals being transmitted by the signal plot (see this indicator's Inputs).

The signal is also connected to the Backtesting-Trading Engine appearing on the chart. The Engine has been configured so that:
1. The entry strat is set to "External indicator", and
2. This indicator's Signal plot has been selected in the Engine's "Connect your indicator here" field at the very bottom of the Engine's Inputs.

Since only the entries are sent to the Engine through the signal in this particular instance, the trades are exited using the Engine's active stop and exit strats.
Release Notes: Fixed comments.
Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.

Want to use this script on a chart?
Tools and ideas for all Pine coders:
Our Pine FAQ & Code:
Pine news broadcasts: or


+1 Reply
So nice - I just found this before I got stuck into trying to figure it for myself. Thank you PineCoders. Really impressive service.
Nice, is there a way to send position size related information also via the signal?
PineCoders arviman
@arviman, The information could be sent on the signal line instead of the stop, but there is currently no provision in the Engine to handle position sizes in the alerts or `strategy.*()` function calls it generates. Position sizing in the Engine is all "internal", if you will, i.e., confined to the trade management module and determined by the risk parameters in the indicator's settings.

The Engine's focus being on real-world trading and alerts not permitting dynamic alert messages in v3, dynamic information like position size couldn't be sent through alerts. Now that we have dynamic alerts in Pine, perhaps the next version of the Engine could allow an externalization of position sizes and their inclusion in alert messages.
Hi, can you explain what some real-world use cases for this would be? I'm trying to figure out how this would be useful in addition to the backtesting engine.
PineCoders CryptoGraffer
@CryptoGraffer, The Engine can use signals from 2 sources: the internal strats in the Engine's code or an external indicator generating a plot whose values have special meaning to the Engine. When an external indicator's signal is hooked up to the Engine through the Engine's Inputs (last field a the bottom), and the Engine's strats (entries, exits or filter) are explicitly configured to use the external signal, then the Engine becomes partially controlled by the external indicator. So let's say you already use a study/indicator that you like and it generates what look like good entry signals, but it doesn't have a stop strategy. You could add a plot() to your indicator respecting the protocol described in this indicator to send entry signals to the Engine, and then use any one or a mix of the Engine's pre-coded stop or exit strats to exit your trades. You would also, then, be able to use the Engine to get stats on your trades, or create alerts on whichever conditions you choose, while all the time it's your indicator that's generating the trade entry signals. This allows you to personalize the Engine's behavior without modifying the Engine's code, and is probably more suited to traders not comfortable modifying the Engine's code, or who just want to run preliminary tests without playing into the Engine's code, with the idea of eventually integrating the indicator's logic directly into the Engine's strats if tests are promising.
+3 Reply
bullywigtrader PineCoders
@PineCoders, Hello, and thanks for the above description of how to implement an indicator with your engine. I'm new to PineCode, and wanted to try and connect an indicator as described, but I am having a tough time understanding the order of things. Do I paste information from my indicator into a copy of this code, and rename it for use? ...or am I to copy information from this code into my indicator of choice and resave? Is there a video by chance showing the steps on how to do this? Thanks for any help you may be able to provide. Cheers!
PineCoders bullywigtrader
@bullywigtrader, Hi, As is mentioned in the comments, your indicator goes in between the comments marked with down and up arrows, as is done in the example above with the MACD indicator.

Depending on what you will be wanting to use your indicator's signals for, you will then need to assign values to all or some of the pairs of variables used for filter, entries or exits (which all have boolean values), and stops (float values). For example, if you only use your indicator for entries, then you only need to define the conditions corresponding to the proper states for variables "EnterLong" and "EnterShort". You can then initialize all others to false for booleans and zero for stops.

Once you confirm the signal is plotting when needed, you're ready to connect it to the Engine.
bullywigtrader PineCoders
@PineCoders, Thank you for the speedy response. I love the idea of being able to test out indicators, but realizing I just don't know enough about Pine Script right now to make this happen right now. I will look into learning PS a bit more before going any further. Cheers.
PineCoders bullywigtrader
@bullywigtrader, At its very simplest, for a buy/sell signal only, you can just create a plot that plots +2 to go long and -2 to go short,and hook that up to the Engine.