package simplex.bn25.makino335926.trading.position;
import java.math.BigDecimal;
// クラス: Position(各銘柄のポジションデータを保存)
public class Position {
private final String ticker; // 銘柄コード
private final String name; // 銘柄名
private long quantity; // 保有数量
private BigDecimal averageUnitPrice; // 平均取得単価
private BigDecimal realizedPnL; // 実現損益
private BigDecimal valuation; // 評価額
private BigDecimal unrealizedPnL; // 評価損益
// コンストラクタ
public Position(String ticker, String name) {
this.ticker = ticker;
this.name = name;
this.quantity = 0;
this.averageUnitPrice = BigDecimal.ZERO;
this.realizedPnL = BigDecimal.ZERO;
this.valuation = null; // 時価情報が未設定の場合
this.unrealizedPnL = null;
}
// GetterおよびSetter
public String getTicker() {
return ticker;
}
public String getName() {
return name;
}
public long getQuantity() {
return quantity;
}
public BigDecimal getAverageUnitPrice() {
return averageUnitPrice;
}
public BigDecimal getRealizedPnL() {
return realizedPnL;
}
public BigDecimal getValuation() {
return valuation;
}
public BigDecimal getUnrealizedPnL() {
return unrealizedPnL;
}
public void updateQuantity(long delta) {
this.quantity += delta;
}
// メソッド:updateAverageUnitPrice(平均取得単価)
public void updateAverageUnitPrice(BigDecimal newPrice, long deltaQuantity) {
BigDecimal totalCost = this.averageUnitPrice.multiply(BigDecimal.valueOf(this.quantity))
.add(newPrice.multiply(BigDecimal.valueOf(deltaQuantity)));
this.quantity += deltaQuantity;
if (this.quantity > 0) {
this.averageUnitPrice = totalCost.divide(BigDecimal.valueOf(this.quantity), 2, BigDecimal.ROUND_HALF_UP);
} else {
this.averageUnitPrice = BigDecimal.ZERO;
}
}
// メソッド:addRealizedPnL(実現損益)
public void addRealizedPnL(BigDecimal amount) {
this.realizedPnL = this.realizedPnL.add(amount);
}
// メソッド:setValuation(評価額)
public void setValuation(BigDecimal marketPrice) {
if (marketPrice != null) {
this.valuation = marketPrice.multiply(BigDecimal.valueOf(this.quantity));
this.unrealizedPnL = this.valuation.subtract(this.averageUnitPrice.multiply(BigDecimal.valueOf(this.quantity)));
} else {
this.valuation = null;
this.unrealizedPnL = null;
}
}
}
コメント