FIX 4.0 Demo 1.0
Loading...
Searching...
No Matches
position.hpp
Go to the documentation of this file.
1
9#pragma once
10
11#include <string>
12#include <cstdint>
13#include <chrono>
14
15namespace fix40 {
16
17// ============================================================================
18// 持仓结构
19// ============================================================================
20
45struct Position {
46 // -------------------------------------------------------------------------
47 // 标识符
48 // -------------------------------------------------------------------------
49 std::string accountId;
50 std::string instrumentId;
51
52 // -------------------------------------------------------------------------
53 // 多头持仓
54 // -------------------------------------------------------------------------
55 int64_t longPosition;
56 double longAvgPrice;
57 double longProfit;
58 double longMargin;
59
60 // -------------------------------------------------------------------------
61 // 空头持仓
62 // -------------------------------------------------------------------------
63 int64_t shortPosition;
65 double shortProfit;
66 double shortMargin;
67
68 // -------------------------------------------------------------------------
69 // 时间戳
70 // -------------------------------------------------------------------------
71 std::chrono::system_clock::time_point updateTime;
72
73 // -------------------------------------------------------------------------
74 // 构造函数
75 // -------------------------------------------------------------------------
76
83 : longPosition(0)
84 , longAvgPrice(0.0)
85 , longProfit(0.0)
86 , longMargin(0.0)
87 , shortPosition(0)
88 , shortAvgPrice(0.0)
89 , shortProfit(0.0)
90 , shortMargin(0.0)
91 , updateTime(std::chrono::system_clock::now())
92 {}
93
100 Position(const std::string& accId, const std::string& instId)
101 : accountId(accId)
102 , instrumentId(instId)
103 , longPosition(0)
104 , longAvgPrice(0.0)
105 , longProfit(0.0)
106 , longMargin(0.0)
107 , shortPosition(0)
108 , shortAvgPrice(0.0)
109 , shortProfit(0.0)
110 , shortMargin(0.0)
111 , updateTime(std::chrono::system_clock::now())
112 {}
113
114 // -------------------------------------------------------------------------
115 // 计算方法
116 // -------------------------------------------------------------------------
117
130 void updateProfit(double lastPrice, int volumeMultiple) {
131 longProfit = (lastPrice - longAvgPrice) * longPosition * volumeMultiple;
132 shortProfit = (shortAvgPrice - lastPrice) * shortPosition * volumeMultiple;
133 }
134
140 double getTotalProfit() const {
141 return longProfit + shortProfit;
142 }
143
149 int64_t getTotalPosition() const {
151 }
152
158 double getTotalMargin() const {
159 return longMargin + shortMargin;
160 }
161
167 bool hasPosition() const {
168 return longPosition > 0 || shortPosition > 0;
169 }
170
176 int64_t getNetPosition() const {
178 }
179
180 // -------------------------------------------------------------------------
181 // 比较操作符(用于测试)
182 // -------------------------------------------------------------------------
183
196 bool operator==(const Position& other) const {
197 return accountId == other.accountId &&
198 instrumentId == other.instrumentId &&
199 longPosition == other.longPosition &&
200 longAvgPrice == other.longAvgPrice &&
201 longProfit == other.longProfit &&
202 longMargin == other.longMargin &&
203 shortPosition == other.shortPosition &&
204 shortAvgPrice == other.shortAvgPrice &&
205 shortProfit == other.shortProfit &&
206 shortMargin == other.shortMargin;
207 }
208
215 bool operator!=(const Position& other) const {
216 return !(*this == other);
217 }
218};
219
220} // namespace fix40
Definition matching_engine.hpp:23
合约持仓
Definition position.hpp:45
double shortProfit
空头浮动盈亏
Definition position.hpp:65
int64_t getTotalPosition() const
获取总持仓量
Definition position.hpp:149
void updateProfit(double lastPrice, int volumeMultiple)
更新浮动盈亏
Definition position.hpp:130
int64_t getNetPosition() const
获取净持仓
Definition position.hpp:176
double getTotalProfit() const
获取总浮动盈亏
Definition position.hpp:140
double longMargin
多头占用保证金
Definition position.hpp:58
bool operator==(const Position &other) const
相等比较操作符
Definition position.hpp:196
double longProfit
多头浮动盈亏
Definition position.hpp:57
bool operator!=(const Position &other) const
不等比较操作符
Definition position.hpp:215
std::string instrumentId
合约代码(如 "IF2601")
Definition position.hpp:50
bool hasPosition() const
检查是否有持仓
Definition position.hpp:167
std::chrono::system_clock::time_point updateTime
最后更新时间
Definition position.hpp:71
int64_t shortPosition
空头持仓量(手数)
Definition position.hpp:63
int64_t longPosition
多头持仓量(手数)
Definition position.hpp:55
double longAvgPrice
多头持仓均价
Definition position.hpp:56
Position()
默认构造函数
Definition position.hpp:82
Position(const std::string &accId, const std::string &instId)
带标识符的构造函数
Definition position.hpp:100
double shortAvgPrice
空头持仓均价
Definition position.hpp:64
std::string accountId
账户ID
Definition position.hpp:49
double getTotalMargin() const
获取总占用保证金
Definition position.hpp:158
double shortMargin
空头占用保证金
Definition position.hpp:66