Repository navigation
Expand file tree
/
Copy pathCSyntheticMarketData.cpp
More file actions
147 lines (126 loc) · 2.89 KB
/
Copy pathCSyntheticMarketData.cpp
File metadata and controls
147 lines (126 loc) · 2.89 KB
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
#include "stdafx.h"
#include "CSyntheticMarketData.h"
#include <string.h>
CSyntheticMarketData::CSyntheticMarketData(const char* product, set<string> emits, CSyntheticCalculate* pCal)
{
m_product = product;
m_emits = emits;
m_emit = nullptr;
memset(m_emit_buf,0, sizeof(m_emit_buf));
if (m_emits.size() == 1)
{
// 只保存第一个
strncpy(m_emit_buf, emits.begin()->data(), sizeof(m_emit_buf));
m_emit = m_emit_buf;
}
memset(&m_leg, 0, sizeof(m_leg));
m_leg.pBuf = new char[LEG_DATA_BUF_LEN]();
memset(m_leg.pBuf, 0, LEG_DATA_BUF_LEN);
m_pCal = pCal;
m_size = 0;
m_count = 0;
m_last_count = -1;
}
CSyntheticMarketData::~CSyntheticMarketData()
{
if (m_leg.pBuf)
{
delete[] m_leg.pBuf;
m_leg.pBuf = nullptr;
}
}
void CSyntheticMarketData::Create(const char* instrument, double weight, char* pData)
{
auto it = m_map.find(instrument);
if (it == m_map.end())
{
LegData lmd = { 0 };
lmd.weight = weight;
lmd.pBuf = pData;
m_map.insert(pair<string, LegData>(instrument, lmd));
m_set.insert(instrument);
}
else
{
}
// 由于这个代码是在Register中调用,合成合约的成份大小是固定的
m_size = m_map.size();
}
void CSyntheticMarketData::UpdateWeight()
{
double sum = 0;
for (auto iter = m_map.begin(); iter != m_map.end(); iter++)
{
sum += iter->second.weight;
}
for (auto iter = m_map.begin(); iter != m_map.end(); iter++)
{
iter->second.weight = iter->second.weight / sum;
}
}
void* CSyntheticMarketData::Calculate(char* pBuf)
{
if (m_pCal == nullptr)
{
return nullptr;
}
lock_guard<mutex> cl(m_cs);
m_pCal->Begin(this, pBuf, &m_leg);
for (auto iter = m_map.begin(); iter != m_map.end(); iter++)
{
// 求和
m_pCal->Sum(this, &iter->second, &m_leg);
}
for (auto iter = m_map.begin(); iter != m_map.end(); iter++)
{
// 算权重,或权重已经算好
m_pCal->ForEach(this, &iter->second, &m_leg);
}
m_pCal->End(this, pBuf, &m_leg);
return m_leg.pBuf;
}
bool CSyntheticMarketData::CheckEmit(const char* instrument, int offest)
{
// 想统计有多少数据齐了时,发现内存区其实初始化时就已经齐了
// 只好强行加一个标志位
if (m_size != m_count)
{
char ch = 0;
for (auto iter = m_map.begin(); iter != m_map.end(); iter++)
{
auto c = iter->second.pBuf[offest];
ch += c;
}
m_count = ch;
}
if (m_last_count == m_count)
{
if (m_emit)
{
// 觉得strcmp要比set中的find要快,所以。。。
return strcmp(m_emit, instrument) == 0;
}
// 没有指定触发合约,都触发
if (m_emits.size() == 0)
{
return true;
}
// 相同判断是否指定合约
auto it = m_emits.find(instrument);
return it != m_emits.end();
}
else
{
m_last_count = m_count;
// 不同表示正在起步几个数据包
return m_size == m_count;
}
}
void* CSyntheticMarketData::GetValue()
{
return m_leg.pBuf;
}
set<string> CSyntheticMarketData::GetInstruments()
{
return m_set;
}