finance

Rounding floats with non-exact representation

那年仲夏 提交于 2019-12-06 00:25:26
We have a problem with rounding of floating point numbers for some financial calculations. Basically, we want to round monetary amounts like 1000000000.555 to 2 decimals. However, the float representation of this number is 1000000000.5549999 and as a result we will round down to 1000000000.55 rather than the correct 1000000000.56. Is there any way we can come around this problem in a safe way? The only thing we have come up with so far is to always add the smallest representable float to the amount before the rounding operation, but we don't know if this is safe in all cases. The code is

Fast loan rate calculation for a big number of loans

好久不见. 提交于 2019-12-05 13:16:41
I have a big data set (around 200k rows) where each row is a loan. I have the loan amount, the number of payments, and the loan payment. I'm trying to get the loan rate. R doesn't have a function for calculating this (at least base R doesn't have it, and I couldn't find it). It isn't that hard to write both a npv and irr functions Npv <- function(i, cf, t=seq(from=0,by=1,along.with=cf)) sum(cf/(1+i)^t) Irr <- function(cf) { uniroot(npv, c(0,100000), cf=cf)$root } And you can just do rate = Irr(c(amt,rep(pmt,times=n))) The problem is when you try to calculate the rate for a lot of payments.

Are there any free APIs for retrieving the S&P 500's component symbols? [closed]

≯℡__Kan透↙ 提交于 2019-12-05 08:23:25
Closed. This question is off-topic . It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . Some sort of free REST API would be ideal, but in general is there any free API or web service or CSV file (that's not behind a password prompt) or anything out there that one can query to get the current list of the S&P 500 index constituents? I've looked on S&P's site itself ( http://www.standardandpoors.com ), through Yahoo Finance's API, and Markit on demand ( http://dev.markitondemand.com/ ), but have not

Get complete stock list for specific index

二次信任 提交于 2019-12-05 07:33:22
问题 Just wondering what kind of free finance API's are out there. I'm looking for something where I can specify the index and get a list of all the companies, by ticker symbol, that are included in that index. Currently I'm only interested in NYSE-listed company stocks. I'd really appreciate any suggestions on API's or methods. 回答1: Other than doing the hard work, like downloading the symbols for the various exchanges from NASDAQ, then is a way to get Google Finance to display all of the stocks

Optimize moving averages calculation - is it possible?

廉价感情. 提交于 2019-12-05 03:48:41
问题 Is it possible to optimize (make it much faster) this piece of code: out <- do.call(rbind, lapply(split(Cl(cumulativeBars), "days"), function(x) { previousFullBars <- barsEndptCl[as.Date(index(barsEndptCl), tz=indexTZ(barsEndptCl)) < as.Date(last(index(x)), tz=indexTZ(x)), ] if (NROW(previousFullBars) >= 4) { last(SMA(last(rbind(previousFullBars, x), n=6), n=5)) } else { xts(NA, order.by=index(x)) } })) Below you can find my original question with all the code example that runs but a bit to

Adding Multiple Chart Series in Quantmod R

时光总嘲笑我的痴心妄想 提交于 2019-12-05 00:55:28
问题 I am trying to plot two charts on one chartSeries in quantmod in R. I am having some difficulty doing this. library(quantmod) tickers <- c('GLD', 'GDX') data <- new.env() getSymbols(tickers, src = 'yahoo', from = '1980-01-01', env = data) chartSeries(Cl(data$GLD), TA="addTA(Cl(data$GDX), on=1)") addRSI() 回答1: You could use chart_Series instead of chartSeries . chart_Series(Cl(data$GLD)) add_TA(Cl(data$GDX), on = 1) And then if you want RSI below in a sub panel, just add add_RSI() . Another

trying to compare two distributions

别说谁变了你拦得住时间么 提交于 2019-12-05 00:33:15
问题 I found this code on internet that compares a normal distribution to different student distributions: x <- seq(-4, 4, length=100) hx <- dnorm(x) degf <- c(1, 3, 8, 30) colors <- c("red", "blue", "darkgreen", "gold", "black") labels <- c("df=1", "df=3", "df=8", "df=30", "normal") plot(x, hx, type="l", lty=2, xlab="x value", ylab="Density", main="Comparison of t Distributions") for (i in 1:4){ lines(x, dt(x,degf[i]), lwd=2, col=colors[i]) } I would like to adapt this to my situation where I

Showing what quarter of a financial year a date is in

a 夏天 提交于 2019-12-04 23:49:49
问题 I'm trying to construct a query that will map two columns, one, a date from a table, the second column an alias to show what quarter and financial year the date falls into. Unfortunately I don't have enough knowledge of SQL to know where to begin. I know that I'd do this with a combination of getdate() and dateadd(MONTH,,) however nothing that I've put together has come close to working. To further complicate this the financial years in Australia go from 1st July- 30th of June so quarter one

Fundamental Data Using IbPy

纵饮孤独 提交于 2019-12-04 21:37:23
I am trying to use IbPY to pull the price of a stock along with its financial statements. I'm new to python and don't entirely understand the complexities of calling some of the different methods within IbPy. I wrote some code to loop through the SP 500 and pull the bid/ask for each stock. I was hoping someone might be able to help me figure out the next step to pull the financial statements. Thoughts on the best way to do that? from ib.opt import ibConnection, message from ib.ext.Contract import Contract from ib.ext.EWrapper import EWrapper from time import sleep import csv with open(r'C:

Pause URL request Downloads

北城余情 提交于 2019-12-04 21:25:34
import urllib.request import re import csv import pandas as pd from bs4 import BeautifulSoup columns = [] data = [] f = open('companylist.csv') csv_f = csv.reader(f) for row in csv_f: stocklist = row print(stocklist) for s in stocklist: print('http://finance.yahoo.com/q?s='+s) optionsUrl = urllib.request.urlopen('http://finance.yahoo.com/q?s='+s).read() soup = BeautifulSoup(optionsUrl, "html.parser") stocksymbol = ['Symbol:', s] optionsTable = [stocksymbol]+[ [x.text for x in y.parent.contents] for y in soup.findAll('td', attrs={'class': 'yfnc_tabledata1','rtq_table': ''}) ] if not columns: