Advertisement
Guest User

Untitled

a guest
May 4th, 2022
44
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None
  1. local statementContent
  2.  
  3. function stringToTimestamp(str)
  4. local datePattern = '(%d%d%d%d)(%d%d)(%d%d)'
  5. local year, month, day = str:match(datePattern)
  6. if year and month and day then
  7. local timestamp = os.time{day=day,month=month,year=year}
  8. return timestamp
  9. end
  10. end
  11.  
  12. function RefreshAccount(account, since)
  13. print("RefreshAccount " .. JSON():set(account):json())
  14.  
  15. if statementContent == nil then
  16. local ec
  17. repeat
  18. statementContent, charset, mimeType = connection:get(
  19. "https://gdcdyn.interactivebrokers.com/Universal/servlet/FlexStatementService.GetStatement?t=" .. token ..
  20. "&q=" .. code .. "&v=3")
  21. local ec=parseBlock(statementContent, 'ErrorCode')
  22. if ec=="1019" then
  23. MM.sleep(1)
  24. end
  25.  
  26. until ec ~="1019"
  27.  
  28. end
  29. if account.accountNumber == "1" then
  30. local positions = parseBlock(statementContent, 'OpenPositions')
  31. local securities = {}
  32. for p in positions:gmatch("<OpenPosition(.-)/>") do
  33. print(p)
  34. local pos = parseargs(p)
  35. securities[#securities + 1] = {
  36. name = pos.description,
  37. isin = pos.isin,
  38. securityNumber = pos.isin,
  39. market = pos.listingExchange,
  40. quantity = pos.position * pos.multiplier,
  41. originalCurrencyAmount = pos.positionValue,
  42. currencyOfOriginalAmount = pos.currency,
  43. price = pos.markPrice,
  44. currencyOfPrice = pos.currency,
  45. purchasePrice = pos.costBasisPrice,
  46. currencyOfPurchasePrice = pos.currency,
  47. exchangeRate = 1 / pos.fxRateToBase,
  48. userdata = {{key="_profit",value=string.format("%.02f", pos.fifoPnlUnrealized*pos.fxRateToBase) .. " EUR / " .. string.format("%.05f", 100/pos.costBasisMoney*pos.positionValue-100) .. " %"}}
  49. --userdata = {{key="_profit",value=string.format("%.02f", pos.fifoPnlUnrealized) .. " USD / " .. string.format("%.05f", 100/pos.costBasisMoney*pos.positionValue-100) .. " %"}}
  50.  
  51. }
  52.  
  53. end
  54. -- Return balance and array of transactions.
  55. return {
  56. securities = securities
  57. }
  58. elseif account.accountNumber == "2" then
  59. local summary = parseBlock(statementContent, 'EquitySummaryInBase')
  60. local cash = 0
  61. for p in summary:gmatch("<EquitySummaryByReportDateInBase(.-)/>") do
  62. print(p)
  63. local pos = parseargs(p)
  64. cash = pos.cash
  65. end
  66. -- array of transactions.
  67. local summary = parseBlock(statementContent, 'StmtFunds')
  68. local transactions = {}
  69. for p in summary:gmatch("<StatementOfFundsLine(.-)/>") do
  70. --print(p)
  71. local sm = parseargs(p)
  72. print(#transactions,sm.transactionID,sm.reportDate,sm.settleDate,sm.description,sm.activityDescription,sm.amount,sm.activityCode)
  73. if sm.activityCode ~= 'ADJ' then
  74. transactions[#transactions + 1] = {
  75. name=sm.description,
  76. amount=sm.amount,
  77. currency="EUR",
  78. bookingDate=stringToTimestamp(sm.reportDate),
  79. valueDate=stringToTimestamp(sm.settleDate),
  80. transactionCode=sm.transactionID,
  81. purpose=sm.activityDescription,
  82. bookingText=sm.activityCode
  83. }
  84. end
  85. end
  86. -- Return balance and array of transactions.
  87. --print(JSON():set(transactions):json())
  88. return {
  89. balance = cash,
  90. transactions = transactions
  91. }
  92. end
  93. end
  94.  
  95. function EndSession()
  96. -- Logout.
  97. end
Advertisement
RAW Paste Data Copied
Advertisement