"Grid trading is most commonly associated with the foreign exchange market. Overall the technique seeks to capitalize on normal price in an asset by placing buy and sell orders at certain regular intervals above and below a predefined base price."
This strategy is best used on sideways markets, without a definitive up or down major trend. Because it doesn't rely on huge vertical movement, this strategy is great for small timeframes. It only goes long. I've set initial_capital to 100 USD. default_qty_value should be your initial capital divided by your amount of grid lines. I'm also assuming a 0.1% commission per trade.
Here's the basic algorithm:
- Create a grid based on an upper-bound (strong resistance) and a lower-bound (strong support)
- Grid lines are spaced evenly between these two bounds. (I recommend anywhere between 5-10 grid lines, but this script lets you use up to 15. More gridlines = more/smaller trades)
- Identify nearest gridline above and below current price (ignoring the very closest grid line)
- If price crosses under a near gridline, buy and recalculate near gridlines
- If price crosses over a near gridline, sell and recalculate near gridlines
- Trades are entered and exited based on a FIFO system. So if price falls 3 grid lines (buy-1, buy-2, buy-3), and subsequently crosses above one grid line, only the first trade will exit (sell-1). If it falls again, it will enter a new trade (buy-4), and if it crosses above again it will sell the original second trade (sell-2). The amount of trades you can be in at once are based on the amount of grid lines you have.
This strategy has no built-in stop loss! This is not a 'set-it-and-forget-it" script. Make sure that price remains within the bounds of your grid. If prices exits above the grid, you're in the money, but you won't be making any more trades. If price exits below the grid, you're 100% staked in whatever you happen to be trading.
This script is more complicated than my last one, but should be more user friendly. Make sure to correctly set your lower-bound and upper-bound based on strong (the default values for these are probably going to be meaningless). If you change your "Grid Quantity" (amount of grid lines) make sure to also change your 'Order Size' property under settings for proper test results (or default_qty_value in the strategy() declaration).
My old version had a handful of bugs in it. I've optimized that logic in addition to adding a few new features
The basic algorithm is slightly tweaked. Buy when you cross under a grid line, and sell when you cross over a grid line. But, instead of dealing with orders in a queue, I'm mapping orders to the Grid Line Array. If you buy on line #1, you will sell that order on line #2, no matter what price does in between. If you buy on line #2, you will sell on line #3. You can only have one open order per grid line. You can't sell on the bottom grid line, and you can't buy on the top grid line.
A grid strategy is traditionally done with limit orders, but this uses market orders. I simply haven't messed with limit orders in pine script yet, but I imagine it would be a pretty small change. Perhaps in a future update I'll add the option.
Unlike my previous version, order quantity is now calculated automatically based on how many grid lines you have.
Similar to my previous version, this script still plots 'near grid lines'. The blue line represents your closest 'buy', while the purple line represents your closest 'sell'. My updated logic doesn't actually use these values any more, but I decided to leave them in for visual aid.
Grid line quantity is capped at 15. This was a fairly arbitrary decision. To increase this limit, you'll need to change the maxval of the input, add plotting code for more lines, and adjust the pyramiding setting
Instead of setting grid bounds manually, you now have to option to set them automatically. These auto bounds will follow price action, turning this strategy from very hands-on to quite hands-off. A 'manual grid' will theoretically* never lose money (excluding slippage and commission), but there's no safety if price falls below your grid, and no trading if price rises above your grid. An 'auto grid' will likely lose money as price trends down, but you never have to worry about price exiting your grid.
I've attached a screen shot of this strategy with Auto Bounds turned on
Profit factor is much lower (~2.8) and drawdown is much higher (~12%), but using auto bounds comes with a sense of security if you're worried about price going beyond your manually set bounds. It adds a sort of built-in stop loss functionality because it will happily take a small loss if the grid shifts down, alleviating your risk and allowing for more trades
Settings for Auto Bound include:
-Source: should bounds be based on recent High & Low, or based on a Simple Moving Average
-Lookback: How far back to look for Highs and Lows -OR- length of simple moving average
-Deviation: if deviation is 0.05 (5%) and Source = High & Low, then add 5% to the upperbound, and minus 5% from the lower bound. If Source = Average, then the upperbound will be 5% up from the SMA, and the lowerbound will be 5% below the SMA. Deviation does not have to be positive if Source = High & Low; you can narrow your bounds instead of widening them. Deviation must be positive if Source = Average.
That's about it I think. I'm probably going to develop a modified version of this specifically geared towards automating with specific third-party services. This would involve some more logic in configuring alert messages. As always, feel free to steal/modify/improve this script; just let me know in the comments what you do!
* my back testing with manual bounds shows a ~82% success rate (should be 100%). There's something going on with the entry and exit id's (it looks like a trade opened with one id is exited with another id). I'm not sure exactly what's going on here in my list of trades, but all of the trade signals are showing up on the chart where they should be with the correct id's. Please leave any insights in the comments.
Maintenance - removed the property from my plotting code; using instead
Bug Fix - added close_entries_rule="ANY" to line up my trade entry/exit id's. This gives me a 100% profit percentage with a manual grid. Of course, this is all fun and games until price falls too far below that bottom grid line, or hits the top once and never again. Keep an eye out if you use manual grid bounds!
Maintenance - removed the transp property from my plotting code; using color.rgb() instead
Dans le plus pur esprit de TradingView, l'auteur de ce texte l'a publié en open-source, afin que les traders puissent le comprendre et le vérifier. Bravo à l'auteur ! Vous pouvez l'utiliser gratuitement, mais la réutilisation de ce code dans une publication est régie par les règles internes. Vous pouvez le mettre en favori pour l'utiliser sur un graphique.
At any rate, thanks for the amazing work and allowing us newbs to use it for free.
And this script is as much yours as it is mine; we're all dipping our feet here in the same communal pool of knowledge.