コード例 #1
0
ファイル: RangeData.cpp プロジェクト: pinchaque/alchemy
  void RangeData::summarizeMonthly(RangeData& newData) const
  {
    newData.clear();

    // nothing to do if we have no data!
    if (size() <= 0)
    {
      return;
    }

    size_type totalPoints = size();
    size_type startIdx = 0;
    size_type endIdx = 1;
    int currMonth = get(0).tradeTime.date().month();

    while (endIdx < totalPoints)
    {
      int newMonth = get(endIdx).tradeTime.date().month();

      // if we are in a new month, then we create a point for up to the
      // previous end index
      if (newMonth != currMonth)
      {
        // the new data point
        Point point = getSummaryPoint(startIdx, endIdx - 1);

        // add the data point!
        newData.add(point);

        // update startIdx to consider next window
        startIdx = endIdx;
        currMonth = newMonth;
      }

      ++endIdx;
    }

    newData.add(getSummaryPoint(startIdx, endIdx - 1));
  }
コード例 #2
0
ファイル: RangeData.cpp プロジェクト: pinchaque/alchemy
  void RangeData::summarize(RangeData& newData, int points) const
  {
    assert(points > 0);

    newData.clear();

    // nothing to do if we have no data!
    if (size() <= 0)
    {
      return;
    }

    size_type totalPoints = size();

    size_type startIdx = 0;

    while (startIdx < totalPoints)
    {
      // [startIdx, endIdx] are the data points we're summarizing
      size_type endIdx = startIdx + points - 1;
      if (endIdx >= totalPoints)
      {
        endIdx = totalPoints - 1;
      }

      // actual number of data points (may be less than points for last
      // batch)
      
      // the new data point
      Point point = getSummaryPoint(startIdx, endIdx);

      // add the data point!
      newData.add(point);

      // update startIdx to consider next window
      startIdx = endIdx + 1;
    }
  }
コード例 #3
0
void TestIStockDataFile::test1()
{
  const char* fname = "testdata/stockdata1.dat";
  const double delta = 0.01;

  IStockDataFile f(m_ctx);

  CPPUNIT_ASSERT_EQUAL(std::string(""), f.getFileName());

  CPPUNIT_ASSERT(f.open(fname));

  CPPUNIT_ASSERT_EQUAL(std::string(fname), f.getFileName());

  {
    RangeData::Point pt1;
    RangeData::Point pt2;
    RangeData::Point pt3;

    CPPUNIT_ASSERT(f.read(pt1));

    CPPUNIT_ASSERT_DOUBLES_EQUAL(0.00, pt1.open, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(1.00, pt1.close, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(2.00, pt1.min, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(3.00, pt1.max, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(4.00, pt1.volume, delta);
    CPPUNIT_ASSERT_EQUAL(std::string("20020131T235959"),
                         boost::posix_time::to_iso_string(pt1.tradeTime));

    CPPUNIT_ASSERT(f.read(pt2));

    CPPUNIT_ASSERT_DOUBLES_EQUAL(10.00, pt2.open, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(11.05, pt2.close, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(12.10, pt2.min, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(13.15, pt2.max, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(14.20, pt2.volume, delta);
    CPPUNIT_ASSERT_EQUAL(std::string("20031231T010203"),
                         boost::posix_time::to_iso_string(pt2.tradeTime));

    CPPUNIT_ASSERT(!f.read(pt3));
  }


  CPPUNIT_ASSERT(f.rewind());

  {
    RangeData data;
    CPPUNIT_ASSERT(f.read(data));
    CPPUNIT_ASSERT_EQUAL(2, int(data.size()));    

    const RangeData::Point& pt1 = data.get(0);
    const RangeData::Point& pt2 = data.get(1);

    CPPUNIT_ASSERT_DOUBLES_EQUAL(0.00, pt1.open, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(1.00, pt1.close, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(2.00, pt1.min, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(3.00, pt1.max, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(4.00, pt1.volume, delta);
    CPPUNIT_ASSERT_EQUAL(std::string("20020131T235959"),
                         boost::posix_time::to_iso_string(pt1.tradeTime));

    CPPUNIT_ASSERT_DOUBLES_EQUAL(10.00, pt2.open, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(11.05, pt2.close, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(12.10, pt2.min, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(13.15, pt2.max, delta);
    CPPUNIT_ASSERT_DOUBLES_EQUAL(14.20, pt2.volume, delta);
    CPPUNIT_ASSERT_EQUAL(std::string("20031231T010203"),
                         boost::posix_time::to_iso_string(pt2.tradeTime));

    data.clear();

    CPPUNIT_ASSERT(f.read(data));
    CPPUNIT_ASSERT_EQUAL(0, int(data.size()));
  }


  f.close();
  CPPUNIT_ASSERT_EQUAL(std::string(""), f.getFileName());
}