OPEN-SOURCE SCRIPT

RSI+PA+DCA Strategy

Aktualisiert
Dear Tradingview community,

This RSI based trading strategy is created as a training exercise. I am not a professional trader, but a committed hobbyist. This not a finished trading strategy meant for trading, but more a combination of different trading ideas I liked to explore deeper. The aim with this exercise was to gain more knowledge and understanding about price averaging and dollar cost averaging strategies. Aside that I wanted to learn how to program a pyramiding strategy, how to plot different order entry layers and how to open positions on a specific time interval.

In this script I adapted code from a couple of strategy examples by Coinrule. Who wrote simple and powerful examples of RSI based strategies and pyramiding strategies.
Optimized RSI Strategy - Buy The Dips (by Coinrule)


Also the HOWTO scripts shared by vitvlkv were very helpful for this exercise. In the script description you can find all the sources to the code.

A PA strategy could be a helpful addition to ease the 'stress-management to buy when price drops and resolution in selling when the price is rising' (Coinrule).

The idea behind the strategy is fairly simple and is based on an RSI strategy of buying low. A position is entered when the RSI and moving average conditions are met. The position is closed when it reaches a specified take profit percentage. As soon as the first the position is openend multiple PA (price average) layers are setup based on a specified percentage of price drop. When the price crosses the layer another position with somewhat the same amount of assets is entered. This causes the average cost price (the red plot line) to decrease. If the price drops more, another similar amount of assets is bought with another price average decrease as result. When the price starts rising again the different positions are separately closed when each reaches its specified take profit. The positions can be re-openend when the price drops again. And so on. When the price rises more and crosses over the average price and reached the specified take profit on top of it, it closes all the positions at once and cancels all orders. From that moment on it waits for another price dip before it opens a new position.

Another option is to activate a DCA function that opens a position based on a fixed specified amount. It enters a position at the start of every week and only when there are already other positions openend and if the current price is below the average price of the position. Like this buying on a time interval can help lowering the average price in case the market is down.

I read in some articles that price averaging is also called dollar cost averaging as the result is somewhat the same. Although DCA is really based on buying on fixed time intervals. These strategies are both considered long term investment strategies that can be profitable in the long run and are not suitable for short term investment schemes. The downturn is that the postion size increases when the general market trend is going down and that you have to patiently wait until the market start rising again.

Another notable aspect is that the logic in this strategy works the way it does because the entries are exited based on the FIFO (first in first out) close entry rule. This means that the first exit is applied to the first entry position that is openend. In other words that when the third entry reaches its take profit level and exits, it actually exits the first entry. If you take a close look in the 'List of Trades' of your Strategy Tester panel, you can see that some 'Long1' entries are closed by an 'Exit 3' and not by an 'Exit 1'. This means that your trade partly loses, but causes a decrease in average price that is later balanced out by lower or repeated entering and closing other positions. You can change this logic to a real sequential way of closing your entries, but this changes the averaging logic considerably. In case you want to test this you need to change, in this line in the strategy call 'close_entries_rule = "FIFO"', the word FIFO to ANY.

In the settings you can specify the percentage of portfolio to use for each trade to spread the risk and for each order a trading fee of 0.075% is calculated.


Versionshinweise
Added a second Take Profit input option specifically for the Exit all Take profit that gets activated a specified % above the price average. (white line).
As bobdenic suggested, the Exit All Take Profit is plotted (red line).
The Moving Average is changed into a green line.
Versionshinweise
Added close_entries_rule = "FIFO" to the strategy declaration. I forgot to add it in the first version. If you change FIFO to ANY it will change the order of closing the entries. Meaning it will close Entry 1 with Close 1, Entry 2 with Close 2, etc. As described above. Use it as you like best.
Cleaned up the script a bit.
Oscillators

Open-source Skript

Ganz im Sinne von TradingView hat dieser Autor sein/ihr Script als Open-Source veröffentlicht. Auf diese Weise können nun das Script auch andere Trader verstehen und prüfen. Vielen Dank an den Autor! Sie können das Script kostenlos verwenden. Die Nutzung dieses Codes in einer Veröffentlichung wird in unseren Hausregeln reguliert. Sie können es als Favoriten auswählen, um es in einem Chart zu verwenden.

Möchten Sie dieses Skript auf einem Chart verwenden?

Haftungsausschluss