simple samples of Lua code

Page 1 of 1
4 posts
simple samples of Lua code

Nikolay
MMEX Developer

Posts: 2303
Joined: Sat Dec 06, 2008 8:27 am
Location: Sankt-Petersburg, Russia
Just simple sample of Lua code.

Select month
Code: Select all
-- File: Test mmGetColumnChoice
-------------------------------------------------------
    message = _("Month")
    heading = "Lua: mmGetColumnChoice"
    choice_array =
        {{_("January")}, {_("February")}, {_("March")}
        , {_("April")}, {_("May ")}, {_("June")}
        , {_("July")}, {_("August")}, {_("September")}
        , {_("October")}, {_("November")}, {_("December")}}
    value, id = mmGetColumnChoice(message, heading, choice_array, 1)
    return id.." - "..value
Re: simple samples of Lua code

Nikolay
MMEX Developer

Posts: 2303
Joined: Sat Dec 06, 2008 8:27 am
Location: Sankt-Petersburg, Russia
Extended sample
Code: Select all
-- File: Test mmGetColumnChoice
-------------------------------------------------------
local function add_html_table_header()
   html = html..mmHTMLBuilder("StartTableRow")
   html = html..mmHTMLBuilder("AddTableHeaderCell", message)
   html = html..mmHTMLBuilder("EndTableRow")
end
local function add_html_table_row(c1, color)
   html = html..mmHTMLBuilder("StartTableRow", color)
   html = html..mmHTMLBuilder("AddTableCell", c1)
   html = html..mmHTMLBuilder("EndTableRow")
end
-- Main Function
-------------------------------------------------------
html = ""
html = html..mmHTMLBuilder("AddHorizontalLine",1)
html = html..mmHTMLBuilder("StartTable")
    message = _("Month")
    heading = _("Lua: GetTableChoice")
    choice_array =
        {{1, _("January")}, {2, _("February")}, {3, _("March")}
        , {4, _("April")}, {5, _("May ")}, {6, _("June")}
        , {7, _("July")}, {8, _("August")}, {9, _("September")}
        , {10, _("October")}, {11, _("November")}, {12, _("December")}}
    value, id = mmGetColumnChoice(message, heading, choice_array, 2)
add_html_table_header()
for i=1, 12, 1 do
    c1 = choice_array[i][2]
    if (choice_array[i][1] == id) then
        color = 'green'
    else
        color ="";
    end
    add_html_table_row(c1, color)
end
   html = html..mmHTMLBuilder("EndTable")
   html = html..mmHTMLBuilder("AddHorizontalLine",10)
return html .. "<br>" ..id.." - "..value
Re: simple samples of Lua code

Nikolay
MMEX Developer

Posts: 2303
Joined: Sat Dec 06, 2008 8:27 am
Location: Sankt-Petersburg, Russia
That another way to create html table
Code: Select all
html = {}

function html:new (v)
  o = {}
  setmetatable(o, self)
  self.__index = self
  self.html = mmHTMLBuilder("StartTable", v)
  return o
end

function html:start_row (v)
  self.html = self.html .. mmHTMLBuilder("StartTableRow", v)
end
function html:end_row ()
  self.html = self.html .. mmHTMLBuilder("EndTableRow")
end
function html:add_cell (v)
  self.html = self.html .. mmHTMLBuilder("AddTableCell", v)
end
function html:end_table ()
  self.html = self.html .. mmHTMLBuilder("EndTable")
end

--Main function
-----------------------------------------------------------
h = html:new("25%")
h:start_row("Silver")
h:add_cell("Test cell#1")
h:add_cell("Test cell#2")
h:end_row()
h:end_table()

return h.html
Re: simple samples of Lua code

Nikolay
MMEX Developer

Posts: 2303
Joined: Sat Dec 06, 2008 8:27 am
Location: Sankt-Petersburg, Russia
That Lua report will represent all currencies as a table and provide additional column with a rates downloaded from yahoo.com
Code: Select all
html = {}
function html:new_table (v)
    o = {}
    setmetatable(o, self)
    self.__index = self
    self.html = mmHTMLBuilder("StartTable", v)
    self.colour1 = "#E1EDFB"
    self.rowid = 0
    self.error_description = ''
    return o
end
function html:add_header (v)
    self.html = self.html .. mmHTMLBuilder("StartTableRow")
        for i in pairs(v) do
            if type(i) == "number" then
                self.html = self.html .. mmHTMLBuilder("AddTableHeaderCell", v[i])
            end
        end
    self.html = self.html .. mmHTMLBuilder("EndTableRow")
end
function html:start_row (v)
    local colour = ""
    if self.rowid%2 == 1 then colour = self.colour1 end
    self.html = self.html .. mmHTMLBuilder("StartTableRow", colour)
    self.rowid = self.rowid +1
end
function html:end_row ()
    self.html = self.html .. mmHTMLBuilder("EndTableRow")
end
function html:add_cell (v)
    self.html = self.html .. mmHTMLBuilder("AddTableCell", v)
end
function html:end_table ()
    self.html = self.html .. mmHTMLBuilder("EndTable")
end
function html:push_error(v)
    self.error_description = self.error_description .. '<br>' .. v
end
function html:output_errors()
    self.html = self.html .. '<br><br>' .. self.error_description
end
function html:get_currencies()
    x = 0
    base_currency_symbol = ''
    sql = [[
    select c.currency_symbol, c.currencyname, c.baseconvrate, c2.currency_symbol as base_symbol
    from currencyformats_v1 c
    left join infotable_v1 i on i.infoname='BASECURRENCYID'
    left join  currencyformats_v1 c2 on c2.currencyid = i.infovalue
    ]]
    v, err = mmGetSQLResultSet(sql)
    if err ~= 0
        then html:push_error(v)
    else
        x = SQLResultSetX
   
        base_currency_symbol = SQLResultSet[1][4]
        if base_currency_symbol == ''
            then base_currency_symbol = 'EUR'
            self:push_error( _("Base Currency Not Set") )
        end

        local site = "http://download.finance.yahoo.com/d/quotes.csv?s="
        for i = 1, x, 1 do
            SQLResultSet[i][5] = SQLResultSet[i][1]
            if SQLResultSet[i][1] == ''
                then SQLResultSet[i][5] = 'EUR'
            end
            site = site .. SQLResultSet[i][5] .. base_currency_symbol .. "=X+"
        end
        site = site .. "&f=sl1&e=.csv"
--self:push_error(site)
        local v, err = mmGetSiteContent(site)
--self:push_error(v)
        if err ~= 0 then self:push_error(v)
        else
            curr = {}
            for line in v:gmatch("[^\n]+") do
                a, b, c = line:match('"(...)(...)=X",([.%d]*)')
                if a then curr[a]=c end
            end
        end

        for i = 1, x, 1 do
            h:start_row()
            h:add_cell(SQLResultSet[i][1])
            h:add_cell(SQLResultSet[i][2])
            h:add_cell(SQLResultSet[i][3])
            if err ~= 0 or SQLResultSet[i][1] == ''
                then h:add_cell('')
            else h:add_cell(curr[SQLResultSet[i][5]])
            end
            h:end_row()
        end
    end
end
-----------------------------------------------------------
--Main function
-----------------------------------------------------------
h = html:new_table("50%")
html:add_header({_("Symbol"), _("Currency"), _("Value"), _("Online")})
html:get_currencies()
h:end_table()
h:output_errors()
return h.html
Page 1 of 1
Who is online

Users browsing this forum: No registered users and 1 guest

cron