计算持仓成本,有没有高效的方法

如下代码所示,order表的字段id是订单号,price是价格,amount下单数量(+买入,-卖出),当amount为正时,持仓成本=cumsum(amount*price)/cumsum(amount),当amount为负(卖出时),引用之前计算好的持仓成本。当全卖出即空仓后,持仓成本需要重新算。下面是我的DolphinDB database代码:

id = take(1..9,1000000)
price = take(101..109,1000000)
amount =take( 1 2 3 -2 -1 -3 1 2 3,1000000)
order = table(id, price, amount)

def avg_price(price, amount){
 hold = 0.0;
 cost = 0.0;
 avg_price = 0.0;
 avg_prices = array(double);
 for ( r in table(price,amount) ) {
  hold += r.amount;
  if ( r.amount > 0 ) {
   cost += r.amount * r.price;
   avg_price = iif(hold > 0, cost/hold, 0);
  } else {
   cost += r.amount * avg_price;
  }
  avg_prices.push!(avg_price)
 }
 return avg_prices;
}

select *, iif (amount < 0, amount*(avg_price - price), 0) as profit 
from (
 select *, avg_price(price, amount) as avg_price
 from order
) 

这个方法效率比较低,100万行数据,执行一次要3-4秒,请问有什么办法改进?

请先 登录 后评论

1 个回答

Jason Tang - 时序数据库技术支持

DolphinDB database 从1.01版本开始支持即时编译JIT(https://github.com/dolphindb/... )。JIT又译及时编译或实时编译,是动态编译的一种形式,可提高程序运行效率。上面函数可写成JIT函数如下:

@jit
def avg_price(price, amount){
    hold = 0.0;
    cost = 0.0;
    avgPrice = 0.0;
    n = size(price)
    avgPrices = array(DOUBLE, n, n, 0)
    for ( i in 0..(n-1))  {
        hold =hold + amount[i];
        if ( amount[i] > 0 ) {
            cost = cost + amount[i] * price[i];
            if(hold > 0) {
                avgPrice = cost/hold;
            }else{  
                avgPrice = 0.0;
            }
        } else {
             cost += amount[i] * avgPrice;; 
        }
        avgPrices[i]=avgPrice
    }
    return avgPrices;
}

对上述函数进行测试,用JIT版本比非JIT大约快200倍。

请先 登录 后评论