GwPosition { public PositionField reducePosition(TradeField tradeField) { String expectSymbol = p.getContract().getUnifiedSymbol(); String actualSymbol = tradeField.getContract().getUnifiedSymbol(); if(!StringUtils.equals(expectSymbol, actualSymbol)) { throw new ContractMismatchException(expectSymbol, actualSymbol); } synchronized (p) { int nPosition = tradeField.getVolume(); if(tradeField.getOffsetFlag()==OffsetFlagEnum.OF_CloseToday) { p.setTdPosition(p.getTdPosition() - nPosition); p.setPosition(p.getPosition() - nPosition); }else if(tradeField.getOffsetFlag()==OffsetFlagEnum.OF_CloseYesterday) { p.setYdPosition(p.getYdPosition() - nPosition); p.setPosition(p.getPosition() - nPosition); }else { p.setPosition(p.getPosition() - nPosition); p.setTdPosition(p.getTdPosition() - Math.min(p.getTdPosition(), nPosition)); p.setYdPosition(p.getPosition() - p.getTdPosition()); } p.setExchangeMargin(p.getOpenPrice() * p.getPosition() * contract.getMultiplier() * marginRatio); p.setUseMargin(p.getExchangeMargin()); } return p.convertTo(); } GwPosition(PositionInfo p); PositionField addPosition(TradeField tradeField); PositionField reducePosition(TradeField tradeField); PositionField frozenPosition(OrderField order); PositionField unfrozenPosition(OrderField order); PositionField updateByTick(TickField tick); PositionField proceedDailySettlement(double settlePrice); PositionField getPosition(); double getUseMargin();  }
@Test(dataProvider = "trade2") public void testReducePosition(TradeField tradeField) { position.reducePosition(tradeField); assertThat(position.getPosition().getPosition()).isEqualTo(2); assertThat(position.getPosition().getTdPosition()).isEqualTo(2); assertThat(position.getPosition().getOpenPrice()).isEqualTo(3546.8); }