|
| 1 | +// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ |
| 2 | +// © RicardoSantos |
| 3 | + |
| 4 | +//@version=4 |
| 5 | +study(title='function: Event Statistics') |
| 6 | +int window = input(200) |
| 7 | +float h = highest(window) |
| 8 | +float l = lowest(window) |
| 9 | +int hb = highestbars(window) |
| 10 | +int lb = lowestbars(window) |
| 11 | + |
| 12 | +f_change_to_window(_window)=>close - close[_window] |
| 13 | +f_change_to_highest_point(_window)=>h - close[_window] // highest(_window) - close[_window] |
| 14 | +f_change_to_lowest_point(_window)=>l - close[_window] // lowest(_window) - close[_window] |
| 15 | +f_highest_bar_from_event(_window)=>_window + hb//highestbars(_window) |
| 16 | +f_lowest_bar_from_event(_window)=>_window + lb//lowestbars(_window) |
| 17 | + |
| 18 | +var float[] stats_change = array.new_float(size=0, initial_value=0.) |
| 19 | +var float[] stats_max = array.new_float(size=0, initial_value=0.) |
| 20 | +var float[] stats_min = array.new_float(size=0, initial_value=0.) |
| 21 | +var int[] stats_peakbar = array.new_int(size=0, initial_value=0) |
| 22 | +var int[] stats_deepbar = array.new_int(size=0, initial_value=0) |
| 23 | + |
| 24 | +f_stat(_event, _window)=> |
| 25 | +//| updates a array with new value |
| 26 | + if _event[_window] |
| 27 | + array.push(id=stats_change, value=f_change_to_window(_window)) |
| 28 | + array.push(id=stats_max, value=f_change_to_highest_point(_window)) |
| 29 | + array.push(id=stats_min, value=f_change_to_lowest_point(_window)) |
| 30 | + array.push(id=stats_peakbar, value=f_highest_bar_from_event(_window)) |
| 31 | + array.push(id=stats_deepbar, value=f_lowest_bar_from_event(_window)) |
| 32 | + |
| 33 | +float ma = sma(close, 10) |
| 34 | +bool event = crossover(close, ma) |
| 35 | +f_stat(event, window) |
| 36 | + |
| 37 | +float avg = array.avg(id=stats_change) |
| 38 | +float avg_max = array.avg(id=stats_max) |
| 39 | +float avg_min = array.avg(id=stats_min) |
| 40 | +int avg_peakbar = array.avg(id=stats_peakbar) |
| 41 | +int avg_deepbar = array.avg(id=stats_deepbar) |
| 42 | + |
| 43 | +plot(event?0:na, title='event', style=plot.style_circles, linewidth=4) |
| 44 | +plot(avg, title='avg', color=#000000, offset=-window) |
| 45 | +plot(avg_max, title='max', color=#00ff00, offset=-window) |
| 46 | +plot(avg_min, title='min', color=#ff0000, offset=-window) |
| 47 | +// plot(avg_peakbar, title='peak', color=#ff0000, offset=-window) |
| 48 | +// plot(avg_deepbar, title='deep', color=#ff0000, offset=-window) |
| 49 | + |
| 50 | +_size = array.size(id=stats_change) |
| 51 | +stavg(_i)=> _size > _i ? array.get(id=stats_change, index=_size - _i) : na |
| 52 | +stmax(_i)=> _size > _i ? array.get(id=stats_max, index=_size - _i) : na |
| 53 | +stpeak(_i)=> _size > _i ? array.get(id=stats_peakbar, index=_size - _i) : na |
| 54 | + |
| 55 | +stmin(_i)=> _size > _i ? array.get(id=stats_min, index=_size - _i) : na |
| 56 | +stdeep(_i)=> _size > _i ? array.get(id=stats_deepbar, index=_size - _i) : na |
| 57 | + |
| 58 | +plot(stavg(01), title='', color=#000000, style=plot.style_circles, offset=window, show_last=1) |
| 59 | +plot(stavg(02), title='', color=#000000, style=plot.style_circles, offset=window, show_last=1) |
| 60 | +plot(stavg(03), title='', color=#000000, style=plot.style_circles, offset=window, show_last=1) |
| 61 | +plot(stavg(04), title='', color=#000000, style=plot.style_circles, offset=window, show_last=1) |
| 62 | +plot(stavg(05), title='', color=#000000, style=plot.style_circles, offset=window, show_last=1) |
| 63 | +plot(stavg(06), title='', color=#000000, style=plot.style_circles, offset=window, show_last=1) |
| 64 | +plot(stavg(07), title='', color=#000000, style=plot.style_circles, offset=window, show_last=1) |
| 65 | +plot(stavg(08), title='', color=#000000, style=plot.style_circles, offset=window, show_last=1) |
| 66 | +plot(stavg(09), title='', color=#000000, style=plot.style_circles, offset=window, show_last=1) |
| 67 | +plot(stavg(10), title='', color=#000000, style=plot.style_circles, offset=window, show_last=1) |
| 68 | +plot(stavg(11), title='', color=#000000, style=plot.style_circles, offset=window, show_last=1) |
| 69 | +plot(stavg(12), title='', color=#000000, style=plot.style_circles, offset=window, show_last=1) |
| 70 | +plot(stavg(13), title='', color=#000000, style=plot.style_circles, offset=window, show_last=1) |
| 71 | +plot(stavg(14), title='', color=#000000, style=plot.style_circles, offset=window, show_last=1) |
| 72 | +plot(stavg(15), title='', color=#000000, style=plot.style_circles, offset=window, show_last=1) |
| 73 | +plot(stavg(16), title='', color=#000000, style=plot.style_circles, offset=window, show_last=1) |
| 74 | +plot(stavg(17), title='', color=#000000, style=plot.style_circles, offset=window, show_last=1) |
| 75 | +plot(stavg(18), title='', color=#000000, style=plot.style_circles, offset=window, show_last=1) |
| 76 | +plot(stavg(19), title='', color=#000000, style=plot.style_circles, offset=window, show_last=1) |
| 77 | +plot(stavg(20), title='', color=#000000, style=plot.style_circles, offset=window, show_last=1) |
| 78 | + |
| 79 | +plot(stmax(01), title='', color=#00ff00, style=plot.style_circles, offset=stpeak(01), show_last=1) |
| 80 | +plot(stmax(02), title='', color=#00ff00, style=plot.style_circles, offset=stpeak(02), show_last=1) |
| 81 | +plot(stmax(03), title='', color=#00ff00, style=plot.style_circles, offset=stpeak(03), show_last=1) |
| 82 | +plot(stmax(04), title='', color=#00ff00, style=plot.style_circles, offset=stpeak(04), show_last=1) |
| 83 | +plot(stmax(05), title='', color=#00ff00, style=plot.style_circles, offset=stpeak(05), show_last=1) |
| 84 | +plot(stmax(06), title='', color=#00ff00, style=plot.style_circles, offset=stpeak(06), show_last=1) |
| 85 | +plot(stmax(07), title='', color=#00ff00, style=plot.style_circles, offset=stpeak(07), show_last=1) |
| 86 | +plot(stmax(08), title='', color=#00ff00, style=plot.style_circles, offset=stpeak(08), show_last=1) |
| 87 | +plot(stmax(09), title='', color=#00ff00, style=plot.style_circles, offset=stpeak(09), show_last=1) |
| 88 | +plot(stmax(10), title='', color=#00ff00, style=plot.style_circles, offset=stpeak(10), show_last=1) |
| 89 | +plot(stmax(11), title='', color=#00ff00, style=plot.style_circles, offset=stpeak(11), show_last=1) |
| 90 | +plot(stmax(12), title='', color=#00ff00, style=plot.style_circles, offset=stpeak(12), show_last=1) |
| 91 | +plot(stmax(13), title='', color=#00ff00, style=plot.style_circles, offset=stpeak(13), show_last=1) |
| 92 | +plot(stmax(14), title='', color=#00ff00, style=plot.style_circles, offset=stpeak(14), show_last=1) |
| 93 | +plot(stmax(15), title='', color=#00ff00, style=plot.style_circles, offset=stpeak(15), show_last=1) |
| 94 | +plot(stmax(16), title='', color=#00ff00, style=plot.style_circles, offset=stpeak(16), show_last=1) |
| 95 | +plot(stmax(17), title='', color=#00ff00, style=plot.style_circles, offset=stpeak(17), show_last=1) |
| 96 | +plot(stmax(18), title='', color=#00ff00, style=plot.style_circles, offset=stpeak(18), show_last=1) |
| 97 | +plot(stmax(19), title='', color=#00ff00, style=plot.style_circles, offset=stpeak(19), show_last=1) |
| 98 | +plot(stmax(20), title='', color=#00ff00, style=plot.style_circles, offset=stpeak(20), show_last=1) |
| 99 | + |
| 100 | +plot(stmin(01), title='', color=#ff0000, style=plot.style_circles, offset=stdeep(01), show_last=1) |
| 101 | +plot(stmin(02), title='', color=#ff0000, style=plot.style_circles, offset=stdeep(02), show_last=1) |
| 102 | +plot(stmin(03), title='', color=#ff0000, style=plot.style_circles, offset=stdeep(03), show_last=1) |
| 103 | +plot(stmin(04), title='', color=#ff0000, style=plot.style_circles, offset=stdeep(04), show_last=1) |
| 104 | +plot(stmin(05), title='', color=#ff0000, style=plot.style_circles, offset=stdeep(05), show_last=1) |
| 105 | +plot(stmin(06), title='', color=#ff0000, style=plot.style_circles, offset=stdeep(06), show_last=1) |
| 106 | +plot(stmin(07), title='', color=#ff0000, style=plot.style_circles, offset=stdeep(07), show_last=1) |
| 107 | +plot(stmin(08), title='', color=#ff0000, style=plot.style_circles, offset=stdeep(08), show_last=1) |
| 108 | +plot(stmin(09), title='', color=#ff0000, style=plot.style_circles, offset=stdeep(09), show_last=1) |
| 109 | +plot(stmin(10), title='', color=#ff0000, style=plot.style_circles, offset=stdeep(10), show_last=1) |
| 110 | +plot(stmin(11), title='', color=#ff0000, style=plot.style_circles, offset=stdeep(11), show_last=1) |
| 111 | +plot(stmin(12), title='', color=#ff0000, style=plot.style_circles, offset=stdeep(12), show_last=1) |
| 112 | +plot(stmin(13), title='', color=#ff0000, style=plot.style_circles, offset=stdeep(13), show_last=1) |
| 113 | +plot(stmin(14), title='', color=#ff0000, style=plot.style_circles, offset=stdeep(14), show_last=1) |
| 114 | +plot(stmin(15), title='', color=#ff0000, style=plot.style_circles, offset=stdeep(15), show_last=1) |
| 115 | +plot(stmin(16), title='', color=#ff0000, style=plot.style_circles, offset=stdeep(16), show_last=1) |
| 116 | +plot(stmin(17), title='', color=#ff0000, style=plot.style_circles, offset=stdeep(17), show_last=1) |
| 117 | +plot(stmin(18), title='', color=#ff0000, style=plot.style_circles, offset=stdeep(18), show_last=1) |
| 118 | +plot(stmin(19), title='', color=#ff0000, style=plot.style_circles, offset=stdeep(19), show_last=1) |
| 119 | +plot(stmin(20), title='', color=#ff0000, style=plot.style_circles, offset=stdeep(20), show_last=1) |
| 120 | + |
| 121 | +// t = '' |
| 122 | +// size = array.size(stats_min) |
| 123 | +// if barstate.islast |
| 124 | +// t := tostring(array.slice(id=stats_min, index_from=size-10, index_to=size)) |
| 125 | +// var label la = label.new(0, 0., '') |
| 126 | +// label.set_x(id=la, x=bar_index) |
| 127 | +// label.set_text(id=la, text=t) |
0 commit comments