|
|
|
@ -10,6 +10,8 @@ import com.sztzjy.forex.trading_trading.controller.PendingOrderController;
|
|
|
|
|
import com.sztzjy.forex.trading_trading.controller.TakeStashController;
|
|
|
|
|
import com.sztzjy.forex.trading_trading.controller.TradingController;
|
|
|
|
|
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.mql5Entity.ForexData;
|
|
|
|
|
import com.sztzjy.forex.trading_trading.mappers.TrainingMapper;
|
|
|
|
|
import com.sztzjy.forex.trading_trading.util.BigDecimalUtils;
|
|
|
|
@ -27,6 +29,7 @@ import javax.annotation.PostConstruct;
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
import java.text.DecimalFormat;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 定时任务
|
|
|
|
@ -64,6 +67,15 @@ public class ScheduledTask {
|
|
|
|
|
@Autowired
|
|
|
|
|
BigDecimalUtils bigDecimalUtils;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
MemberService memberService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
TakeStashService takeStashService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
PendingOrderService pendingOrderService;
|
|
|
|
|
|
|
|
|
|
@Scheduled(cron = "0 * * * * *") // 修改实训状态 每分钟执行一次
|
|
|
|
|
public void updateTrainingStatus() {
|
|
|
|
|
trainingMapper.updateTrainingStatusToInProgress();
|
|
|
|
@ -104,7 +116,7 @@ public class ScheduledTask {
|
|
|
|
|
String batchId = IdUtil.simpleUUID();
|
|
|
|
|
for (ForexMarketData data : forexMarketDataList) {
|
|
|
|
|
Double buyPic = modifyDecimal(data.getBuyPic(), random);
|
|
|
|
|
Double openPriDouble = Double.valueOf(data.getOpenPri());
|
|
|
|
|
// Double openPriDouble = Double.valueOf(data.getOpenPri());
|
|
|
|
|
String diffAmo = bigDecimalUtils.sub(String.valueOf(buyPic), data.getOpenPri()).toString();
|
|
|
|
|
ForexMarketData resultData = new ForexMarketData(
|
|
|
|
|
buyPic,
|
|
|
|
@ -163,7 +175,6 @@ public class ScheduledTask {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//error
|
|
|
|
|
//监听止损止盈 根据code获取当前买卖价格 如果价格高于/低于止损止盈 则按照止损止盈的值进行平仓
|
|
|
|
|
//查询实训表中状态为1的所有实训并获取ID 获取持仓表中所有状态为0 且止损和止盈不为空的所有数据 获取当前价位如果
|
|
|
|
|
@Scheduled(cron = "0 */2 * * * ?")
|
|
|
|
@ -177,7 +188,7 @@ public class ScheduledTask {
|
|
|
|
|
|
|
|
|
|
List<String> trainingIdList = trainingService.selectProceedTraining();
|
|
|
|
|
for (int i = 0; i < trainingIdList.size(); i++) {
|
|
|
|
|
Set<String> stashSet = redisUtil.keys("*stashId*");
|
|
|
|
|
Set<String> stashSet = redisUtil.keys("trainingId_"+trainingIdList.get(i)+"_stashId*");
|
|
|
|
|
Iterator<String> iterator = stashSet.iterator();
|
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
|
String key = iterator.next();
|
|
|
|
@ -193,49 +204,63 @@ public class ScheduledTask {
|
|
|
|
|
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 margin = takeStashController.startUSDMarginNeed(trainingId, memberId, 0, tradingCode, buySellType, transactionVolume);
|
|
|
|
|
Double profitLoss=(stopLoss-priceTransaction)*transactionVolume*Constant.PEACEQUANTITY/stopLoss; //止损盈利
|
|
|
|
|
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
|
|
|
|
|
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin,sellPic);
|
|
|
|
|
redisUtil.del("trainingId_"+trainingId+"_stashId_"+stashId);
|
|
|
|
|
}
|
|
|
|
|
if(stopWin!=-1 && sellPic>=stopWin){
|
|
|
|
|
Double margin = takeStashController.startUSDMarginNeed(trainingId, memberId, 0, tradingCode, buySellType, transactionVolume);
|
|
|
|
|
Double profitLoss=(stopWin-priceTransaction)*transactionVolume*Constant.PEACEQUANTITY/stopWin; //止盈盈利
|
|
|
|
|
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
|
|
|
|
|
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin,sellPic);
|
|
|
|
|
redisUtil.del("trainingId_"+trainingId+"_stashId_"+stashId);
|
|
|
|
|
}
|
|
|
|
|
}else { //开仓方式为卖 则平仓方式为买 先获取买价
|
|
|
|
|
Double buyPic = forexMarketData.getBuyPic();
|
|
|
|
|
if(stopLoss!=-1 && buyPic<=stopLoss){
|
|
|
|
|
Double margin = takeStashController.startUSDMarginNeed(trainingId, memberId, 0, tradingCode, buySellType, transactionVolume);
|
|
|
|
|
Double profitLoss=(priceTransaction-stopLoss)*transactionVolume*Constant.PEACEQUANTITY/stopLoss;
|
|
|
|
|
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
|
|
|
|
|
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin,buyPic);
|
|
|
|
|
redisUtil.del("trainingId_"+trainingId+"_stashId_"+stashId);
|
|
|
|
|
}
|
|
|
|
|
if(stopWin!=-1 && buyPic>=stopWin){
|
|
|
|
|
Double margin = takeStashController.startUSDMarginNeed(trainingId, memberId, 0, tradingCode, buySellType, transactionVolume);
|
|
|
|
|
Double profitLoss=(priceTransaction-stopWin)*transactionVolume*Constant.PEACEQUANTITY/stopWin;
|
|
|
|
|
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
|
|
|
|
|
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin,buyPic);
|
|
|
|
|
redisUtil.del("trainingId_"+trainingId+"_stashId_"+stashId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}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 margin = takeStashController.endUSDMarginNeed( trainingId, memberId,0 , tradingCode, buySellType, transactionVolume, priceTransaction);
|
|
|
|
|
Double profitLoss=(stopLoss-priceTransaction)*transactionVolume*Constant.PEACEQUANTITY;
|
|
|
|
|
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
|
|
|
|
|
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin,sellPic);
|
|
|
|
|
redisUtil.del("trainingId_"+trainingId+"_stashId_"+stashId);
|
|
|
|
|
}
|
|
|
|
|
if(stopWin!=-1 && sellPic>=stopWin){
|
|
|
|
|
Double margin = takeStashController.endUSDMarginNeed( trainingId, memberId,0 , tradingCode, buySellType, transactionVolume, priceTransaction);
|
|
|
|
|
Double profitLoss=(stopWin-priceTransaction)*transactionVolume*Constant.PEACEQUANTITY;
|
|
|
|
|
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
|
|
|
|
|
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin,sellPic);
|
|
|
|
|
redisUtil.del("trainingId_"+trainingId+"_stashId_"+stashId);
|
|
|
|
|
}
|
|
|
|
|
}else { //开仓方式为卖 则平仓方式为买 先获取买价
|
|
|
|
|
Double buyPic = forexMarketData.getBuyPic();
|
|
|
|
|
if(stopLoss!=-1 && buyPic<=stopLoss){
|
|
|
|
|
Double margin = takeStashController.endUSDMarginNeed( trainingId, memberId,0 , tradingCode, buySellType, transactionVolume, priceTransaction);
|
|
|
|
|
Double profitLoss=(priceTransaction-stopLoss)*transactionVolume*Constant.PEACEQUANTITY;
|
|
|
|
|
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
|
|
|
|
|
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin,buyPic);
|
|
|
|
|
redisUtil.del("trainingId_"+trainingId+"_stashId_"+stashId);
|
|
|
|
|
}
|
|
|
|
|
if(stopWin!=-1 && buyPic>=stopWin){
|
|
|
|
|
Double margin = takeStashController.endUSDMarginNeed( trainingId, memberId,0 , tradingCode, buySellType, transactionVolume, priceTransaction);
|
|
|
|
|
Double profitLoss=(priceTransaction-stopWin)*transactionVolume*Constant.PEACEQUANTITY;
|
|
|
|
|
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin);
|
|
|
|
|
takeStashController.updateMemberAndTakeStash(memberId,stashId,profitLoss,margin,buyPic);
|
|
|
|
|
redisUtil.del("trainingId_"+trainingId+"_stashId_"+stashId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -248,32 +273,75 @@ public class ScheduledTask {
|
|
|
|
|
//挂单自动开仓(到期自动撤单) 定时方法
|
|
|
|
|
@Scheduled(cron = "0 */2 * * * ?")
|
|
|
|
|
public void OpenPendingOrder() {
|
|
|
|
|
Set<String> pendingOrderSet = redisUtil.keys("pengingOrder*");
|
|
|
|
|
Set<String> pendingOrderSet = redisUtil.keys("*pengingOrder*");
|
|
|
|
|
List<ForexMarketData> forexDateList = redisUtil.get("ForexDateList");
|
|
|
|
|
Map<String, ForexMarketData> forexMarketDateMap = forexMarketDateUtil.getForexMarketDateMap(forexDateList);
|
|
|
|
|
|
|
|
|
|
for (String key : pendingOrderSet) {
|
|
|
|
|
Map<Object, Object> pengingOrderMap = redisUtil.hmget(key);
|
|
|
|
|
redisUtil.del(key);
|
|
|
|
|
Boolean expire = pendingOrderController.isExpire(pengingOrderMap, key); //判断是否到期
|
|
|
|
|
if (expire) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
//判断是否开仓
|
|
|
|
|
Boolean isPpen = pendingOrderController.isOpenTakeStash(pengingOrderMap,forexMarketDateMap);
|
|
|
|
|
if(isPpen){
|
|
|
|
|
if(!isPpen){
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
pendingOrderService.cancelOrder((String) pengingOrderMap.get("pendingOrderId"));
|
|
|
|
|
JSONObject jsonObject = pendingOrderController.pengingKeyReturnJSONObject(pengingOrderMap);
|
|
|
|
|
tradingController.getMarketQuotation(jsonObject);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//自动清空实例到期的所有redisKey
|
|
|
|
|
//自动清空实例到期的所有redisKey 并结算
|
|
|
|
|
@Scheduled(cron = "0 */2 * * * ?")
|
|
|
|
|
public void clearRedisKeyByTrainingId(){
|
|
|
|
|
List<String> trainingIds=trainingMapper.selectTrainingIdByStatus();
|
|
|
|
|
Set<Object> trainingSet = redisUtil.sGet(Constant.NOSETTLEANACCOUNTTRAINING);
|
|
|
|
|
List<String> trainingListRedis = trainingSet.stream().map(Object::toString).collect(Collectors.toList()); //获取未结算的所有实训id
|
|
|
|
|
List<String> trainingList=trainingMapper.selectTrainingIdByStatus(); //获取状态码为结束的实训ID
|
|
|
|
|
List<String> trainingIds = new ArrayList<>(trainingListRedis);
|
|
|
|
|
trainingIds.retainAll(trainingList);
|
|
|
|
|
//需要结算的id为 在trainingList中存在 且
|
|
|
|
|
for (int i = 0; i < trainingIds.size(); i++) {
|
|
|
|
|
redisUtil.del("trainingId_"+trainingIds+"*");
|
|
|
|
|
//根据trainingId 查询member表和持仓表 并结算一次保存
|
|
|
|
|
String trainingId = trainingIds.get(i);
|
|
|
|
|
List<Member> memberList = memberService.findByTrainingId(trainingId);
|
|
|
|
|
if (memberList.isEmpty() || memberList==null){
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
for (int j = 0; j < memberList.size(); j++) {
|
|
|
|
|
Member member = memberList.get(j);
|
|
|
|
|
String memberId = member.getMemberId();
|
|
|
|
|
List<TakeStash> takeStashList=takeStashService.selectAllByMemberIdAndStatus(memberId,0);
|
|
|
|
|
if(takeStashList.isEmpty() || memberList==null){
|
|
|
|
|
member.setPositionProfitLoss(0.0);
|
|
|
|
|
memberService.updateByPrimaryKeySelective(member);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
for (int k = 0; k < takeStashList.size(); k++) {
|
|
|
|
|
TakeStash takeStash = takeStashList.get(k);
|
|
|
|
|
ForexMarketData forexMarketDateByCode = forexMarketDateUtil.getForexMarketDateByCode(takeStash.getTradingCode());
|
|
|
|
|
String buySellType = takeStash.getBuySellType();
|
|
|
|
|
Double profitAndLoss = takeStashService.flashProfitAndLossByTakeStash(takeStash);
|
|
|
|
|
takeStash.setProfitAndLossByClose(profitAndLoss);
|
|
|
|
|
if("buy".equals(buySellType)){
|
|
|
|
|
String sellPic = forexMarketDateByCode.getSellPic();
|
|
|
|
|
takeStash.setTradingMargin(bigDecimalUtils.mul(Double.parseDouble(sellPic),1,2));
|
|
|
|
|
}else {
|
|
|
|
|
takeStash.setTradingMargin(bigDecimalUtils.mul(forexMarketDateByCode.getBuyPic(),1,2));
|
|
|
|
|
}
|
|
|
|
|
takeStashService.updateByPrimaryKeySelective(takeStash); //进行持仓结算
|
|
|
|
|
}
|
|
|
|
|
//member结算 获取一次持仓盈亏
|
|
|
|
|
Double memberProfitAndLoss = takeStashController.flashTotalPositionProfitLoss(memberId);
|
|
|
|
|
member.setPositionProfitLoss(memberProfitAndLoss);
|
|
|
|
|
memberService.updateByPrimaryKeySelective(member);
|
|
|
|
|
}
|
|
|
|
|
redisUtil.del("trainingId_"+trainingId+"*");
|
|
|
|
|
redisUtil.setRemove(Constant.NOSETTLEANACCOUNTTRAINING,trainingId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|