/
BarFactory.cpp
76 lines (66 loc) · 2.77 KB
/
BarFactory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/************************************************************************
* Copyright(c) 2009, One Unified. All rights reserved. *
* *
* This file is provided as is WITHOUT ANY WARRANTY *
* without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
* This software may not be used nor distributed without proper license *
* agreement. *
* *
* See the file LICENSE.txt for redistribution information. *
************************************************************************/
#include "StdAfx.h"
#include <algorithm>
#include "BarFactory.h"
namespace ou { // One Unified
namespace tf { // TradeFrame
CBarFactory::CBarFactory(duration_t nSeconds) :
m_nBarWidthSeconds( std::max<duration_t>( 1, nSeconds ) ), m_1Sec( time_duration( 0, 0, 1 ) )
{
}
CBarFactory::~CBarFactory(void) {
OnNewBarStarted = NULL;
OnBarUpdated = NULL;
OnBarComplete = NULL;
}
void CBarFactory::Add(const ptime &dt, price_t val, volume_t volume) {
duration_t seconds = dt.time_of_day().total_seconds();
duration_t interval = seconds / m_nBarWidthSeconds;
if ( m_bar.IsNull() ) {
m_bar.Close( val );
m_bar.High( val );
m_bar.Low( val );
m_bar.Open( val );
m_bar.Volume( volume );
m_curInterval = interval;
m_bar.DateTime( ptime( dt.date(), time_duration( 0, 0, interval * m_nBarWidthSeconds, 0 ) ) );
m_dtLastIntermediateEmission = dt - m_1Sec; // prime the value
if ( NULL != OnNewBarStarted ) OnNewBarStarted( m_bar );
}
else {
if ( interval > m_curInterval ) { // emit bar and start again
if ( NULL != OnBarComplete ) OnBarComplete( m_bar );
m_bar.Close( val );
m_bar.High( val );
m_bar.Low( val );
m_bar.Open( val );
m_bar.Volume( volume );
m_curInterval = interval;
m_bar.DateTime( ptime( dt.date(), time_duration( 0, 0, interval * m_nBarWidthSeconds, 0 ) ) );
if ( NULL != OnNewBarStarted ) OnNewBarStarted( m_bar );
}
else { // update current interval
m_bar.Close( val );
m_bar.High( std::max( m_bar.High(), val ) );
m_bar.Low( std::min( m_bar.Low(), val ) );
m_bar.Volume( m_bar.Volume() + volume );
}
}
if ( m_1Sec <= ( dt - m_dtLastIntermediateEmission ) ) {
if ( NULL != OnBarUpdated ) OnBarUpdated( m_bar );
m_dtLastIntermediateEmission = dt;
}
}
} // namespace tf
} // namespace ou