GwPosition { public PositionField addPosition(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(); int oPosition = p.getPosition(); int tdPosition = p.getTdPosition(); double cost = p.getPrice() * p.getPosition() * contract.getMultiplier(); double nCost = tradeField.getPrice() * tradeField.getVolume() * contract.getMultiplier(); double openCost = p.getOpenPrice() * p.getPosition() * contract.getMultiplier(); p.setTdPosition(tdPosition + nPosition); p.setPosition(oPosition + nPosition); p.setPrice((cost + nCost) / (p.getPosition() * contract.getMultiplier())); p.setOpenPrice((openCost + nCost) / (p.getPosition() * contract.getMultiplier())); p.setContractValue(p.getOpenPrice() * p.getPosition() * contract.getMultiplier()); p.setExchangeMargin(p.getContractValue() * 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 = "trade1") public void testAddPosition(TradeField tradeField) { position.addPosition(tradeField); assertThat(position.getPosition().getPosition()).isEqualTo(8); assertThat(position.getPosition().getTdPosition()).isEqualTo(8); assertThat(position.getPosition().getOpenPrice()).isEqualTo(3555.125); }