问题
This script is developed to generate waves with cumulative volume according to the specified conditions. On the last bars, there are cases when conditions do not determine which wave is formed up or down. This is accepted as a fact and we agree to wait for the next bars to form. After new bars determine the direction of the wave, the previous bars should be added as the beginning of this new wave. It was assumed that the fulfillment of the conditions for the formation of a wave will be stored in one series and corrections will be made when transferring to another series. Is it possible to fix the written code or make the implementation by another method, for example, through arrays or offset?
I have reworked the script to use arrays. A few more clarifications on the algorithm. An time series of "-1", "0" and "1" is built.
"-1" - DOWN wave "0" - neutral wave "1" - UP wave.
Based on this series, an array is created in which all "0" values are replaced by the first non-zero value on the right. This should work! BUT for some reason it doesn't work.
study("Weis Wave Volume VSA", shorttitle="WWV m1_v4 arr")
var wave = array.new_int(0)
//First main condition for up / down wave formation
CondUp1 = close[2] < close[0] and close[1] < close[0] ? 1 : 0
CondDn1 = close[2] > close[0] and close[1] > close[0] ? -1 : 0
//Second additional condition for up / down wave formation
CondUp2 = open[1] <= close[0] and close[1] < close[0] ? 2 : 0
CondDn2 = open[1] >= close[0] and close[1] > close[0] ? -2 : 0
//Time series for a wave
wavetemp = 0
if CondUp1 != 0 and CondDn1 == 0
wavetemp := 1
if CondUp1 == 0 and CondDn1 != 0
wavetemp := -1
if CondUp1 == 0 and CondDn1 == 0 and CondUp2 != 0 and CondDn2 == 0
wavetemp := 1
if CondUp1 == 0 and CondDn1 == 0 and CondUp2 == 0 and CondDn2 != 0
wavetemp := -1
//Array for wave
begin_index = int(na)
if nz(wavetemp[1]) == 0 and nz(wavetemp[0]) != 0
begin_index := max(array.lastindexof(wave, 1), array.lastindexof(wave,-1))
array.push(wave, wavetemp[0])
for i = begin_index + 1 to bar_index - 1
array.set(wave, i, wavetemp[0]) // New value for historic elements
else
array.push(wave, wavetemp[0])
vol = float(na)
vol := bar_index == 0 ? nz(volume) : ( array.get(wave, bar_index - 1) == array.get(wave, bar_index) ? nz(vol[1]) + nz(volume) : nz(volume) )
up = float(na)
up := array.get(wave, bar_index) == 1 ? vol : na
pd = float(na)
pd := array.get(wave, bar_index) == 0 ? vol : na
dn = float(na)
dn := array.get(wave, bar_index) == -1 ? vol : na
plot(up, style=plot.style_columns, color=color.green)
plot(pd, style=plot.style_columns, color=color.black)
plot(dn, style=plot.style_columns, color=color.red)
[UPDATE]
Alternative description of the question.
There are conditions that create a series
wavetemp
of values "-1", "0" and "1".From the series
wavetemp
, create a new serieswave
according to the rules:
a. if the value of the series wavetemp
is equal to "-1" or "1", then it is assigned a new series wave
;
b. if the value of the series wavetemp
is "0", then wait until the series of the value is equal to "-1" or "1", all the missing element of the new series wave
is issued on a first non-zero value
回答1:
It turned out to make a working script through the use of offset. From a programming point of view, this is terrible, but I have no other ideas. My idea is that the last bars have a different plot for each individual bar.
plot(wave_vol, style=plot.style_columns, editable=false, color=wave_col, offset = -3)
plot(wave_vol_end2, style=plot.style_columns, editable=false, color=wave_col_end2, offset = -2, show_last = 1)
plot(wave_vol_end1, style=plot.style_columns, editable=false, color=wave_col_end1, offset = -1, show_last = 1)
plot(wave_vol_end0, style=plot.style_columns, editable=false, color=wave_col_end0, offset = 0, show_last = 1)
来源:https://stackoverflow.com/questions/65382313/updating-values-or-creating-a-new-series