00 Averaging Down Backtest Strategy by RPAlawyer v21FOR EDUCATIONAL PURPOSES ONLY! THE CODE IS NOT YET FULLY DEVELOPED, BUT IT CAN PROVIDE INTERESTING DATA AND INSIGHTS IN ITS CURRENT STATE.
This strategy is an 'averaging down' backtester strategy. The goal of averaging/doubling down is to buy more of an asset at a lower price to reduce your average entry price.
This backtester code proves why you shouldn't do averaging down, but the code can be developed (and will be developed) further, and there might be settings even in its current form that prove that averaging down can be done effectively.
Different averaging down strategies exist:
- Linear/Fixed Amount: buy $1000 every time price drops 5%
- Grid Trading: Placing orders at price levels, often with increasing size, like $1000 at -5%, $2000 at -10%
- Martingale: doubling the position size with each new entry
- Reverse Martingale: decreasing position size as price falls: $4000, then $2000, then $1000
- Percentage-Based: position size based on % of remaining capital, like 10% of available funds at each level
- Dynamic/Adaptive: larger entries during high volatility, smaller during low
- Logarithmic: position sizes increase logarithmically as price drops
Unlike the above average costing strategies, it applies averaging down (I use DCA as a synonym) at a very strong trend reversal. So not at a certain predetermined percentage negative PNL % but at a trend reversal signaled by an indicator - hence it most closely resembles a dynamically moving grid DCA strategy.
Both entering the trade and averaging down assume a strong trend. The signals for trend detection are provided by an indicator that I published under the name '00 Parabolic SAR Trend Following Signals by RPAlawyer', but any indicator that generates numeric signals of 1 and -1 for buy and sell signals can be used.
The indicator must be connected to the strategy: in the strategy settings under 'External Source' you need to select '00 Parabolic SAR Trend Following Signals by RPAlawyer: Connector'. From this point, the strategy detects when the indicator generates buy and sell signals.
The strategy considers a strong trend when a buy signal appears above a very conservative ATR band, or a sell signal below the ATR band. The conservative ATR is chosen to filter ranging markets. This very conservative ATR setting has a default multiplier of 8 and length of 40. The multiplier can be increased up to 10, but there will be very few buy and sell signals at that level and DCA requirements will be very high. Trade entry and DCA occur at these strong trends. In the settings, the 'ATR Filter' setting determines the entry condition (e.g., ATR Filter multiplier of 9), and the 'DCA ATR' determines when DCA will happen (e.g., DCA ATR multiplier of 6).
The DCA levels and DCA amounts are determined as follows:
The first DCA occurs below the DCA Base Deviation% level (see settings, default 3%) which acts as a threshold. The thick green line indicates the long position avg price, and the thin red line below the green line indicates the 3% DCA threshold for long positions. The thick red line indicates the short position avg price, and the thin red line above the thick red line indicates the short position 3% DCA threshold. DCA size multiplier defines the DCA amount invested.
If the loss exceeds 3% AND a buy signal arrives below the lower ATR band for longs, or a sell signal arrives above the upper ATR band for shorts, then the first DCA will be executed. So the first DCA won't happen at 3%, rather 3% is a threshold where the additional condition is that the price must close above or below the ATR band (let's say the first DCA occured at 8%) – this is why the code resembles a dynamic grid strategy, where the grid moves such that alongside the first 3% threshold, a strong trend must also appear for DCA. At this point, the thick green/red line moves because the avg price is modified as a result of the DCA, and the thin red line indicating the next DCA level also moves. The next DCA level is determined by the first DCA level, meaning modified avg price plus an additional +8% + (3% * the Step Scale Multiplier in the settings). This next DCA level will be indicated by the modified thin red line, and the price must break through this level and again break through the ATR band for the second DCA to occur.
Since all this wasn't complicated enough, and I was always obsessed by the idea that when we're sitting in an underwater position for days, doing DCA and waiting for the price to correct, we can actually enter a short position on the other side, on which we can realize profit (if the broker allows taking hedge positions, Binance allows this in Europe).
This opposite position in this strategy can open from the point AFTER THE FIRST DCA OF THE BASE POSITION OCCURS. This base position first DCA actually indicates that the price has already moved against us significantly so time to earn some money on the other side. Breaking through the ATR band is also a condition for entry here, so the hedge position entry is not automatic, and the condition for further DCA is breaking through the DCA Base Deviation (default 3%) and breaking through the ATR band. So for the 'hedge' or rather opposite position, the entry and further DCA conditions are the same as for the base position. The hedge position avg price is indicated by a thick black line and the Next Hedge DCA Level is indicated by a thin black line.
The TPs are indicated by green labels for base positions and red labels for hedge positions.
No SL built into the strategy at this point but you are free to do your coding.
Summary data can be found in the upper right corner.
The fantastic trend reversal indicator Machine learning: Lorentzian Classification by jdehorty can be used as an external indicator, choose 'backtest stream' for the external source. The ATR Band multiplicators need to be reduced to 5-6 when using Lorentz.
The code can be further developed in several aspects, and as I write this, I already have a few ideas 😊
Averagingdown
Average Down [Zeiierman]AVERAGING DOWN
Averaging down is an investment strategy that involves buying additional contracts of an asset when the price drops. This way, the investor increases the size of their position at discounted prices. The averaging down strategy is highly debated among traders and investors because it can either lead to huge losses or great returns. Nevertheless, averaging down is often used and favored by long-term investors and contrarian traders. With careful/proper risk management, averaging down can cover losses and magnify the returns when the asset rebounds. However, the main concern for a trader is that it can be hard to identify the difference between a pullback or the start of a new trend.
HOW DOES IT WORK
Averaging down is a method to lower the average price at which the investor buys an asset. A lower average price can help investors come back to break even quicker and, if the price continues to rise, get an even bigger upside and thus increase the total profit from the trade. For example, We buy 100 shares at $60 per share, a total investment of $6000, and then the asset drops to $40 per share; in order to come back to break even, the price has to go up 50%. (($60/$40) - 1)*100 = 50%.
The power of Averaging down comes into play if the investor buys additional shares at a lower price, like another 100 shares at $40 per share; the total investment is ($6000+$4000 = $10000). The average price for the investment is now $50. (($60 x 100) + ($40 x 100))/200; in order to get back to break even, the price has to rise 25% ($50/$40)-1)*100 = 25%, and if the price continues up to $60 per share, the investor can secure a profit at 16%. So by averaging down, investors and traders can cover the losses easier and potentially have more profit to secure at the end.
THE AVERAGE DOWN TRADINGVIEW TOOL
This script/indicator/trading tool helps traders and investors to get the average price of their position. The tool works for Long and Short and displays the entry price, average price, and the PnL in points.
HOW TO USE
Use the tool to calculate the average price of your long or short position in any market and timeframe.
Get the current PnL for the investment and keep track of your entry prices.
APPLY TO CHART
When you apply the tool on the chart, you have to select five entry points, and within the setting panel, you can choose how many of these five entry points are active and how many contracts each entry has. Then, the tool will display your average price based on the entries and the number of contracts used at each price level.
LONG
Set your entries and the number of contracts at each price level. The indicator will then display all your long entries and at what price you will break even. The entry line changes color based on if the entry is in profit or loss.
SHORT
Set your entries and the number of contracts at each price level. The indicator will then display all your short entries and at what price you will break even. The entry line changes color based on if the entry is in profit or loss.
-----------------
Disclaimer
Copyright by Zeiierman.
The information contained in my Scripts/Indicators/Ideas/Algos/Systems does not constitute financial advice or a solicitation to buy or sell any securities of any type. I will not accept liability for any loss or damage, including without limitation any loss of profit, which may arise directly or indirectly from the use of or reliance on such information.
All investments involve risk, and the past performance of a security, industry, sector, market, financial product, trading strategy, backtest, or individual’s trading does not guarantee future results or returns. Investors are fully responsible for any investment decisions they make. Such decisions should be based solely on an evaluation of their financial circumstances, investment objectives, risk tolerance, and liquidity needs.
My Scripts/Indicators/Ideas/Algos/Systems are only for educational purposes!