2016-03-16 81 views
-1
{ 
    "table": "alltrades", 
    "rows": 
    [ 
     { 
      "timestamp": 1458042948000, 
      "tid": 4525616, 
      "price": 0.03137600, 
      "amount": 2.18500000, 
      "type": 1 
     }, 
     { 
      "timestamp": 1458042949000, 
      "tid": 4525617, 
      "price": 0.03137600, 
      "amount": 2.18500000, 
      "type": 1 
     }, 
     { 
      "timestamp": 1458042952000, 
      "tid": 4525618, 
      "price": 0.03137500, 
      "amount": 0.00477131, 
      "type": 2 
     }, 
     { 
      "timestamp": 1458042953000, 
      "tid": 4525619, 
      "price": 0.03137500, 
      "amount": 0.00475697, 
      "type": 2 
     }, 
     { 
      "timestamp": 1458042953000, 
      "tid": 4525620, 
      "price": 0.03137500, 
      "amount": 0.00037928, 
      "type": 2 
     }, 
     { 
      "timestamp": 1458042954000, 
      "tid": 4525621, 
      "price": 0.03137500, 
      "amount": 0.00024350, 
      "type": 2 
     }, 
     { 
      "timestamp": 1458042954000, 
      "tid": 4525622, 
      "price": 0.03137500, 
      "amount": 2.00000000, 
      "type": 2 
     }, 
     { 
      "timestamp": 1458042954000, 
      "tid": 4525623, 
      "price": 0.03137600, 
      "amount": 0.00018831, 
      "type": 1 
     }, 
     { 
      "timestamp": 1458042955000, 
      "tid": 4525624, 
      "price": 0.03137600, 
      "amount": 0.00003273, 
      "type": 1 
     }, 
     { 
      "timestamp": 1458042957000, 
      "tid": 4525625, 
      "price": 0.03137600, 
      "amount": 0.00077868, 
      "type": 1 
     }, 
     { 
      "timestamp": 1458042958000, 
      "tid": 4525626, 
      "price": 0.03137600, 
      "amount": 0.00000411, 
      "type": 1 
     }] 
} 

條件我有一個像上面我需要基於以下條件 他們羣裏型1=buy2=selltid意味着交換 如果我們買一個發生在時間x數據,另一個買1秒後,另一個買1秒後,另一買1秒後,然後3秒無交易,然後再買 然後我們有4個交易 ,我們需要總結在一起 ,然後休息3秒 之後會發生另一次購買,但我們不會再添加那個,那麼最好的辦法是返回timestamp x ,包括總金額和VWAP價格的duration: 3 secondstype=buytype=sellJSON數據分組基於node.js的中

所以條件等在它們之間相同的交換,相同的類型和1秒時間間隔,然後組數據等 average = (amount*price + amount1*price1)/(amount + amount1)

,並且如果交換不同類型或不同的時間或不同的時間間隔比離開,因爲它是。

+0

這是什麼問題? (讓貿易交易){if(trade.type === 1 &&/* whatever * /)/ *在這裏找到所有匹配* /}' –

+0

但是如何讓同一交易同類型和一秒。間隔然後agrigate他們? –

回答

2
var last = 0; 
    var last1 = 0; 
    var tmp = _.groupBy(obj["rows"], function(d){ 
     var test2 = d["timestamp"]; 
     if(last == 0){ 
      last = test2; 
     }else{ 
      if((test2 - last1) <= 1000 && (test2 - last1) >= 0){ 

      } 
      else 
      { 
       last = test2; 
      } 
     } 
     last1 = test2; 
     return last; 
    }); 
    // Show the temporary result :o) 
    // Now group the result with currency code 
    var tmp2 = {}; 
    _.each(tmp, function(t, unit){ 
     tmp2[unit] = _.groupBy(t, function(d){ 
     return d["type"]; 
     }); 
    }); 


    // show the temp result again 
    var finalResult = []; 
    _.each(tmp2, function(t, unit){ 
     _.each(t, function(items, currency){ 
     var total = 0; 
     var totalamount = 0; 
     var count = 0; 
     var VWAP = 0; 
     //console.log(items); 
     _.each(items, function(item){ 
      total += item["price"];  // should also * currencyCode? 
      totalamount += item["amount"]; 
      VWAP += item["price"] * item["amount"]; 
      count++; 
     }); 
     finalResult.push({ 
      "timestamp" : unit 
      , "price" : total 
      , "amount" : totalamount 
      , "VWAP" : VWAP/count 
      , "type" : currency // Update it yourself :o) 
      , "count" : count 
     }); 
     }); 
    }); 

    console.log(finalResult); 
+0

我用上面的函數來創建json數據 –