-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathWatch.h
203 lines (146 loc) · 7.36 KB
/
Watch.h
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/************************************************************************
* Copyright(c) 2013, One Unified. All rights reserved. *
* email: [email protected] *
* *
* 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. *
************************************************************************/
#pragma once
#include <memory>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <OUCommon/Delegate.h>
#include <TFTimeSeries/TimeSeries.h>
#include <TFTrading/Instrument.h>
#include <TFTrading/ProviderInterface.h>
#include <TFIQFeed/Symbol.h>
// 20151228 convert the delegate to a signal? a little slower maybe.
// 20160122 will want to set signals on provider so watch/unwatch as provider transitions connection states
// 20220425 marketdepth is supplied via SimluationEngine only currently
// TODO: integrate into iqfeed so marketdepth can make use of strands
// TODO: use strands in simulation engine, similar to what is done in iqfeed provider & symbols
namespace ou { // One Unified
namespace tf { // TradeFrame
class Watch {
public:
using pWatch_t = std::shared_ptr<Watch>;
using idInstrument_t = Instrument::idInstrument_t;
using pInstrument_t = Instrument::pInstrument_t;
using pProvider_t = ou::tf::ProviderInterfaceBase::pProvider_t;
using Fundamentals = ou::tf::iqfeed::Fundamentals;
struct Summary {
int nOpenInterest;
int nTotalVolume;
double dblOpen;
double dblTrade; // last trade?
Summary(): nOpenInterest{}, nTotalVolume{}, dblOpen{}, dblTrade {} {}
};
Watch( pInstrument_t& pInstrument, pProvider_t pDataProvider );
Watch( const Watch& rhs );
virtual ~Watch();
virtual Watch& operator=( const Watch& rhs );
bool operator< ( const Watch& rhs ) const { return m_pInstrument->GetInstrumentName() < rhs.m_pInstrument->GetInstrumentName(); };
bool operator<=( const Watch& rhs ) const { return m_pInstrument->GetInstrumentName() <= rhs.m_pInstrument->GetInstrumentName(); };
pInstrument_t GetInstrument() { return m_pInstrument; }
const idInstrument_t& GetInstrumentName() const { return m_pInstrument->GetInstrumentName(); }
const idInstrument_t& GetInstrumentName( keytypes::eidProvider_t id ) const { return m_pInstrument->GetInstrumentName( id ); }
void SetProvider( pProvider_t& pDataProvider );
pProvider_t GetProvider() { return m_pDataProvider; };
bool Watching() const { return 0 != m_cntWatching; };
// TODO: these need spinlocks
inline const Quote& LastQuote() const { return m_quote; }; // may have thread sync issue
inline const Trade& LastTrade() const { return m_trade; }; // may have thread sync issue
inline const DepthByMM& LastDepthByMM() const { return m_depth_mm; }; // may have thread sync issue
inline const DepthByOrder& LastDepthByOrder() const { return m_depth_order; }; // may have thread sync issue
const Fundamentals& GetFundamentals() const { assert( m_pFundamentals ); return *m_pFundamentals; };
const Summary& GetSummary() const { return m_summary; };
Quotes& GetQuotes() { return m_quotes; }; // not const so can delegate
Trades& GetTrades() { return m_trades; }; // not const so can delegate
DepthsByMM& GetDepthsByMM() { return m_depths_mm; }; // not const so can delegate
DepthsByOrder& GetDepthsByOrder() { return m_depths_order; }; // not const so can delegate
ou::Delegate<const Quote&> OnQuote;
ou::Delegate<const Trade&> OnTrade;
ou::Delegate<const DepthByMM&> OnDepthByMM; // simulator
ou::Delegate<const DepthByOrder&> OnDepthByOrder; // simulator
ou::Delegate<const Fundamentals&> OnFundamentals; // iqfeed
ou::Delegate<const Summary&> OnSummary; // iqfeed
virtual bool StartWatch();
virtual bool StopWatch();
virtual void EmitValues( bool bEmitName = true ) const;
void RecordSeries( bool bRecord ) { m_bRecordSeries = bRecord; } // true by default
bool RecordingSeries() const { return m_bRecordSeries; }
virtual void SaveSeries( const std::string& sPrefix );
virtual void SaveSeries( const std::string& sPrefix, const std::string& sDaily );
virtual void ClearSeries();
// track quotes (maybe rename as such), facilitates order submission with decent spread
void EnableStatsAdd();
void EnableStatsRemove();
using tupleSpreadStats_t = std::tuple<bool, size_t, double>;
tupleSpreadStats_t SpreadStats() const {
return std::make_tuple( m_cntBestSpread > ( m_cntTotalSpread / 6 ), m_cntBestSpread, m_dblBestSpread );
}
protected:
// use an iterator instead? or keep as is as it facilitates multi-thread append and access operations
// or will the stuff in TBB help with this type of access?
bool m_bRecordSeries;
ou::tf::Quote m_quote;
ou::tf::Trade m_trade;
ou::tf::DepthByMM m_depth_mm;
ou::tf::DepthByOrder m_depth_order;
ou::tf::Quotes m_quotes;
ou::tf::Trades m_trades;
ou::tf::DepthsByMM m_depths_mm;
ou::tf::DepthsByOrder m_depths_order;
pInstrument_t m_pInstrument;
pProvider_t m_pDataProvider;
private:
bool m_bWatchingEnabled;
bool m_bWatching; // in/out of connected state
bool m_bEventsAttached; // code validation
size_t m_cntWatching;
size_t m_nEnableStats;
ou::tf::iqfeed::IQFeedSymbol::pFundamentals_t m_pFundamentals;
Summary m_summary;
ou::tf::Trade::price_t m_PriceMax;
ou::tf::Trade::price_t m_PriceMin;
ou::tf::Trade::volume_t m_VolumeTotal;
// number of quotes at spread
using mapQuoteDistribution_t = std::map<double,size_t>;
mapQuoteDistribution_t m_mapQuoteDistribution;
size_t m_cntTotalSpread;
size_t m_cntBestSpread;
double m_dblBestSpread;
void Initialize();
void AddEvents();
void RemoveEvents();
void HandleConnecting( int );
void HandleConnected( int );
void HandleDisconnecting( int );
void HandleDisconnected( int );
void EnableWatch();
void DisableWatch();
void HandleQuote( const Quote& );
void HandleTrade( const Trade& );
void HandleDepthByMM( const DepthByMM& );
void HandleDepthByOrder( const DepthByOrder& );
void HandleIQFeedFundamentalMessage( ou::tf::iqfeed::IQFeedSymbol::pFundamentals_t );
void HandleIQFeedSummaryMessage( ou::tf::iqfeed::IQFeedSymbol::pSummary_t );
void HandleTimeSeriesAllocation( Trades::size_type count );
template<typename Archive>
void save( Archive& ar, const unsigned int version ) const {
//ar & boost::serialization::base_object<const InstrumentInfo>(*this);
}
template<typename Archive>
void load( Archive& ar, const unsigned int version ) {
//ar & boost::serialization::base_object<InstrumentInfo>(*this);
}
BOOST_SERIALIZATION_SPLIT_MEMBER()
};
} // namespace tf
} // namespace ou