INVITE-ONLY SCRIPT

TTP Grid Backtester

Updated
This pine script strategy allows to backtest Grid bots.

This initial version offers the following features:
- Set the top and bottom limits of the grid
- Plots the average position price, realised and unrealised profits
- Set the value to invest
- This script is a strategy so you can check each individual buy/sell, stats and all included with strategies


What does it do:
- Depending where is located the initial close price relative to the grid (above, below, inside) it will buy for as many levels are above the price.
- It will disable a level that recently filled an order (in the way grids bot do)
- When the grid starts it will disable the closest grid level
- It places limit orders in the active levels and many levels can be filled in a single candle
- You can activate recalculate on each order filled, which will allow to fill further needed orders if the price swings up and down crossing multiple times multiple grid levels but I have found that doing this can compromise the accuracy of the price used on those levels (there are minor gaps between the filled price and the original level price)

How to use it:
- When you add this strategy to the chart you will be asked to select the top and bottom limits of the grid
- Notice you can always select the strategy in the chart and drag and drop the limits to adjust the grid
- Once the grid is in place, you can use either lower chart timeframes for higher accuracy of the stats, or higher timeframes if you want to privilege longer periods of testing

How to set the correct "initial capital"
In order to prevent getting wrong stats you need to make sure you are using the correct initial capital. If you put less than what you are actually using your results will be over inflated. If you set an initial capital below what the bot requires, your results will be smaller than they should.

- If you want to use exactly 100% of the capital for the grid use then first select what amount per level you want to use. Set this value in the settings of the indicator (if you are trading BTCUSD pair, how much BTC you want to use per level, 0.01 for example).
- Once you have set this value, then open the TradingView "Data Window" to be able to visualise the calculation of cash required to run the grid that the strategy is giving you. In our example with BTCUSD this value will be given in USD.
- Enter this amount in the "Properties" tab, "Initial Capital". If you enter the exact amount you will be using all for the grid usage.
- The grid first action will be to buy 0.01 for each level that is above the current price in the first candle of the chart. If there are no levels above it won't do any initial investment.
- The rest of the cash will be use to buy levels below if the price goes to the lower range of the grid later

Intention of this script
I built this script to help me understand better how grid bots work.
Understanding the flow of realised vs unrealised profits in a grid can help me understand why sometimes even if you are in red on unrealised profits, you can still compensate with realised profits and many other tricky scenarios with grids.
Sometimes I'm running a grid bot and would like to simulate how much better (or worst) it would have been to run it using different limits.

Future work and ideas
Initially I'm focusing on confirming that the grid behaves correctly and that the stats are as accurate as possible.
That is the first priority.
Once I feel more confident with the accuracy I will consider adding some of the following ideas (not in any particular order):
- Table with stats including: price of each level, times the level filled an order, times it was use for selling/buying, etc. Time it took to become in realised profit. Comparison against profits from buy & hold.
- Trailing TP/SL
- Entry/exit price
- Trading time window: only trade between the specified dates/times
- Alerts
Release Notes
- Added time window to restrict backtesting only within the given start and end dates/times
- Fixed issue that levels below initial price were trading with 0.01 ignoring the user parameter for funds per level
Release Notes
- Improved accuracy
- Graphic stats plot buy and hold for easy comparison
- Grid profits are drawn above the top grid level to reduce overlap with price
- Overall color and layout improvements
- Once the strat reaches the end time it closes all positions (selling all unrealised profits)
- TradingView Data window shows further stats
Release Notes
- Improved stats in the TradingView "data window" panel
- If period and levels are close to the entry price, disable level that is closer to the price (what a real grid bot does to prevent over buying a level)
- Extra chart and layout tweaks
- Remove plots from area outside the time trading window
Release Notes
NEW mode: calculate amount per level: When this option is enabled the value of 'amount spent per level' will be ignored and we will calculate one based on the provided initial capital value, the number of levels and the initial price at the time the grid starts. By enabling this mode you are using 100% of the initial capital for the grid. The calculated amount per level is shown in the data window.

Fix bug: there was a bug that the grid would stop trading after two orders were filled in the previous candle: one above and one below the current price. If this happens now we assume the most recent to get filled was the one closer to the last close price. There's no way to guarantee this but its the best compromise solution.
Release Notes
- Improved better calculation of levels needed
- Support for non-USD pairs, now you can use automated level calculation on pairs like ETHBTC
- PNL chart scale. Use this value to adjust the scale of the in-chart PNL plots so they are in a similar scale to the grid. This is very useful for pairs like ETHBTC where the value of the token is fractional and the profits are not causing the profits to be too large to be next to the grid. Now you can use this parameter to keep everything in visible range.
Release Notes
Grid not starting when price above range issue was fixed.
Release Notes
Added upper and lower stop loss matching the new grid features offered by 3commas.
Support for stop loss action: stop bot and buy/sell base.
Release Notes
Show SL levels only when enabled.
You can drag and drop SL levels when enabled.
Release Notes
- Fixed issue that was causing grid levels to become not visible in the chart
- Added option to customise the grid colors.
- Added instructions table (you can hide them from the script settings)
- Minor changes: start/end time moved towards top of the form, by default stats are disable to keep chart cleaner, fixed pine script 5 warnings.
backtestbacktesterbacktestingeducationalGRIDgridbotgridsgridtradingstrategy

Invite-only script

Access to this script is restricted to users authorized by the author and usually requires payment. You can add it to your favorites, but you will only be able to use it after requesting permission and obtaining it from its author. Contact TheTradingParrot for more information, or follow the author's instructions below.

TradingView does not suggest paying for a script and using it unless you 100% trust its author and understand how the script works. In many cases, you can find a good open-source alternative for free in our Community Scripts.

Author's instructions

You can get access to this strategy here: https://thetradingparrot.com/tiers

Want to use this script on a chart?

Warning: please read before requesting access.

Join our membership to gain access to the private scripts.
thetradingparrot.com
Also on:

Disclaimer