Plotting a general ZigZag curve with possible threshold value

I want to use zigzag curve to describe the trend of simple data. here is a list as

lstPrices={4.36,4.32,4.2,4.2,4.22,4.12,4.28,4.29,4.29,4.31,4.25,4.35,4.59,4.68,4.61,4.59,5.05,4.95,5.09,5.11,4.99,4.96,5.11,5.37,5.6,5.38,5.42,5.36,4.9,4.92,4.98,4.89,4.99,4.8,4.79,4.62,4.65,4.7,4.68,4.7,4.81,4.84,4.77,4.85,4.78,4.69,4.71,4.66,4.69,4.78,4.78,4.81,4.85,4.78,5.1,5.29,5.19,5.28,5.22,5.18,5.07,5.08,5.09,5.07,5.1,5.05,5.05,5.13,5.1,5.09,5.21,5.24,5.26,5.35,5.19,5.24,5.09,5.18,5.19,5.18,5.13,5.15,5.06,5.09,5.08,5.01,4.99,4.99,4.94,4.98,4.92,4.87,4.91,4.91,4.92,4.95,4.9,4.93,4.99,5.04,4.98,5.17,5.07,5.08,5.14,5.17,5.08,5.53,5.57,5.49,5.47,5.64,5.48,5.47,5.31,5.36,5.35,5.31,5.37,5.35};

and I give new definition of FindPeaks and the related.

JFindPeaks[list_?ListQ] := MapAt[Round, FindPeaks[list] // N, {All, 1}]
JFindValleys[list_?ListQ] := Module[{x, y}, Map[({x, y} = #; {x, -y}) &, JFindPeaks[-list]]]
JFindExtremes[list_?ListQ] := Sort[JFindPeaks[list]~Join~JFindValleys[list]]

then some lists are computed as

peaks = JFindPeaks[lstPrices];
valls = JFindValleys[lstPrices];
extrs = JFindExtremes[lstPrices];

and two plots too,

p1 = ListLinePlot[lstPrices,
   Epilog -> {
     {Red, PointSize[0.015], Point[peaks]},
     {Blue, PointSize[0.015], Point[valls]}},
   PlotStyle -> Directive[Black, Dotted]
   ];
p2 = Graphics@Line@extrs;

finnally, the target plot comes out.

Show[p1, p2,
 AspectRatio -> 1/GoldenRatio,
 Frame -> True,
 GridLines -> Automatic,
 GridLinesStyle -> Directive[Gray, Dotted],
 ImageSize -> Large
 ]

It’s like this,

dNBIQ Plotting a general ZigZag curve with possible threshold value

but the most I want to get could be like the following one or the other similarly, or these sub-peaks-valleys should be ellminated on the plot.

JVeZN Plotting a general ZigZag curve with possible threshold value

so how to realize it? Maybe a threshold value is necessary. Thanks!

1 Answer

Let’s block ads! (Why?)

Recent Questions – Mathematica Stack Exchange