Hot answers tagged

17

My deal is HFT so what I care about is read/load data from file or DB quickly in memory perform very efficient data-munging operations (group,transform) visualize easily the data I think is is pretty clear that 3. goes to R, graphics and ggplot2 and others allow you to plot anything from scratch with little effort. About 1. and 2. I am amazed reading ...


13

I've used both R and Python with Pandas in a professional quantitative financial work to do both large and small scale projects. I would strongly recommend Python with Pandas over R for most new projects in the field especially in time series analysis. While I don't dispute vonjd in that you will find more libraries in R with algorithms on the bleeding ...


13

This is interesting because I see another trend: Matlab is being replaced by R, but I guess this is another story. I use R for my academic (I am also teaching this stuff) as well as my consulting work (I am mainly working in the $\mathbb{P}$ area, with some excursions into $\mathbb{Q}$). I tried Python but it didn't work for me. I think the main reasons I ...


11

Instead of wild guesses about R's/python's future in the community, here some facts: The following query on StackExchange Data Explorer counts the number of questions that have <r> or <python> tags. If you scroll down on one of the three webpages provided below, you can see a graph with data on a monthly basis. You can easily run this query on ...


8

For data analysis, particularly for large data analysis project, pretty much most of the top quant hedge funds and a lot of the banks are using Python (over R) for a couple of reasons but many still have bits and pieces of R for specific packages or functions (I work at a bank and interface with quite a few quant hedge funds on data analysis): Earlier ...


4

For the tasks listed, both Python and R perform very well. There are some packages in Python not in R and vice versa. My solution for this is to simply call R from Python. This allows for the best of both worlds. It is also important to note I do not write any R code other than calling an R library from Python. Calling Python from R does not work equally ...


4

This does the trick. Given "U8", function will get the IMM date for Sep 2018. Given "U4", function gets the IMM date for Sep 2024 (or it will until we've passed that date). Could be easily modified to also pull historical IMMs from a given longer symbol eg. "U2014". def getIMMDate(IMMcode): '''Takes 2 digit IMM code and returns effective date as datetime ...


4

OpenTSDB is good for large-scale time series storage. metrilyx/opentsdb-pandas and wiktorski/opentsdb_pandas seems to provide the interface with pandas. OpenTSDB and HBase rough performance test | MoreDevs provides a benchmark, may not exactly match your requirements but you can try.


4

The installation process should be the same as on Linux. Once you have the C++ QuantLib library installed (instructions for that are on the QuantLib site, at http://quantlib.org/install/macosx.shtml) you can download the latest QuantLib-SWIG release, uncompress it and run: ./configure make -C Python sudo make -C Python install Note that the above work ...


4

In the call to Bisection.solve, the question mark must be the Python function whose zero you want to find. In your case, it should be something reproducing the logic of IRRSolver::operator() in Mick Hittesdorf's code, i.e., something like this (which I haven't tested): cashflows = fixedRateBond.cashflows() npv = fixedRateBond.NPV() def ...


3

They expire 30 days before the expiration of the S&P monthly options. The latter usually expire on the third Friday of the month (however, in rare cases the S&P opts. expire on Thursday because the Friday is a holiday; the last time it happened was April 17, 2014 since April 18 2014 was a NYSE holiday). Neglecting the holiday thing, the expiration ...


3

What you need is more mutual information rather than Shannon entropy. It is dedicated to capture the influence of one variable on another (you can think about it as a non linear version of Pearson correlations). They are closely related since the mutual information $I$ between two variables $X$ and $Y$ reads: $$I(X;Y) = H(X,Y) - H(X|Y) - H(Y|X)$$ where $H$ ...


3

You can get this using a pandas rolling_max to find the past maximum in a window to calculate the current day's drawdown, then use a rolling_min to determine the maximum drawdown that has been experienced. Lets say we wanted the moving 1-year (252 trading day) maximum drawdown experienced by a particular symbol. The following should do the trick: import ...


3

I found out what I was doing wrong - the OLS function was regressing with no intercept value - so I had to use the "add_constant" method to add an intercept term to the X series (z_lag) as follows: z_lag = np.roll(z_array,1) z_lag[0] = 0 z_ret = z_array - z_lag z_ret[0] = 0 #adds intercept terms to X variable for regression z_lag2 = sm.add_constant(z_lag) ...


3

Remember that all back testing is full of lies assumptions. Latency (both line latency and latency internal to the exchanges), adverse selection, market impact (yes, even you have market impact), etc, are all based on assumptions. These assumptions are educated guesses at best, but more often terrible models are used (you always get filled at at mid!) and ...


2

I calculate duration in Python using numpy, it's nice and simple: def durations(cfs, rates, price, ytm, no_coupons): import numpy as np mac_dur = np.sum([cfs[i]*i/np.power(1+rates[i],i) for i in range(len(cfs))])/price mod_dur = mac_dur/(1+ytm/no_coupons) return mac_dur, mod_dur


2

A (now) very mature (imho) Python backtesting framework is "backtrader": https://github.com/mementum/backtrader Blog posts with samples and new developments: http://www.backtrader.com The documentation on readthedocs can be reached from any of the above links Some features: Can run in (pseudo)event-mode (called 'next') or (pseudo)vectorized mode (called ...


2

The major advantage of Python (w/ pandas) over R is that Python supports OOP (object-oriented programming). It makes sense to organize a large code base using a hierarchy of classes. Python also supports the notion of polymorphism so that we can use well-known design patterns (e.g., Strategy, Observer, etc.) in our code.


2

It is all in the code:: Rcpp::List rl = Rcpp::List::create(Rcpp::Named("value") = opt.NPV(), Rcpp::Named("delta") = opt.delta(), Rcpp::Named("gamma") = opt.gamma(), Rcpp::Named("vega") = (excType=="european") ? opt.vega() : R_NaN, ...


2

PYTHON I have found this class from the statsmodels library for calculating Garch models. Unfortunately, I have not seen MGARCH class/library. Below you can see the basic information about the garch models in mentioned class from the statsmodels. Probably you have to implement it by your own in python, so this class might be used as a starting point. ...


2

There is a times series DBMS (InfiniFlux) that can be easily used with Python. The database is not open source but it does provide a free version for evaluation, too. So you can try whether the DBMS is suitable for your project. You are asking 2M rows should be processed in less than 30 seconds, InfiniFlux can store and retrieve more than 500,000 data ...


2

The code below pulls AAPL time series from Yahoo Finance, computes mean/std and simulates 100 paths that are 20 days long. Input: import pandas as pd import numpy as np from numpy.random import normal # bring data ticker = 'AAPL' url = 'http://real-chart.finance.yahoo.com/table.csv?s=%s' % ticker data = pd.read_csv(url, index_col='Date', parse_dates=True) ...


2

Because your python script ends and with it the socket that ibpy has opened to connect to TWS. The demo samples in the sources of ibpy use simple time.sleep(x) to make sure some information has been delivered (for example fancy_marketdata.py) before the script ends. Try to remove the time.sleep(x) lines and see how the script also ends instantly. Have a ...


2

backtrader (https://github.com/mementum/backtrader) can do 1 and 3 and is in the process of getting 2 ironed out. A live data feed from IB will make it into the next release (due in the next few days) and it will then be down to mapping of orders. On the project page you can see a list of other similar (some more, some less) python projects and may prove to ...


1

The average would be called the mid-price, not the best in my opinion, but that depends on your modeling. Another strategy is to weight the bid and offer prices according to size, also called the micro-price or bid-offer weighted price. This has the advantage of moving your calculated price closer to where it is traded as volume is depleted from whatever ...


1

Metastock uses a binary format that you would have to convert to text before exporting it to a SQL table. Also, there is no Python library that could extract data directly from Metastock's end-of-day servers. You may consider quandl which allows Python developers to download pricing data as well as economic indicators. You can download CSV, JSON, or XML. If ...


1

As soon as you're comfortable with Python, let's do this exercise in three steps: Download data and calculate cumulative returns (or value of your position as if you invested $1 in each of the stocks) Define function that will capture stock movements in excess of predefined threshold, 10% in this case. This function is going to be the "indicator" you ...


1

I'm guessing your best bet would be to get setup with an SFTP account with Bloomberg. Just contact your rep to get the ball rolling. We upload 10's of protfolio's 10's of times a day. I don't think there is an actual api for pulling data from PRTU as I'm not sure exaclty what you'd pull other than the last upload time.


1

In Python, simple geometric returns: import numpy as np import pandas as pd sp500 = pd.io.data.DataReader('^GSPC', 'yahoo')['Close'] simple_ret = sp500.pct_change() (1+simple_ret).cumprod()[-1] -1 0.74751768460019963 Log-returns: log_ret = np.log(1+simple_ret) np.exp(log_ret.cumsum()[-1]) -1 0.74751768460020074 In ...


1

When doing series like this in Python, I usually just add 1 to each return, then multiply across these sums for cumulative returns. Such as, if my returns over three days were -5.2%, 2.1% & 4.8%, then the values I would store would be: 1 + (-0.052) = 0.948 1 + (0.021) = 1.021 1 + (0.048) = 1.048 Then, to calculate my cumulative returns, I ...



Only top voted, non community-wiki answers of a minimum length are eligible