The dataset named crass
looks like -
> dput(crass)
structure(list(WT_TRADE_PRICE = c(3801, 3801, 3801, 3797, 3797,
3796.2125, 3800, 3797, 3795.09523809524, 3794, 3793, 3793, 3793.8,
3794.72, 3793.02777777778, 3789, 3790, 3788, 3788, 3788), min = c(3801,
3801, 3801, 3797, 3797, 3795, 3800, 3797, 3794, 3794, 3793, 3793,
3793, 3794, 3790, 3789, 3790, 3788, 3788, 3788), max = c(3801,
3801, 3801, 3797, 3797, 3800, 3800, 3797, 3797, 3794, 3793, 3793,
3794, 3797, 3794, 3789, 3790, 3788, 3788, 3788), Bid = c(3801,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA), Ask = c(3802, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA, -20L
), class = c("tbl_df", "tbl", "data.frame"))
# A tibble: 20 x 5
WT_TRADE_PRICE min max Bid Ask
<dbl> <dbl> <dbl> <dbl> <dbl>
1 3801 3801 3801 3801 3802
2 3801 3801 3801 NA NA
3 3801 3801 3801 NA NA
4 3797 3797 3797 NA NA
5 3797 3797 3797 NA NA
6 3796. 3795 3800 NA NA
7 3800 3800 3800 NA NA
8 3797 3797 3797 NA NA
9 3795. 3794 3797 NA NA
10 3794 3794 3794 NA NA
11 3793 3793 3793 NA NA
12 3793 3793 3793 NA NA
13 3794. 3793 3794 NA NA
14 3795. 3794 3797 NA NA
15 3793. 3790 3794 NA NA
16 3789 3789 3789 NA NA
17 3790 3790 3790 NA NA
18 3788 3788 3788 NA NA
19 3788 3788 3788 NA NA
20 3788 3788 3788 NA NA
As can be seen, two variables ask
& bid
have only initial values which need to be filled iteratively using the following logic.
- If
WT_TRDAE_PRICE
is >=
than previous value of Ask
OR WT_TRADE_PRICE
will be checked whether >
than mean of previous bid
and ask
- then current ask
will be set equal to current row max
variable and bid
will be set equal to previous bid
value.
- Else , current
ask
will be set to previous ask
value & bid
to max
.
Pseudo code -
if(WT_TRADE_PRICE >= L(Ask) | WT_TRADE_PRICE > (L(Bid)+L(Ask))/2)
{
Bid = L(Bid), Ask = max
}
else
{
Bid = min, Ask = L(Ask)
}
Final output -
SNo. |
WT_TRADE_PRICE |
min |
max |
Bid |
Ask |
1 |
3801 |
3801 |
3801 |
3801 |
3802 |
2 |
3801 |
3801 |
3801 |
3801 |
3802 |
3 |
3801 |
3801 |
3801 |
3801 |
3802 |
4 |
3797 |
3797 |
3797 |
3797 |
3802 |
5 |
3797 |
3797 |
3797 |
3797 |
3802 |
6 |
3796. |
3795 |
3800 |
3795 |
3802 |
7 |
3800 |
3800 |
3800 |
3795 |
3800 |
8 |
3797 |
3797 |
3797 |
3797 |
3800 |
9 |
3795. |
3794 |
3797 |
3794 |
3800 |
10 |
3794 |
3794 |
3794 |
3794 |
3800 |
11 |
3793 |
3793 |
3793 |
3793 |
3800 |
12 |
3793 |
3793 |
3793 |
3793 |
3800 |
13 |
3794. |
3793 |
3794 |
3793 |
3800 |
14 |
3795. |
3794 |
3797 |
3793 |
3797 |
15 |
3793. |
3790 |
3794 |
3790 |
3797 |
16 |
3789 |
3789 |
3789 |
3789 |
3797 |
17 |
3790 |
3790 |
3790 |
3790 |
3797 |
18 |
3788 |
3788 |
3788 |
3788 |
3797 |
19 |
3788 |
3788 |
3788 |
3788 |
3797 |
20 |
3788 |
3788 |
3788 |
3788 |
3797 |
See Question&Answers more detail:
os