PageRenderTime 56ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ParaBond/src/scaly/parabond/test/price/OnePortfolio00.scala

http://scaly.googlecode.com/
Scala | 96 lines | 48 code | 18 blank | 30 comment | 0 complexity | 1a8241c8f9041a27485936af26257b39 MD5 | raw file
  1. /*
  2. * Copyright (c) Scaly Contributors
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the Scaly Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package scaly.parabond.test
  28. import org.scalatest.junit.JUnitSuite
  29. import org.junit.Assert._
  30. import org.junit.Test
  31. import org.apache.log4j.PropertyConfigurator
  32. import scaly.parabond.entry.SimpleBond
  33. import scaly.parabond.value.SimpleBondValuator
  34. import scaly.parabond.util.Helper
  35. import com.mongodb.casbah.MongoCursor
  36. import com.mongodb.casbah.MongoConnection
  37. import com.mongodb.casbah.commons.MongoDBObject
  38. import com.mongodb.BasicDBList
  39. import com.mongodb.BasicDBObject
  40. import scaly.parabond.util.MongoHelper
  41. class OnePortfolio00 {
  42. val fitter =
  43. List(
  44. 0.022250408186472507
  45. - 0.0034445001723609914,
  46. 0.0015772414196917963,
  47. -1.6876893139502916E-4,
  48. 7.1081143657141285E-6,
  49. -1.0265615090890005E-7)
  50. val mongo = MongoConnection("127.0.0.1")("parabond")
  51. @Test
  52. def test {
  53. val portfsCollecton = mongo("Portfolios")
  54. val portfsQuery = MongoDBObject("id" -> 1)
  55. val portfsCursor : MongoCursor = portfsCollecton.find(portfsQuery)
  56. val bondIds = MongoHelper.asList(portfsCursor,"instruments")
  57. val bondsCollection = mongo("Bonds")
  58. val t0 = System.nanoTime
  59. val value = bondIds.foldLeft(0.0) { (sum, id) =>
  60. val bondsQuery = MongoDBObject("id" -> id)
  61. val bondsCursor: MongoCursor = bondsCollection.find(bondsQuery)
  62. val bond = MongoHelper.asBond(bondsCursor)
  63. // print("bond(" + bond + ") = ")
  64. //
  65. val valuator = new SimpleBondValuator(bond, fitter)
  66. val price = valuator.price
  67. // println("%8.2f".format(price))
  68. //
  69. sum + price
  70. }
  71. val t1 = System.nanoTime
  72. val dt = (t1 - t0) / 1000000000.0
  73. println("value = %10.2f dt = %f".format(value,dt))
  74. }
  75. def process(bondIds : List[Int]) {
  76. bondIds.foreach(println)
  77. }
  78. }