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)?
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?
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 }}
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).
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.
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.