修改挂单开仓和止损止盈BUg

pull/1/head
yz 2 years ago
parent 13af3adad2
commit e43fee0498

@ -20,10 +20,37 @@ public class Constant {
/**
*
*/
//合约量
public static final Double PEACEQUANTITY = 100000.0;
//杠杆倍数
public static final Double LEVER = 100.0;
public static final Double PEACEQUANTITY = 100000.0; //合约量
public static final Double LEVER = 100.0; //杠杆倍数
public static final Double LEVERQUANTITY=PEACEQUANTITY/LEVER; // 和约量/杠杆倍数
public static final String PRACTICE_TRAINING_ID="999999999"; //练习实训ID
public static final Integer PRACTICE_SCHOOL_ID=999999999; //练习学校id
public static final String TRAININGNAME="AdminStudentPractice"; //练习实训名
public static final String TRAINING_STATUS_FINISHED="FINISHED"; //实训状态 实训结束
public static final String BUY_BUYSELLTYPE="buy"; // 交易类型 买
public static final String SELL_BUYSELLTYPE="sell"; // 交易类型 卖
public static final String BUYLIMIT_BUYSELLTYPE="buyLimit"; // 交易类型 限价买入
public static final String SELLLIMIT_BUYSELLTYPE="sellLimit"; // 交易类型 限价卖出
public static final String SELLSTOP_BUYSELLTYPE="sellStop"; // 交易类型 止损卖出
public static final String BUYSTOP_BUYSELLTYPE="buyStop"; // 交易类型 止损买入
/**
* redis
*/
public static final String NOSETTLEANACCOUNTTRAINING="NoSettleAnAccountTraining"; //未结算的实训
public static final String FOREXDATELIST="ForexDateList"; //外汇接口对象
public static final Double LEVERQUANTITY=PEACEQUANTITY/LEVER;
}

@ -74,17 +74,20 @@ public class MemberController {
public ResultEntity getMemberId(@RequestBody JSONObject jsonObject) {
String name = jsonObject.getString("name");
String trainingId = jsonObject.getString("trainingId");
Integer schoolId =999999999;
if(!"999999999".equals(trainingId)){
String trainingStatus=jsonObject.getString("trainingStatus");
Integer schoolId =Constant.PRACTICE_SCHOOL_ID;
if(!Constant.PRACTICE_TRAINING_ID.equals(trainingId)){
schoolId = jsonObject.getInteger("schoolId");
}
Member member = memberService.selectByNameAndSchoolIdAndTrainingID(name, schoolId, trainingId);
Double positionProfitLoss = takeStashController.flashTotalPositionProfitLoss(member.getMemberId());
if (positionProfitLoss==null){
positionProfitLoss=0.0;
Double positionProfitLoss=0.0;
if(!Constant.TRAINING_STATUS_FINISHED.equals(trainingStatus)){
positionProfitLoss = takeStashController.flashTotalPositionProfitLoss(member.getMemberId());
if (positionProfitLoss==null){
positionProfitLoss=0.0;
}
member.setPositionProfitLoss(positionProfitLoss); //设置持仓盈亏
}
member.setPositionProfitLoss(positionProfitLoss); //设置持仓盈亏
Double initialCapital = member.getInitialCapital();//初始资金
Double cumulativeProfitLoss = member.getCumulativeProfitLoss(); //累计盈亏
if(cumulativeProfitLoss==null){
@ -195,13 +198,13 @@ public class MemberController {
Double nowSellPic = Double.valueOf(forexData.getSellPic()); //当前卖价
Double profitAndLoss;
if (tradingCode.startsWith("USD")) { //美元在前
if ("buy".equals(buySellType)) { //买
if (Constant.BUY_BUYSELLTYPE.equals(buySellType)) { //买
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.LEVERQUANTITY / nowSellPic;
} else { //卖
profitAndLoss = (priceTransaction - nowBuyPic) * volumeTransaction * Constant.LEVERQUANTITY / nowBuyPic;
}
} else { //美元在后
if ("buy".equals(buySellType)) { //买
if (Constant.BUY_BUYSELLTYPE.equals(buySellType)) { //买
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.LEVERQUANTITY;
} else { //卖
profitAndLoss = (nowBuyPic - priceTransaction) * volumeTransaction * Constant.LEVERQUANTITY;

@ -1,10 +1,10 @@
package com.sztzjy.forex.trading_trading.controller;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.sztzjy.forex.trading_trading.annotation.AnonymousAccess;
import com.sztzjy.forex.trading_trading.config.Constant;
import com.sztzjy.forex.trading_trading.dto.PengdingVo;
import com.sztzjy.forex.trading_trading.dto.TakeStashVO;
import com.sztzjy.forex.trading_trading.entity.ForexMarketData;
@ -15,7 +15,6 @@ import com.sztzjy.forex.trading_trading.util.RedisUtil;
import com.sztzjy.forex.trading_trading.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@ -67,7 +66,7 @@ public class PendingOrderController {
return new ResultEntity(HttpStatus.OK, "获取挂单数据成功", pageInfo);
}
//撤单 撤单成功后 根据pendingOrderId删除redis键
//撤单 撤单成功后 根据pendingOrderId删除redis键 error
@AnonymousAccess
@PostMapping("cancelOrder")
public ResultEntity cancelOrder(@RequestBody JSONObject jsonObject) {
@ -87,22 +86,22 @@ public class PendingOrderController {
ForexMarketData forexDate = forexMarketDataMap.get(tradingCode);
Double buyPic = forexDate.getBuyPic();
Double sellPic = Double.valueOf(forexDate.getSellPic());
if ("buyLimit".equals(buySellType)) { //限价买进
if (Constant.BUYLIMIT_BUYSELLTYPE.equals(buySellType)) { //限价买进
if (buyPic < priceCommission) {
return true;
}
}
if ("sellLimit".equals(buySellType)) { //限价卖出
if (Constant.SELLLIMIT_BUYSELLTYPE.equals(buySellType)) { //限价卖出
if (sellPic > priceCommission) {
return true;
}
}
if ("buyStop".equals(buySellType)) { //止损买入
if (Constant.BUYSTOP_BUYSELLTYPE.equals(buySellType)) { //止损买入
if (buyPic > priceCommission) {
return true;
}
}
if ("sellStop".equals(buySellType)) {
if (Constant.SELLSTOP_BUYSELLTYPE.equals(buySellType)) {
if (sellPic < priceCommission) {
return true;
}
@ -115,8 +114,8 @@ public class PendingOrderController {
public Boolean isExpire(Map<Object, Object> pengingOrderMap, String key) {
Date validityTime = (Date) pengingOrderMap.get("validityTime"); //获取有效期
Date currentDate = new Date();
if (validityTime.after(currentDate)) {
pendingOrderService.cancelOrder((String) pengingOrderMap.get("pendingOrderId"));
if (currentDate.after(validityTime)) {
pendingOrderService.cancelOrder(String.valueOf(pengingOrderMap.get("pendingOrderId")) );
redisUtil.del(key);
return true;
}
@ -130,10 +129,10 @@ public class PendingOrderController {
Object transactionVolume = pengingOrderMap.get("transactionVolume");
Object buySellTypeObj = pengingOrderMap.get("buySellType");
String buySellType= String.valueOf(buySellTypeObj);
if(buySellType.contains("buy")){
buySellType="buy";
if(buySellType.contains(Constant.BUY_BUYSELLTYPE)){
buySellType=Constant.BUY_BUYSELLTYPE;
}else {
buySellType="sell";
buySellType=Constant.SELL_BUYSELLTYPE;
}
Object stopLoss = pengingOrderMap.get("stopLoss");
Object stopWin = pengingOrderMap.get("stopWin");
@ -171,16 +170,16 @@ public class PendingOrderController {
Double nowBuyPic = forexData.getBuyPic(); //当前买价
Double nowSellPic = Double.valueOf(forexData.getSellPic()); //当前卖价
if (tradingCode.startsWith("USD")) { //美元在前
if ("buy".equals(buySellType)) { //买
pengdingVo.setCurrentPrice(nowBuyPic);
} else { //卖
if (Constant.BUY_BUYSELLTYPE.equals(buySellType)) { //买
pengdingVo.setCurrentPrice(nowSellPic);
} else { //卖
pengdingVo.setCurrentPrice(nowBuyPic);
}
} else { //美元在后
if ("buy".equals(buySellType)) { //买
pengdingVo.setCurrentPrice(nowBuyPic);
} else { //卖
if (Constant.BUY_BUYSELLTYPE.equals(buySellType)) { //买
pengdingVo.setCurrentPrice(nowSellPic);
} else { //卖
pengdingVo.setCurrentPrice(nowBuyPic);
}
}
pengdingVos.add(pengdingVo);

@ -62,20 +62,29 @@ public class TakeStashController {
public ResultEntity getTakeStashList(@RequestBody JSONObject jsonObject) {
Integer pageIndex = jsonObject.getInteger("index");
Integer pageSize = jsonObject.getInteger("size");
String trainingStatus=jsonObject.getString("trainingStatus");
TakeStash takeStash = jsonObject.getObject("takeStash", TakeStash.class);
String trainingId = takeStash.getTrainingId();
if(trainingId==null){
trainingId="999999999";
trainingId=Constant.PRACTICE_TRAINING_ID;
}
PageInfo<TakeStash> pageInfo = takeStashService.findTakeStashByTrainingIdAndMemberIdAndStatus(trainingId, takeStash.getMemberId(), takeStash.getStatus(), pageIndex, pageSize);
List<TakeStash> takeStashList = pageInfo.getList();
List<TakeStashVO> takeStashVOList = new ArrayList<>();
for (int i = 0; i < takeStashList.size(); i++) {
TakeStashVO takeStashVO = new TakeStashVO(takeStashList.get(i));
if(Constant.TRAINING_STATUS_FINISHED.equals(trainingStatus)){ //实训结束后
takeStashVO.setCurrentPrice(takeStashList.get(i).getTradingMargin()); //将trading_margin保存到CurrentPrice返回 trading_margin为实训结束后的价格 记录实训结束后实时价格
takeStashVO.setProfitAndLoss(takeStashList.get(i).getProfitAndLossByClose()); //profit_and_loss_by_close 保存实训结束后的盈亏
}
takeStashVOList.add(takeStashVO);
}
if (takeStash.getStatus() == 0) {
takeStashVOList = flashProfitAndLoss(takeStashVOList).getBody().getData();
if(!Constant.TRAINING_STATUS_FINISHED.equals(trainingStatus)){
if (takeStash.getStatus() == 0) {
takeStashVOList = flashProfitAndLoss(takeStashVOList).getBody().getData();
}
}else {
}
PageInfo<TakeStashVO> voPageInfo = new PageInfo<>();
voPageInfo.setTotal(pageInfo.getTotal());
@ -111,7 +120,7 @@ public class TakeStashController {
Double nowSellPic = Double.valueOf(forexData.getSellPic()); //当前卖价
Double profitAndLoss;
if (tradingCode.startsWith("USD")) { //美元在前
if ("buy".equals(buySellType)) { //买
if (Constant.BUY_BUYSELLTYPE.equals(buySellType)) { //买
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.PEACEQUANTITY / nowSellPic;
takeStashVO.setCurrentPrice(nowSellPic);
} else { //卖
@ -119,7 +128,7 @@ public class TakeStashController {
takeStashVO.setCurrentPrice(nowBuyPic);
}
} else { //美元在后
if ("buy".equals(buySellType)) { //买
if (Constant.BUY_BUYSELLTYPE.equals(buySellType)) { //买
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.PEACEQUANTITY;
takeStashVO.setCurrentPrice(nowSellPic);
} else { //卖
@ -151,13 +160,13 @@ public class TakeStashController {
Double nowSellPic = Double.valueOf(forexData.getSellPic()); //当前卖价
Double profitAndLoss;
if (tradingCode.startsWith("USD")) { //美元在前
if ("buy".equals(buySellType)) { //买
if (Constant.BUY_BUYSELLTYPE.equals(buySellType)) { //买
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.PEACEQUANTITY / nowSellPic;
} else { //卖
profitAndLoss = (priceTransaction - nowBuyPic) * volumeTransaction * Constant.PEACEQUANTITY / nowBuyPic;
}
} else { //美元在后
if ("buy".equals(buySellType)) { //买
if (Constant.BUY_BUYSELLTYPE.equals(buySellType)) { //买
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.PEACEQUANTITY;
} else { //卖
profitAndLoss = (priceTransaction-nowBuyPic) * volumeTransaction * Constant.PEACEQUANTITY;
@ -220,19 +229,19 @@ public class TakeStashController {
if (tradingCode.startsWith("USD")) { //美元在前 返还资金表达式为
//可用保证金计算
margin = startUSDMarginNeed(trainingId,memberId,0,tradingCode,buySellType,volumeTransaction);
if ("buy".equals(buySellType)) { //如果持仓方向为买 则平仓方向为卖
if (Constant.BUY_BUYSELLTYPE.equals(buySellType)) { //如果持仓方向为买 则平仓方向为卖
priceTransactionCloser = nowSellPic;
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.PEACEQUANTITY / nowSellPic;
} else if ("sell".equals(buySellType)) { //如果持仓方向为卖 则平仓方向为买 可用资金=可用资金+当前买价*1000*买卖手
} else if (Constant.SELL_BUYSELLTYPE.equals(buySellType)) { //如果持仓方向为卖 则平仓方向为买 可用资金=可用资金+当前买价*1000*买卖手
priceTransactionCloser = nowBuyPic;
profitAndLoss = (priceTransaction - nowBuyPic) * volumeTransaction * Constant.PEACEQUANTITY / nowBuyPic;
}
} else { //美元在后
margin =endUSDMarginNeed(trainingId,memberId,0,tradingCode,buySellType,takeStashNew.getVolumeTransaction(),priceTransaction);
if ("buy".equals(buySellType)) { //如果持仓方向为买 则平仓方向为卖 可用资金=可用资金+1000*买卖手
if (Constant.BUY_BUYSELLTYPE.equals(buySellType)) { //如果持仓方向为买 则平仓方向为卖 可用资金=可用资金+1000*买卖手
priceTransactionCloser = nowSellPic;
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.PEACEQUANTITY;
} else if ("sell".equals(buySellType)) { //如果持仓方向为卖 则平仓方向为买 可用资金=可用资金+当前买价*1000*买卖手
} else if (Constant.SELL_BUYSELLTYPE.equals(buySellType)) { //如果持仓方向为卖 则平仓方向为买 可用资金=可用资金+当前买价*1000*买卖手
priceTransactionCloser = nowBuyPic;
profitAndLoss = ( priceTransaction - nowBuyPic ) * volumeTransaction * Constant.PEACEQUANTITY;
}
@ -250,7 +259,6 @@ public class TakeStashController {
cumulativeProfitLoss=cumulativeProfitLoss+profitAndLoss;
member.setCumulativeProfitLoss(bigDecimalUtils.mul(cumulativeProfitLoss,1,2)); //修改累计盈亏
member.setYield(bigDecimalUtils.div(cumulativeProfitLoss,member.getInitialCapital(),8)); //修改收益率
System.out.println(bigDecimalUtils.mul(bigDecimalUtils.div(cumulativeProfitLoss,member.getInitialCapital()),1,10));
memberService.updateByPrimaryKeySelective(member);
flashTotalPositionProfitLoss(member.getMemberId());
@ -280,7 +288,7 @@ public class TakeStashController {
Double sellTotalVolumeTransaction=0.0; //sell类型交易总量
Double highVolumeTransaction=0.0; //记录buy/sell类型交易总量中较高的一个
if(takeStashList.size()==1){
if("buy".equals(buySellType)){
if(Constant.BUY_BUYSELLTYPE.equals(buySellType)){
margin=transactionVolume*priceTransaction*Constant.LEVERQUANTITY;
}else {
margin=transactionVolume*priceTransaction*Constant.LEVERQUANTITY;
@ -292,7 +300,7 @@ public class TakeStashController {
String buySellTypeTakeStash = takeStash.getBuySellType();
Double volumeTransactionTakeStash = takeStash.getVolumeTransaction(); //持仓交易量
Double priceTransactionTakeStash = takeStash.getPriceTransaction(); //持仓交易价格
if("buy".equals(buySellTypeTakeStash)){
if(Constant.BUY_BUYSELLTYPE.equals(buySellTypeTakeStash)){
buyTotalMargin=buyTotalMargin+volumeTransactionTakeStash*priceTransactionTakeStash;
buyTotalVolumeTransaction=buyTotalVolumeTransaction+volumeTransactionTakeStash;
}else {
@ -311,7 +319,7 @@ public class TakeStashController {
//获取相同tradingCode持仓表总使用保证金
Double totalMargin=(buyTotalMargin+sellTotalMargin)/totalVolumeTransaction*Constant.LEVERQUANTITY*highVolumeTransaction;
//获取去除平仓数据后的相同tradingCode持仓表总使用保证金
if("buy".equals(buySellType)){
if(Constant.BUY_BUYSELLTYPE.equals(buySellType)){
if (buyTotalVolumeTransaction-transactionVolume>=sellTotalVolumeTransaction){ //设置平仓后 sell\buy的最高交易交易总量
highVolumeTransaction=buyTotalVolumeTransaction;
}else {
@ -345,14 +353,14 @@ public class TakeStashController {
for (int i = 0; i < takeStashList.size(); i++) {
TakeStash takeStash = takeStashList.get(i);
String buySellTypeTakeStash = takeStash.getBuySellType();
if("buy".equals(buySellTypeTakeStash)){
if(Constant.BUY_BUYSELLTYPE.equals(buySellTypeTakeStash)){
buyTotalVolumeTransaction=buyTotalVolumeTransaction+takeStash.getVolumeTransaction();
}else {
sellTotalVolumeTransaction=sellTotalVolumeTransaction+takeStash.getVolumeTransaction();
}
}
}
if("buy".equals(buySellType)){
if(Constant.BUY_BUYSELLTYPE.equals(buySellType)){
if(buyTotalVolumeTransaction>=sellTotalVolumeTransaction){
if(buyTotalVolumeTransaction-transactionVolume>=sellTotalVolumeTransaction){
margin=transactionVolume*Constant.LEVERQUANTITY;
@ -377,7 +385,7 @@ public class TakeStashController {
}
public void updateMemberAndTakeStash(String memberId, String stashId , Double CumulativeProfitLoss,Double margin) { //margin平仓后保证金变化量
public void updateMemberAndTakeStash(String memberId, String stashId , Double profitLoss,Double margin,Double priceTransactionCloser) { //margin平仓后保证金变化量
Member member = memberService.selectByPrimaryKey(memberId);
Double marginUsed = member.getMarginUsed();//未修改前member表中成员保证金
@ -386,17 +394,22 @@ public class TakeStashController {
Integer closingTrades = member.getClosingTrades();
member.setClosingTrades(closingTrades++);
Double cumulativeProfitLoss = member.getCumulativeProfitLoss(); //为修改前member表中的累积盈亏
cumulativeProfitLoss=bigDecimalUtils.add(cumulativeProfitLoss, CumulativeProfitLoss);
cumulativeProfitLoss=bigDecimalUtils.add(cumulativeProfitLoss, profitLoss);
member.setCumulativeProfitLoss(bigDecimalUtils.mul(cumulativeProfitLoss,1,2)); //修改累计盈亏
member.setYield(bigDecimalUtils.mul(bigDecimalUtils.div(cumulativeProfitLoss,member.getInitialCapital()),1,4)); //修改收益率
member.setYield(bigDecimalUtils.mul(bigDecimalUtils.div(cumulativeProfitLoss,member.getInitialCapital()),1,8)); //修改收益率
memberService.updateByPrimaryKeySelective(member);
flashTotalPositionProfitLoss(member.getMemberId());
TakeStash takeStash = new TakeStash();
takeStash.setStashId(stashId);
takeStash.setStatus(2);
takeStash.setProfitAndLossByClose(bigDecimalUtils.mul(CumulativeProfitLoss,1,2)); //盈亏=返还资金-进仓时使用的保证金
takeStash.setPriceTransactionClose(priceTransactionCloser);
takeStash.setProfitAndLossByClose(bigDecimalUtils.mul(profitLoss,1,2)); //盈亏=返还资金-进仓时使用的保证金
takeStash.setTimeTransactionClose(new Date());
takeStashService.updateByPrimaryKeySelective(takeStash);
wainingService.compareMarginLevels(member.getMemberId(), member.getTrainingId());//更改可用保证金后 调用预警
}
}

@ -112,10 +112,6 @@ public class TradingController {
Double buyPic = forexData.getBuyPic();//当前买价
Double sellPic = Double.valueOf(forexData.getSellPic());//当前买价
Double priceTransaction=0.0; //记录当前交易价格(买价或卖价)
if(priceCommission!=-1){
buyPic=priceCommission;
sellPic=priceCommission;
}
Member member = memberService.getMemberByMemberIdAndTrainingId(memberId, trainingId);
Double availableFunds = getAvailableFunds(member); //获取账户可用资金
Double margin = 0.0; //记录所需保证金
@ -129,17 +125,21 @@ public class TradingController {
}
//如果方式为卖 则止损高于卖价 获利低于买价
if (transactionType.equals("sjkc")) {//市价开仓
if(priceCommission!=-1){
buyPic=priceCommission;
sellPic=priceCommission;
}
String stashId = IdUtil.simpleUUID(); //设置市价开仓ID
if (tradingCode.startsWith("USD")) { //美元在前
margin = startUSDMarginNeed(trainingId,memberId,0,tradingCode,buySellType,transactionVolume);
if(availableFunds<margin){ //判断可用资金是否足够
return new ResultEntity(HttpStatus.BAD_REQUEST, "可用资金不足");
}
if ("buy".equals(buySellType)) {
if (Constant.BUY_BUYSELLTYPE.equals(buySellType)) {
priceTransaction=buyPic;
TakeStash takeStash = returnTakeStash(stashId,memberId, trainingId, tradingCode, currencyName, buySellType, transactionVolume, buyPic, stopLoss, stopWin);
takeStashService.insertTakeStash(takeStash);
} else if ("sell".equals(buySellType)) {
} else if (Constant.SELL_BUYSELLTYPE.equals(buySellType)) {
priceTransaction=sellPic;
TakeStash takeStash = returnTakeStash(stashId,memberId, trainingId, tradingCode, currencyName, buySellType, transactionVolume, sellPic, stopLoss, stopWin);
takeStashService.insertTakeStash(takeStash);
@ -150,11 +150,11 @@ public class TradingController {
if (availableFunds < margin) {
return new ResultEntity(HttpStatus.BAD_REQUEST, "可用资金不足");
}
if ("buy".equals(buySellType)) {
if (Constant.BUY_BUYSELLTYPE.equals(buySellType)) {
priceTransaction=buyPic;
TakeStash takeStash = returnTakeStash(stashId,memberId, trainingId, tradingCode, currencyName, buySellType, transactionVolume, buyPic, stopLoss, stopWin);
takeStashService.insertTakeStash(takeStash);
} else if ("sell".equals(buySellType)) {
} else if (Constant.SELL_BUYSELLTYPE.equals(buySellType)) {
priceTransaction=sellPic;
TakeStash takeStash = returnTakeStash(stashId,memberId, trainingId, tradingCode, currencyName, buySellType, transactionVolume, sellPic, stopLoss, stopWin);
takeStashService.insertTakeStash(takeStash);
@ -165,12 +165,12 @@ public class TradingController {
if(stopLossWinFlag==true){
Map map=new HashMap();
if(stopLoss==null){
map.put("stopLoss",-1);
map.put("stopLoss",-1.0);
}else {
map.put("stopLoss",stopLoss);
}
if(stopWin==null){
map.put("stopWin",-1);
map.put("stopWin",-1.0);
}else {
map.put("stopWin",stopWin);
}
@ -180,32 +180,32 @@ public class TradingController {
map.put("transactionVolume",transactionVolume);
map.put("priceTransaction",priceTransaction);
map.put("memberId",memberId);
map.put("trainingId_",trainingId);
map.put("trainingId",trainingId);
redisUtil.hmset("trainingId_"+trainingId+"_stashId_"+stashId,map);
}
} else if (transactionType.equals("gdkc")) {//挂单开仓 生成的数据为挂单数据 挂单数据生效 判断可用资金是否充足
String pendingOrderId = IdUtil.simpleUUID(); //设置市价开仓ID
Date validityTime = jsonObject.getDate("validityTime"); //获取挂单有效期
if ("buyLimit".equals(buySellType)) { //限价买进(低价买进) 下单时买入价低于当前买价
if (Constant.BUYLIMIT_BUYSELLTYPE.equals(buySellType)) { //限价买进(低价买进) 下单时买入价低于当前买价
if (priceCommission >= buyPic) {
return new ResultEntity(HttpStatus.BAD_REQUEST, "限价买进价位不能高于当前买价");
}
}
if ("sellLimit".equals(buySellType)) {//限价卖出(高价卖出) 下单时卖出价高于当前卖价
if (Constant.SELLLIMIT_BUYSELLTYPE.equals(buySellType)) {//限价卖出(高价卖出) 下单时卖出价高于当前卖价
if (priceCommission <= sellPic) {
return new ResultEntity(HttpStatus.BAD_REQUEST, "限价卖出价位不能低于当前卖价");
}
}
if ("buyStop".equals(buySellType)) {//止损买进(高价买进) 下单时买入价格高于当前买价
if (Constant.BUYSTOP_BUYSELLTYPE.equals(buySellType)) {//止损买进(高价买进) 下单时买入价格高于当前买价
if (priceCommission <= buyPic) {
return new ResultEntity(HttpStatus.BAD_REQUEST, "止损买进价位不能低于当前买价");
}
}
if ("sellStop".equals(buySellType)) {//止损卖出(低价卖出)下单时卖出价格低于当前卖价
if (Constant.SELLSTOP_BUYSELLTYPE.equals(buySellType)) {//止损卖出(低价卖出)下单时卖出价格低于当前卖价
if (priceCommission >= sellPic) {
return new ResultEntity(HttpStatus.BAD_REQUEST, "止损卖出价位不能高于当前卖价");
}
}
Date validityTime = jsonObject.getDate("validityTime"); //获取挂单有效期
String pendingOrderId = IdUtil.simpleUUID(); //设置市价开仓ID
PendingOrder pendingOrder = returnPendingOrder(pendingOrderId,memberId, trainingId, tradingCode,currencyName,buySellType, transactionVolume, priceCommission, stopLoss, stopWin, validityTime);
insertPendingOrder(pendingOrder);//保存挂单
@ -213,7 +213,7 @@ public class TradingController {
Map map=new HashMap();
map.put("tradingCode",tradingCode);
map.put("currencyName",currencyName);
map.put("transactionType",transactionType);
map.put("transactionType","sjkc");
map.put("transactionVolume",transactionVolume);
map.put("buySellType",buySellType);
map.put("stopLoss",stopLoss);
@ -270,7 +270,7 @@ public class TradingController {
//判断止损止赢 逻辑:如果方式为买 则止损低于买价 获利高于买价 / 如果方式为卖 则止损高于卖价 获利低于买价 (可以只传一个参数)
public boolean getWinOrLossStop(Double stopLoss, Double stopWin, String buySellType, ForexMarketData forexData) {
if ("buy".equals(buySellType)) {
if (buySellType.startsWith(Constant.BUY_BUYSELLTYPE)) {
Double buyPic = forexData.getBuyPic();
//判断stopLoss 和stopWin 是否为空
if (null != stopLoss && null != stopWin) {
@ -292,7 +292,7 @@ public class TradingController {
return false;
}
} else if ("sell".equals(buySellType)) {
} else if (buySellType.startsWith(Constant.SELL_BUYSELLTYPE)) {
Double sellPic = Double.valueOf(forexData.getSellPic());
if (null != stopLoss && null != stopWin) {
if (stopLoss > sellPic && stopWin < sellPic) {
@ -340,8 +340,6 @@ public class TradingController {
//保存挂单 挂单保存后 将挂单ID和有效期存入redis
public void insertPendingOrder(PendingOrder pendingOrder){
String uuid = IdUtil.simpleUUID();
pendingOrder.setPendingOrderId(uuid);
pendingOrderService.insert(pendingOrder);
}
@ -359,14 +357,14 @@ public class TradingController {
for (int i = 0; i < takeStashList.size(); i++) {
TakeStash takeStash = takeStashList.get(i);
String buySellTypeTakeStash = takeStash.getBuySellType();
if("buy".equals(buySellTypeTakeStash)){
if(Constant.BUY_BUYSELLTYPE.equals(buySellTypeTakeStash)){
buyTotalVolumeTransaction=buyTotalVolumeTransaction+takeStash.getVolumeTransaction();
}else {
sellTotalVolumeTransaction=sellTotalVolumeTransaction+takeStash.getVolumeTransaction();
}
}
}
if("buy".equals(buySellType)){
if(Constant.BUY_BUYSELLTYPE.equals(buySellType)){
if(sellTotalVolumeTransaction>buyTotalVolumeTransaction){
if(sellTotalVolumeTransaction>buyTotalVolumeTransaction+transactionVolume){
margin=0.0;
@ -402,7 +400,7 @@ public class TradingController {
Double sellTotalVolumeTransaction=0.0; //sell类型交易总量
Double highVolumeTransaction=0.0; //记录buy/sell类型交易总量中较高的一个
if(takeStashList.isEmpty()){
if("buy".equals(buySellType)){
if(Constant.BUY_BUYSELLTYPE.equals(buySellType)){
margin=transactionVolume*buyPic*Constant.LEVERQUANTITY;
}else {
margin=transactionVolume*sellPic*Constant.LEVERQUANTITY;
@ -414,7 +412,7 @@ public class TradingController {
String buySellTypeTakeStash = takeStash.getBuySellType();
Double volumeTransactionTakeStash = takeStash.getVolumeTransaction(); //持仓交易量
Double priceTransactionTakeStash = takeStash.getPriceTransaction(); //持仓交易价格
if("buy".equals(buySellTypeTakeStash)){
if(Constant.BUY_BUYSELLTYPE.equals(buySellTypeTakeStash)){
buyTotalMargin=buyTotalMargin+volumeTransactionTakeStash*priceTransactionTakeStash;
buyTotalVolumeTransaction=buyTotalVolumeTransaction+volumeTransactionTakeStash;
}else {
@ -431,7 +429,7 @@ public class TradingController {
}
Double marginBeforeTrading=(buyTotalMargin+sellTotalMargin)/totalVolumeTransaction*Constant.LEVERQUANTITY*highVolumeTransaction; //进仓之前的保证金
totalVolumeTransaction=totalVolumeTransaction+transactionVolume; //交易总量增加本次进仓的交易量
if("buy".equals(buySellType)){
if(Constant.BUY_BUYSELLTYPE.equals(buySellType)){
buyTotalMargin=buyTotalMargin+transactionVolume*buyPic;
if (buyTotalVolumeTransaction+transactionVolume>=sellTotalVolumeTransaction){ //设置带sell\buy的最高交易交易总量
highVolumeTransaction=buyTotalVolumeTransaction+transactionVolume;
@ -485,13 +483,13 @@ public class TradingController {
Double nowSellPic = Double.valueOf(forexData.getSellPic()); //当前卖价
Double profitAndLoss;
if (tradingCode.startsWith("USD")) { //美元在前
if ("buy".equals(buySellType)) { //买
if (Constant.BUY_BUYSELLTYPE.equals(buySellType)) { //买
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.PEACEQUANTITY / nowSellPic;
} else { //卖
profitAndLoss = (priceTransaction - nowBuyPic) * volumeTransaction * Constant.PEACEQUANTITY / nowBuyPic;
}
} else { //美元在后
if ("buy".equals(buySellType)) { //买
if (Constant.BUY_BUYSELLTYPE.equals(buySellType)) { //买
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.PEACEQUANTITY;
} else { //卖
profitAndLoss = (priceTransaction-nowBuyPic) * volumeTransaction * Constant.PEACEQUANTITY;

@ -6,6 +6,7 @@ import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.sztzjy.forex.trading_trading.annotation.Permission;
import com.sztzjy.forex.trading_trading.annotation.aspect.PermissionType;
import com.sztzjy.forex.trading_trading.config.Constant;
import com.sztzjy.forex.trading_trading.config.security.JwtUser;
import com.sztzjy.forex.trading_trading.config.security.TokenProvider;
import com.sztzjy.forex.trading_trading.dto.TrainingBO;
@ -15,6 +16,7 @@ import com.sztzjy.forex.trading_trading.entity.Training;
import com.sztzjy.forex.trading_trading.service.GradeWeightService;
import com.sztzjy.forex.trading_trading.service.MemberService;
import com.sztzjy.forex.trading_trading.service.TrainingService;
import com.sztzjy.forex.trading_trading.util.RedisUtil;
import com.sztzjy.forex.trading_trading.util.ResultEntity;
import com.sztzjy.forex.trading_trading.util.TzApi;
import io.swagger.annotations.Api;
@ -45,6 +47,8 @@ public class TrainingController {
private final HttpServletRequest request;
private final GradeWeightService gradeWeightService;
@Autowired
RedisUtil redisUtil;
@Permission(codes = PermissionType.TRAINING_MANAGEMENT_ADD)
@ -73,6 +77,7 @@ public class TrainingController {
//生成训练账号member
buildPracticeMembers(members);
redisUtil.sSet(Constant.NOSETTLEANACCOUNTTRAINING,training.getTrainingId());
return new ResultEntity(HttpStatus.OK);
}
@ -244,13 +249,13 @@ public class TrainingController {
List<Member> memberList=new ArrayList<>();
for (int i = 0; i < members.size(); i++) {
Member member = members.get(i);
if (memberService.checkUser("999999999", member.getStudentNumber())) { //解决相同账号会出现生成多个练习账号的问题
if (memberService.checkUser(Constant.PRACTICE_TRAINING_ID, member.getStudentNumber())) { //解决相同账号会出现生成多个练习账号的问题
continue;
}
member.setMemberId(IdUtil.simpleUUID());
member.setTrainingId("999999999");
member.setTrainingName("AdminStudentPractice");
member.setSchoolId(999999999);
member.setTrainingId(Constant.PRACTICE_TRAINING_ID);
member.setTrainingName(Constant.TRAININGNAME);
member.setSchoolId(Constant.PRACTICE_SCHOOL_ID);
memberList.add(member);
}
if (memberList.size()>0){
@ -259,12 +264,12 @@ public class TrainingController {
}
private void buildPracticeMember(Member member) {
if (memberService.checkUser("999999999", member.getStudentNumber())) {
if (memberService.checkUser(Constant.PRACTICE_TRAINING_ID, member.getStudentNumber())) {
}else {
member.setMemberId(IdUtil.simpleUUID());
member.setTrainingId("999999999");
member.setTrainingName("AdminStudentPractice");
member.setSchoolId(999999999);
member.setTrainingId(Constant.PRACTICE_TRAINING_ID);
member.setTrainingName(Constant.TRAININGNAME);
member.setSchoolId(Constant.PRACTICE_SCHOOL_ID);
memberService.insert(member);
}
}

@ -6,6 +6,7 @@ import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
@Mapper
public interface TakeStashMapper {

@ -10,6 +10,8 @@ import com.sztzjy.forex.trading_trading.controller.PendingOrderController;
import com.sztzjy.forex.trading_trading.controller.TakeStashController;
import com.sztzjy.forex.trading_trading.controller.TradingController;
import com.sztzjy.forex.trading_trading.entity.ForexMarketData;
import com.sztzjy.forex.trading_trading.entity.Member;
import com.sztzjy.forex.trading_trading.entity.TakeStash;
import com.sztzjy.forex.trading_trading.entity.mql5Entity.ForexData;
import com.sztzjy.forex.trading_trading.mappers.TrainingMapper;
import com.sztzjy.forex.trading_trading.util.BigDecimalUtils;
@ -27,6 +29,7 @@ import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;
/**
*
@ -64,6 +67,15 @@ public class ScheduledTask {
@Autowired
BigDecimalUtils bigDecimalUtils;
@Autowired
MemberService memberService;
@Autowired
TakeStashService takeStashService;
@Autowired
PendingOrderService pendingOrderService;
@Scheduled(cron = "0 * * * * *") // 修改实训状态 每分钟执行一次
public void updateTrainingStatus() {
trainingMapper.updateTrainingStatusToInProgress();
@ -104,7 +116,7 @@ public class ScheduledTask {
String batchId = IdUtil.simpleUUID();
for (ForexMarketData data : forexMarketDataList) {
Double buyPic = modifyDecimal(data.getBuyPic(), random);
Double openPriDouble = Double.valueOf(data.getOpenPri());
// Double openPriDouble = Double.valueOf(data.getOpenPri());
String diffAmo = bigDecimalUtils.sub(String.valueOf(buyPic), data.getOpenPri()).toString();
ForexMarketData resultData = new ForexMarketData(
buyPic,
@ -163,7 +175,6 @@ public class ScheduledTask {
}
//error
//监听止损止盈 根据code获取当前买卖价格 如果价格高于/低于止损止盈 则按照止损止盈的值进行平仓
//查询实训表中状态为1的所有实训并获取ID 获取持仓表中所有状态为0 且止损和止盈不为空的所有数据 获取当前价位如果
@Scheduled(cron = "0 */2 * * * ?")
@ -177,7 +188,7 @@ public class ScheduledTask {
List<String> trainingIdList = trainingService.selectProceedTraining();
for (int i = 0; i < trainingIdList.size(); i++) {
Set<String> stashSet = redisUtil.keys("*stashId*");
Set<String> stashSet = redisUtil.keys("trainingId_"+trainingIdList.get(i)+"_stashId*");
Iterator<String> iterator = stashSet.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
@ -193,49 +204,63 @@ public class ScheduledTask {
String buySellType = String.valueOf(map.get("buySellType"));
ForexMarketData forexMarketData = forexDateMap.get(tradingCode);
if (tradingCode.startsWith("USD")){ //美元在前
Double margin = takeStashController.startUSDMarginNeed(trainingId, memberId, 0, tradingCode, buySellType, transactionVolume);
if ("buy".equals(buySellType)) { //开仓方式为买 则平仓方式为卖 先获取卖价
Double sellPic = Double.valueOf(forexMarketData.getSellPic());
if(stopLoss!=-1 && sellPic<=stopLoss){
Double margin = takeStashController.startUSDMarginNeed(trainingId, memberId, 0, tradingCode, buySellType, transactionVolume);
Double profitLoss=(stopLoss-priceTransaction)*transactionVolume*Constant.PEACEQUANTITY/stopLoss; //止损盈利
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin,sellPic);
redisUtil.del("trainingId_"+trainingId+"_stashId_"+stashId);
}
if(stopWin!=-1 && sellPic>=stopWin){
Double margin = takeStashController.startUSDMarginNeed(trainingId, memberId, 0, tradingCode, buySellType, transactionVolume);
Double profitLoss=(stopWin-priceTransaction)*transactionVolume*Constant.PEACEQUANTITY/stopWin; //止盈盈利
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin,sellPic);
redisUtil.del("trainingId_"+trainingId+"_stashId_"+stashId);
}
}else { //开仓方式为卖 则平仓方式为买 先获取买价
Double buyPic = forexMarketData.getBuyPic();
if(stopLoss!=-1 && buyPic<=stopLoss){
Double margin = takeStashController.startUSDMarginNeed(trainingId, memberId, 0, tradingCode, buySellType, transactionVolume);
Double profitLoss=(priceTransaction-stopLoss)*transactionVolume*Constant.PEACEQUANTITY/stopLoss;
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin,buyPic);
redisUtil.del("trainingId_"+trainingId+"_stashId_"+stashId);
}
if(stopWin!=-1 && buyPic>=stopWin){
Double margin = takeStashController.startUSDMarginNeed(trainingId, memberId, 0, tradingCode, buySellType, transactionVolume);
Double profitLoss=(priceTransaction-stopWin)*transactionVolume*Constant.PEACEQUANTITY/stopWin;
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin,buyPic);
redisUtil.del("trainingId_"+trainingId+"_stashId_"+stashId);
}
}
}else { //美元在后
Double margin = takeStashController.endUSDMarginNeed( trainingId, memberId,0 , tradingCode, buySellType, transactionVolume, priceTransaction);
if ("buy".equals(buySellType)) { //开仓方式为买 则平仓方式为卖 先获取卖价
Double sellPic = Double.valueOf(forexMarketData.getSellPic());
if(stopLoss!=-1 && sellPic<=stopLoss){
Double margin = takeStashController.endUSDMarginNeed( trainingId, memberId,0 , tradingCode, buySellType, transactionVolume, priceTransaction);
Double profitLoss=(stopLoss-priceTransaction)*transactionVolume*Constant.PEACEQUANTITY;
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin,sellPic);
redisUtil.del("trainingId_"+trainingId+"_stashId_"+stashId);
}
if(stopWin!=-1 && sellPic>=stopWin){
Double margin = takeStashController.endUSDMarginNeed( trainingId, memberId,0 , tradingCode, buySellType, transactionVolume, priceTransaction);
Double profitLoss=(stopWin-priceTransaction)*transactionVolume*Constant.PEACEQUANTITY;
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin,sellPic);
redisUtil.del("trainingId_"+trainingId+"_stashId_"+stashId);
}
}else { //开仓方式为卖 则平仓方式为买 先获取买价
Double buyPic = forexMarketData.getBuyPic();
if(stopLoss!=-1 && buyPic<=stopLoss){
Double margin = takeStashController.endUSDMarginNeed( trainingId, memberId,0 , tradingCode, buySellType, transactionVolume, priceTransaction);
Double profitLoss=(priceTransaction-stopLoss)*transactionVolume*Constant.PEACEQUANTITY;
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin,buyPic);
redisUtil.del("trainingId_"+trainingId+"_stashId_"+stashId);
}
if(stopWin!=-1 && buyPic>=stopWin){
Double margin = takeStashController.endUSDMarginNeed( trainingId, memberId,0 , tradingCode, buySellType, transactionVolume, priceTransaction);
Double profitLoss=(priceTransaction-stopWin)*transactionVolume*Constant.PEACEQUANTITY;
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin,buyPic);
redisUtil.del("trainingId_"+trainingId+"_stashId_"+stashId);
}
}
}
@ -248,32 +273,75 @@ public class ScheduledTask {
//挂单自动开仓(到期自动撤单) 定时方法
@Scheduled(cron = "0 */2 * * * ?")
public void OpenPendingOrder() {
Set<String> pendingOrderSet = redisUtil.keys("pengingOrder*");
Set<String> pendingOrderSet = redisUtil.keys("*pengingOrder*");
List<ForexMarketData> forexDateList = redisUtil.get("ForexDateList");
Map<String, ForexMarketData> forexMarketDateMap = forexMarketDateUtil.getForexMarketDateMap(forexDateList);
for (String key : pendingOrderSet) {
Map<Object, Object> pengingOrderMap = redisUtil.hmget(key);
redisUtil.del(key);
Boolean expire = pendingOrderController.isExpire(pengingOrderMap, key); //判断是否到期
if (expire) {
continue;
}
//判断是否开仓
Boolean isPpen = pendingOrderController.isOpenTakeStash(pengingOrderMap,forexMarketDateMap);
if(isPpen){
if(!isPpen){
continue;
}
pendingOrderService.cancelOrder((String) pengingOrderMap.get("pendingOrderId"));
JSONObject jsonObject = pendingOrderController.pengingKeyReturnJSONObject(pengingOrderMap);
tradingController.getMarketQuotation(jsonObject);
}
}
//自动清空实例到期的所有redisKey
//自动清空实例到期的所有redisKey 并结算
@Scheduled(cron = "0 */2 * * * ?")
public void clearRedisKeyByTrainingId(){
List<String> trainingIds=trainingMapper.selectTrainingIdByStatus();
Set<Object> trainingSet = redisUtil.sGet(Constant.NOSETTLEANACCOUNTTRAINING);
List<String> trainingListRedis = trainingSet.stream().map(Object::toString).collect(Collectors.toList()); //获取未结算的所有实训id
List<String> trainingList=trainingMapper.selectTrainingIdByStatus(); //获取状态码为结束的实训ID
List<String> trainingIds = new ArrayList<>(trainingListRedis);
trainingIds.retainAll(trainingList);
//需要结算的id为 在trainingList中存在 且
for (int i = 0; i < trainingIds.size(); i++) {
redisUtil.del("trainingId_"+trainingIds+"*");
//根据trainingId 查询member表和持仓表 并结算一次保存
String trainingId = trainingIds.get(i);
List<Member> memberList = memberService.findByTrainingId(trainingId);
if (memberList.isEmpty() || memberList==null){
continue;
}
for (int j = 0; j < memberList.size(); j++) {
Member member = memberList.get(j);
String memberId = member.getMemberId();
List<TakeStash> takeStashList=takeStashService.selectAllByMemberIdAndStatus(memberId,0);
if(takeStashList.isEmpty() || memberList==null){
member.setPositionProfitLoss(0.0);
memberService.updateByPrimaryKeySelective(member);
continue;
}
for (int k = 0; k < takeStashList.size(); k++) {
TakeStash takeStash = takeStashList.get(k);
ForexMarketData forexMarketDateByCode = forexMarketDateUtil.getForexMarketDateByCode(takeStash.getTradingCode());
String buySellType = takeStash.getBuySellType();
Double profitAndLoss = takeStashService.flashProfitAndLossByTakeStash(takeStash);
takeStash.setProfitAndLossByClose(profitAndLoss);
if("buy".equals(buySellType)){
String sellPic = forexMarketDateByCode.getSellPic();
takeStash.setTradingMargin(bigDecimalUtils.mul(Double.parseDouble(sellPic),1,2));
}else {
takeStash.setTradingMargin(bigDecimalUtils.mul(forexMarketDateByCode.getBuyPic(),1,2));
}
takeStashService.updateByPrimaryKeySelective(takeStash); //进行持仓结算
}
//member结算 获取一次持仓盈亏
Double memberProfitAndLoss = takeStashController.flashTotalPositionProfitLoss(memberId);
member.setPositionProfitLoss(memberProfitAndLoss);
memberService.updateByPrimaryKeySelective(member);
}
redisUtil.del("trainingId_"+trainingId+"*");
redisUtil.setRemove(Constant.NOSETTLEANACCOUNTTRAINING,trainingId);
}
}

@ -3,9 +3,13 @@ package com.sztzjy.forex.trading_trading.service;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sztzjy.forex.trading_trading.config.Constant;
import com.sztzjy.forex.trading_trading.entity.ForexMarketData;
import com.sztzjy.forex.trading_trading.entity.TakeStash;
import com.sztzjy.forex.trading_trading.entity.TakeStashExample;
import com.sztzjy.forex.trading_trading.mappers.TakeStashMapper;
import com.sztzjy.forex.trading_trading.util.BigDecimalUtils;
import com.sztzjy.forex.trading_trading.util.ForexMarketDateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -16,8 +20,13 @@ public class TakeStashService {
@Autowired
TakeStashMapper takeStashMapper;
@Autowired
BigDecimalUtils bigDecimalUtils;
@Autowired
ForexMarketDateUtil forexMarketDateUtil;
public void insertTakeStash(TakeStash takeStash){
takeStash.setStashId(IdUtil.simpleUUID());
takeStashMapper.insert(takeStash);
}
@ -52,6 +61,7 @@ public class TakeStashService {
takeStashMapper.updateWinLossByPrimaryKey(takeStash);
}
public List<TakeStash> findTakeStashByTrainingIdAndMemberIdAndStatusAndTradingCode(String trainingId, String memberId, int status, String tradingCode) {
TakeStashExample example=new TakeStashExample();
TakeStashExample.Criteria criteria = example.createCriteria();
@ -59,4 +69,34 @@ public class TakeStashService {
List<TakeStash> takeStashes = takeStashMapper.selectByExample(example);
return takeStashes;
}
public Double flashProfitAndLossByTakeStash(TakeStash takeStash) {
if (0 != takeStash.getStatus()) {
return null;
}
String buySellType = takeStash.getBuySellType(); //买入或卖出
String tradingCode = takeStash.getTradingCode(); //交易品种
Double priceTransaction = takeStash.getPriceTransaction(); //交易价格
Double volumeTransaction = takeStash.getVolumeTransaction(); //交易量
ForexMarketData forexData = forexMarketDateUtil.getForexMarketDateByCode(tradingCode);
Double nowBuyPic = forexData.getBuyPic(); //当前买价
Double nowSellPic = Double.valueOf(forexData.getSellPic()); //当前卖价
Double profitAndLoss;
if (tradingCode.startsWith("USD")) { //美元在前
if ("buy".equals(buySellType)) { //买
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.PEACEQUANTITY / nowSellPic;
} else { //卖
profitAndLoss = (priceTransaction - nowBuyPic) * volumeTransaction * Constant.PEACEQUANTITY / nowBuyPic;
}
} else { //美元在后
if ("buy".equals(buySellType)) { //买
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.PEACEQUANTITY;
} else { //卖
profitAndLoss = (priceTransaction-nowBuyPic) * volumeTransaction * Constant.PEACEQUANTITY;
}
}
return bigDecimalUtils.mul(profitAndLoss,1,2);
}
}

Loading…
Cancel
Save