python - 基于csv数据使用nsetools并行获取股票价格

标签 python performance python-2.7 csv financial

import time
import argparse
import pandas as pd
from nsetools import Nse

nse = Nse()
t = time.time()
FILE_LOCATION = ''  # csv file, blank because path not relevant to others
df = pd.read_csv(FILE_LOCATION)

CSV 文件内容:

Instrument,Qty,Avg buy price
APLAPOLLO,3,949.95
AVANTIFEED,6,554.55
BALAMINES,9,337.72
BALMLAWRIE,4,258.5
BANCOINDIA,15,217
DCMSHRIRAM,12,261.4
GHCL,12,267.2
GIPCL,27,101.95
JAMNAAUTO,15,182.1
JBCHEPHARM,15,344.85
KEI,24,143.95
KPRMILL,6,569.65
KRBL,9,312
MPHASIS,6,533.95
SHEMAROO,2,413.25

代码:

# using argparse to provide options for obtaining closePrice or buyPrice1
# of stocks
parser = argparse.ArgumentParser(description='Stock Quote fetcher')
parser.add_argument('-r', '--realtime', help='Obtain realtime stock\
                    quotes', action='store_true')
args = parser.parse_args()


def get_closing(stock):
    """Function to obtain closePrice or buyPrice1 of stocks"""
    if args.realtime:
        return nse.get_quote(stock)['buyPrice1']
    else:
        return nse.get_quote(stock)['closePrice']


# calculating current value of investment
current_value = sum(get_closing(row[0]) * row[1] for index, row in
                    df.iterrows())

print(current_value)
print("Completed in ", time.time() - t)

目前股票价格是使用生成器表达式顺序获取的。这样做需要 18-25 秒来计算当前的投资值(value)。有什么办法可以同时获得这些价格并计算投资的现值吗?

最佳答案

我已经 fork 了存储库 here ,我一直致力于将 nsetools API 移动到基于 DataFrame 的方法,并使用线程来减少获取多个引号所需的时间。

截至今天的最新版本 v1.1.0 目前已实现所有这些功能。

但是,不保证 python2 兼容性。如果您希望请求更多功能,请务必创建一个新问题。我会继续努力并不断更新存储库。

关于python - 基于csv数据使用nsetools并行获取股票价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41693675/

相关文章:

python - 'ascii' 编解码器无法解码字节 0xef 的位置

Python:从字典中替换文本文件中的多个单词

Python 元编程 : generate a function signature with type annotation

python - SQLAlchemy:如何永久按列对表进行排序?

python - 使用 tf.data.Dataset.batch 时出现的问题

python:lib/site-packages/site.py 和 lib/site.py 之间的相互作用

python - 如何使用 python 处理 dbus 接口(interface)的属性?

c# - 在 Windows 窗体中呈现文本的最快 API?

matlab - 绘制随机数的非满矩阵

c# - 在 C# 中使用值参数模拟模板