FIX 4.0 Demo 1.0
Loading...
Searching...
No Matches
instrument.hpp
Go to the documentation of this file.
1
9#pragma once
10
11#include <string>
12#include <cstdint>
13#include <cmath>
14
15namespace fix40 {
16
17// ============================================================================
18// 合约信息结构
19// ============================================================================
20
48struct Instrument {
49 // -------------------------------------------------------------------------
50 // 标识符
51 // -------------------------------------------------------------------------
52 std::string instrumentId;
53 std::string exchangeId;
54 std::string productId;
55
56 // -------------------------------------------------------------------------
57 // 交易参数
58 // -------------------------------------------------------------------------
59 double priceTick;
61 double marginRate;
62
63 // -------------------------------------------------------------------------
64 // 价格限制(从行情更新)
65 // -------------------------------------------------------------------------
69
70 // -------------------------------------------------------------------------
71 // 构造函数
72 // -------------------------------------------------------------------------
73
80 : priceTick(0.0)
82 , marginRate(0.0)
83 , upperLimitPrice(0.0)
84 , lowerLimitPrice(0.0)
86 {}
87
98 Instrument(const std::string& instId, const std::string& exchId,
99 const std::string& prodId, double tick, int multiple, double margin)
100 : instrumentId(instId)
101 , exchangeId(exchId)
102 , productId(prodId)
103 , priceTick(tick)
104 , volumeMultiple(multiple)
105 , marginRate(margin)
106 , upperLimitPrice(0.0)
107 , lowerLimitPrice(0.0)
108 , preSettlementPrice(0.0)
109 {}
110
111 // -------------------------------------------------------------------------
112 // 计算方法
113 // -------------------------------------------------------------------------
114
127 double calculateMargin(double price, int64_t volume) const {
128 return price * volume * volumeMultiple * marginRate;
129 }
130
141 bool isPriceValid(double price) const {
142 // 如果涨跌停价格未设置,认为价格有效
143 if (lowerLimitPrice <= 0 && upperLimitPrice <= 0) {
144 return true;
145 }
146 return price >= lowerLimitPrice && price <= upperLimitPrice;
147 }
148
159 bool isPriceTickValid(double price) const {
160 if (priceTick <= 0) {
161 return true;
162 }
163 double remainder = std::fmod(price, priceTick);
164 // 使用容差比较
165 return remainder < 1e-9 || (priceTick - remainder) < 1e-9;
166 }
167
176 void updateLimitPrices(double upper, double lower) {
177 upperLimitPrice = upper;
178 lowerLimitPrice = lower;
179 }
180
181 // -------------------------------------------------------------------------
182 // 比较操作符(用于测试)
183 // -------------------------------------------------------------------------
184
196 bool operator==(const Instrument& other) const {
197 return instrumentId == other.instrumentId &&
198 exchangeId == other.exchangeId &&
199 productId == other.productId &&
200 priceTick == other.priceTick &&
202 marginRate == other.marginRate &&
206 }
207
214 bool operator!=(const Instrument& other) const {
215 return !(*this == other);
216 }
217};
218
219} // namespace fix40
Definition matching_engine.hpp:23
合约静态信息
Definition instrument.hpp:48
Instrument(const std::string &instId, const std::string &exchId, const std::string &prodId, double tick, int multiple, double margin)
带基本参数的构造函数
Definition instrument.hpp:98
std::string productId
品种代码(如 "IF")
Definition instrument.hpp:54
bool isPriceValid(double price) const
检查价格是否在涨跌停范围内
Definition instrument.hpp:141
void updateLimitPrices(double upper, double lower)
更新涨跌停价格
Definition instrument.hpp:176
double calculateMargin(double price, int64_t volume) const
计算开仓保证金
Definition instrument.hpp:127
double priceTick
最小变动价位(如 0.2)
Definition instrument.hpp:59
double marginRate
保证金率(如 0.12 表示 12)
Definition instrument.hpp:61
double lowerLimitPrice
跌停价
Definition instrument.hpp:67
bool isPriceTickValid(double price) const
检查价格是否符合最小变动价位
Definition instrument.hpp:159
bool operator==(const Instrument &other) const
相等比较操作符
Definition instrument.hpp:196
Instrument()
默认构造函数
Definition instrument.hpp:79
double upperLimitPrice
涨停价
Definition instrument.hpp:66
bool operator!=(const Instrument &other) const
不等比较操作符
Definition instrument.hpp:214
int volumeMultiple
合约乘数(如 300)
Definition instrument.hpp:60
double preSettlementPrice
昨结算价
Definition instrument.hpp:68
std::string instrumentId
合约代码(如 "IF2601")
Definition instrument.hpp:52
std::string exchangeId
交易所代码(如 "CFFEX")
Definition instrument.hpp:53