The following is a pseudo code for the calc of the index prices:
(code is in red)
’ Create variables as numbers.
ThisIndex = 0
ThisQty = 0
ThisPerc = 0
Yesterday = 0
YesterQty = 0
YesterPers = 0
'Select what we need in the database
Set rs_Index = conn.Execute (“SELECT * FROM where itemtype is in the right category and where its the final bid on the specific item”)
'Loop through the database to get them all
do while not rs_Index.eof
'First we get all the entries that match the search criteria and is within the last 7 days.
'Date compare - If its 1, its 1 day ago, 2.. 2 days ago.. and so on..
if DateDiff(“d”,rs_Index(“Dato”),date()) < 7 then
'Add the current rs QTY to the total qty within the past week, when it loops the total qty increases…
ThisQty = ThisQty + rs_Index(“qty”)
'Then we add the selling percentage to the formular, this also increases for each loop.
ThisPerc = ThisPerc + (rs_Index(“qty”) * ((rs_Index(“Bidprice”) / rs_Index(“Price”))*100))
'Next 7 days
elseIF DateDiff(“d”,rs_Index(“Dato”),date()) > 7 AND DateDiff(“d”,rs_Index(“Dato”),date()) < 14 then
'Add the current rs QTY to the total qty a 7-14 days ago
YesterQty = YesterQty + rs_Index(“qty”)
YesterPers = YesterPers + (rs_Index(“qty”) * ((rs_Index(“Bidprice”) / rs_Index(“Price”))*100))
end if
'Then we move to the next rs( result set) from the database..
rs_Index.movenext
'Then we loop back to the “Do while”
loop
'When we have looped through the database we close the result set, so we dont take up too much RAM.. prehaps MA could learn somethign ?? :-p
rs_Index.close
set rs_Index = nothing
’ Now we know the database has been looped through and we have added all the information to the variables we need. So now we can calc the index, and how it has performed in the last 14 days.
ThisIndex = Round(ThisPerc / ThisQty,2)
Yesterday = Round(YesterPers / YesterQty,2)
’ Then we decide which image (Arrow) we should print to the page (the printing code is not provided here… This is JUST the back end code..
If ThisIndex > Yesterday then
UpDown = “up.jpg”
else
UpDown = “down.jpg”
end if
'Then we calc the details number.. That is the number shown in the () on the page, that shows how much the index have moved within the last 14 days.
Detail = Round(ThisIndex - Yesterday,3)
In essence what the code does is that it takes all (percentages * qty) / qty
So if we come up with some fictive numbers.. like
1500 ingots sold for 140 %
10 ingots sold for 185 %
1 ingot sold for 900%
The equation will be:
index = ((1500 * 140) + (10 * 185) + (1 * 900)) / 1511 = (210000 + 1850 + 900) / 1511 = 140.80.
This is prehaps easy enough to calc for 3 (in this case) minerals.. but the database loops through arround 500 entries (only looping thogh entries for the last 14 days)..
The index therefore show what the average selling “value” has been for the specific category in the last 14 days, and how the market is moving.
Note that the code here has been “prettyfied” so it isent soo complex as it is in the actual code.. but the basics is the same…
Also.. Each time you enter the index page you calc. all the index’ on the fly.. thats why it can take a bit of time before the page loads…