I got an email saying my description of this script wasn't good enough. So i'll try and add some more detail on this update.
This particular update turns the Hits Required setting into Bars Required, which I think is far more useful. It also calculates the average number of trades per month you would end up having to perform.
Now for some overall details on what this script does:
This script is designed to allow the user to easily perform a variety of back tests allowing them to figure out how any given trading theory they have would have worked in the past.
The big limitation this script has is that each indicator it looks at has to produce a fixed range of numbers, it cannot adjust or follow the price like the MA or Bollinger Bands. Those indicators must first be converted into a fixed range of numbers by some other script.
Another limitation is that this script can only use indicators already present on the layout. I was unable to get pinescript 6 to work which seems to have the ability to reference indicators which are not present.
This script has settings that are divided into 5 sections. The first 4 sections are copies of each other. They consist of a:
- Source (which is a data feed from another indicator in the layout)
- Overbought and Oversold levels (which are numeric values that tell the script to trip a buy or sell signal when the indicator crosses over). It is important that the value goes just beyond the configured value to trigger the action, just hitting it is not enough.
- Bars Required (which tell the script to only trigger the signal if the value has remained crossed for a certain number of bars. By setting this value to 0 you can effectively turn off, or tell the script to ignore the values for that Threshold.
Those settings are repeated 4 times, and they work as an AND sequence. So all of your configured Sources have to cross their configured values before a buy or sell signal is triggered.
The last section in the settings is the Backtest configuration. While buy and sell signals are nice, it is even better if you can test strategies using those signals. So this section allows you to try out a few different buy and sell strategies.
- Starting Equity: The amount of money you plan to start with for this trading bag.
- Position Size: a percentage of the equity that will be used for the trades. This percent is used in different manners depending on the choice made in Buy-In Type.
- Buy-In Type: currently there are 4 different options here, Original, Available, Stepped, Multiple
- - Original: Makes a buy using the configured percentage of the starting equity, that buy amount never changes no matter how much money is made.
- - Available: just like original, except that whenever the cash on hand is greater than we started with, it adjusts the buy amount to be the configured percentage of that new greater amount.
- - Stepped: just like Available except that it adds the configured percentage of itself to itself if the next buy is lower than the last one, and it subtracts that from itself if the next buy is greater than the last one. However it never buys less than the configured percentage.
- - Multiple: just liked Stepped except that instead of adding and subtracting a percentage of itself to itself, it adds and subtracts a multiple of the original buy amount.
- Start Date / End Date: the date range you want to flash buy/sell signals for and perform a back test on. Useful if the asset has changed how it performs and you want to target it's performance during a specific period of its history.
- Timing: Begin/End indicates whether you want the buy/sell signals to trigger as soon as all your conditions are met, or at the end of the period during which they were met.
- Show Labels: None/USD/Asset, in addition to the buy and sell signals, you can display when the backtest actually had money left and took action on one of those signals, as well as the amount of the asset purchased, or the dollars used to make the purchase. This really helps make it clear what the backtest is doing.
There are no configurable options for sells. The are processed in a FIFO (first in first out) manner. So the amount of the asset purchased is the amount that gets sold, in order, for each sell signal.
The screenshot shows the resulting table of data the back test produces in addition to the buy/sell flags on the graph itself. In this table you have:
- Profit: The cash profit made from successful trades.
- ROI: The total return on investment made throughout the entire tested date range.
- ROI/yr: The amount of ROI averaged by year. So the ROI is divided by the number of days tested, then multiplied by 365 to display the equivalent yearly amount.
- Win%: since sells are performed FIFO, the scripts tracks if the sell price is greater than the buy price for that transaction. This is a percentage of how often that buy/sell pair was profitable.
- Buys/Sells: The number of buys and sells performed over the tested period. Also includes a averaged calculation for the number performed per month to help visualize the effort required.
- Initial: the configured starting equity, also the number of shares of the asset that could have been purchased on the very first buy flag if the entire amount had been used. For exponential assets it helps determine if the strategy is better than just buying dips and holding over time.
- Open: The value of the shares still open that have not yet had sell signals.
- Cash: The amount of cash currently available to be placed on the next trade.
- Total: The total value of the bag at the end of the test. Also includes a calculation of how many shares that total value could purchase at the current share price.
Unfortunately this back test does not handle failure well, so if you have a failing strategy it will just run out of money. The values on the resulting chart may also be a bit misleading if the strategy began failing but has not yet sold the asset.
This backtest was inspired by the one written by Invest Answers, and seeks to improve on that original idea.