diff --git a/src/main/java/com/sztzjy/forex/trading_trading/config/Constant.java b/src/main/java/com/sztzjy/forex/trading_trading/config/Constant.java index 71afc6a..39f134d 100644 --- a/src/main/java/com/sztzjy/forex/trading_trading/config/Constant.java +++ b/src/main/java/com/sztzjy/forex/trading_trading/config/Constant.java @@ -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; } diff --git a/src/main/java/com/sztzjy/forex/trading_trading/controller/MemberController.java b/src/main/java/com/sztzjy/forex/trading_trading/controller/MemberController.java index 4026d6c..f66579e 100644 --- a/src/main/java/com/sztzjy/forex/trading_trading/controller/MemberController.java +++ b/src/main/java/com/sztzjy/forex/trading_trading/controller/MemberController.java @@ -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; diff --git a/src/main/java/com/sztzjy/forex/trading_trading/controller/PendingOrderController.java b/src/main/java/com/sztzjy/forex/trading_trading/controller/PendingOrderController.java index ee53908..f96a44d 100644 --- a/src/main/java/com/sztzjy/forex/trading_trading/controller/PendingOrderController.java +++ b/src/main/java/com/sztzjy/forex/trading_trading/controller/PendingOrderController.java @@ -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 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); diff --git a/src/main/java/com/sztzjy/forex/trading_trading/controller/TakeStashController.java b/src/main/java/com/sztzjy/forex/trading_trading/controller/TakeStashController.java index 208a02b..5aab8e0 100644 --- a/src/main/java/com/sztzjy/forex/trading_trading/controller/TakeStashController.java +++ b/src/main/java/com/sztzjy/forex/trading_trading/controller/TakeStashController.java @@ -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 pageInfo = takeStashService.findTakeStashByTrainingIdAndMemberIdAndStatus(trainingId, takeStash.getMemberId(), takeStash.getStatus(), pageIndex, pageSize); List takeStashList = pageInfo.getList(); List 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 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());//更改可用保证金后 调用预警 } + + } diff --git a/src/main/java/com/sztzjy/forex/trading_trading/controller/TradingController.java b/src/main/java/com/sztzjy/forex/trading_trading/controller/TradingController.java index 565d9ed..a83fc09 100644 --- a/src/main/java/com/sztzjy/forex/trading_trading/controller/TradingController.java +++ b/src/main/java/com/sztzjy/forex/trading_trading/controller/TradingController.java @@ -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= 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; diff --git a/src/main/java/com/sztzjy/forex/trading_trading/controller/TrainingController.java b/src/main/java/com/sztzjy/forex/trading_trading/controller/TrainingController.java index 91c67a6..c2da4a1 100644 --- a/src/main/java/com/sztzjy/forex/trading_trading/controller/TrainingController.java +++ b/src/main/java/com/sztzjy/forex/trading_trading/controller/TrainingController.java @@ -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 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); } } diff --git a/src/main/java/com/sztzjy/forex/trading_trading/mappers/TakeStashMapper.java b/src/main/java/com/sztzjy/forex/trading_trading/mappers/TakeStashMapper.java index e5ca43b..0c94e0c 100644 --- a/src/main/java/com/sztzjy/forex/trading_trading/mappers/TakeStashMapper.java +++ b/src/main/java/com/sztzjy/forex/trading_trading/mappers/TakeStashMapper.java @@ -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 { diff --git a/src/main/java/com/sztzjy/forex/trading_trading/service/ScheduledTask.java b/src/main/java/com/sztzjy/forex/trading_trading/service/ScheduledTask.java index d67e992..b333436 100644 --- a/src/main/java/com/sztzjy/forex/trading_trading/service/ScheduledTask.java +++ b/src/main/java/com/sztzjy/forex/trading_trading/service/ScheduledTask.java @@ -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 trainingIdList = trainingService.selectProceedTraining(); for (int i = 0; i < trainingIdList.size(); i++) { - Set stashSet = redisUtil.keys("*stashId*"); + Set stashSet = redisUtil.keys("trainingId_"+trainingIdList.get(i)+"_stashId*"); Iterator 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 pendingOrderSet = redisUtil.keys("pengingOrder*"); + Set pendingOrderSet = redisUtil.keys("*pengingOrder*"); List forexDateList = redisUtil.get("ForexDateList"); Map forexMarketDateMap = forexMarketDateUtil.getForexMarketDateMap(forexDateList); for (String key : pendingOrderSet) { Map 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 trainingIds=trainingMapper.selectTrainingIdByStatus(); + Set trainingSet = redisUtil.sGet(Constant.NOSETTLEANACCOUNTTRAINING); + List trainingListRedis = trainingSet.stream().map(Object::toString).collect(Collectors.toList()); //获取未结算的所有实训id + List trainingList=trainingMapper.selectTrainingIdByStatus(); //获取状态码为结束的实训ID + List 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 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 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); } } diff --git a/src/main/java/com/sztzjy/forex/trading_trading/service/TakeStashService.java b/src/main/java/com/sztzjy/forex/trading_trading/service/TakeStashService.java index eebba21..f965e06 100644 --- a/src/main/java/com/sztzjy/forex/trading_trading/service/TakeStashService.java +++ b/src/main/java/com/sztzjy/forex/trading_trading/service/TakeStashService.java @@ -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 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 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); + } + }