交易逻辑修改

pull/1/head
yz 2 years ago
parent 8eff3a6ace
commit 4b34c7f855

@ -5,15 +5,20 @@ import com.github.pagehelper.PageInfo;
import com.sztzjy.forex.trading_trading.annotation.AnonymousAccess;
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.MemberVO;
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.Training;
import com.sztzjy.forex.trading_trading.service.GradeWeightService;
import com.sztzjy.forex.trading_trading.service.MemberService;
import com.sztzjy.forex.trading_trading.service.TakeStashService;
import com.sztzjy.forex.trading_trading.service.TrainingService;
import com.sztzjy.forex.trading_trading.util.BigDecimalUtils;
import com.sztzjy.forex.trading_trading.util.ForexMarketDateUtil;
import com.sztzjy.forex.trading_trading.util.ResultEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@ -49,10 +54,18 @@ public class MemberController {
@Autowired
private HttpServletRequest request;
@Autowired
TakeStashService takeStashService;
@Autowired
ForexMarketDateUtil forexMarketDateUtil;
@AnonymousAccess
@PostMapping("getMemberById")
public ResultEntity getMember(@RequestBody String memberId) {
Member member = memberService.selectByPrimaryKey(memberId);
Double availableFunds = getAvailableFunds(member);
member.setAvailableFunds(availableFunds);
return new ResultEntity(HttpStatus.OK, "根据成员ID获取成员对象", member);
}
@ -61,17 +74,35 @@ public class MemberController {
public ResultEntity getMemberId(@RequestBody JSONObject jsonObject) {
String name = jsonObject.getString("name");
String trainingId = jsonObject.getString("trainingId");
Integer schoolId = jsonObject.getInteger("schoolId");
Integer schoolId =999999999;
if(!"999999999".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;
}
member.setPositionProfitLoss(positionProfitLoss); //设置持仓盈亏
Double totalAssets = member.getTotalAssets();
Double initialCapital = member.getInitialCapital();//初始资金
Double totalAssets = initialCapital+positionProfitLoss;
Double netValue = bigDecimalUtils.add(totalAssets, positionProfitLoss);
member.setTotalAssets(totalAssets); //设置总资产
member.setNetValue(netValue); //设置净值
Double cumulativeProfitLoss = member.getCumulativeProfitLoss(); //累计盈亏
if(cumulativeProfitLoss==null){
member.setCumulativeProfitLoss(0.0);
}
Double marginUsed = member.getMarginUsed(); //获取已用保证金
if (marginUsed==null || marginUsed==0.0){
marginUsed=0.0;
member.setMarginLevel(null);
member.setMarginUsed(0.0);
}else {
member.setMarginLevel(bigDecimalUtils.div(netValue, marginUsed, 2)); //设置保证金水平
}
member.setAvailableFunds(bigDecimalUtils.sub(netValue, marginUsed)); //设置可用资金
member.setMarginLevel(bigDecimalUtils.div(netValue, marginUsed, 2)); //设置保证金水平
return new ResultEntity(HttpStatus.OK, "获取成员ID", member);
}
@ -122,4 +153,57 @@ public class MemberController {
PageInfo<Member> members = memberService.pageListMemberByTrainingId(trainingId, classId,keyword,index, size);
return new ResultEntity<PageInfo<Member>>(members);
}
//获取可用资金
public Double getAvailableFunds(Member member){
Double positionProfitLoss = flashTotalPositionProfitLoss(member.getMemberId());
if (positionProfitLoss==null){
positionProfitLoss=0.0;
}
Double initialCapital = member.getInitialCapital();//初始资金
Double totalAssets = initialCapital+positionProfitLoss;
Double netValue = bigDecimalUtils.add(totalAssets, positionProfitLoss);
Double cumulativeProfitLoss = member.getCumulativeProfitLoss(); //累计盈亏
if(cumulativeProfitLoss==null){
member.setCumulativeProfitLoss(0.0);
}
Double marginUsed = member.getMarginUsed(); //获取已用保证金
if (marginUsed==null){
marginUsed=0.0;
}
return bigDecimalUtils.sub(netValue, marginUsed);
}
public Double flashTotalPositionProfitLoss(String memberId) {
List<TakeStash> takeStashList = takeStashService.selectAllByMemberIdAndStatus(memberId, 0);
Double totalProfitAndLoss = 0.0;
for (int i = 0; i < takeStashList.size(); i++) {
TakeStash takeStash = takeStashList.get(i);
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.LEVERQUANTITY / nowSellPic;
} else { //卖
profitAndLoss = (priceTransaction - nowBuyPic) * volumeTransaction * Constant.LEVERQUANTITY / nowBuyPic;
}
} else { //美元在后
if ("buy".equals(buySellType)) { //买
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.LEVERQUANTITY;
} else { //卖
profitAndLoss = (nowBuyPic - priceTransaction) * volumeTransaction * Constant.LEVERQUANTITY;
}
}
totalProfitAndLoss = totalProfitAndLoss + profitAndLoss;
totalProfitAndLoss = bigDecimalUtils.mul(totalProfitAndLoss, 1, 2);
}
return totalProfitAndLoss;
}
}

@ -44,7 +44,7 @@ public class TakeStashController {
MemberService memberService;
@Autowired
ForexMarketDateUtil forexMarketDateController;
ForexMarketDateUtil forexMarketDateUtil;
@Autowired
TrainingService trainingService;
@ -101,7 +101,7 @@ public class TakeStashController {
String tradingCode = takeStashVO.getTradingCode(); //交易品种
Double priceTransaction = takeStashVO.getPriceTransaction(); //交易价格
Double volumeTransaction = takeStashVO.getVolumeTransaction(); //交易量
ForexMarketData forexData = forexMarketDateController.getForexMarketDateByCode(tradingCode);
ForexMarketData forexData = forexMarketDateUtil.getForexMarketDateByCode(tradingCode);
Double nowBuyPic = forexData.getBuyPic(); //当前买价
Double nowSellPic = Double.valueOf(forexData.getSellPic()); //当前卖价
Double profitAndLoss;
@ -141,7 +141,7 @@ public class TakeStashController {
String tradingCode = takeStash.getTradingCode(); //交易品种
Double priceTransaction = takeStash.getPriceTransaction(); //交易价格
Double volumeTransaction = takeStash.getVolumeTransaction(); //交易量
ForexMarketData forexData = forexMarketDateController.getForexMarketDateByCode(tradingCode);
ForexMarketData forexData = forexMarketDateUtil.getForexMarketDateByCode(tradingCode);
Double nowBuyPic = forexData.getBuyPic(); //当前买价
Double nowSellPic = Double.valueOf(forexData.getSellPic()); //当前卖价
Double profitAndLoss;
@ -207,7 +207,7 @@ public class TakeStashController {
Double priceTransaction = takeStashNew.getPriceTransaction(); //获取进仓时交易价格
String trainingId = takeStashNew.getTrainingId();
Double priceTransactionCloser = 0.0; //记录出仓时交易价格
ForexMarketData forexData = forexMarketDateController.getForexMarketDateByCode(tradingCode);
ForexMarketData forexData = forexMarketDateUtil.getForexMarketDateByCode(tradingCode);
Double profitAndLoss = 0.0; //盈亏
Double nowSellPic = Double.valueOf(forexData.getSellPic());//当前卖价
Double nowBuyPic = forexData.getBuyPic(); //当前买价
@ -237,6 +237,9 @@ public class TakeStashController {
marginUsed = bigDecimalUtils.mul(marginUsed-margin,1,2); //修改成员保证金
member.setMarginUsed(marginUsed);
Integer closingTrades = member.getClosingTrades();
if(closingTrades==null){
closingTrades=1;
}
member.setClosingTrades(closingTrades++);
Double cumulativeProfitLoss = member.getCumulativeProfitLoss(); //为修改前member表中的累积盈亏
member.setCumulativeProfitLoss(bigDecimalUtils.add(cumulativeProfitLoss, profitAndLoss)); //修改累计盈亏
@ -249,7 +252,7 @@ public class TakeStashController {
takeStash.setStashId(stashId);
takeStash.setStatus(2);
takeStash.setPriceTransactionClose(priceTransactionCloser);
takeStash.setProfitAndLossByClose(profitAndLoss);
takeStash.setProfitAndLossByClose(bigDecimalUtils.mul(profitAndLoss,1,2));
takeStash.setTimeTransactionClose(new Date());
takeStashService.updateByPrimaryKeySelective(takeStash);
@ -336,7 +339,7 @@ public class TakeStashController {
if("buy".equals(buySellType)){
if(buyTotalVolumeTransaction>=sellTotalVolumeTransaction){
if(buyTotalVolumeTransaction-transactionVolume>=sellTotalVolumeTransaction){
margin=(buyTotalVolumeTransaction-transactionVolume)*Constant.LEVERQUANTITY;
margin=transactionVolume*Constant.LEVERQUANTITY;
}else {
margin=(buyTotalVolumeTransaction-sellTotalVolumeTransaction)*Constant.LEVERQUANTITY;
}
@ -346,7 +349,7 @@ public class TakeStashController {
}else {
if(sellTotalVolumeTransaction>=buyTotalVolumeTransaction){
if(sellTotalVolumeTransaction-transactionVolume>=buyTotalVolumeTransaction){
margin=(sellTotalVolumeTransaction-transactionVolume)*Constant.LEVERQUANTITY;
margin=transactionVolume*Constant.LEVERQUANTITY;
}else {
margin=(sellTotalVolumeTransaction-buyTotalVolumeTransaction)*Constant.LEVERQUANTITY;
}
@ -378,7 +381,4 @@ public class TakeStashController {
wainingService.compareMarginLevels(member.getMemberId(), member.getTrainingId());//更改可用保证金后 调用预警
}
}

@ -11,6 +11,7 @@ import com.sztzjy.forex.trading_trading.entity.PendingOrder;
import com.sztzjy.forex.trading_trading.entity.TakeStash;
import com.sztzjy.forex.trading_trading.service.*;
import com.sztzjy.forex.trading_trading.util.BigDecimalUtils;
import com.sztzjy.forex.trading_trading.util.ForexMarketDateUtil;
import com.sztzjy.forex.trading_trading.util.RedisUtil;
import com.sztzjy.forex.trading_trading.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
@ -36,7 +37,6 @@ public class TradingController {
@Autowired
TakeStashService takeStashService;
@Autowired
RedisUtil redisUtil;
@ -52,6 +52,10 @@ public class TradingController {
@Autowired
WainingService wainingService;
@Autowired
ForexMarketDateUtil forexMarketDateUtil;
//获取市场报价
@AnonymousAccess
@PostMapping("getMarketQuotation")
@ -110,14 +114,14 @@ public class TradingController {
sellPic=priceCommission;
}
Member member = memberService.getMemberByMemberIdAndTrainingId(memberId, trainingId);
Double availableFunds = member.getAvailableFunds(); //获取账户可用资金
Double availableFunds = getAvailableFunds(member); //获取账户可用资金
Double margin = 0.0; //记录所需保证金
Boolean stopLossWinFlag=false;
if (null != stopLoss || null != stopWin) { //判断止损止赢是否合理 如果stopLoss stopWin都为null则跳过
stopLossWinFlag=true;
boolean winOrLossStopBoolean = getWinOrLossStop(stopLoss, stopWin, buySellType, forexData);
if (winOrLossStopBoolean == false) {
return new ResultEntity(HttpStatus.BAD_REQUEST, "止损或获利输入错误");
return new ResultEntity(HttpStatus.BAD_REQUEST, "止损或止盈输入错误");
}
}
//如果方式为卖 则止损高于卖价 获利低于买价
@ -245,11 +249,17 @@ public class TradingController {
return takeStash;
}
//更新可用资金及已用保证金
//更新已用保证金
private void updateMemberAvailableFundsAndMarginUsed(Member member,Double margin) {
Double marginUsed=member.getMarginUsed();
member.setMarginUsed(marginUsed + margin); //设置已用保证金
if (marginUsed==null){
marginUsed=0.0;
}
member.setMarginUsed(bigDecimalUtils.mul(bigDecimalUtils.add(marginUsed,margin),1,2)); //设置已用保证金
Integer openingTrades = member.getOpeningTrades(); //获取开仓次数
if(openingTrades==null){
openingTrades=0;
}
member.setOpeningTrades(openingTrades++);//设置开仓次数
memberService.updateByPrimaryKeySelective(member);
wainingService.compareMarginLevels(member.getMemberId(),member.getTrainingId());//更改可用保证金后 调用预警
@ -354,16 +364,24 @@ public class TradingController {
}
}
if("buy".equals(buySellType)){
if(transactionVolume+buyTotalVolumeTransaction<=sellTotalVolumeTransaction){
margin=0.0;
if(sellTotalVolumeTransaction>buyTotalVolumeTransaction){
if(sellTotalVolumeTransaction>buyTotalVolumeTransaction+transactionVolume){
margin=0.0;
}else {
margin=(transactionVolume+buyTotalVolumeTransaction-sellTotalVolumeTransaction)*Constant.LEVERQUANTITY;
}
}else {
margin=(transactionVolume+buyTotalVolumeTransaction-sellTotalVolumeTransaction)*Constant.LEVERQUANTITY;
margin=transactionVolume*Constant.LEVERQUANTITY;
}
}else {
if(transactionVolume+sellTotalVolumeTransaction<=buyTotalVolumeTransaction){
margin=0.0;
if(buyTotalVolumeTransaction>sellTotalVolumeTransaction){
if(buyTotalVolumeTransaction>sellTotalVolumeTransaction+transactionVolume){
margin=0.0;
}else {
margin=(transactionVolume+sellTotalVolumeTransaction-buyTotalVolumeTransaction)*Constant.LEVERQUANTITY;
}
}else {
margin=(transactionVolume+sellTotalVolumeTransaction-buyTotalVolumeTransaction)*Constant.LEVERQUANTITY;
margin=transactionVolume*Constant.LEVERQUANTITY;
}
}
return margin;
@ -403,21 +421,83 @@ public class TradingController {
totalVolumeTransaction=totalVolumeTransaction+volumeTransactionTakeStash;
}
}
if (buyTotalVolumeTransaction>=sellTotalVolumeTransaction){ //设置带sell\buy的最高交易交易总量
highVolumeTransaction=buyTotalVolumeTransaction;
}else {
highVolumeTransaction=sellTotalVolumeTransaction;
}
Double marginBeforeTrading=(buyTotalMargin+sellTotalMargin)/totalVolumeTransaction*Constant.LEVERQUANTITY*highVolumeTransaction; //进仓之前的保证金
totalVolumeTransaction=totalVolumeTransaction+transactionVolume; //交易总量增加本次进仓的交易量
if("buy".equals(buySellType)){
buyTotalMargin=buyTotalMargin+transactionVolume*buyPic;
if (buyTotalVolumeTransaction+transactionVolume>=sellTotalVolumeTransaction){ //设置带sell\buy的最高交易交易总量
highVolumeTransaction=buyTotalVolumeTransaction+transactionVolume;
}else {
highVolumeTransaction=sellTotalVolumeTransaction;
}
}else {
sellTotalMargin=sellTotalMargin+transactionVolume*sellPic;
if (sellTotalVolumeTransaction+transactionVolume>=buyTotalVolumeTransaction){ //设置带sell\buy的最高交易交易总量
highVolumeTransaction=sellTotalVolumeTransaction+transactionVolume;
}else {
highVolumeTransaction=buyTotalVolumeTransaction;
}
}
if (buyTotalVolumeTransaction>=sellTotalVolumeTransaction){ //设置带sell\buy的最高交易交易总量
highVolumeTransaction=buyTotalVolumeTransaction;
}else {
highVolumeTransaction=sellTotalVolumeTransaction;
}
margin=(buyTotalMargin+sellTotalMargin)/totalVolumeTransaction*Constant.LEVERQUANTITY*highVolumeTransaction;
Double marginAfterTrading=(buyTotalMargin+sellTotalMargin)/totalVolumeTransaction*Constant.LEVERQUANTITY*highVolumeTransaction;
// margin=marginAfterTrading-marginBeforeTrading;
return marginAfterTrading;
}
return margin;
//获取可用资金
public Double getAvailableFunds(Member member){
Double positionProfitLoss = flashTotalPositionProfitLoss(member.getMemberId());
if (positionProfitLoss==null){
positionProfitLoss=0.0;
}
Double initialCapital = member.getInitialCapital();//初始资金
Double totalAssets = initialCapital+positionProfitLoss;
Double netValue = bigDecimalUtils.add(totalAssets, positionProfitLoss);
Double cumulativeProfitLoss = member.getCumulativeProfitLoss(); //累计盈亏
if(cumulativeProfitLoss==null){
member.setCumulativeProfitLoss(0.0);
}
Double marginUsed = member.getMarginUsed(); //获取已用保证金
if (marginUsed==null){
marginUsed=0.0;
}
return bigDecimalUtils.sub(netValue, marginUsed);
}
public Double flashTotalPositionProfitLoss(String memberId) {
List<TakeStash> takeStashList = takeStashService.selectAllByMemberIdAndStatus(memberId, 0);
Double totalProfitAndLoss = 0.0;
for (int i = 0; i < takeStashList.size(); i++) {
TakeStash takeStash = takeStashList.get(i);
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.LEVERQUANTITY / nowSellPic;
} else { //卖
profitAndLoss = (priceTransaction - nowBuyPic) * volumeTransaction * Constant.LEVERQUANTITY / nowBuyPic;
}
} else { //美元在后
if ("buy".equals(buySellType)) { //买
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.LEVERQUANTITY;
} else { //卖
profitAndLoss = (nowBuyPic - priceTransaction) * volumeTransaction * Constant.LEVERQUANTITY;
}
}
totalProfitAndLoss = totalProfitAndLoss + profitAndLoss;
totalProfitAndLoss = bigDecimalUtils.mul(totalProfitAndLoss, 1, 2);
}
return totalProfitAndLoss;
}
}

Loading…
Cancel
Save