Without martingale or averaging down, automated trading wouldn’t have been so popular over the internet in the last years. Promised of eternal money returns without any cons is of course not real. However, learn to code “grid” orders is educational and that’s the purpose of this article.
It surprised me that no one has ever shared any averaging down (also called grid) trading strategy on the website so far. Since this is something very interesting in terms of coding techniques, I decided to write this blog article to share simple codes and thoughts about this so popular way of trading around the internet.
I have seen so much trading strategies dealing with averaging down / grid other the last years, and of course coded myself a lot of them in many different ways that you can call me a “guru” 🙂 In fact, grid strategies are popular and interesting in a way that any loosing or not so good strategy can be turned into a money machine.. Almost like martingale position sizing, averaging down never offer to loose a trade (in its pure and simple version) and that is the main reason you are always a winner while trading it.
Have you ever seen this kind of equity curve on the internet? Charming isn’t it?
What is averaging down trading? Why is it called “grid”?
Recover loosing position(s) not closed is the main purpose of averaging down.
Let’s take an example of a simple moving average crossover trading strategy:
In this picture, we are trading 3 EMA vs 10 EMA, we took a short trade on the first bearish crossover that occurred at 9:00AM..
We don’t have any stoploss on this trade and it is not going in the good direction. Since we are convinced here that the trend is bearish and many other traders are thinking the same as us, we decide to add another sell order the first time we’ll see another red Heikin Ashi candlestick.. let’s do it!
_ first position opened @ 10440
_ second order opened @ 10460
if the price go down to at least to (10440+10460)/2 = 10450 points, we are safe.. The first order loose 10 points, while the second one gain 10 points.
.. but in our example the price didn’t go down and so we add another order @10463:
We are building a grid of sell orders, to trap the price where we want it to, at our orders’ average open price.
Price didn’t go south in our example, a fourth trade opened @ 10470 with a new average open price of the whole orders basket @ 10468.25. Finally after some hesitation, price drop down to the average price and all orders could have been exited for a compound little profit or breakeven.
We have successfully managed the 1st loosing trade .. while added 2 other loosing ones in the same row we have also dig into our money balance with a large drawdown .. but hey! we survived! 🙂
When to average down the trades?
As you have probably now understand, this orders management technique can be added to any strategy with also any way of order re-enter you can imagine : fixed points step, dynamic one, new signals, support/resistance etc.
There is no pre-defined rules of adding orders, that’s up to your trading style and your ability to spot good re-entry point when price will tend to reverse. But something I learned so far, it is that the more orders in the grid, the easier to return to the average price of the orders’ basket.
Grid / averaging down codes for prorealtime
You’ll find below the code of the simple example strategy I explained in the article. This is a simple “framework” you can extend in many ways in your own strategy. So feel free to explore, discuss and share what you found in the forums or/in the automated trading codes library of the website.
To build the code, we only need to know if we are or not already on market with shortonmarket, if we are above the last trade entry with tradeprice, and monitor the average open price with positionprice.
defparam cumulateorders = true
EMA3 = exponentialaverage
EMA10 = exponentialaverage
if barindex>1 then
// first order (EMA cross)
if not shortonmarket and EMA3 crosses under EMA10 then
SELLSHORT 1 SHARE AT MARKET
// close position with bullish EMA cross only if we are in profit and we are not already averaging down (1 position only)
if countofposition=-1 and haclose<tradeprice and EMA3 crosses over EMA10 then
EXITSHORT AT MARKET
// averaging down
if shortonmarket and haopen>haclose and haopen<haclose and haclose>tradeprice then
SELLSHORT 1 SHARE AT MARKET
// monitor the average price of whole orders and close them accordingly
if shortonmarket and haclose<positionprice and countofposition<-1 then
EXITSHORT AT MARKET
Like always, you can download the code in the attached file at the end of the blog post.
Never loose again, great! Where is the limit of averaging down?
This is dangerous, like any other martingale system, with averaging down, you are trying to beat the market and that’s not possible at all. If time were infinite and if you could have infinite money on your trading account, that could be possible .. but stop dreaming life is not a scifi movie! 🙂
The only con of grid orders is that you have to face up with large equity drawdown with many lines of loosing orders in your trading console. Whatever the strategy you applied to enter at first signal and average down, if you don’t offer to loose, you’ll met this con.
Through my trading programming services company, I came across many customers that asked this kind of trades management in their strategies. I even know some people that make a living with grid strategies, so I believe (and I’m sure it is the case for any automated trading system), that you need to rely on your strategy if you decide to let a code script deciding for you when and how to enter the market. So it’s up to everyone and I’m not here to give you secret weapons to be rich.. because there are no secret weapons!