|
|
|
@ -9,6 +9,8 @@ import com.sztzjy.forex.trading_trading.entity.Member;
|
|
|
|
|
import com.sztzjy.forex.trading_trading.entity.TakeStash;
|
|
|
|
|
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.ForexMarketDateUtil;
|
|
|
|
|
import com.sztzjy.forex.trading_trading.util.RedisUtil;
|
|
|
|
|
import com.sztzjy.forex.trading_trading.util.ResultEntity;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
@ -18,10 +20,7 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("api/takeStash")
|
|
|
|
@ -39,18 +38,26 @@ public class TakeStashController {
|
|
|
|
|
@Autowired
|
|
|
|
|
MemberService memberService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
ForexMarketDateUtil forexMarketDateController;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
TrainingService trainingService;
|
|
|
|
|
|
|
|
|
|
//获取当前持仓表 status 0为获取当前持仓/1为挂单持仓/2为历史持仓
|
|
|
|
|
@AnonymousAccess
|
|
|
|
|
@PostMapping("getTakeStashList")
|
|
|
|
|
public ResultEntity getTakeStashList(@RequestBody TakeStash takeStash) {
|
|
|
|
|
List<TakeStash> takeStashList = takeStashService.findTakeStashByTrainingIdAndMemberIdAndStatus(takeStash.getTrainingId(),takeStash.getMemberId(), takeStash.getStatus());
|
|
|
|
|
List<TakeStashVO> takeStashVOList=new ArrayList<>();
|
|
|
|
|
List<TakeStash> takeStashList = takeStashService.findTakeStashByTrainingIdAndMemberIdAndStatus(takeStash.getTrainingId(), takeStash.getMemberId(), takeStash.getStatus());
|
|
|
|
|
List<TakeStashVO> takeStashVOList = new ArrayList<>();
|
|
|
|
|
for (int i = 0; i < takeStashList.size(); i++) {
|
|
|
|
|
TakeStashVO takeStashVO=new TakeStashVO(takeStashList.get(i));
|
|
|
|
|
TakeStashVO takeStashVO = new TakeStashVO(takeStashList.get(i));
|
|
|
|
|
takeStashVOList.add(takeStashVO);
|
|
|
|
|
}
|
|
|
|
|
if (takeStash.getStatus() == 0) {
|
|
|
|
|
takeStashVOList = flashProfitAndLoss(takeStashVOList).getBody().getData();
|
|
|
|
|
return new ResultEntity(HttpStatus.OK, "返回持仓数据",takeStashVOList);
|
|
|
|
|
}
|
|
|
|
|
return new ResultEntity(HttpStatus.OK, "返回持仓数据", takeStashVOList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//根据持仓ID获取单个持仓数据
|
|
|
|
@ -65,45 +72,78 @@ public class TakeStashController {
|
|
|
|
|
//持仓盈亏和当前价格刷新
|
|
|
|
|
@AnonymousAccess
|
|
|
|
|
@PostMapping("flashProfitAndLoss")
|
|
|
|
|
public ResultEntity<List<TakeStashVO>> flashProfitAndLoss(@RequestBody List<TakeStashVO> takeStashVOList){
|
|
|
|
|
List<TakeStashVO> takeStashVOS=new ArrayList<>();
|
|
|
|
|
public ResultEntity<List<TakeStashVO>> flashProfitAndLoss(@RequestBody List<TakeStashVO> takeStashVOList) {
|
|
|
|
|
List<TakeStashVO> takeStashVOS = new ArrayList<>();
|
|
|
|
|
for (int i = 0; i < takeStashVOList.size(); i++) {
|
|
|
|
|
TakeStashVO takeStashVO = takeStashVOList.get(i);
|
|
|
|
|
if(0!=takeStashVO.getStatus()){
|
|
|
|
|
if (0 != takeStashVO.getStatus()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
String buySellType = takeStashVO.getBuySellType(); //买入或卖出
|
|
|
|
|
String tradingCode = takeStashVO.getTradingCode(); //交易品种
|
|
|
|
|
Double priceTransaction = takeStashVO.getPriceTransaction(); //交易价格
|
|
|
|
|
Double volumeTransaction = takeStashVO.getVolumeTransaction(); //交易量
|
|
|
|
|
ForexMarketData forexData = getForexMarketDateByCode(tradingCode);
|
|
|
|
|
ForexMarketData forexData = forexMarketDateController.getForexMarketDateByCode(tradingCode);
|
|
|
|
|
Double nowBuyPic = forexData.getBuyPic(); //当前买价
|
|
|
|
|
Double nowSellPic = Double.valueOf(forexData.getSellPic()); //当前卖价
|
|
|
|
|
Double profitAndLoss;
|
|
|
|
|
if (tradingCode.startsWith("USD")) { //美元在前
|
|
|
|
|
if("buy".equals(buySellType)){ //买
|
|
|
|
|
profitAndLoss= (nowBuyPic-priceTransaction)*volumeTransaction*100000/nowBuyPic;
|
|
|
|
|
if ("buy".equals(buySellType)) { //买
|
|
|
|
|
profitAndLoss = (nowBuyPic - priceTransaction) * volumeTransaction * 100000 / nowBuyPic;
|
|
|
|
|
takeStashVO.setCurrentPrice(nowBuyPic);
|
|
|
|
|
}else { //卖
|
|
|
|
|
profitAndLoss= (nowSellPic-priceTransaction)*volumeTransaction*100000/nowSellPic;
|
|
|
|
|
} else { //卖
|
|
|
|
|
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * 100000 / nowSellPic;
|
|
|
|
|
takeStashVO.setCurrentPrice(nowSellPic);
|
|
|
|
|
}
|
|
|
|
|
}else { //美元在后
|
|
|
|
|
if("buy".equals(buySellType)){ //买
|
|
|
|
|
profitAndLoss= (nowBuyPic-priceTransaction)*volumeTransaction*100000;
|
|
|
|
|
} else { //美元在后
|
|
|
|
|
if ("buy".equals(buySellType)) { //买
|
|
|
|
|
profitAndLoss = (nowBuyPic - priceTransaction) * volumeTransaction * 100000;
|
|
|
|
|
takeStashVO.setCurrentPrice(nowBuyPic);
|
|
|
|
|
}else { //卖
|
|
|
|
|
profitAndLoss= (nowSellPic-priceTransaction)*volumeTransaction*100000;
|
|
|
|
|
} else { //卖
|
|
|
|
|
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * 100000;
|
|
|
|
|
takeStashVO.setCurrentPrice(nowSellPic);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
takeStashVO.setProfitAndLoss(profitAndLoss);
|
|
|
|
|
takeStashVOS.add(takeStashVO);
|
|
|
|
|
}
|
|
|
|
|
if(0==takeStashVOS.size()){
|
|
|
|
|
return new ResultEntity(HttpStatus.OK, "当前持仓数据为空",null);
|
|
|
|
|
if (0 == takeStashVOS.size()) {
|
|
|
|
|
return new ResultEntity(HttpStatus.OK, "当前持仓数据为空", null);
|
|
|
|
|
}
|
|
|
|
|
return new ResultEntity(HttpStatus.OK, "持仓盈亏和当前价格刷新", takeStashVOS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//总持仓盈亏刷新 获取member下所有持仓数据
|
|
|
|
|
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 = forexMarketDateController.getForexMarketDateByCode(tradingCode);
|
|
|
|
|
Double nowBuyPic = forexData.getBuyPic(); //当前买价
|
|
|
|
|
Double nowSellPic = Double.valueOf(forexData.getSellPic()); //当前卖价
|
|
|
|
|
Double profitAndLoss;
|
|
|
|
|
if (tradingCode.startsWith("USD")) { //美元在前
|
|
|
|
|
if ("buy".equals(buySellType)) { //买
|
|
|
|
|
profitAndLoss = (nowBuyPic - priceTransaction) * volumeTransaction * 100000 / nowBuyPic;
|
|
|
|
|
} else { //卖
|
|
|
|
|
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * 100000 / nowSellPic;
|
|
|
|
|
}
|
|
|
|
|
} else { //美元在后
|
|
|
|
|
if ("buy".equals(buySellType)) { //买
|
|
|
|
|
profitAndLoss = (nowBuyPic - priceTransaction) * volumeTransaction * 100000;
|
|
|
|
|
} else { //卖
|
|
|
|
|
profitAndLoss = (nowSellPic - priceTransaction) * volumeTransaction * 100000;
|
|
|
|
|
}
|
|
|
|
|
return new ResultEntity(HttpStatus.OK, "持仓盈亏和当前价格刷新",takeStashVOS);
|
|
|
|
|
}
|
|
|
|
|
totalProfitAndLoss=totalProfitAndLoss+profitAndLoss;
|
|
|
|
|
}
|
|
|
|
|
return totalProfitAndLoss;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//修改当前持仓
|
|
|
|
@ -123,10 +163,10 @@ public class TakeStashController {
|
|
|
|
|
if (winOrLossStopBoolean == false) {
|
|
|
|
|
return new ResultEntity(HttpStatus.BAD_REQUEST, "止损或获利输入错误");
|
|
|
|
|
}
|
|
|
|
|
if (null!=stopLoss){
|
|
|
|
|
if (null != stopLoss) {
|
|
|
|
|
takeStash.setStopLoss(stopLoss);
|
|
|
|
|
}
|
|
|
|
|
if( null != stopWin){
|
|
|
|
|
if (null != stopWin) {
|
|
|
|
|
takeStash.setStopWin(stopWin);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -134,6 +174,7 @@ public class TakeStashController {
|
|
|
|
|
return new ResultEntity(HttpStatus.OK, "修改持仓成功");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//当前持仓平仓 status设置为2
|
|
|
|
|
@AnonymousAccess
|
|
|
|
|
@PostMapping("closeTakeStash")
|
|
|
|
@ -147,61 +188,80 @@ public class TakeStashController {
|
|
|
|
|
String buySellType = takeStashNew.getBuySellType();
|
|
|
|
|
String tradingCode = takeStashNew.getTradingCode();
|
|
|
|
|
Double volumeTransaction = takeStashNew.getVolumeTransaction();
|
|
|
|
|
ForexMarketData forexData = getForexMarketDateByCode(tradingCode);
|
|
|
|
|
Double backFund=0.0; //返还资金
|
|
|
|
|
if (tradingCode.startsWith("USD")) { //美元在前 返还资金表达式为 1000*卖/买价*买卖手
|
|
|
|
|
if("buy".equals(buySellType)){ //如果持仓方向为买 则平仓方向为卖 可用资金=可用资金+当前卖价*1000*买卖手
|
|
|
|
|
backFund=Double.valueOf(forexData.getSellPic())*1000*volumeTransaction;
|
|
|
|
|
availableFunds=availableFunds+backFund;
|
|
|
|
|
}else if("sell".equals(buySellType)){ //如果持仓方向为卖 则平仓方向为买 可用资金=可用资金+当前买价*1000*买卖手
|
|
|
|
|
backFund=forexData.getBuyPic()*1000*volumeTransaction;
|
|
|
|
|
availableFunds=availableFunds+backFund;
|
|
|
|
|
Double priceTransaction = takeStashNew.getPriceTransaction();
|
|
|
|
|
ForexMarketData forexData = forexMarketDateController.getForexMarketDateByCode(tradingCode);
|
|
|
|
|
Double backFund = 0.0; //返还资金
|
|
|
|
|
Double CumulativeProfitLoss=0.0; //盈亏
|
|
|
|
|
Double nowSellPic = Double.valueOf(forexData.getSellPic());//当前卖价
|
|
|
|
|
Double nowBuyPic = forexData.getBuyPic(); //当前买价
|
|
|
|
|
if (tradingCode.startsWith("USD")) { //美元在前 返还资金表达式为
|
|
|
|
|
if ("buy".equals(buySellType)) { //如果持仓方向为买 则平仓方向为卖
|
|
|
|
|
CumulativeProfitLoss=(nowSellPic - priceTransaction) * volumeTransaction * 100000 / nowSellPic;
|
|
|
|
|
backFund = 1000 * volumeTransaction+CumulativeProfitLoss;
|
|
|
|
|
availableFunds = availableFunds + backFund;
|
|
|
|
|
} else if ("sell".equals(buySellType)) { //如果持仓方向为卖 则平仓方向为买 可用资金=可用资金+当前买价*1000*买卖手
|
|
|
|
|
CumulativeProfitLoss=( priceTransaction-nowBuyPic ) * volumeTransaction * 100000 / nowBuyPic;
|
|
|
|
|
backFund = 1000 * volumeTransaction+CumulativeProfitLoss;
|
|
|
|
|
availableFunds = availableFunds + backFund;
|
|
|
|
|
}
|
|
|
|
|
}else { //美元在后
|
|
|
|
|
backFund=1000*volumeTransaction;
|
|
|
|
|
if("buy".equals(buySellType)){ //如果持仓方向为买 则平仓方向为卖 可用资金=可用资金+1000*买卖手
|
|
|
|
|
availableFunds=availableFunds+backFund;
|
|
|
|
|
}else if("sell".equals(buySellType)){ //如果持仓方向为卖 则平仓方向为买 可用资金=可用资金+当前买价*1000*买卖手
|
|
|
|
|
availableFunds=availableFunds+backFund;
|
|
|
|
|
} else { //美元在后
|
|
|
|
|
backFund = 1000 * volumeTransaction;
|
|
|
|
|
if ("buy".equals(buySellType)) { //如果持仓方向为买 则平仓方向为卖 可用资金=可用资金+1000*买卖手
|
|
|
|
|
CumulativeProfitLoss=(priceTransaction-nowSellPic)*volumeTransaction*100000/nowSellPic;
|
|
|
|
|
backFund=1000*volumeTransaction*nowSellPic+CumulativeProfitLoss;
|
|
|
|
|
availableFunds = availableFunds + backFund;
|
|
|
|
|
} else if ("sell".equals(buySellType)) { //如果持仓方向为卖 则平仓方向为买 可用资金=可用资金+当前买价*1000*买卖手
|
|
|
|
|
CumulativeProfitLoss=( priceTransaction-nowBuyPic)*volumeTransaction*100000/nowBuyPic;
|
|
|
|
|
backFund=1000*volumeTransaction*nowBuyPic+CumulativeProfitLoss;
|
|
|
|
|
availableFunds = availableFunds + backFund;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Double marginUsed = member.getMarginUsed();//未修改前member表中成员保证金
|
|
|
|
|
Double tradingMargin = takeStashNew.getTradingMargin(); //进仓时使用的保证金
|
|
|
|
|
marginUsed=marginUsed-tradingMargin; //修改成员保证金
|
|
|
|
|
marginUsed = marginUsed - tradingMargin; //修改成员保证金
|
|
|
|
|
member.setAvailableFunds(availableFunds);
|
|
|
|
|
member.setMarginUsed(marginUsed);
|
|
|
|
|
Integer closingTrades = member.getClosingTrades();
|
|
|
|
|
member.setClosingTrades(closingTrades++);
|
|
|
|
|
Double cumulativeProfitLoss = member.getCumulativeProfitLoss(); //为修改前member表中的累积盈亏
|
|
|
|
|
member.setCumulativeProfitLoss(cumulativeProfitLoss+CumulativeProfitLoss); //修改累计盈亏
|
|
|
|
|
member.setTotalAssets(member.getInitialCapital()+cumulativeProfitLoss+CumulativeProfitLoss); //设置总资产
|
|
|
|
|
memberService.updateByPrimaryKeySelective(member);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TakeStash takeStash = new TakeStash();
|
|
|
|
|
takeStash.setStashId(stashId);
|
|
|
|
|
takeStash.setStatus(2);
|
|
|
|
|
takeStash.setProfitAndLossByClose(backFund- tradingMargin); //盈亏=返还资金-进仓时使用的保证金
|
|
|
|
|
takeStash.setProfitAndLossByClose(backFund - tradingMargin); //盈亏=返还资金-进仓时使用的保证金
|
|
|
|
|
takeStashService.updateByPrimaryKeySelective(takeStash);
|
|
|
|
|
return new ResultEntity(HttpStatus.OK, "平仓成功");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//监听止损止盈 根据code获取当前买卖价格 如果价格高于/低于止损止盈 则按照止损止盈的值进行平仓
|
|
|
|
|
//获取持仓表中所有状态为0 且止损和止盈部为空的所有数据 获取当前价位如果
|
|
|
|
|
public void monitorStopLossProfit(){
|
|
|
|
|
// takeStashService.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void updateMemberAndTakeStash(String memberId,String stashId,Double backFund,Double CumulativeProfitLoss){
|
|
|
|
|
TakeStash takeStashNew = takeStashService.selectByPrimaryKey(stashId);
|
|
|
|
|
Member member = memberService.selectByPrimaryKey(memberId);
|
|
|
|
|
|
|
|
|
|
//根据tradingCode获取ForexMarketData对象
|
|
|
|
|
public ForexMarketData getForexMarketDateByCode(String tradingCode){
|
|
|
|
|
List<ForexMarketData> forexDateList = redisUtil.get("ForexDateList");
|
|
|
|
|
Map<String,ForexMarketData> map=new HashMap<>();
|
|
|
|
|
if (forexDateList.size() > 0) {
|
|
|
|
|
for (int i = 0; i < forexDateList.size(); i++) {
|
|
|
|
|
String getCode = forexDateList.get(i).getCode();
|
|
|
|
|
map.put(getCode,forexDateList.get(i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ForexMarketData forexData = map.get(tradingCode);
|
|
|
|
|
return forexData;
|
|
|
|
|
Double marginUsed = member.getMarginUsed();//未修改前member表中成员保证金
|
|
|
|
|
Double tradingMargin = takeStashNew.getTradingMargin(); //进仓时使用的保证金
|
|
|
|
|
marginUsed = marginUsed - tradingMargin; //修改成员保证金
|
|
|
|
|
Double availableFunds = member.getAvailableFunds(); //可用资金
|
|
|
|
|
availableFunds=availableFunds+backFund;
|
|
|
|
|
member.setAvailableFunds(availableFunds);
|
|
|
|
|
member.setMarginUsed(marginUsed);
|
|
|
|
|
Integer closingTrades = member.getClosingTrades();
|
|
|
|
|
member.setClosingTrades(closingTrades++);
|
|
|
|
|
Double cumulativeProfitLoss = member.getCumulativeProfitLoss(); //为修改前member表中的累积盈亏
|
|
|
|
|
member.setCumulativeProfitLoss(cumulativeProfitLoss+CumulativeProfitLoss); //修改累计盈亏
|
|
|
|
|
member.setTotalAssets(member.getInitialCapital()+cumulativeProfitLoss+CumulativeProfitLoss); //设置总资产
|
|
|
|
|
memberService.updateByPrimaryKeySelective(member);
|
|
|
|
|
|
|
|
|
|
TakeStash takeStash = new TakeStash();
|
|
|
|
|
takeStash.setStashId(stashId);
|
|
|
|
|
takeStash.setStatus(2);
|
|
|
|
|
takeStash.setProfitAndLossByClose(backFund - tradingMargin); //盈亏=返还资金-进仓时使用的保证金
|
|
|
|
|
takeStashService.updateByPrimaryKeySelective(takeStash);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|