OPEN-SOURCE SCRIPT

Backtest any Indicator v5

Happy Trade,

here you get the opportunity to backtest any of your indicators like a strategy without converting them into a strategy. You can choose to go long or go short and detailed time filters. Further more you can set the take profit and stop loss, initial capital, quantity per trade and set the exchange fees. You get an overall result table and even a detailed, scroll-able table with all trades. In the Image 1 you see the provided info tables about all Trades and the Result Summary. Further more every trade is marked by a background color, Labels and Levels. An opening Label with the trade direction and trade number. A closing Label again with the trade number, the trades profit in % and the total amount of $ after all past trades. A green line for the take profit level and a red line for the stop loss.

快照
Image 1


Example

For this description we choose the Stochastic RSI indicator from TradingView as it is. In Image 2 is shown the performance of it with decent settings.
Timeframe=45, BTCUSD, 2023-08-01 - 2023-10-20
Stoch RSI: k=30, d=40, RSI-length=140, stoch-length=140
Backtest any Indicator: input signal=Stoch RSI, goLong, take profit=9.1%, stop loss=2.5%, start capital=1000$, qty=5%, fee=0.1%, no Session Filter

快照
Image 2


Usage

1) You need to know the name of the boolean (or integer) variable of your indicator which hold the buy condition. Lets say that this boolean variable is called BUY. If this BUY variable is not plotted on the chart you simply add the following code line at the end of your pine script.
For boolean (true/false) BUY variables use this:



And in case your script's BUY variable is an integer or float then use instate the following code line:



2) Probably the name of this BUY variable in your indicator is not BUY. Simply replace in the code line above the BUY with the name of your script's trade condition variable.

3) Save your changed Indicator script.

4) Then add this 'Backtest any Indicator' script to the chart ...

5) and go to the settings of it. Choose under "Settings -> Buy Signal" your Indicator. So in the example above choose <Stoch RSI: K>.
The form is usually: '<name of your indicator> : BUY'. Then you see something like Image 2

6) Decide which trade direction the BUY signal should trigger. A go Long or a go Short by set the hook or not.

Now you have a backtest of your Indicator without converting it into a strategy. You may change the setting of your Indicator to the best results and setup the following strategy settings like Time- and Session Filter, Stop Loss, Take Profit etc. More of it below in the section Settings Menu.

Appereance

In the Image 2 you see on the right side the List of Trades. To scroll down you go into the settings again and decrease the scroll value. So you can see all trades that have happened before. In case there is an open trade you will find it at the last position of the list.

Every Long trade is green back grounded while Short trades are red.

Every trade begins with a label that show goLong or goShort and its number. And ends with another label again with its number, Profit in % and the resulting total amount of cash.

If activated you further see the Take Profit as a green line and the Stop Loss as a orange line. In the settings you can set their percentage above or below the entry price.

You also see the Result Summary below. Here you find the usual stats of a strategy of all closed trades. The profit after total amount of fees, amount of trades, Profit Factor and the total amount of fees.

Settings Menu

In the settings menu you will find the following high-lighted sections. Most of the settings have a question mark on their right side. Move over it with the cursor to read specific explanation.

Input Signal of your Indicator: Under Buy you set the trade signal of your Indicator. And under Target you set the value when a trade should happen. In the Example with the Stochastic RSI above we used 20. Below you can set the trade direction, let it be go short when hooked or go long when unhooked.

Trade Settings & List of Trades: Take Profit set the target price of any trade. Stop Loss set the price to step out when a trade goes the wrong direction. Check mark the List of Trades to see any single trade with their stats. In case that there are more trades as fits in the list you can scroll down the list by decrease the value Scroll.

Time Filter: You can set a Start Time or deactivate it by leave it unhooked. The same with End Time.

Session Filter: here you can choose to activate it on weekly base. Which days of the week should be trading and those without. And also on daily base from which time on and until trade are possible. Outside of all times and sessions there will be no new trades if activated.

Invest Settings: here you can choose the amount of cash to start with. The Quantity percentage define for every trade how much of the cash should be invested and the Fee percentage which have to be payed every trade. Open position and closing position.

Other Announcements

This Backtest script don't use the strategy functions of TradingView. It is programmed as an indicator. All trades get executed at candle closing. This script use the functionality "Indicator-on-Indicator" from TradingView.

Conclusion

So now it is your turn, take your promising indicators and connect it to that Backtest script. With it you get a fast impression of how successful your indicator will trade. You don't have to relay on coders who maybe add cheating code lines. Further more you can check with the Time Filter under which market condition you indicator perform the best or not so well. Also with the Session Filter you can sort out repeating good market conditions for your indicator. Even you can check with the GoShort XOR GoLong check mark the trade signals of you indicator in opposite trade direction with one click. And compare your indicators under the same conditions and get the results just after 2 clicks. Thanks to the in-build fee setting you get an impression how much a 0.1% fee cost you in total.

Cheers
backtestingPine utilitiesstatisticsstrategyuniversal

开源脚本

本着真正的TradingView精神,此脚本的作者已将其开源,以便交易者可以理解和验证它。向作者致敬!您可以免费使用它,但在出版物中重复使用此代码受网站规则约束。 您可以收藏它以在图表上使用。

想在图表上使用此脚本?

免责声明