-
Notifications
You must be signed in to change notification settings - Fork 92
Query Finance API for Stocks #152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a9ac0ec
Allow querying four stocks with start and end date
jonpsy 0e39165
accept a vector of stocks
jonpsy a42db0a
fix default filename
jonpsy f545732
no need to print
jonpsy 4f32200
shed column names and first row
jonpsy 56780f1
normalize
jonpsy 2debd87
- filepath created if not exist
jonpsy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#ifndef CPORTFOLIO_HPP | ||
#define CPORTFOLIO_HPP | ||
|
||
#define PY_SSIZE_T_CLEAN | ||
#include <Python.h> | ||
#include <string> | ||
|
||
int PortFolio(const std::vector<std::string>& stocks, | ||
const std::string& dataSource, | ||
const std::string& start, | ||
const std::string& end, | ||
const std::string& filePath = "portfolio.csv") | ||
{ | ||
PyObject *pName, *pModule, *pFunc; | ||
PyObject *pArgs, *pValue; | ||
int i; | ||
|
||
Py_Initialize(); | ||
PyRun_SimpleString("import sys"); | ||
PyRun_SimpleString("sys.path.append(\".\")"); | ||
PyRun_SimpleString("sys.path.append(\"/srv/conda/envs/notebook/include/\")"); | ||
pName = PyUnicode_DecodeFSDefault("portfolio"); | ||
|
||
pModule = PyImport_Import(pName); | ||
Py_DECREF(pName); | ||
|
||
if (pModule != NULL) | ||
{ | ||
pFunc = PyObject_GetAttrString(pModule, "cportfolio"); | ||
size_t numStocks = stocks.size(); | ||
if (pFunc && PyCallable_Check(pFunc)) | ||
{ | ||
pArgs = PyTuple_New(numStocks + 4); | ||
|
||
for(size_t idx = 0; idx < numStocks; ++idx) | ||
{ | ||
PyObject* pValueCurrentStock = PyUnicode_FromString(stocks[idx].c_str()); | ||
PyTuple_SetItem(pArgs, idx, pValueCurrentStock); | ||
} | ||
|
||
PyObject* pValueSource= PyUnicode_FromString(dataSource.c_str()); | ||
PyTuple_SetItem(pArgs, numStocks, pValueSource); | ||
|
||
PyObject* pValueStart = PyUnicode_FromString(start.c_str()); | ||
PyTuple_SetItem(pArgs, numStocks + 1, pValueStart); | ||
|
||
PyObject* pValueEnd = PyUnicode_FromString(end.c_str()); | ||
PyTuple_SetItem(pArgs, numStocks + 2, pValueEnd); | ||
|
||
PyObject* pValueFilePath = PyUnicode_FromString(filePath.c_str()); | ||
PyTuple_SetItem(pArgs, numStocks + 3, pValueFilePath); | ||
|
||
pValue = PyObject_CallObject(pFunc, pArgs); | ||
Py_DECREF(pArgs); | ||
if (pValue != NULL) | ||
{ | ||
Py_DECREF(pValue); | ||
} | ||
else | ||
{ | ||
Py_DECREF(pFunc); | ||
Py_DECREF(pModule); | ||
PyErr_Print(); | ||
fprintf(stderr,"Call failed\n"); | ||
return 1; | ||
} | ||
} | ||
else | ||
{ | ||
if (PyErr_Occurred()) | ||
PyErr_Print(); | ||
} | ||
|
||
Py_XDECREF(pFunc); | ||
Py_DECREF(pModule); | ||
} | ||
else | ||
{ | ||
PyErr_Print(); | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import os | ||
from pandas_datareader import data | ||
|
||
def cportfolio(stocks, dataSource, start, end, filePath='portfolio.csv'): | ||
test = data.DataReader(stocks , dataSource, start=start, end=end) | ||
test = test['Adj Close'] | ||
|
||
returns = np.log(test/test.shift(1)) | ||
returns.reset_index(inplace=True) | ||
# Drop the first row since it contains NaN. | ||
returns = returns.iloc[1:] | ||
# Normalize dates. | ||
returns['Date'] = returns['Date'].apply(lambda x : x.strftime('%Y%m%d')) | ||
|
||
# Create directory if doesn't exist. | ||
directory = os.path.dirname(filePath) | ||
if not os.path.exists(directory): | ||
os.makedirs(directory) | ||
|
||
returns.to_csv(filePath, header=False, index=False) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!