增加member数据时 同时生成训练账号

pull/1/head
yz 2 years ago
parent 71256efc7a
commit 9f544e0549

@ -15,4 +15,15 @@ public class Constant {
* Basic
*/
public static final String BASIC = "Basic";
/**
*
*/
//合约量
public static final Double PEACEQUANTITY = 100000.0;
//杠杆倍数
public static final Double LEVER = 100.0;
public static final Double LEVERQUANTITY=PEACEQUANTITY/LEVER;
}

@ -4,6 +4,7 @@ package com.sztzjy.forex.trading_trading.controller;
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.TakeStashVO;
import com.sztzjy.forex.trading_trading.entity.ForexMarketData;
import com.sztzjy.forex.trading_trading.entity.Member;
@ -106,26 +107,22 @@ public class TakeStashController {
Double profitAndLoss;
if (tradingCode.startsWith("USD")) { //美元在前
if ("buy".equals(buySellType)) { //买
// profitAndLoss =(nowBuyPic - priceTransaction) * volumeTransaction * 100000.0 / nowBuyPic;
profitAndLoss = bigDecimalUtils.div(bigDecimalUtils.mul(bigDecimalUtils.mul(bigDecimalUtils.sub(nowSellPic, priceTransaction), volumeTransaction), 100000), nowSellPic, 2);
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.LEVERQUANTITY / nowSellPic;
takeStashVO.setCurrentPrice(nowSellPic);
} else { //卖
// profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * 100000.0 / nowSellPic;
profitAndLoss = bigDecimalUtils.div(bigDecimalUtils.mul(bigDecimalUtils.mul(bigDecimalUtils.sub(nowBuyPic, priceTransaction), volumeTransaction), 100000), nowBuyPic, 2);
profitAndLoss = (priceTransaction - nowBuyPic) * volumeTransaction * Constant.LEVERQUANTITY / nowBuyPic;
takeStashVO.setCurrentPrice(nowBuyPic);
}
} else { //美元在后
if ("buy".equals(buySellType)) { //买
// profitAndLoss = (nowBuyPic - priceTransaction) * volumeTransaction * 100000.0;
profitAndLoss = bigDecimalUtils.mul(bigDecimalUtils.mul(bigDecimalUtils.sub(nowSellPic, priceTransaction), volumeTransaction), 100000, 2);
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.LEVERQUANTITY;
takeStashVO.setCurrentPrice(nowSellPic);
} else { //卖
// profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * 100000.0;
profitAndLoss = bigDecimalUtils.mul(bigDecimalUtils.mul(bigDecimalUtils.sub(nowBuyPic, priceTransaction), volumeTransaction), 100000, 2);
profitAndLoss = (nowBuyPic - priceTransaction) * volumeTransaction * Constant.LEVERQUANTITY;
takeStashVO.setCurrentPrice(nowBuyPic);
}
}
takeStashVO.setProfitAndLoss(profitAndLoss);
takeStashVO.setProfitAndLoss(bigDecimalUtils.mul(profitAndLoss, 1, 2));
takeStashVOS.add(takeStashVO);
}
if (0 == takeStashVOS.size()) {
@ -150,26 +147,21 @@ public class TakeStashController {
Double profitAndLoss;
if (tradingCode.startsWith("USD")) { //美元在前
if ("buy".equals(buySellType)) { //买
// profitAndLoss =(nowBuyPic - priceTransaction) * volumeTransaction * 100000.0 / nowBuyPic;
profitAndLoss = bigDecimalUtils.div(bigDecimalUtils.mul(bigDecimalUtils.mul(bigDecimalUtils.sub(nowSellPic, priceTransaction), volumeTransaction), 100000), nowSellPic, 2);
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.LEVERQUANTITY / nowSellPic;
} else { //卖
// profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * 100000.0 / nowSellPic;
profitAndLoss = bigDecimalUtils.div(bigDecimalUtils.mul(bigDecimalUtils.mul(bigDecimalUtils.sub(nowBuyPic, priceTransaction), volumeTransaction), 100000), nowBuyPic, 2);
profitAndLoss = (priceTransaction - nowBuyPic) * volumeTransaction * Constant.LEVERQUANTITY / nowBuyPic;
}
} else { //美元在后
if ("buy".equals(buySellType)) { //买
// profitAndLoss = (nowBuyPic - priceTransaction) * volumeTransaction * 100000.0;
profitAndLoss = bigDecimalUtils.mul(bigDecimalUtils.mul(bigDecimalUtils.sub(nowSellPic, priceTransaction), volumeTransaction), 100000, 2);
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.LEVERQUANTITY;
} else { //卖
// profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * 100000.0;
profitAndLoss = bigDecimalUtils.mul(bigDecimalUtils.mul(bigDecimalUtils.sub(nowBuyPic, priceTransaction), volumeTransaction), 100000, 2);
profitAndLoss = (nowBuyPic - priceTransaction) * volumeTransaction * Constant.LEVERQUANTITY;
}
}
// totalProfitAndLoss = totalProfitAndLoss + profitAndLoss;
totalProfitAndLoss=bigDecimalUtils.add(totalProfitAndLoss,profitAndLoss);
totalProfitAndLoss = totalProfitAndLoss + profitAndLoss;
totalProfitAndLoss = bigDecimalUtils.mul(totalProfitAndLoss, 1, 2);
}
return totalProfitAndLoss;
}
//修改当前持仓
@ -208,58 +200,49 @@ public class TakeStashController {
String stashId = jsonObject.getString("stashId");
String memberId = jsonObject.getString("memberId");
TakeStash takeStashNew = takeStashService.selectByPrimaryKey(stashId);
//member方面 设置可用资金和保证金 可用资金=可用资金+买/卖所得钱 保证金=保证金-交易使用的保证金
Member member = memberService.selectByPrimaryKey(memberId);
Double availableFunds = member.getAvailableFunds(); //可用资金
String buySellType = takeStashNew.getBuySellType();
String tradingCode = takeStashNew.getTradingCode();
Double volumeTransaction = takeStashNew.getVolumeTransaction();
Double priceTransaction = takeStashNew.getPriceTransaction(); //获取进仓时交易价格
String trainingId = takeStashNew.getTrainingId();
Double priceTransactionCloser = 0.0; //记录出仓时交易价格
ForexMarketData forexData = forexMarketDateController.getForexMarketDateByCode(tradingCode);
Double backFund = 0.0; //返还资金
Double profitAndLoss = 0.0; //盈亏
Double nowSellPic = Double.valueOf(forexData.getSellPic());//当前卖价
Double nowBuyPic = forexData.getBuyPic(); //当前买价
Double margin=0.0; //记录保证金变化量
if (tradingCode.startsWith("USD")) { //美元在前 返还资金表达式为
//可用保证金计算
margin = startUSDMarginNeed(trainingId,memberId,0,tradingCode,buySellType,volumeTransaction);
if ("buy".equals(buySellType)) { //如果持仓方向为买 则平仓方向为卖
priceTransactionCloser = nowSellPic;
// CumulativeProfitLoss = (nowSellPic - priceTransaction) * volumeTransaction * 100000.0 / nowSellPic;
profitAndLoss = bigDecimalUtils.div(bigDecimalUtils.mul(bigDecimalUtils.mul(bigDecimalUtils.sub(nowSellPic, priceTransaction), volumeTransaction), 100000), nowSellPic, 2);
backFund = bigDecimalUtils.add(bigDecimalUtils.mul(priceTransaction,bigDecimalUtils.mul(1000,volumeTransaction),2),profitAndLoss);
availableFunds = bigDecimalUtils.add(availableFunds,backFund);
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.LEVERQUANTITY / nowSellPic;
} else if ("sell".equals(buySellType)) { //如果持仓方向为卖 则平仓方向为买 可用资金=可用资金+当前买价*1000*买卖手
priceTransactionCloser = nowBuyPic;
profitAndLoss = bigDecimalUtils.div(bigDecimalUtils.mul(bigDecimalUtils.mul(bigDecimalUtils.sub(nowBuyPic, priceTransaction), volumeTransaction), 100000), nowBuyPic, 2);
backFund = bigDecimalUtils.add(bigDecimalUtils.mul(priceTransaction,bigDecimalUtils.mul(1000,volumeTransaction),2),profitAndLoss);
availableFunds = bigDecimalUtils.add(availableFunds,backFund);
profitAndLoss = (priceTransaction - nowBuyPic) * volumeTransaction * Constant.LEVERQUANTITY / nowBuyPic;
}
} else { //美元在后
backFund = 1000 * volumeTransaction;
margin =endUSDMarginNeed(trainingId,memberId,0,tradingCode,buySellType,takeStashNew.getVolumeTransaction(),priceTransaction);
if ("buy".equals(buySellType)) { //如果持仓方向为买 则平仓方向为卖 可用资金=可用资金+1000*买卖手
priceTransactionCloser = nowSellPic;
profitAndLoss = bigDecimalUtils.mul(bigDecimalUtils.mul(bigDecimalUtils.sub(nowSellPic, priceTransaction), volumeTransaction), 100000, 2);
backFund = bigDecimalUtils.add(bigDecimalUtils.mul(priceTransaction,bigDecimalUtils.mul(1000,volumeTransaction),2),profitAndLoss);
availableFunds = bigDecimalUtils.add(availableFunds,backFund);
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * Constant.LEVERQUANTITY;
} else if ("sell".equals(buySellType)) { //如果持仓方向为卖 则平仓方向为买 可用资金=可用资金+当前买价*1000*买卖手
priceTransactionCloser = nowBuyPic;
profitAndLoss = bigDecimalUtils.mul(bigDecimalUtils.mul(bigDecimalUtils.sub(nowBuyPic, priceTransaction), volumeTransaction), 100000, 2);
backFund = bigDecimalUtils.add(bigDecimalUtils.mul(priceTransaction,bigDecimalUtils.mul(1000,volumeTransaction),2),profitAndLoss);
availableFunds = bigDecimalUtils.add(availableFunds,backFund);
profitAndLoss = (nowBuyPic - priceTransaction) * volumeTransaction * Constant.LEVERQUANTITY;
}
}
Double marginUsed = member.getMarginUsed();//未修改前member表中成员保证金
Double tradingMargin = takeStashNew.getTradingMargin(); //进仓时使用的保证金
marginUsed = bigDecimalUtils.sub(marginUsed,tradingMargin); //修改成员保证金
member.setAvailableFunds(availableFunds);
marginUsed = bigDecimalUtils.mul(marginUsed-margin,1,2); //修改成员保证金
member.setMarginUsed(marginUsed);
Integer closingTrades = member.getClosingTrades();
member.setClosingTrades(closingTrades++);
Double cumulativeProfitLoss = member.getCumulativeProfitLoss(); //为修改前member表中的累积盈亏
member.setCumulativeProfitLoss(bigDecimalUtils.add(cumulativeProfitLoss,profitAndLoss)); //修改累计盈亏
member.setTotalAssets(bigDecimalUtils.add(bigDecimalUtils.add(member.getInitialCapital(),cumulativeProfitLoss),profitAndLoss)); //设置总资产
member.setCumulativeProfitLoss(bigDecimalUtils.add(cumulativeProfitLoss, profitAndLoss)); //修改累计盈亏
member.setTotalAssets(bigDecimalUtils.add(bigDecimalUtils.add(member.getInitialCapital(), cumulativeProfitLoss), profitAndLoss)); //设置总资产
memberService.updateByPrimaryKeySelective(member);
flashTotalPositionProfitLoss(member.getMemberId());
TakeStash takeStash = new TakeStash();
@ -270,45 +253,132 @@ public class TakeStashController {
takeStash.setTimeTransactionClose(new Date());
takeStashService.updateByPrimaryKeySelective(takeStash);
wainingService.compareMarginLevels(member.getMemberId(),member.getTrainingId());//更改可用保证金后 调用预警
wainingService.compareMarginLevels(member.getMemberId(), member.getTrainingId());//更改可用保证金后 调用预警
return new ResultEntity(HttpStatus.OK, "平仓成功");
}
//美元在后 返回保证金变化量 公式sell交易量*交易价格+sell交易量*价格+.../交易总量+(buy交易量*交易价格+buy交易量*价格+...)/交易总量)*合约量/杠杆倍数*sellbuy最高交易量
public Double endUSDMarginNeed(String trainingId,String memberId,int status,String tradingCode,String buySellType,Double transactionVolume,Double priceTransaction){
Double margin=0.0;
//获取持仓表中tradingCode所有buysell情况 根据trainingId和memberId查询交易品种=tradingCode的所有数据
List<TakeStash> takeStashList=takeStashService.findTakeStashByTrainingIdAndMemberIdAndStatusAndTradingCode(trainingId,memberId,status,tradingCode);
Double buyTotalMargin=0.0; //类型为买的 交易量*交易价格之和
Double sellTotalMargin=0.0; //类型为卖的 交易量*交易价格之和
Double totalVolumeTransaction=0.0; //交易总量
Double buyTotalVolumeTransaction=0.0; //buy类型交易总量
Double sellTotalVolumeTransaction=0.0; //sell类型交易总量
Double highVolumeTransaction=0.0; //记录buy/sell类型交易总量中较高的一个
if(takeStashList.size()==1){
if("buy".equals(buySellType)){
margin=transactionVolume*priceTransaction*Constant.LEVERQUANTITY;
}else {
margin=transactionVolume*priceTransaction*Constant.LEVERQUANTITY;
}
return margin;
}else {
for (int i = 0; i < takeStashList.size(); i++) {
TakeStash takeStash = takeStashList.get(i);
String buySellTypeTakeStash = takeStash.getBuySellType();
Double volumeTransactionTakeStash = takeStash.getVolumeTransaction(); //持仓交易量
Double priceTransactionTakeStash = takeStash.getPriceTransaction(); //持仓交易价格
if("buy".equals(buySellTypeTakeStash)){
buyTotalMargin=buyTotalMargin+volumeTransactionTakeStash*priceTransactionTakeStash;
buyTotalVolumeTransaction=buyTotalVolumeTransaction+volumeTransactionTakeStash;
}else {
sellTotalMargin=sellTotalMargin+volumeTransactionTakeStash*priceTransactionTakeStash;
sellTotalVolumeTransaction=sellTotalVolumeTransaction+volumeTransactionTakeStash;
}
totalVolumeTransaction=totalVolumeTransaction+volumeTransactionTakeStash;
}
}
if (buyTotalVolumeTransaction>=sellTotalVolumeTransaction){ //设置带sell\buy的最高交易交易总量
highVolumeTransaction=buyTotalVolumeTransaction;
}else {
highVolumeTransaction=sellTotalVolumeTransaction;
}
//获取相同tradingCode持仓表总使用保证金
Double totalMargin=(buyTotalMargin+sellTotalMargin)/totalVolumeTransaction*Constant.LEVERQUANTITY*highVolumeTransaction;
//获取去除平仓数据后的相同tradingCode持仓表总使用保证金
Double totalMarginRemoveCloseTakeStash=0.0;
if("buy".equals(buySellType)){
totalMarginRemoveCloseTakeStash=((buyTotalMargin-transactionVolume*priceTransaction)+sellTotalMargin)/totalVolumeTransaction*Constant.LEVERQUANTITY*highVolumeTransaction;
}else {
totalMarginRemoveCloseTakeStash=(buyTotalMargin+(sellTotalMargin-transactionVolume*priceTransaction))/totalVolumeTransaction*Constant.LEVERQUANTITY*highVolumeTransaction;
}
margin=totalMargin-totalMarginRemoveCloseTakeStash;
return margin;
}
public void updateMemberAndTakeStash(String memberId, String stashId, Double backFund, Double CumulativeProfitLoss) {
TakeStash takeStashNew = takeStashService.selectByPrimaryKey(stashId);
Member member = memberService.selectByPrimaryKey(memberId);
Double marginUsed = member.getMarginUsed();//未修改前member表中成员保证金
Double tradingMargin;//进仓时使用的保证金
if (takeStashNew.getTradingMargin()!=null){
tradingMargin=0.0;
//美元在前 返回保证金变化量
public Double startUSDMarginNeed(String trainingId,String memberId,int status,String tradingCode,String buySellType,Double transactionVolume){
Double margin=0.0;
//获取持仓表中tradingCode所有buysell情况 根据trainingId和memberId查询交易品种=tradingCode的所有数据
List<TakeStash> takeStashList=takeStashService.findTakeStashByTrainingIdAndMemberIdAndStatusAndTradingCode(trainingId,memberId,status,tradingCode);
Double buyTotalVolumeTransaction=0.0;
Double sellTotalVolumeTransaction=0.0;
if(takeStashList.size()==1){
margin=transactionVolume*Constant.LEVERQUANTITY;
return margin;
}else {
tradingMargin = takeStashNew.getTradingMargin();
for (int i = 0; i < takeStashList.size(); i++) {
TakeStash takeStash = takeStashList.get(i);
String buySellTypeTakeStash = takeStash.getBuySellType();
if("buy".equals(buySellTypeTakeStash)){
buyTotalVolumeTransaction=buyTotalVolumeTransaction+takeStash.getVolumeTransaction();
}else {
sellTotalVolumeTransaction=sellTotalVolumeTransaction+takeStash.getVolumeTransaction();
}
}
}
marginUsed = bigDecimalUtils.sub(marginUsed,tradingMargin); //修改成员保证金
Double availableFunds = member.getAvailableFunds(); //可用资金
availableFunds = bigDecimalUtils.add(availableFunds,backFund);
member.setAvailableFunds(availableFunds);
member.setMarginUsed(marginUsed);
if("buy".equals(buySellType)){
if(buyTotalVolumeTransaction>=sellTotalVolumeTransaction){
if(buyTotalVolumeTransaction-transactionVolume>=sellTotalVolumeTransaction){
margin=(buyTotalVolumeTransaction-transactionVolume)*Constant.LEVERQUANTITY;
}else {
margin=(buyTotalVolumeTransaction-sellTotalVolumeTransaction)*Constant.LEVERQUANTITY;
}
}else {
margin=0.0;
}
}else {
if(sellTotalVolumeTransaction>=buyTotalVolumeTransaction){
if(sellTotalVolumeTransaction-transactionVolume>=buyTotalVolumeTransaction){
margin=(sellTotalVolumeTransaction-transactionVolume)*Constant.LEVERQUANTITY;
}else {
margin=(sellTotalVolumeTransaction-buyTotalVolumeTransaction)*Constant.LEVERQUANTITY;
}
}else {
margin=0.0;
}
}
return margin;
}
public void updateMemberAndTakeStash(String memberId, String stashId , Double CumulativeProfitLoss,Double margin) { //margin平仓后保证金变化量
Member member = memberService.selectByPrimaryKey(memberId);
Double marginUsed = member.getMarginUsed();//未修改前member表中成员保证金
marginUsed = bigDecimalUtils.sub(marginUsed, margin); //修改成员保证金
member.setMarginUsed(bigDecimalUtils.mul(marginUsed,1,2));
Integer closingTrades = member.getClosingTrades();
member.setClosingTrades(closingTrades++);
Double cumulativeProfitLoss = member.getCumulativeProfitLoss(); //为修改前member表中的累积盈亏
member.setCumulativeProfitLoss(bigDecimalUtils.add(cumulativeProfitLoss,CumulativeProfitLoss)); //修改累计盈亏
member.setTotalAssets(bigDecimalUtils.add(CumulativeProfitLoss,bigDecimalUtils.add(member.getInitialCapital(),cumulativeProfitLoss))); //设置总资产
member.setCumulativeProfitLoss(bigDecimalUtils.mul(bigDecimalUtils.add(cumulativeProfitLoss, CumulativeProfitLoss),1,2)); //修改累计盈亏
memberService.updateByPrimaryKeySelective(member);
TakeStash takeStash = new TakeStash();
takeStash.setStashId(stashId);
takeStash.setStatus(2);
takeStash.setProfitAndLossByClose(bigDecimalUtils.sub(backFund,tradingMargin)); //盈亏=返还资金-进仓时使用的保证金
takeStash.setProfitAndLossByClose(bigDecimalUtils.mul(CumulativeProfitLoss,1,2)); //盈亏=返还资金-进仓时使用的保证金
takeStashService.updateByPrimaryKeySelective(takeStash);
wainingService.compareMarginLevels(member.getMemberId(),member.getTrainingId());//更改可用保证金后 调用预警
wainingService.compareMarginLevels(member.getMemberId(), member.getTrainingId());//更改可用保证金后 调用预警
}
}

@ -2,9 +2,9 @@ package com.sztzjy.forex.trading_trading.controller;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.sztzjy.forex.trading_trading.annotation.AnonymousAccess;
import com.sztzjy.forex.trading_trading.config.Constant;
import com.sztzjy.forex.trading_trading.entity.ForexMarketData;
import com.sztzjy.forex.trading_trading.entity.Member;
import com.sztzjy.forex.trading_trading.entity.PendingOrder;
@ -124,44 +124,35 @@ public class TradingController {
if (transactionType.equals("sjkc")) {//市价开仓
String stashId = IdUtil.simpleUUID(); //设置市价开仓ID
if (tradingCode.startsWith("USD")) { //美元在前
if (availableFunds < transactionVolume * 1000.0) { //判断可用资金是否足够
margin = startUSDMarginNeed(trainingId,memberId,0,tradingCode,buySellType,transactionVolume);
if(availableFunds<margin){ //判断可用资金是否足够
return new ResultEntity(HttpStatus.BAD_REQUEST, "可用资金不足");
}
margin=bigDecimalUtils.mul(transactionVolume,1000,2); //所需保证金
availableFunds = bigDecimalUtils.sub(availableFunds,margin);
if ("buy".equals(buySellType)) {
priceTransaction=buyPic;
TakeStash takeStash = returnTakeStash(stashId,memberId, trainingId, tradingCode, currencyName, buySellType, transactionVolume, buyPic, stopLoss, stopWin , margin);
TakeStash takeStash = returnTakeStash(stashId,memberId, trainingId, tradingCode, currencyName, buySellType, transactionVolume, buyPic, stopLoss, stopWin);
takeStashService.insertTakeStash(takeStash);
} else if ("sell".equals(buySellType)) {
priceTransaction=sellPic;
TakeStash takeStash = returnTakeStash(stashId,memberId, trainingId, tradingCode, currencyName, buySellType, transactionVolume, sellPic, stopLoss, stopWin , margin);
TakeStash takeStash = returnTakeStash(stashId,memberId, trainingId, tradingCode, currencyName, buySellType, transactionVolume, sellPic, stopLoss, stopWin);
takeStashService.insertTakeStash(takeStash);
}
updateMemberAvailableFundsAndMarginUsed(member, availableFunds,margin);
} else { //美元在后 使用可用资金表达式为 买卖手*1000*卖/买价 判断可用资金是否足够 先判断是买还是卖
updateMemberAvailableFundsAndMarginUsed(member,margin);
} else { //美元在后 使用可用资金表达式为 买卖手*1000*卖/买价 判断可用资金是否足够
margin =endUSDMarginNeed(trainingId,memberId,0,tradingCode,buySellType,transactionVolume,buyPic,sellPic);
if (availableFunds < margin) {
return new ResultEntity(HttpStatus.BAD_REQUEST, "可用资金不足");
}
if ("buy".equals(buySellType)) {
priceTransaction=buyPic;
// margin=transactionVolume * 1000.0 * buyPic; //所需保证金
margin=bigDecimalUtils.mul(bigDecimalUtils.mul(transactionVolume,1000,2),buyPic,2);
if (availableFunds < margin) {
return new ResultEntity(HttpStatus.BAD_REQUEST, "可用资金不足");
}
availableFunds = availableFunds - margin;
TakeStash takeStash = returnTakeStash(stashId,memberId, trainingId, tradingCode, currencyName, buySellType, transactionVolume, buyPic, stopLoss, stopWin , margin);
TakeStash takeStash = returnTakeStash(stashId,memberId, trainingId, tradingCode, currencyName, buySellType, transactionVolume, buyPic, stopLoss, stopWin);
takeStashService.insertTakeStash(takeStash);
} else if ("sell".equals(buySellType)) {
priceTransaction=sellPic;
// margin=transactionVolume * 1000.0 * sellPic; //所需保证金
margin=bigDecimalUtils.mul(bigDecimalUtils.mul(transactionVolume,1000,2),sellPic,2);
if (availableFunds < margin) {
return new ResultEntity(HttpStatus.BAD_REQUEST, "可用资金不足");
}
availableFunds = availableFunds - margin;
TakeStash takeStash = returnTakeStash(stashId,memberId, trainingId, tradingCode, currencyName, buySellType, transactionVolume, sellPic, stopLoss, stopWin , margin);
TakeStash takeStash = returnTakeStash(stashId,memberId, trainingId, tradingCode, currencyName, buySellType, transactionVolume, sellPic, stopLoss, stopWin);
takeStashService.insertTakeStash(takeStash);
}
updateMemberAvailableFundsAndMarginUsed(member, availableFunds, margin);
updateMemberAvailableFundsAndMarginUsed(member, margin);
}
//保存持仓数据后 保存到redis中
if(stopLossWinFlag==true){
@ -182,6 +173,7 @@ public class TradingController {
map.put("transactionVolume",transactionVolume);
map.put("priceTransaction",priceTransaction);
map.put("memberId",memberId);
map.put("trainingId_",trainingId);
redisUtil.hmset("trainingId_"+trainingId+"_stashId_"+stashId,map);
}
} else if (transactionType.equals("gdkc")) {//挂单开仓 生成的数据为挂单数据 挂单数据生效 判断可用资金是否充足
@ -233,7 +225,7 @@ public class TradingController {
//返回持仓对象
private TakeStash returnTakeStash(String stashId,String memberId, String trainingId, String tradingCode,String currencyName, String buySellType, Double transactionVolume, Double buyOrSellPic, Double stopLoss, Double stopWin,Double tradingMargin) {
private TakeStash returnTakeStash(String stashId,String memberId, String trainingId, String tradingCode,String currencyName, String buySellType, Double transactionVolume, Double buyOrSellPic, Double stopLoss, Double stopWin) {
TakeStash takeStash = new TakeStash();
Date now = new Date();
String orderNumberStr = DateUtil.format(now, "yyyyMMddHHmmss") + System.currentTimeMillis();
@ -250,13 +242,11 @@ public class TradingController {
takeStash.setStopWin(stopWin);
takeStash.setTimeTransaction(now);
takeStash.setStatus(0);
takeStash.setTradingMargin(tradingMargin);
return takeStash;
}
//更新可用资金及已用保证金
private void updateMemberAvailableFundsAndMarginUsed(Member member, Double availableFunds,Double margin) {
member.setAvailableFunds(availableFunds); //设置可用资金
private void updateMemberAvailableFundsAndMarginUsed(Member member,Double margin) {
Double marginUsed=member.getMarginUsed();
member.setMarginUsed(marginUsed + margin); //设置已用保证金
Integer openingTrades = member.getOpeningTrades(); //获取开仓次数
@ -342,5 +332,92 @@ public class TradingController {
pendingOrderService.insert(pendingOrder);
}
//美元在前 返回保证金变化量
public Double startUSDMarginNeed(String trainingId,String memberId,int status,String tradingCode,String buySellType,Double transactionVolume){
Double margin=0.0;
//获取持仓表中tradingCode所有buysell情况 根据trainingId和memberId查询交易品种=tradingCode的所有数据
List<TakeStash> takeStashList=takeStashService.findTakeStashByTrainingIdAndMemberIdAndStatusAndTradingCode(trainingId,memberId,status,tradingCode);
Double buyTotalVolumeTransaction=0.0;
Double sellTotalVolumeTransaction=0.0;
if(takeStashList.isEmpty()){
margin=transactionVolume*Constant.LEVERQUANTITY;
return margin;
}else {
for (int i = 0; i < takeStashList.size(); i++) {
TakeStash takeStash = takeStashList.get(i);
String buySellTypeTakeStash = takeStash.getBuySellType();
if("buy".equals(buySellTypeTakeStash)){
buyTotalVolumeTransaction=buyTotalVolumeTransaction+takeStash.getVolumeTransaction();
}else {
sellTotalVolumeTransaction=sellTotalVolumeTransaction+takeStash.getVolumeTransaction();
}
}
}
if("buy".equals(buySellType)){
if(transactionVolume+buyTotalVolumeTransaction<=sellTotalVolumeTransaction){
margin=0.0;
}else {
margin=(transactionVolume+buyTotalVolumeTransaction-sellTotalVolumeTransaction)*Constant.LEVERQUANTITY;
}
}else {
if(transactionVolume+sellTotalVolumeTransaction<=buyTotalVolumeTransaction){
margin=0.0;
}else {
margin=(transactionVolume+sellTotalVolumeTransaction-buyTotalVolumeTransaction)*Constant.LEVERQUANTITY;
}
}
return margin;
}
//美元在后 返回保证金变化量 公式sell交易量*交易价格+sell交易量*价格+.../交易总量+(buy交易量*交易价格+buy交易量*价格+...)/交易总量)*合约量/杠杆倍数*sellbuy最高交易量
public Double endUSDMarginNeed(String trainingId,String memberId,int status,String tradingCode,String buySellType,Double transactionVolume,Double buyPic,Double sellPic){
Double margin=0.0;
//获取持仓表中tradingCode所有buysell情况 根据trainingId和memberId查询交易品种=tradingCode的所有数据
List<TakeStash> takeStashList=takeStashService.findTakeStashByTrainingIdAndMemberIdAndStatusAndTradingCode(trainingId,memberId,status,tradingCode);
Double buyTotalMargin=0.0; //类型为买的 交易量*交易价格之和
Double sellTotalMargin=0.0; //类型为卖的 交易量*交易价格之和
Double totalVolumeTransaction=0.0; //交易总量
Double buyTotalVolumeTransaction=0.0; //buy类型交易总量
Double sellTotalVolumeTransaction=0.0; //sell类型交易总量
Double highVolumeTransaction=0.0; //记录buy/sell类型交易总量中较高的一个
if(takeStashList.isEmpty()){
if("buy".equals(buySellType)){
margin=transactionVolume*buyPic*Constant.LEVERQUANTITY;
}else {
margin=transactionVolume*sellPic*Constant.LEVERQUANTITY;
}
return margin;
}else {
for (int i = 0; i < takeStashList.size(); i++) {
TakeStash takeStash = takeStashList.get(i);
String buySellTypeTakeStash = takeStash.getBuySellType();
Double volumeTransactionTakeStash = takeStash.getVolumeTransaction(); //持仓交易量
Double priceTransactionTakeStash = takeStash.getPriceTransaction(); //持仓交易价格
if("buy".equals(buySellTypeTakeStash)){
buyTotalMargin=buyTotalMargin+volumeTransactionTakeStash*priceTransactionTakeStash;
buyTotalVolumeTransaction=buyTotalVolumeTransaction+volumeTransactionTakeStash;
}else {
sellTotalMargin=sellTotalMargin+volumeTransactionTakeStash*priceTransactionTakeStash;
sellTotalVolumeTransaction=sellTotalVolumeTransaction+volumeTransactionTakeStash;
}
totalVolumeTransaction=totalVolumeTransaction+volumeTransactionTakeStash;
}
}
totalVolumeTransaction=totalVolumeTransaction+transactionVolume; //交易总量增加本次进仓的交易量
if("buy".equals(buySellType)){
buyTotalMargin=buyTotalMargin+transactionVolume*buyPic;
}else {
sellTotalMargin=sellTotalMargin+transactionVolume*sellPic;
}
if (buyTotalVolumeTransaction>=sellTotalVolumeTransaction){ //设置带sell\buy的最高交易交易总量
highVolumeTransaction=buyTotalVolumeTransaction;
}else {
highVolumeTransaction=sellTotalVolumeTransaction;
}
margin=(buyTotalMargin+sellTotalMargin)/totalVolumeTransaction*Constant.LEVERQUANTITY*highVolumeTransaction;
return margin;
}
}

@ -234,6 +234,7 @@ public class TrainingController {
member.setMemberId(IdUtil.simpleUUID());
member.setTrainingId("999999999");
member.setTrainingName("AdminStudentPractice");
member.setSchoolId(999999999);
}
memberService.insertAll(members);
}
@ -242,6 +243,7 @@ public class TrainingController {
member.setMemberId(IdUtil.simpleUUID());
member.setTrainingId("999999999");
member.setTrainingName("AdminStudentPractice");
member.setSchoolId(999999999);
memberService.insert(member);
}

@ -5,6 +5,7 @@ import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.sztzjy.forex.trading_trading.annotation.AnonymousAccess;
import com.sztzjy.forex.trading_trading.common.mql5API.Mql5API;
import com.sztzjy.forex.trading_trading.config.Constant;
import com.sztzjy.forex.trading_trading.controller.PendingOrderController;
import com.sztzjy.forex.trading_trading.controller.TakeStashController;
import com.sztzjy.forex.trading_trading.controller.TradingController;
@ -186,60 +187,55 @@ public class ScheduledTask {
Double stopWin = (Double) map.get("stopWin");
String stashId = (String) map.get("stashId");
String memberId = (String) map.get("memberId");
String trainingId = (String) map.get("trainingId");
Double transactionVolume = (Double) map.get("transactionVolume");
Double priceTransaction = (Double) map.get("priceTransaction");
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 cumulativeProfitLoss=(stopLoss-priceTransaction)*transactionVolume*100000/stopLoss;
Double backFund=transactionVolume*1000+cumulativeProfitLoss;
takeStashController.updateMemberAndTakeStash(memberId,stashId,backFund,cumulativeProfitLoss);
Double profitLoss=(stopLoss-priceTransaction)*transactionVolume*Constant.LEVERQUANTITY/stopLoss; //止损盈利
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
}
if(stopWin!=-1 && sellPic>=stopWin){
Double cumulativeProfitLoss=0.0;
Double backFund=transactionVolume*1000+(stopWin-priceTransaction)*transactionVolume*100000/stopWin;
takeStashController.updateMemberAndTakeStash(memberId,stashId,backFund,cumulativeProfitLoss);
Double profitLoss=(stopWin-priceTransaction)*transactionVolume*Constant.LEVERQUANTITY/stopWin; //止盈盈利
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
}
}else { //开仓方式为卖 则平仓方式为买 先获取买价
Double buyPic = forexMarketData.getBuyPic();
if(stopLoss!=-1 && buyPic<=stopLoss){
Double cumulativeProfitLoss=0.0;
Double backFund=transactionVolume*1000+(priceTransaction-stopLoss)*transactionVolume*100000/stopLoss;
takeStashController.updateMemberAndTakeStash(memberId,stashId,backFund,cumulativeProfitLoss);
Double profitLoss=(priceTransaction-stopLoss)*transactionVolume*Constant.LEVERQUANTITY/stopLoss;
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
}
if(stopWin!=-1 && buyPic>=stopWin){
Double cumulativeProfitLoss=0.0;
Double backFund=transactionVolume*1000+(priceTransaction-stopWin)*transactionVolume*100000/stopWin;
takeStashController.updateMemberAndTakeStash(memberId,stashId,backFund,cumulativeProfitLoss);
Double profitLoss=(priceTransaction-stopWin)*transactionVolume*Constant.LEVERQUANTITY/stopWin;
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
}
}
}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 cumulativeProfitLoss=0.0;
Double backFund=transactionVolume*1000*stopLoss;
takeStashController.updateMemberAndTakeStash(memberId,stashId,backFund,cumulativeProfitLoss);
Double profitLoss=(stopLoss-priceTransaction)*transactionVolume*Constant.LEVERQUANTITY;
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
}
if(stopWin!=-1 && sellPic>=stopWin){
Double cumulativeProfitLoss=0.0;
Double backFund=transactionVolume*1000*stopWin;
takeStashController.updateMemberAndTakeStash(memberId,stashId,backFund,cumulativeProfitLoss);
Double profitLoss=(stopWin-priceTransaction)*transactionVolume*Constant.LEVERQUANTITY;
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
}
}else { //开仓方式为卖 则平仓方式为买 先获取买价
Double buyPic = forexMarketData.getBuyPic();
if(stopLoss!=-1 && buyPic<=stopLoss){
Double cumulativeProfitLoss=0.0;
Double backFund=transactionVolume*1000*stopLoss;
takeStashController.updateMemberAndTakeStash(memberId,stashId,backFund,cumulativeProfitLoss);
Double profitLoss=(stopLoss-priceTransaction)*transactionVolume*Constant.LEVERQUANTITY;
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
}
if(stopWin!=-1 && buyPic>=stopWin){
Double cumulativeProfitLoss=0.0;
Double backFund=transactionVolume*1000*stopWin;
takeStashController.updateMemberAndTakeStash(memberId,stashId,backFund,cumulativeProfitLoss);
Double profitLoss=(stopWin-priceTransaction)*transactionVolume*Constant.LEVERQUANTITY;
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
}
}
}

@ -52,4 +52,11 @@ 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();
criteria.andMemberIdEqualTo(memberId).andTrainingIdEqualTo(trainingId).andStatusEqualTo(status).andTradingCodeEqualTo(tradingCode);
List<TakeStash> takeStashes = takeStashMapper.selectByExample(example);
return takeStashes;
}
}

Loading…
Cancel
Save