2016-04-22 46 views
-1

我是新来的python,但不是编程。我怀疑在Python中处理以下内容有一个更明智的方法。我正在寻找表现,不一定是简短的或风格的变化,尽管我愿意改变这两方面的表现。如何提高Python数据表/字典/列表操作的性能?

for asset in securities: 
    if asset not in open_orders: 
     currentPrice = data.current(asset,'price') 
     if asset in assetOwnedHighPrice: 
      if assetOwnedHighPrice[asset] < currentPrice: 
       assetOwnedHighPrice[asset] = currentPrice 
     if asset in assetOwnedLowPrice and currentPrice > 0: 
      if assetOwnedLowPrice[asset] > currentPrice: 
       assetOwnedLowPrice[asset] = currentPrice 

我略微知道设置运算符(方法),如x = y.mean()。我想在两个清单(assetOwnedHighPrice,assetOwnedLowPrice)中结束的是所有资产的最高和最低值。这两个清单预先用0代表所有资产,并随后定期更新。

这个运行的平台将允许建立一个熊猫数据表,其中包含所有的价格。所以我可以说

currentPrices = data.history(securities,'price',1,'1m') 

currentPrices然后将是一个数据表与证券作为列和一行价格。

+5

你能给我们一些样本数据和预期的输出,使我们的生活更轻松。你总是可以使用'min,max'内置的插件 – Bahrom

+0

除了使用'[option1,option2] [x> 0]'技巧来消除对条件分支的需求之外,还有很少的优化,但是没有额外的有关需要完成什么的信息。 –

+0

编辑orignal帖子添加运行该平台将允许建立一个熊猫数据库中的所有价格。所以我可以说 currentPrices = data.history(证券,'价格',1,'1m') currentPrices然后将是一个数据表,证券作为列和一行价格。 –

回答

0

您提到您将进行回溯测试,因此您将拥有所有历史定价。

让下面是代理设置:

import pandas as pd 
from StringIO import StringIO 

text = """Date,200001,200230,200247,200291,200597 
2016-03-31,593.6399,41.830002,11.800001,25.100007,151.45 
2016-04-01,598.5,42.190003,11.57,25.350007,152.52001 
2016-04-04,593.18995,42.320008,11.36,26.0,152.07001 
2016-04-05,586.1399,42.039994,11.07,25.380005,150.0 
2016-04-06,602.08008,43.050004,11.08,25.940003,150.02001 
2016-04-07,591.42994,42.610001,10.86,25.820008,148.25 
2016-04-08,594.6001,42.369996,10.960001,26.339997,149.35001 
2016-04-11,595.92994,42.479996,11.020001,26.080002,149.25 
2016-04-12,603.16993,42.820008,11.16,26.130005,149.63001 
2016-04-13,614.82007,43.350007,11.23,27.470002,151.23""" 

price_df = pd.read_csv(StringIO(text), index_col=0, parse_dates=[0]) 

然后你就可以生产的历史高位价格与历史价格低,像这样:

hi_df = price_df.cummax() 

lo_df = price_df.cummin() 

price_df看起来像

   200001  200230  200247  200291  200597 
Date                
2016-03-31 593.63990 41.830002 11.800001 25.100007 151.45000 
2016-04-01 598.50000 42.190003 11.570000 25.350007 152.52001 
2016-04-04 593.18995 42.320008 11.360000 26.000000 152.07001 
2016-04-05 586.13990 42.039994 11.070000 25.380005 150.00000 
2016-04-06 602.08008 43.050004 11.080000 25.940003 150.02001 
2016-04-07 591.42994 42.610001 10.860000 25.820008 148.25000 
2016-04-08 594.60010 42.369996 10.960001 26.339997 149.35001 
2016-04-11 595.92994 42.479996 11.020001 26.080002 149.25000 
2016-04-12 603.16993 42.820008 11.160000 26.130005 149.63001 
2016-04-13 614.82007 43.350007 11.230000 27.470002 151.23000 

hi_df and lo_df lo好吧,像这些分别

print hi_df 

       200001  200230  200247  200291  200597 
Date                
2016-03-31 593.63990 41.830002 11.800001 25.100007 151.45000 
2016-04-01 598.50000 42.190003 11.800001 25.350007 152.52001 
2016-04-04 598.50000 42.320008 11.800001 26.000000 152.52001 
2016-04-05 598.50000 42.320008 11.800001 26.000000 152.52001 
2016-04-06 602.08008 43.050004 11.800001 26.000000 152.52001 
2016-04-07 602.08008 43.050004 11.800001 26.000000 152.52001 
2016-04-08 602.08008 43.050004 11.800001 26.339997 152.52001 
2016-04-11 602.08008 43.050004 11.800001 26.339997 152.52001 
2016-04-12 603.16993 43.050004 11.800001 26.339997 152.52001 
2016-04-13 614.82007 43.350007 11.800001 27.470002 152.52001 

print lo_df 

       200001  200230  200247  200291 200597 
Date               
2016-03-31 593.63990 41.830002 11.800001 25.100007 151.45 
2016-04-01 593.63990 41.830002 11.570000 25.100007 151.45 
2016-04-04 593.18995 41.830002 11.360000 25.100007 151.45 
2016-04-05 586.13990 41.830002 11.070000 25.100007 150.00 
2016-04-06 586.13990 41.830002 11.070000 25.100007 150.00 
2016-04-07 586.13990 41.830002 10.860000 25.100007 148.25 
2016-04-08 586.13990 41.830002 10.860000 25.100007 148.25 
2016-04-11 586.13990 41.830002 10.860000 25.100007 148.25 
2016-04-12 586.13990 41.830002 10.860000 25.100007 148.25 
2016-04-13 586.13990 41.830002 10.860000 25.100007 148.25