Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
krambox committed Dec 26, 2021
0 parents commit 7e28af9
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
Empty file added README.md
Empty file.
113 changes: 113 additions & 0 deletions ibkr.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
WebBanking {
version = 1.0,
country = "de",
description = "Include your IBKR stock portfolio in MoneyMoney.",
services = {"IBKR"}
}

local parseargs = function(s)
local arg = {}
string.gsub(s, "([%-%w]+)=([\"'])(.-)%2", function(w, _, a)
arg[w] = a
end)
return arg
end

local parseBlock = function(content, k)
return string.match(content, "^.+<" .. k .. ">(.+)</" .. k .. ">.+$")
end

local connection = Connection()
local token
local query
local code

function SupportsBank(protocol, bankCode)
return protocol == ProtocolWebBanking and bankCode == "IBKR"
end

function InitializeSession(protocol, bankCode, username, customer, password)
token = password
query = username
connection = Connection()
local content = connection:get(
"https://gdcdyn.interactivebrokers.com/Universal/servlet/FlexStatementService.SendRequest?t=" .. token .. "&q=" ..
query .. "&v=3")
local status = string.match(content, "^.+<Status>(.+)</Status>.+$")
if status == "Success" then
code = string.match(content, "^.+<ReferenceCode>(.+)</ReferenceCode>.+$")
print("8:" .. code)
else
return content
end
end

function ListAccounts(knownAccounts)
local account = {
name = "IBKR",
accountNumber = 1,
currency = "EUR",
portfolio = true,
type = "AccountTypePortfolio"
}
local account2 = {
name = "IBKR Cash",
accountNumber = 2,
currency = "EUR",
type = "AccountTypeOther"
}

return {account, account2}
end

local statementContent

function RefreshAccount(account, since)
print("RefreshAccount " .. JSON():set(account):json())

if statementContent == nil then
statementContent = connection:get(
"https://gdcdyn.interactivebrokers.com/Universal/servlet/FlexStatementService.GetStatement?t=" .. token ..
"&q=" .. code .. "&v=3")
end
if account.accountNumber == "1" then
local positions = parseBlock(statementContent, 'OpenPositions')
local securities = {}
for p in positions:gmatch("<OpenPosition(.-)/>") do
print(p)
local pos = parseargs(p)
securities[#securities + 1] = {
name = pos.symbol,
securityNumber = pos.isin,
market = pos.listingExchange,
quantity = pos.position * pos.multiplier,
price = pos.markPrice,
currencyOfPrice = pos.currency,
purchasePrice = pos.costBasisPrice,
currencyOfPurchasePrice = pos.currency,
exchangeRate = pos.fxRateToBase
}
end
-- Return balance and array of transactions.
return {
securities = securities
}
elseif account.accountNumber == "2" then
local summary = parseBlock(statementContent, 'EquitySummaryInBase')
local cash = 0
for p in summary:gmatch("<EquitySummaryByReportDateInBase(.-)/>") do
print(p)
local pos = parseargs(p)
cash = pos.cash
end
-- Return balance and array of transactions.
return {
balance = cash,
transactions = {}
}
end
end

function EndSession()
-- Logout.
end

0 comments on commit 7e28af9

Please sign in to comment.