Thursday, May 22, 2025

TradingView Backtest Script

 Like many people I have tried my hand at dabbling in the stock market over the years, and I have found that I am not very good at it.  For years I dreamed about have a predictive program that just told me what to do, but the only ones in existence were very expensive, far out of reach of the normal person.

Enter Trading View and Grok.

I was able to write a script that could go back in time on any given ticker, and replay a lot of different theory ideas I had to see if they would actually make money over time.



I know this script has it's limitations, and I had a few ideas on how to make it even more flexible.  But for a free tool this is already invaluable for most people to test out their ideas.  You can think of it like paper trading, only instant.  You plug in your "plan" for what signals you want to use for trading, set a date range.  Tell it how much money you want to play with, and what calculations you want to use for how much to buy/sell on each interaction, and it will instantly tell you how well you would do.

The biggest limitation is that the TradingView free account only allows a total of 2 indicators at a time.  And this script is not an indicator itself, it relies on other indicators to help it do the more complex calculations it then makes buy/sell decisions on.

Fortunately there are a lot of free indicators out there you can use to generate values, you can then use my "Multi Indicator Buy/Sell with Backtest" script/tool to see how well the indicator you have chosen does over time.  It's a bit painful changing out indicators one after another trying to find a decent one, so most people should start with the basics like RSI or Fear and Greed.

If you have a complex custom idea for a trend you would like to generate, with Grok it is now pretty simple to create your own indicator that you can use with this tool.  In fact the indicator you see being used in the screenshot for the tool is a custom volatility indicator I had Grok write for me.  It calculates the percent change over a time period that the underlying asset has done; the idea is that you trade during period of high volatility.  So if the value spikes up you sell, if it crashes down you buy; for assets that you are confident will mean revert this is a pretty effective approach.



No comments: