OPEN-SOURCE SCRIPT

Peak/Valley Estimation

Early Signal

Estimating the Peaks and Valleys or extrema of the price is one of the best way to catch up early movements of a trend. Of course there is no perfect way to do so, if we want a perfect estimation of peaks and valleys then we must use a non causal indicator (repainting), if we want a causal indicator (non repainting) then we will need to tradeoff accuracy for allowing our indicator to be causal, its always a matter of tradeoff at the end when trying to have a desired effect (smoothness/lag for filters).Our indicator is causal, it wont repaint but the accuracy will depend on various parameters.

In order to detect peaks and valleys in a certain period we must detrend the price, this mean subtracting it by its moving average. We take the absolute value of this result and we filter it with a local linear regression (LSMA) in order to eliminate noise, then we make the assumption that the highest of our result is or a peak or a valley of the price, so we divide our detrended calculation by its highest and we get a scaled result. Lets call this final result the peak index.

Parameters

There are 3 parameters in this indicator, a length parameter who control the period of the highest mentioned above, a smooth parameter who smooth our detrended price, and finally a mod parameter who select the trigger method for estimating a peak/valley.

Here are how mods work :

  • mod = 1 : when the peak index is equal to 1 and the previous value is not equal to 1 then we have a peak/valley. Its the fastest of the 3 mods but the one with less accuracy.
  • mod = 2 : when the peak index crossunder 0.8 then we have a peak/valley. This method is more robust but slower than the previous one.
  • mod = 3 : when the peak index is not equal to 1 and the previous peak index is equal to 1 then we have a peak/valley. Its an average of the precedents mod in term of speed and accuracy.


Lower length values tend to estimate the peak/valley of short periods of time but can also lead to the reverse desired effect (breakouts signals). Smoothing is important since it reduce the number of noise in our calculation and therefore help to get better results, its a parameter that should be high, sometimes higher than length if this one is low.

สแนปชอต

Estimation of medium terms peaks/valleys with length and smooth parameter both period 100 and mod = 3

สแนปชอต

Estimation peaks in palladium way to early, an example of bad accuracy. Such behaviour can be fixed with a change in the parameters.

Complementarity With Classics Indicators

As i said before its always a matter of tradeoff, here we get faster signals but we loose in accuracy, at the contrary classics indicators often have slower signals but with more accuracy. Mixing both of them can provide additional robustness in a strategy, lets take back our palladium case, using mod 3 could have been better, but its still not optimal, so lets use a classic indicator such as a moving average of period 200, our conditions are :

  • Long when our peak/valley estimator estimated a valley and the price crossover our moving average.
  • Short when our peak/valley estimator estimated a peak and the price crossunder our moving average.


here is an exemple of such signal :
สแนปชอต

We balanced our tradeoff in a way to fix both methods problems, of course its still not a perfect fix but it provide more robustness.

Other Uses

The indicator can also be used only as an order closing indicator, its safer than taking a position based on its estimation. The indicator can also give a use to the peak index used in the calculation as a trend strength indicator.

สแนปชอต

Values below 0.5 indicate a ranging market while values over 0.5 indicate a trending market.Since its a scaled measure you can use it a smoothing constant in a adaptive filter.

Conclusions

I showed how to estimate peaks and valleys and how to use such information in order to make better decision when using classical indicators, of course at the end nothing is perfect and considering the non stationarity of the markets the parameters efficiency could change drastically.

For any questions/demands feel free to pm me, i would be happy to help you

norepaintpeakpeakspeak_tradingsignalTrend Analysistrendsignalvalley

สคริปต์โอเพนซอร์ซ

ด้วยเจตนารมณ์หลักของ TradingView ผู้เขียนสคริปต์นี้ได้เผยแพร่เป็นโอเพนซอร์ส เพื่อให้เทรดเดอร์สามารถเข้าใจและตรวจสอบได้ ต้องขอบคุณผู้เขียน! ที่ให้คุณใช้ได้ฟรี แต่การนำโค้ดนี้ไปใช้ในการเผยแพร่ซ้ำจะต้องอยู่ภายใต้ กฎระเบียบการใช้งาน คุณสามารถตั้งเป็นรายการโปรดเพื่อใช้บนชาร์ตได้

ต้องการที่จะใช้สคริปต์นี้บนชาร์ตใช่ไหม?


Check out the indicators we are making at luxalgo: tradingview.com/u/LuxAlgo/
และใน:

คำจำกัดสิทธิ์ความรับผิดชอบ