FOR 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 😊
In pieno spirito TradingView, l'autore di questo script lo ha pubblicato open-source, in modo che i trader possano comprenderlo e verificarlo. Un saluto all'autore! È possibile utilizzarlo gratuitamente, ma il riutilizzo di questo codice in una pubblicazione è regolato dal nostro Regolamento. Per aggiungerlo al grafico, mettilo tra i preferiti.
Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.