OPEN-SOURCE SCRIPT

Enhanced Divergence Indicator / Strategy (many oscillators)

Hi, Guys!

So, I am publishing a divergence script, with the ability to choose from many indicators, which is equipped to serve either as a strategy or an indicator (or both).

In my opinion, trading with indicators is not something that can consistently bring you profit. But one of the most effective ways to use an indicator is precisely divergence, since it also contains information about imbalance in the price action. This is still one of the main tasks of technical analysis of price movements.

That is why I decided to make a script public, which I myself use with some additional functions, and here I am publishing the main ones. Most of its elements can be found in other community scripts, but not quite collected in one, and not all. The main difference is that here I provide an opportunity to refine the divergences, by using a filter for the minimum price difference in the two extremes, the minimum difference in the extremes of the indicator and something else that you will not find anywhere in free code. As far as I can, I have also made a filter for the minimum reverse movement of the indicator between its two extremes, which make up the divergence. In the settings, I have called it "Minimum Oscillator Pullback".

I'm not a programmer, so my script is crude and inelegant, but overall it does the job.
I added the ability to use a few more widespread filters, but with some small additional options. For example, you can display a fast and slow moving average, but the good thing is that among them there is also T3 - one of the best MAs for showing a trend. You should keep in mind, however, that this way of using a trend is not very good when using divergences.
I also added an underestimated indicator as a filter, which could be quite effective here. It is the Stochastic Momentum Index. I have given the option to use a different timeframe for it. Usually, in oscillators, overbought and oversold zones are searched for, but here its more effective use is rather the opposite. It actually shows the strength of the trend. That's why I made an option for its reversed use, and in addition, its limit levels are also variable.

There is also a filter for eliminating trading days and/or trading hours.

To make the code more informative, I have provided an opportunity to test the strategy with leverage.
There is an option to use TP and SL.

Regarding closing a position, there are also several options. I have not seen anyone else use it, but with a lot of testing, I have found that the SMI mentioned and used as a filter is a very good indicator for exiting a position. This is one thing. But something even better that I have found and put in the code is the use of standard deviation. Most algo-traders use Average True Range for exit. Well, I have personally found with a lot of historical data that Standard Deviation is actually much more effective for this.
For variety, and also because such trading systems exist, I have added the option to close after a certain number of candles. Here I have also added an additional functionality - closing on a candle in the opposite direction of the open position, after the specified number of candles have passed.
Apart from this, there is also an option to use VWAP for exit.

You will see that there are more than a dozen indicators to choose from for divergence. I have tested dozens, maybe hundreds of others, which at first glance seem very suitable for this. But in practice I have found that they do not really add anything.
Keep in mind that in different timeframes, in different market conditions, and different assets behave differently. For some, some indicators are better, but in another timeframe they are weak.
In addition, the filters for improving divergence sometimes behave strangely (for example, for an oscillator it may be good to accept a negative and very large value for the minimum movement between its extremes). This is because they are not standardized and have different scales. But if you play around with the options enough, you will understand what works for you.

Now I can't think of anything more to say, inside the options things should be relatively clear. If there are adequate questions that I am able to answer (I remind you that I am an amateur), I will write in the comments. I am sure that this code will be useful for many, but do not rely too much on it and do not take risks without testing - both with historical data and paper trading. As you know, in any case, nothing is guaranteed in the future.

I think I missed something important.
When you use the script as an indicator, a line will always appear when there is a divergence. It may seem strange to you on the price movement, but keep in mind that it shows exactly where the extremes of the oscillator, which is not visible on the chart, are. A sign will appear on this line when the divergence meets your other conditions - the filters and enhancements included.

In addition, there are options to limit the divergence indication to a number of candles. In practice, this is necessary and improves the results. It is very important to understand that in order for the script to indicate the last extreme, which we will use to open a position, it must first have determined that we have already gone in the opposite direction. Therefore, the options specify candles to the left, but also candles to the right after the peak, to verify that this is really a peak (or bottom). Many believe that this makes divergences bad for trading, since the signal is actually received later. Well, this is not entirely true and you can check it yourself. You can safely set the right candles to 0 and you will see that there are many false signals. Usually it is best to use 2 candles on the right for a signal and if the divergence is good, they still give a good entry. In certain conditions it is good with just one candle.
DivergenceOscillatorsPivot points and levels

オープンソーススクリプト

TradingViewの精神に則り、このスクリプトの作者は、トレーダーが理解し検証できるようにオープンソースで公開しています。作者に敬意を表します!無料で使用することができますが、このコードを投稿で再利用するには、ハウスルールに準拠する必要があります。 お気に入りに登録してチャート上でご利用頂けます。

チャートでこのスクリプトを利用したいですか?

免責事項