|
|
|
@ -16,6 +16,7 @@ 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.util.StringUtils;
|
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
@ -113,7 +114,7 @@ public class TradingController {
|
|
|
|
|
Double sellPic = Double.valueOf(forexData.getSellPic());//当前买价
|
|
|
|
|
Double priceTransaction=0.0; //记录当前交易价格(买价或卖价)
|
|
|
|
|
Member member = memberService.getMemberByMemberIdAndTrainingId(memberId, trainingId);
|
|
|
|
|
Double availableFunds = getAvailableFunds(member); //获取账户可用资金
|
|
|
|
|
Double availableFunds = memberService.getAvailableFunds(member); //获取账户可用资金
|
|
|
|
|
Double margin = 0.0; //记录所需保证金
|
|
|
|
|
Boolean stopLossWinFlag=false;
|
|
|
|
|
if (null != stopLoss || null != stopWin) { //判断止损止赢是否合理 如果stopLoss stopWin都为null则跳过
|
|
|
|
@ -125,9 +126,11 @@ public class TradingController {
|
|
|
|
|
}
|
|
|
|
|
//如果方式为卖 则止损高于卖价 获利低于买价
|
|
|
|
|
if (transactionType.equals("sjkc")) {//市价开仓
|
|
|
|
|
if(priceCommission!=-1){
|
|
|
|
|
buyPic=priceCommission;
|
|
|
|
|
sellPic=priceCommission;
|
|
|
|
|
if(priceCommission!=null){
|
|
|
|
|
if(priceCommission!=-1){
|
|
|
|
|
buyPic=priceCommission;
|
|
|
|
|
sellPic=priceCommission;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
String stashId = IdUtil.simpleUUID(); //设置市价开仓ID
|
|
|
|
|
if (tradingCode.startsWith("USD")) { //美元在前
|
|
|
|
@ -265,7 +268,7 @@ public class TradingController {
|
|
|
|
|
}
|
|
|
|
|
member.setOpeningTrades(openingTrades++);//设置开仓次数
|
|
|
|
|
memberService.updateByPrimaryKeySelective(member);
|
|
|
|
|
wainingService.compareMarginLevels(member.getMemberId(),member.getTrainingId());//更改可用保证金后 调用预警
|
|
|
|
|
// wainingService.compareMarginLevels(member.getMemberId(),member.getTrainingId());//更改可用保证金后 调用预警
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//判断止损止赢 逻辑:如果方式为买 则止损低于买价 获利高于买价 / 如果方式为卖 则止损高于卖价 获利低于买价 (可以只传一个参数)
|
|
|
|
@ -449,56 +452,4 @@ public class TradingController {
|
|
|
|
|
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 (Constant.BUY_BUYSELLTYPE.equals(buySellType)) { //买
|
|
|
|
|
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.PEACEQUANTITY / nowSellPic;
|
|
|
|
|
} else { //卖
|
|
|
|
|
profitAndLoss = (priceTransaction - nowBuyPic) * volumeTransaction * Constant.PEACEQUANTITY / nowBuyPic;
|
|
|
|
|
}
|
|
|
|
|
} else { //美元在后
|
|
|
|
|
if (Constant.BUY_BUYSELLTYPE.equals(buySellType)) { //买
|
|
|
|
|
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.PEACEQUANTITY;
|
|
|
|
|
} else { //卖
|
|
|
|
|
profitAndLoss = (priceTransaction-nowBuyPic) * volumeTransaction * Constant.PEACEQUANTITY;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
totalProfitAndLoss = totalProfitAndLoss + profitAndLoss;
|
|
|
|
|
totalProfitAndLoss = bigDecimalUtils.mul(totalProfitAndLoss, 1, 2);
|
|
|
|
|
}
|
|
|
|
|
return totalProfitAndLoss;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|