簡體   English   中英

Python 更新數據幀的更快方法

[英]Python Faster way of updating dataframe

我正在嘗試測試三種不同股票的買賣。 我創建了一個課程,我計划將其插入人工智能系統並嘗試找到一種策略。 這目前有效。 您可以買入 [symbol],賣出 [symbol],然后繼續。 某些功能花費的時間太長。 我相信這是一種更快、更 Python-y 的方式來做到這一點。 我的背景是 JavaScript。

我正在使用數據框來存儲交易。 未平倉交易沒有 closeTimeStamp。 利潤是sell_price - 已平倉交易的buy_price 或利潤是sell_price - 未平倉交易(即空頭)的current_quote 利潤是current_quote - 多頭未平倉交易的buy_price。 在我更新了 Holdings_with_quotes 之后,我可以對利潤列進行求和,並獲得未平倉和已平倉交易的當前值。

    self.trades = pd.DataFrame(columns=['symbol', 'buy_price', 'sell_price', 'current_quote', 'openTimeStamp', 'closeTimeStamp', 'profit', 'multiplier'])

這個功能需要這么多時間。

  def update_holdings_with_quotes(self):
    start = time.time()
    if self.current_contracts > 0:
      quotes = self.quotes
      for symbol in ['/ES', '/NQ', '/YM']:
      # for symbol in self.trades['symbol']:
        current_price = self.quotes.loc[symbol]['lastPriceInDouble']
        multiplier = self.quotes.loc[symbol]['futureMultiplier']
        self.trades.loc[self.trades['closeTimeStamp'].isnull() & (self.trades['symbol'] == symbol), 'current_quote'] = current_price
        self.trades.loc[self.trades['closeTimeStamp'].isnull() & (self.trades['symbol'] == symbol) & (self.trades['action'] == Actions.BUY), 'profit'] =  (current_price - self.trades['buy_price']) * multiplier
        self.trades.loc[self.trades['closeTimeStamp'].isnull() & (self.trades['symbol'] == symbol) & (self.trades['action'] == Actions.SELL), 'profit'] =  (self.trades['sell_price'] - current_price)  * multiplier

      self.current_value = self.initial_value + self.trades['profit'].sum()
      self.current_gain = self.current_value - self.initial_value
    print("update_holdings_with_quotes time: {}".format(time.time() - start))

基本上,如果交易沒有 closeTimeStamp 又名交易仍然打開,我將循環遍歷我的數據框中的三個報價和設置值。 我嘗試使用一組靜態符號,但這並沒有加快速度。

我可以使用數據框以外的其他東西。 我只是使用它,因為我認為它會有所幫助。

*** 我根據使用兩個數據框而不是一個的建議編輯了該函數。 一種用於開放交易,一種用於關閉交易。 那沒有多大幫助。

  def update_holdings_with_quotes(self):
    start = time.time()
    if self.current_contracts > 0:
      quotes = self.quotes
      for symbol in ['/ES', '/NQ', '/YM']:
      # for symbol in self.trades['symbol']:
        current_price = self.quotes.loc[symbol]['lastPriceInDouble']
        multiplier = self.quotes.loc[symbol]['futureMultiplier']
        self.open_trades.loc[(self.open_trades['symbol'] == symbol), 'current_quote'] = current_price
        self.open_trades.loc[(self.open_trades['symbol'] == symbol) & (self.open_trades['action'] == Actions.BUY), 'profit'] =  (current_price - self.open_trades['buy_price']) * multiplier
        self.open_trades.loc[(self.open_trades['symbol'] == symbol) & (self.open_trades['action'] == Actions.SELL), 'profit'] =  (self.open_trades['sell_price'] - current_price)  * multiplier

      self.current_value = self.initial_value + self.open_trades['profit'].sum() + self.closed_trades['profit'].sum()
      self.current_gain = self.current_value - self.initial_value
      # self.logger.info('initial_value={} current_value={} current_contracts={}'.format(self.initial_value, self.current_value, self.current_contracts))
      self.check_status()
    print("update_holdings_with_quotes time: {}".format(time.time() - start))

這是它變慢的部分:

    self.trades.loc[self.trades['closeTimeStamp'].isnull() & (self.trades['symbol'] == symbol), 'current_quote'] = current_price
    self.trades.loc[self.trades['closeTimeStamp'].isnull() & (self.trades['symbol'] == symbol) & (self.trades['action'] == Actions.BUY), 'profit'] =  (current_price - self.trades['buy_price']) * multiplier
    self.trades.loc[self.trades['closeTimeStamp'].isnull() & (self.trades['symbol'] == symbol) & (self.trades['action'] == Actions.SELL), 'profit'] =  (self.trades['sell_price'] - current_price)  * multiplier

特別是在每個循環中多次詢問以查看整個“交易”DataFrame 僅用於索引:

self.trades['closeTimeStamp'].isnull()
self.trades['symbol'] == symbol

一個直接的解決方案是將您的 DataFrame trades分成兩個closed_tradesopen_trades 隨着時間的推移,后者應該明顯更小,並極大地加快查找速度。 隨着您跟蹤的股票的增長,通過子類進一步將其拆分為符號可能是明智的。

另一種方法是跟蹤數組中未平倉頭寸的索引。 每次添加交易時,只需將maxIndex+1添加到您的數組中。 如果您關閉交易,請從列表中刪除該指數。 只要確保不要重新索引。

import numpy as np
ix_open = np.array([...])
ix_closed = np.array([...])

# after closing a few trades:
ix_open = np.setdiff1d(ix_open, ix_closed)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM