I wonder about how much it possible to reset sort function so it sort consistently trading volume executed during a specific period (e.g each quarter ) ,

Cheers,

I wonder about how much it possible to reset sort function so it sort consistently trading volume executed during a specific period (e.g each quarter ) ,

Cheers,

as far as I know sort function accept only arguments input to determine interval of array needed to be sorted

this mean the function look for whole array values within a specific interval then sorting as a whole and one part

i need to divide the array into small groups or parts so that sort function will sort only the array values for a certain specific part

like in the below chart , sort function make the value of array (volume) in order from first day of interval (2 jan ,2013) to the last day of interval (31 dec, 2013)

but what if i need to divide this interval into 4 quarter

from first of january to last of march

from first of april to last of june

from first of july to last of september

from first of october to last of december

and getting sorted volume executed during each period , how can i do this ?

@Sebastian try this (verbose) snippet:

```
bi = BarIndex();
// Lookup is a smart way to to find the bar closest to a certain date
q1s = Lookup(bi, StrToDateTime("2017-01-01"), 1);
q1e = Lookup(bi, StrToDateTime("2017-03-31"), -1);
q2s = Lookup(bi, StrToDateTime("2017-04-01"), 1);
q2e = Lookup(bi, StrToDateTime("2017-06-30"), -1);
q3s = Lookup(bi, StrToDateTime("2017-07-01"), 1);
q3e = Lookup(bi, StrToDateTime("2017-09-30"), -1);
q4s = Lookup(bi, StrToDateTime("2017-10-01"), 1);
q4e = Lookup(bi, StrToDateTime("2017-12-31"), -1);
V = Sort(V, q1s, q1e);
V = Sort(V, q2s, q2e);
V = Sort(V, q3s, q3e);
V = Sort(V, q4s, q4e);
Plot(V, "Sorted Volume", colorDefault, styleHistogram);
SetBarsRequired (-2, -2);
```

If you want to apply it to the entire volume without using hardcoded dates, if no one else already did it and will kindly share here his/her code, you'll have to figure out a smart function that will process each "period" (month, quarter, year) from the first bar to the last one.

1 Like

Thanks beppe for your helpful answer

i deal with charts that go back to 1970 ( some is earlier ) and actually i wrote a code similar to your one

however the code was too long to amibroker to handle with it and the application become unresponsive

if some of expert here can provide a helpful hint for writing such function ( not providing the code it self ) it would be nice

Best Regards,

Sebastian

You only need eight lines of code by using inbuilt Sparse function**s**.

Mark my words: just eight lines! And it will work on **entire** array (so for every year).

Eight lines minus these lines

```
mth = Month();
yr = Year();
```

So it is even just **six** lines of code. So better remember number **six**. Because that's what it is **six** lines of code. 666 like devil's code. Not much effort. Just super hot. Now just re-think again (ever heard of looping?). It is sooo simple that it is not even worth it to mention it.

I did not use this function before , but I will try in the next hours

Thanks PanoS and fxshrat for helpful reply

After many trials i write this formula

```
BP=ValueWhen(Month()==1,True);
EP=ValueWhen(Month()==4,True);
F1=Flip(BP,EP);
S1=SparseExpand(F1,SparseCompress(F1,V));
S2=SparseExpand(F1,SORT(S1));
Plot(S2,"S2",colorLightOrange,2);
```

It provide me with sorted volume for the first 3 months of the first year in chart , after this success i have now to use loop

however using loop gave me a unexpected result , the formula generate errors and I don't know a specific reason for that

```
for( I=1 ; I<BarCount ; I=I+3 )
{
BP=ValueWhen(Month()==i,True);
EP=ValueWhen(Month()==i+3,True);
F1=Flip(BP,EP);
S1=SparseExpand(F1,SparseCompress(F1,V));
S2[i]=SparseExpand(F1,SORT(S1));
}
Plot(S2,"S2",colorLightOrange,2);
```

Please , tell me what's the wrong with my formula and which part should I amend ?

it is good to see that you spent some time to learn how it works!

So, here are some additional tips to help you to write your own solution (it is always the best way to learn!).

**No need to loop over the single bars.**

As a first step, you need to create an array that will "isolate" the period you want to sort.

For instance, to sort a specific month (let's say Oct 2017) you could write something like:

```
query_points = (Month() == 10) AND (Year() == 2017);
```

This will create an array where only the bars that have a data with a month number == 10 AND the year number == 2017 will have a 1 (true) value. All the other elements (each element corresponding to a bar in your chart) will be 0.

[Optional]

If needed, you can confirm the condition results, using a simple exploration:

```
query_points = (Month() == 10) AND (Year() == 2017);
Filter = 1;
AddColumn(Month(), "Month()", 1.0);
AddColumn(Month(), "Year()", 1.0);
AddColumn(query_points, "Query", 1.0);
```

Then you pass this array ('query_points' corresponds to your F1) to the SparseCompress function.

As explained in the docs, *the function gets values from 'data' array at points defined by non-zero values of 'query_points' array and compresses them so they are squeezed at the end of resulting array* that, as per your example, we will call it S1. The 'data' here is the **Volume** array.

Then, you Sort() the compressed array (S1) and you expand it back (to S2); *the function expands values from compressed 'data' array at points defined by non-zero values of 'query_points'*.

Note that you should do it for the entire array (no [] are needed).

For testing purposes (when sorting a single period) you may want to Plot the resulting array directly.

When you have clearly figured out the above steps, you can incorporate them into a loop that will iterate (repeating the above operations) over single months, quarters, years.

In the loop, you'll have to change the condition to "isolate" each subsequent period.

Obviously, you can structure your loop to sort the entire array, or you can limit it to a specific period.

When using the loop, you need to add one extra step: incorporate the resulting sorted array (S2) in the original one, to avoid multiple plots.

```
V = iif(query_points, S2, V);
```

At the end you can plot the resulting array, that is a combination of many sorted "periods".

Hope it helps. If not, feel free to ask again.

1 Like

Thank you very much @beppe for excellent and detailed explanation , appreciating this so much

I think i understands the first part of your comment , however my problem boils down to loop

I watched three videos on youtube about loop to write my formula however i still unable to figure out the right way to do it

every time i got sorted volume only for LAST quarter

this my last try to code formula , please tell me what do you think about it ?

```
// First Part of Formula _ Sorting Volume For a Specific Period
QP=query_points = Month()>=1 AND Month()<=3 AND Year() == 2013;
SV1=Sorted_Vol_1 = Sort(SparseCompress(QP,V));
SV2=Sorted_Vol_2 = SparseExpand(QP,SV1);
//Second Part _ Repeating the First Process
for (I=1 ; I<18 ; I=i+3)
{
M=IIf(i%12==0,1,i%12);
Y=int(i/13);
QP=query_points = Month()>=M AND Month()<=(M+2) AND Year() == 2013+Y;
SV1=Sorted_Vol_1 = Sort(SparseCompress(QP,V));
SV2=Sorted_Vol_2 = SparseExpand(QP,SV1);
}
Plot(IIf(QP,SV2,Null),"BP",colorLightOrange,2);
```

any suggestion to write a better formula is much appreciated .. thanks