|
|
|
@ -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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|