Lua: Currencies Report

Page 1 of 1
3 posts
Lua: Currencies Report

Nikolay
MMEX Developer

Posts: 2296
Joined: Sat Dec 06, 2008 8:27 am
Location: Sankt-Petersburg, Russia
Code: Select all
local html = {}
function html:init ()
   o = {}
   setmetatable(o, self)
   self.__index = self
   self.html = mmHTMLBuilder("StartCenter")
   self.colour1 = "#E1EDFB"
   self.rowid = 0
   self.error_description = ''
   return o
end
function html:new_table (v)
   self.html = self.html .. mmHTMLBuilder("StartTable", v)
   self.colour1 = "#E1EDFB"
end
function html:end_table ()
   self.html = self.html .. mmHTMLBuilder("EndTable")
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: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
         html:start_row()
         html:add_cell(SQLResultSet[i][1])
         html:add_cell(SQLResultSet[i][2])
         html:add_cell(SQLResultSet[i][3])
         if err ~= 0 or SQLResultSet[i][1] == ''
            then html:add_cell('')
         else html:add_cell(curr[SQLResultSet[i][5]])
         end
         html:end_row()
      end
   end
end
-----------------------------------------------------------
--Main function
-----------------------------------------------------------

html:init()
html:new_table("50%")
html:add_header({_("Symbol"), _("Currency"), _("Value"), _("Online")})
html:get_currencies()
html:end_table()

html:output_errors()
return html.html
Re: Lua: Currencies Report

johnm
New MMEX User

Posts: 6
Joined: Wed Jan 23, 2013 6:41 am
Error: SQLITE_ERROR[1]: near "local": syntax error
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Re: Lua: Currencies Report

Nikolay
MMEX Developer

Posts: 2296
Joined: Sat Dec 06, 2008 8:27 am
Location: Sankt-Petersburg, Russia
[quote="johnm"]Error: SQLITE_ERROR[1]: near "local": syntax error
[quote]
That Lua not SQL report. It's only for development vertion of mmex (0.9.9.2)
Page 1 of 1
Who is online

Users browsing this forum: No registered users and 1 guest