【问题标题】:Obtain stock prices in parallel using nsetools based on csv data基于csv数据使用nsetools并行获取股票价格
【发布时间】:2017-01-17 09:41:19
【问题描述】:
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 秒来计算当前的投资价值。有什么方法可以同时获得这些价格并计算当前投资价值?

【问题讨论】:

  • 问题可能是nse 需要一些时间来获取数据。也许它可以在一个请求中获得许多股票。
  • nsetools 没有提供一次请求获取多只股票价格的功能。
  • 并行化进程,不知道用nse能打开多少个连接。但是您可以分叉到 N 个进程中,该进程只询问一个 sock 并将其作为附加列保存到 csv 中,等待所有进程完成,然后求和。
  • 由于这些是通过 Internet 传输的文件(我假设),它们是 I/O 绑定的,并且多个线程可以正常工作来传输文件。

标签: python performance python-2.7 csv financial


【解决方案1】:

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

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

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

【讨论】:

  • 我尝试访问链接,github说找不到页面
  • 感谢您的指出。我已经更新了链接。请检查
  • 详细文档的链接抛出 404 not found 错误
  • 是的。我知道。它也有一个未解决的问题。我无法生成文档。如果可以使用适当的文档开始 PR,那将是非常有帮助的。代码使用文档字符串正确注释,因此文档只是使用 sphinx(或任何替代)调用正确命令的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-23
  • 1970-01-01
  • 2019-06-27
  • 2019-06-02
  • 2021-03-04
  • 1970-01-01
相关资源
最近更新 更多