Code Monkey home page Code Monkey logo

node-finance's Introduction

FINANCE

NPM version

This module contains an implementation of Markowitz algorithm for the portfolio optimization, a routine for retrieving historical prices from Yahoo, statistical information for stocks and a routine for calculating implied volatility using Black and Scholes formula.

Example

See examples directory.

For a frontend, see node-conpa.

Installation

To install with npm:

npm install finance

Tested with Node.js 14.x, R 3.4.3 and Rserve 1.7.3.

Methods

keystatistics.getKeyStatistics(params, callback)

It retrieves the key statistics for the stocks and returns an array of objects to create an uneditable form on front-end side.

Params

  • symbol asset symbol.

Callback response

  • keyStatistics object.

optionchain.getOptionChainFromYahoo(params, callback)

It retrieves the strike values for calls and puts from Yahoo! Finance.

Params

  • symbol asset symbol.
  • expiration expiration month. Has to be in the form: "YYYY-MM-DD", example: "2016-01-03".

Callback response

  • optionChain

    • strike strike value of the asset.
    • expDateStr expire date (string).
    • expDate expire date (Date).
    • now Date.
    • calls vector of call values.
    • puts vector of put values.

parsecsv.parse(arr, options)

Arguments

  • arr is the string containing the comma separated value content.

  • options

    • skipHeader flag to skip the first row (dafault false).
    • delimeter is the delimeter between the fields (default ",").
    • reverse to reverse the rows (default false).
    • column is the column to extract (default 0).
    • replaceZeroes flag to replace zeroes with the previous value (default false).
    • skipNRecords flag to skip records (default 0).

performances.getPerformances(x, weights)

It calculates the weighted performance for a matrix.

Arguments

  • x matrix containing the values (i.e. the asset returns).
  • weights the weights

Returns a vector containing the weighted perfomance of the matrix.

portfolio.getOptimalPortfolio(params, callback, config)

It creates an optimal portfolio. If config is defined, the method call a Rserve instance, otherwise a native implementation is used.

Params

  • prods vector of symbols.
  • referenceDate reference date (String).
  • targetReturn weekly target return, if undefined, the mean of returns.
  • lows vector of constraints.
  • highs vector of constraints.
  • shorts a logical indicating whether shortsales are allowed.

Callback response

  • perf performances vector.

  • message error message, if empty the optimization is fine.

  • optim details of quadprog response.

    • solution vector of weights.
    • value the value of the quadratic function at the solution.
    • unconstrained_solution vector of the unconstrained minimizer.
    • iterations the number of iterations the algorithm needed.
    • iact vector with the indices of the active constraints at the solution.
    • message error message, if empty the optimization is fine.
    • pm portfolio return.
    • ps portfolio risk.

Config

  • host hostname or ip address of R instance.
  • port port of Rserve instance.
  • user username for remote connection of Rserve instance.
  • password password for remote connection of Rserve instance.
  • debug boolean to enable rio logging.

portfolio.getScriptOptimalPortfolio(params, callback)

It retrieves the source code of the R script calculating the optimal portfolio.

Params See portfolio.getOptimalPortfolio.

Callback response

  • source the source code of the script.

quotes.getQuotes(symbol, refDate, callback)

It retrieves the prices from Yahoo! finance.

Arguments

  • symbol asset symbol.
  • refDate reference date (Date).

Callback response

  • error calback error.

  • symbol asset symbol.

  • prices

    • beforeRefDate CSV string of prices before reference date.
    • afterRefDate CSV string of prices after reference date.

returns.getReturns(symbols, refDate, callback)

It retrieves the prices from Yahoo! finance and calculates the log returns of the close prices.

Arguments

  • symbols vector containing the symbols of the assets.
  • refDate reference date (String).

Callback response

  • returns

    • message message error.
    • beforeRefDate vector of log returns of close prices before reference date.
    • afterRefDate vector of log returns of close prices after reference date.

riskfreerate.getRiskFreeRateFromYahoo(callback)

It retrieves the risk free rate from Yahoo! Finance.

Callback response

  • riskfree risk free rate.

volatility.getImpliedVolatility(params, callback)

It calculates the implied volatility for an option using Black and Scholes formula.

Arguments

  • symbol asset symbol.

Callback response

  • option

    • strike strike of the asset.
    • riskfree risk free rate.
    • expDate expire date (string).
    • callVolatility implied volatility for the calls.
    • putVolatility implied volatility for the puts.

node-finance's People

Contributors

albertosantini avatar dependabot[bot] avatar theosp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-finance's Issues

matrix D in quadratic function is not positive definite!

Hi Alberto,

Great repo, thanks a lot.
The only reason, why I cannot use it, is the error:
"matrix D in quadratic function is not positive definite!".
I tried applying node-finance to normal stock portfolios.
I think, I should not be the only one having this issue.
Do you get this message as well ?
Is it a principal problem,
or is there any work around ?
I think you took the approach from R.
Does R have the same issue ?
Are there alternative NodeJS solutions ?

Thanks in advance

long/short portfolio

Hello Alberto,
Great work. Thanks a lot.
I'm wondering how to get the optimum for a long/short portfolio.
I checked out portfolioOptim( x, pm, lows, highs )
with the lows vector set to [ -1, -1, -1 ],
but I didn't get a solution.
Can portfolioOptim() calculate long/short portfolios ?
How to call it ?
Thanks.
Ulrich

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.