新版仿ok交易所-后端
zj
2025-02-08 75018b2f492444248d8b476d9703bb312d2befc3
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package com.yami.trading.bean.contract.domain;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Date;
 
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.yami.trading.common.domain.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
 
/**
 * 非按金额订单Entity
 *
 * @author lucas
 * @version 2023-03-29
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("t_contract_order")
public class ContractOrder extends BaseEntity {
    public final static String STATE_SUBMITTED = "submitted";
    public final static String STATE_CREATED = "created";
    /**
     * 多仓
     */
    public final static String DIRECTION_BUY = "buy";
    /**
     * 空仓
     */
    public final static String DIRECTION_SELL = "sell";
    private static final long serialVersionUID = 1L;
    /**
     * PARTY_ID
     */
    private String partyId;
    /**
     * 代码
     */
    private String symbol;
    /**
     * 订单 号
     */
    private String orderNo;
    /**
     * "buy":买(多) "sell":卖(空)
     */
    private String direction;
    /**
     * 每手金额
     */
    private BigDecimal unitAmount;
    /**
     * 平仓退回金额
     */
    private BigDecimal amountClose;
    /**
     * 手续费
     */
    private BigDecimal fee;
    /**
     * 保证金(剩余)
     */
    private BigDecimal deposit ;
    /**
     * 保证金
     */
    private BigDecimal depositOpen ;
    /**
     * 收益
     */
    private BigDecimal profit;
    /**
     * 成交均价(成本)
     */
    private BigDecimal tradeAvgPrice;
    /**
     * 止盈触发价格
     */
    private BigDecimal stopPriceProfit;
    /**
     * 止损触发价格
     */
    private BigDecimal stopPriceLoss;
    /**
     * 最小浮动
     */
    private BigDecimal pips;
    /**
     * 最小浮动金额(以交易金额计算)
     */
    private BigDecimal pipsAmount;
    /**
     * 状态。submitted 已提交(持仓), created 完成(平仓)
     */
    private String state = "submitted";
    /**
     * LEVER_RATE
     */
    private BigDecimal leverRate;
    /**
     * 委托数量(剩余)(张)
     */
    private BigDecimal volume;
    /**
     * 委托数量(张)
     */
    private BigDecimal volumeOpen;
    /**
     * 平仓时间
     */
    private Long closeTime;
    /**
     * 平仓均价
     */
    private BigDecimal closeAvgPrice;
 
    /**
     * 平仓时间戳
     */
    private Long closeTimeTs;
    /**
     * 强平价格
     */
    private String forceClosePrice;
    /**
     * 涨跌幅
     */
    @TableField(exist = false)
    private BigDecimal changeRatio;
 
    private String orderPriceType;
 
    /**
     * 仓位类型:0:逐仓 1:全仓
     */
    private Integer locationType = 0;
 
    public BigDecimal getAmountClose() {
        if(amountClose == null){
            amountClose = BigDecimal.ZERO;
        }
        return amountClose;
    }
 
 
 
    public BigDecimal getDeposit() {
        if(deposit == null){
            deposit = BigDecimal.ZERO;
        }
        return deposit;
    }
 
    public BigDecimal getDepositOpen() {
        if(depositOpen == null){
            depositOpen = BigDecimal.ZERO;
        }
        return depositOpen;
    }
 
    public BigDecimal getTradeAvgPrice() {
        if(tradeAvgPrice == null){
            tradeAvgPrice = BigDecimal.ZERO;
        }
        return tradeAvgPrice;
    }
 
    public BigDecimal getStopPriceProfit() {
        if(stopPriceProfit == null){
            stopPriceProfit = BigDecimal.ZERO;
        }
        return stopPriceProfit;
    }
 
    public BigDecimal getStopPriceLoss() {
        if(stopPriceLoss == null){
            stopPriceLoss = BigDecimal.ZERO;
        }
        return stopPriceLoss;
    }
 
    public BigDecimal getChangeRatio() {
//        if(amountClose == null){
//            amountClose = BigDecimal.ZERO;
//        }
//        if(profit == null){
//            profit = BigDecimal.ZERO;
//        }
//        if(deposit == null){
//            deposit = BigDecimal.ZERO;
//        }
//        if(depositOpen == null){
//            depositOpen = BigDecimal.ZERO;
//        }
//        if (STATE_SUBMITTED.equals(state)) {
//            changeRatio = amountClose.add(profit).add(deposit).subtract(depositOpen).divide(depositOpen,10 , RoundingMode.HALF_UP);
//        } else {
//            changeRatio = amountClose.add(deposit).subtract(depositOpen).divide(depositOpen, 10 , RoundingMode.HALF_UP);
//        }
//
//        changeRatio = changeRatio.multiply(new BigDecimal("100")).setScale(2, BigDecimal.ROUND_HALF_UP);
        // 计算收益率,指定除法时的精度
        BigDecimal changeRatio = profit.divide(depositOpen, 10, BigDecimal.ROUND_DOWN)  // 设置精度为10位
                .multiply(new BigDecimal("100"));
 
        // 保留两位小数
        changeRatio = changeRatio.setScale(2, BigDecimal.ROUND_DOWN);
 
        return changeRatio;
    }
 
    public BigDecimal getProfit() {
        if(profit == null){
            return BigDecimal.ZERO;
        }
        return profit;
    }
}