Module:BassaridMarketActivity: Difference between revisions
From MicrasWiki
Jump to navigationJump to search
NewZimiaGov (talk | contribs) No edit summary Tag: Manual revert |
NewZimiaGov (talk | contribs) No edit summary |
||
| Line 28: | Line 28: | ||
end | end | ||
- | local marketHistory = {} | ||
local function updateMarketHistory(date, investorName, stocks) | |||
if not marketHistory[investorName] then | |||
marketHistory[investorName] = {} | |||
end | |||
table.insert(marketHistory[investorName], {date = date, stocks = stocks}) | |||
end | |||
local function generateGraph() | |||
if next(marketHistory) == nil then | |||
return "<script>Graph data is missing</script>" | |||
end | |||
local traces = {} | |||
for investorName, data in pairs(marketHistory) do | |||
local x = {} | |||
local y = {} | |||
for _, entry in ipairs(data) do | |||
table.insert(x, string.format("'%s'", entry.date)) | |||
table.insert(y, entry.stocks) | |||
end | |||
table.insert(traces, string.format([[ | |||
{ | |||
x: [%s], | |||
y: [%s], | |||
mode: 'lines+markers', | |||
name: '%s' | |||
} | |||
]], table.concat(x, ", "), table.concat(y, ", "), investorName)) | |||
end | |||
return [[ | |||
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script> | |||
<div id="marketGraph"></div> | |||
<script> | |||
var data = [ ]] .. table.concat(traces, ", ") .. [[ ]; | |||
var layout = { | |||
title: 'Investor Stocks Over Time', | |||
xaxis: {title: 'Date'}, | |||
yaxis: {title: 'Stocks'} | |||
}; | |||
Plotly.newPlot('marketGraph', data, layout); | |||
</script> | |||
]] | |||
end | |||
local investors = { | local investors = { | ||
{name = "Vaeringheim Regional Investor", stocks = 17424}, | {name = "Vaeringheim Regional Investor", stocks = 17424}, | ||
| Line 40: | Line 89: | ||
} | } | ||
local function simulateDailyActivity(investor) | local function simulateDailyActivity(investor) | ||
local buyStocks = math.random(0, 500) | local buyStocks = math.random(0, 500) | ||
| Line 117: | Line 97: | ||
end | end | ||
function p.generateGraph() | |||
local date = getCurrentDate() | |||
function p. | |||
local date | |||
for _, investor in ipairs(investors) do | for _, investor in ipairs(investors) do | ||
simulateDailyActivity(investor) | |||
updateMarketHistory(date, investor.name, investor.stocks) | |||
end | end | ||
return generateGraph() | |||
end | end | ||
return p | return p | ||
Revision as of 20:55, 25 December 2024
Documentation for this module may be created at Module:BassaridMarketActivity/doc
local p = {}
local function getCurrentDate()
local startDate = os.time({year = 1999, month = 8, day = 6})
local secondsInDay = 86400
local daysPerYear = 183
local currentDate = os.time()
local totalDaysElapsed = math.floor((currentDate - startDate) / secondsInDay)
local yearFraction = totalDaysElapsed / daysPerYear
local psscYear = math.floor(yearFraction)
local dayOfYear = math.floor((yearFraction - psscYear) * daysPerYear) + 1
local month, dayOfMonth
if dayOfYear <= 61 then
month = 1
dayOfMonth = dayOfYear
elseif dayOfYear <= 122 then
month = 2
dayOfMonth = dayOfYear - 61
else
month = 3
dayOfMonth = dayOfYear - 122
end
return string.format("%d/%d/%d [[PSSC]]", dayOfMonth, month, psscYear), dayOfMonth, month
end
local marketHistory = {}
local function updateMarketHistory(date, investorName, stocks)
if not marketHistory[investorName] then
marketHistory[investorName] = {}
end
table.insert(marketHistory[investorName], {date = date, stocks = stocks})
end
local function generateGraph()
if next(marketHistory) == nil then
return "<script>Graph data is missing</script>"
end
local traces = {}
for investorName, data in pairs(marketHistory) do
local x = {}
local y = {}
for _, entry in ipairs(data) do
table.insert(x, string.format("'%s'", entry.date))
table.insert(y, entry.stocks)
end
table.insert(traces, string.format([[
{
x: [%s],
y: [%s],
mode: 'lines+markers',
name: '%s'
}
]], table.concat(x, ", "), table.concat(y, ", "), investorName))
end
return [[
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<div id="marketGraph"></div>
<script>
var data = [ ]] .. table.concat(traces, ", ") .. [[ ];
var layout = {
title: 'Investor Stocks Over Time',
xaxis: {title: 'Date'},
yaxis: {title: 'Stocks'}
};
Plotly.newPlot('marketGraph', data, layout);
</script>
]]
end
local investors = {
{name = "Vaeringheim Regional Investor", stocks = 17424},
{name = "Alpazkigz Regional Investor", stocks = 16214},
{name = "Odiferia Regional Investor", stocks = 14718},
{name = "Hafaan Regional Investor", stocks = 3630},
{name = "Jeseri Regional Investor", stocks = 1870},
{name = "Hatch Ministry Regional Investor", stocks = 7810},
{name = "New South Jangsong Division", stocks = 11085},
{name = "Haifan Bassaridia Division", stocks = 12500},
}
local function simulateDailyActivity(investor)
local buyStocks = math.random(0, 500)
local sellStocks = math.random(0, 500)
investor.stocks = investor.stocks + buyStocks - sellStocks
return buyStocks, sellStocks
end
function p.generateGraph()
local date = getCurrentDate()
for _, investor in ipairs(investors) do
simulateDailyActivity(investor)
updateMarketHistory(date, investor.name, investor.stocks)
end
return generateGraph()
end
return p