Python:使用时序财务数据向量化模拟器

问题描述

一天内来自多个行情的行情数据的数据帧: df = pd.DataFrame({'time':[090000,090000,090000,090001 ... 150000],'ticker':[A,B,A,C ... Z],'price':[10, 20,10,30 ... 40],'volume':[-10,20,-100 ... 50]})

使用for循环制作了一个模拟器,但是当然要花很长时间才能进行回测。 我列出了一个行情清单,并循环了每个行情清单,以便通过行情清单创建子数据帧并测试逻辑。 这是我对代码进行矢量化的尝试(但是它只会使性能降低一半):

def logic(time_ticker):
   preset = selected_df.index.isin(pd.date_range(end=time_ticker,periods=2,freq='S').shift(-1)) #doing this to see how many transactions happened during two seconds ex) if time_ticker is 090000 than it will look for transactions in 095958 and 095959 but not 090000
   adj_df = selected_df.loc[preset]
   if len(adj_df) >5: #see if more than 5 transactions occurred during 2 seconds
      if adj_df.volume.sum() > 100: #if sum of volumes during 2 seconds is great than 100 shares
         buy()

def simulator(ticker):
   preset = df.ticker.isin([ticker]) #from main df,make sub-dataframe consist of selected ticker
   selected_df = df.loc[preset]
   time_idx = selected_df[~selected_df.index.duplicated()].index #time index of ex) of 090000 ~ 150000
   list(map(lambda row: logic(row),time_idx))

def main()
   df = pd.read_csv(PATH_FILE_BOOK) #load df
   ticker_list = list(set(df['ticker'].tolist())) #make ticker list
   list(map(lambda row: simulator(row),id_list)) 

main()

如何使用矢量化加快处理速度?或任何处理技巧将不胜感激。 谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)