How to retrieve historical information in details

Hi,

Suppose I have a template with some details regarding the historical purchase of some stocks.

Example:

Date Stock Action Amount Cumulative
1/10/2013 A Purchase 100 100
10/10/2013 B Purchase 50 50
1/05/2014 A Purchase 60 160
1/01/2016 A Sell 20 140

Suppose I want to retrieve the cumulative amount of stocks A on a given date (1/1/2015), which is 160 here. How can I go about this (with code please)?

Thanks!

@Bart_Verhaeghe,

Where do you want to retrieve this information? In the account template, or do you want to retrieve it somewhere else?

Part of your solution is in here, if you want to retrieve it somewhere else.

And I don’t get your example; shouldn’t it be 100+50+60 = 210? :thinking:

Whatever your object is where that data is stored for each stock movement, you’ll need to go over your collection again with a for-loop, and put an if-statement in there that adds the cumulative amount in a register. That register can be used for the total amount.

For example (this is just quickly an example) :

{% fori stock in custom.stocks %}
| {% input stock.date as:date %}  | {% input stock.value as:currency %}
{% endfori %}

{% t "Input date calculate stock value" %}
{% input custom.date.calc as:date %}

{% for stock in custom.stocks %}
{% if custom.date.calc > stock.date %}
{% $1+ stock.value %}
{% endif %}
{% endfor %}

{% t "Stock value =" %}{{ $1 | currency }}

Can you try with this?

Hey Sven, I’ll try this solution (should have thought of this myself).

Now, the count is conditional in two respects, being a) the underlying shares (A) and b) the date on which we want to look in the data. Sorry I didn’t make this clear enough.

But I’ll try with your code (and with these two conditions cumulatively).

Good luck @Bart_Verhaeghe, always great to hear with what code you made it work :muscle:

Hi Sven,

This worked … thanks, but I have an additional question.

Suppose I have the table as given at the top of this thread. The stocks are A, B, A and A.

How can I create an array with the unique values in this table (being A and B)?

Thanks!

Hello @Bart_Verhaeghe,

I think it’s better that for each question you have, you create a new topic. That way, it becomes clear what topics are solved and which ones are not. :relaxed:

To answer your question, you could use the uniq filter for this.

Example:

{% assign my_array = "ants, bugs, bees, bugs, ants" | split: ", " %}

{{ my_array | uniq | join: ", " }} 

Hi Sven,

Thanks for your answer, but I already knew this. The question is how to get each element of the string you mentioned above (“ants, bugs, bees, …”) from the table mentioned above.

Next time, I’ll create a new topic.

@Bart_Verhaeghe

You can create your array like this (put this before the forloop begins):

{% assign my_array = "" %}

Then for each loop you’ll create your array like this :


{% for item in custom.collection %}
  {% assign my_array = my_array | append:item %}
  {% unless forloop.last %}
    {% assign my_array = my_array | append:"," %}
  {% endunless %}
{% endfor %}

So each loop the value of item gets added to the variable my_array. Also, after the adding of the name (item) a komma is added, unless you are in the last loop.

You now have created a variable you can use as an array.

OK thank you very much Sven.