forked from portfolio-performance/portfolio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractSecurityTransactionModel.java
637 lines (526 loc) · 22.2 KB
/
AbstractSecurityTransactionModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
package name.abuchen.portfolio.ui.dialogs.transactions;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import com.ibm.icu.text.MessageFormat;
import name.abuchen.portfolio.model.Client;
import name.abuchen.portfolio.model.Portfolio;
import name.abuchen.portfolio.model.PortfolioTransaction;
import name.abuchen.portfolio.model.PortfolioTransaction.Type;
import name.abuchen.portfolio.model.Security;
import name.abuchen.portfolio.model.Transaction;
import name.abuchen.portfolio.money.CurrencyConverter;
import name.abuchen.portfolio.money.CurrencyConverterImpl;
import name.abuchen.portfolio.money.CurrencyUnit;
import name.abuchen.portfolio.money.ExchangeRate;
import name.abuchen.portfolio.money.ExchangeRateTimeSeries;
import name.abuchen.portfolio.money.Money;
import name.abuchen.portfolio.money.Values;
import name.abuchen.portfolio.snapshot.PortfolioSnapshot;
import name.abuchen.portfolio.snapshot.SecurityPosition;
import name.abuchen.portfolio.ui.Messages;
public abstract class AbstractSecurityTransactionModel extends AbstractModel
{
public enum Properties
{
portfolio, security, account, date, time, shares, quote, grossValue, exchangeRate, inverseExchangeRate, //
convertedGrossValue, forexFees, fees, forexTaxes, taxes, total, note, exchangeRateCurrencies, //
inverseExchangeRateCurrencies, transactionCurrency, transactionCurrencyCode, securityCurrencyCode, //
calculationStatus;
}
protected final Client client;
protected PortfolioTransaction.Type type;
protected Portfolio portfolio;
protected Security security;
protected LocalDate date = LocalDate.now();
protected LocalTime time = LocalTime.MIDNIGHT;
protected long shares;
protected BigDecimal quote = BigDecimal.ONE;
protected long grossValue;
protected BigDecimal exchangeRate = BigDecimal.ONE;
protected long convertedGrossValue;
protected long forexFees;
protected long fees;
protected long forexTaxes;
protected long taxes;
protected long total;
protected String note;
private IStatus calculationStatus = ValidationStatus.ok();
public AbstractSecurityTransactionModel(Client client, Type type)
{
this.client = client;
this.type = type;
}
@Override
public String getHeading()
{
return type.toString();
}
public abstract boolean accepts(Type type);
public abstract void setSource(Object source);
public abstract boolean hasSource();
@Override
public void resetToNewTransaction()
{
setShares(0);
setGrossValue(0);
setConvertedGrossValue(0);
setTotal(0);
setFees(0);
setTaxes(0);
setForexFees(0);
setForexTaxes(0);
setNote(null);
}
protected void fillFromTransaction(PortfolioTransaction transaction)
{
this.security = transaction.getSecurity();
LocalDateTime transactionDate = transaction.getDateTime();
this.date = transactionDate.toLocalDate();
this.time = transactionDate.toLocalTime();
this.shares = transaction.getShares();
this.total = transaction.getAmount();
this.note = transaction.getNote();
// will be overwritten if forex data exists
this.exchangeRate = BigDecimal.ONE;
transaction.getUnits().forEach(unit -> {
switch (unit.getType())
{
case GROSS_VALUE:
this.exchangeRate = unit.getExchangeRate();
this.grossValue = unit.getForex().getAmount();
this.quote = BigDecimal.valueOf(
this.grossValue * Values.Share.factor() / (this.shares * Values.Amount.divider()));
break;
case FEE:
if (unit.getForex() != null)
this.forexFees += unit.getForex().getAmount();
else
this.fees += unit.getAmount().getAmount();
break;
case TAX:
if (unit.getForex() != null)
this.forexTaxes += unit.getForex().getAmount();
else
this.taxes += unit.getAmount().getAmount();
break;
default:
throw new UnsupportedOperationException();
}
});
this.convertedGrossValue = calculateConvertedGrossValue();
if (exchangeRate.equals(BigDecimal.ONE))
{
// units contained no information about forex
this.grossValue = convertedGrossValue;
this.quote = transaction.getGrossPricePerShare().toBigDecimal();
}
firePropertyChange(Properties.calculationStatus.name(), this.calculationStatus,
this.calculationStatus = calculateStatus());
}
protected void writeToTransaction(PortfolioTransaction transaction)
{
transaction.clearUnits();
if (fees != 0)
transaction.addUnit(new Transaction.Unit(Transaction.Unit.Type.FEE, //
Money.of(getTransactionCurrencyCode(), fees)));
if (taxes != 0)
transaction.addUnit(new Transaction.Unit(Transaction.Unit.Type.TAX, //
Money.of(getTransactionCurrencyCode(), taxes)));
boolean hasForex = !getTransactionCurrencyCode().equals(getSecurityCurrencyCode());
if (hasForex)
{
if (forexFees != 0)
transaction.addUnit(new Transaction.Unit(Transaction.Unit.Type.FEE, //
Money.of(getTransactionCurrencyCode(),
Math.round(forexFees * exchangeRate.doubleValue())), //
Money.of(getSecurityCurrencyCode(), forexFees), //
exchangeRate));
if (forexTaxes != 0)
transaction.addUnit(new Transaction.Unit(Transaction.Unit.Type.TAX, //
Money.of(getTransactionCurrencyCode(),
Math.round(forexTaxes * exchangeRate.doubleValue())), //
Money.of(getSecurityCurrencyCode(), forexTaxes), //
exchangeRate));
transaction.addUnit(new Transaction.Unit(Transaction.Unit.Type.GROSS_VALUE, //
Money.of(getTransactionCurrencyCode(), convertedGrossValue), //
Money.of(getSecurityCurrencyCode(), grossValue), //
exchangeRate));
}
}
@Override
public IStatus getCalculationStatus()
{
return calculationStatus;
}
/**
* Check whether calculation works out. The separate validation is needed
* because the model does prevent negative values in methods
* {@link #calcGrossValue(long, long, long)} and
* {@link #calcTotal(long, long, long)}. Due to the limited precision of the
* quote (2 digits currently) and the exchange rate (4 digits), the gross
* value and converted gross value are checked against a range.
*/
private IStatus calculateStatus()
{
if (shares == 0L)
return ValidationStatus.error(MessageFormat.format(Messages.MsgDialogInputRequired, Messages.ColumnShares));
if ((grossValue == 0L || convertedGrossValue == 0L) && type != PortfolioTransaction.Type.DELIVERY_OUTBOUND)
return ValidationStatus
.error(MessageFormat.format(Messages.MsgDialogInputRequired, Messages.ColumnSubTotal));
// check whether gross value is in range
long lower = Math.round(shares * quote.add(BigDecimal.valueOf(-0.01)).doubleValue() * Values.Amount.factor()
/ Values.Share.divider());
long upper = Math.round(shares * quote.add(BigDecimal.valueOf(0.01)).doubleValue() * Values.Amount.factor()
/ Values.Share.divider());
if (grossValue < lower || grossValue > upper)
return ValidationStatus.error(Messages.MsgIncorrectSubTotal);
// check whether converted gross value is in range
upper = Math.round(grossValue * exchangeRate.add(BigDecimal.valueOf(0.0001)).doubleValue());
lower = Math.round(grossValue * exchangeRate.add(BigDecimal.valueOf(-0.0001)).doubleValue());
if (convertedGrossValue < lower || convertedGrossValue > upper)
return ValidationStatus.error(Messages.MsgIncorrectConvertedSubTotal);
// check total
long t = calculateTotal();
if (t != total)
return ValidationStatus.error(MessageFormat.format(Messages.MsgIncorrectTotal, Values.Amount.format(t)));
if (total == 0L && type != PortfolioTransaction.Type.DELIVERY_OUTBOUND)
return ValidationStatus.error(MessageFormat.format(Messages.MsgDialogInputRequired, Messages.ColumnTotal));
return ValidationStatus.ok();
}
public Portfolio getPortfolio()
{
return portfolio;
}
public void setPortfolio(Portfolio portfolio)
{
firePropertyChange(Properties.portfolio.name(), this.portfolio, this.portfolio = portfolio);
if (security != null)
{
updateSharesAndQuote();
updateExchangeRate();
}
}
public Security getSecurity()
{
return security;
}
public void setSecurity(Security security)
{
String oldCurrencyCode = getSecurityCurrencyCode();
String oldExchangeRateCurrencies = getExchangeRateCurrencies();
String oldInverseExchangeRateCurrencies = getInverseExchangeRateCurrencies();
firePropertyChange(Properties.security.name(), this.security, this.security = security);
firePropertyChange(Properties.securityCurrencyCode.name(), oldCurrencyCode, getSecurityCurrencyCode());
firePropertyChange(Properties.exchangeRateCurrencies.name(), oldExchangeRateCurrencies,
getExchangeRateCurrencies());
firePropertyChange(Properties.inverseExchangeRateCurrencies.name(), oldInverseExchangeRateCurrencies,
getInverseExchangeRateCurrencies());
updateSharesAndQuote();
updateExchangeRate();
}
protected void updateSharesAndQuote()
{
// do not auto-suggest shares and quote when editing an existing transaction
if (hasSource())
return;
if (type == PortfolioTransaction.Type.SELL || type == PortfolioTransaction.Type.DELIVERY_OUTBOUND)
{
boolean hasPosition = false;
if (portfolio != null)
{
// since the security position has always the currency of the
// investment vehicle, actually no conversion is needed. Hence
// we can use an arbitrary converter.
CurrencyConverter converter = new CurrencyConverterImpl(getExchangeRateProviderFactory(),
CurrencyUnit.EUR);
PortfolioSnapshot snapshot = PortfolioSnapshot.create(portfolio, converter, date);
SecurityPosition position = snapshot.getPositionsBySecurity().get(security);
if (position != null)
{
setShares(position.getShares());
setTotal(position.calculateValue().getAmount());
hasPosition = true;
}
}
if (!hasPosition)
{
setShares(0);
setQuote(BigDecimal.valueOf(security.getSecurityPrice(date).getValue() / Values.Quote.divider()));
}
}
else
{
setQuote(BigDecimal.valueOf(security.getSecurityPrice(date).getValue() / Values.Quote.divider()));
}
}
protected void updateExchangeRate()
{
// do not auto-suggest exchange rate when editing an existing transaction
if (hasSource())
return;
if (getTransactionCurrencyCode().equals(getSecurityCurrencyCode()))
{
setExchangeRate(BigDecimal.ONE);
}
else if (!getTransactionCurrencyCode().isEmpty() && !getSecurityCurrencyCode().isEmpty())
{
ExchangeRateTimeSeries series = getExchangeRateProviderFactory() //
.getTimeSeries(getSecurityCurrencyCode(), getTransactionCurrencyCode());
if (series != null)
setExchangeRate(series.lookupRate(date).orElse(new ExchangeRate(date, BigDecimal.ONE)).getValue());
else
setExchangeRate(BigDecimal.ONE);
}
}
public LocalDate getDate()
{
return date;
}
public LocalTime getTime()
{
return time;
}
public void setDate(LocalDate date)
{
firePropertyChange(Properties.date.name(), this.date, this.date = date);
updateExchangeRate();
}
public void setTime(LocalTime time)
{
firePropertyChange(Properties.time.name(), this.time, this.time = time);
updateExchangeRate();
}
public long getShares()
{
return shares;
}
public void setShares(long shares)
{
firePropertyChange(Properties.shares.name(), this.shares, this.shares = shares);
if (quote.doubleValue() != 0)
{
triggerGrossValue(
Math.round(shares * quote.doubleValue() * Values.Amount.factor() / Values.Share.divider()));
}
else if (grossValue != 0 && shares != 0)
{
setQuote(BigDecimal.valueOf(grossValue * Values.Share.factor() / (shares * Values.Amount.divider())));
}
firePropertyChange(Properties.calculationStatus.name(), this.calculationStatus,
this.calculationStatus = calculateStatus());
}
public BigDecimal getQuote()
{
return quote;
}
public void setQuote(BigDecimal quote)
{
firePropertyChange(Properties.quote.name(), this.quote, this.quote = quote);
triggerGrossValue(Math.round(shares * quote.doubleValue() * Values.Amount.factor() / Values.Share.divider()));
firePropertyChange(Properties.calculationStatus.name(), this.calculationStatus,
this.calculationStatus = calculateStatus());
}
public long getGrossValue()
{
return grossValue;
}
public void setGrossValue(long grossValue)
{
triggerGrossValue(grossValue);
if (shares != 0)
{
BigDecimal newQuote = BigDecimal
.valueOf(grossValue * Values.Share.factor() / (shares * Values.Amount.divider()));
firePropertyChange(Properties.quote.name(), this.quote, this.quote = newQuote);
}
firePropertyChange(Properties.calculationStatus.name(), this.calculationStatus,
this.calculationStatus = calculateStatus());
}
public void triggerGrossValue(long grossValue)
{
firePropertyChange(Properties.grossValue.name(), this.grossValue, this.grossValue = grossValue);
triggerConvertedGrossValue(Math.round(exchangeRate.doubleValue() * grossValue));
}
public BigDecimal getExchangeRate()
{
return exchangeRate;
}
public void setExchangeRate(BigDecimal exchangeRate)
{
BigDecimal newRate = exchangeRate == null ? BigDecimal.ZERO : exchangeRate;
BigDecimal oldInverseRate = getInverseExchangeRate();
firePropertyChange(Properties.exchangeRate.name(), this.exchangeRate, this.exchangeRate = newRate);
firePropertyChange(Properties.inverseExchangeRate.name(), oldInverseRate, getInverseExchangeRate());
triggerConvertedGrossValue(Math.round(newRate.doubleValue() * grossValue));
firePropertyChange(Properties.calculationStatus.name(), this.calculationStatus,
this.calculationStatus = calculateStatus());
}
public BigDecimal getInverseExchangeRate()
{
return BigDecimal.ONE.divide(exchangeRate, 10, RoundingMode.HALF_DOWN);
}
public void setInverseExchangeRate(BigDecimal rate)
{
setExchangeRate(BigDecimal.ONE.divide(rate, 10, RoundingMode.HALF_DOWN));
}
public long getConvertedGrossValue()
{
return convertedGrossValue;
}
public void setConvertedGrossValue(long convertedGrossValue)
{
triggerConvertedGrossValue(convertedGrossValue);
if (grossValue != 0)
{
BigDecimal newExchangeRate = BigDecimal.valueOf(convertedGrossValue).divide(BigDecimal.valueOf(grossValue),
10, RoundingMode.HALF_UP);
BigDecimal oldInverseRate = getInverseExchangeRate();
firePropertyChange(Properties.exchangeRate.name(), this.exchangeRate, this.exchangeRate = newExchangeRate);
firePropertyChange(Properties.inverseExchangeRate.name(), oldInverseRate, getInverseExchangeRate());
triggerTotal(calculateTotal()); // forex fees and taxes might change
}
firePropertyChange(Properties.calculationStatus.name(), this.calculationStatus,
this.calculationStatus = calculateStatus());
}
public void triggerConvertedGrossValue(long convertedGrossValue)
{
firePropertyChange(Properties.convertedGrossValue.name(), this.convertedGrossValue,
this.convertedGrossValue = convertedGrossValue);
triggerTotal(calculateTotal());
}
public long getFees()
{
return fees;
}
public void setFees(long fees)
{
firePropertyChange(Properties.fees.name(), this.fees, this.fees = fees);
triggerTotal(calculateTotal());
firePropertyChange(Properties.calculationStatus.name(), this.calculationStatus,
this.calculationStatus = calculateStatus());
}
public long getForexFees()
{
return forexFees;
}
public void setForexFees(long forexFees)
{
firePropertyChange(Properties.forexFees.name(), this.forexFees, this.forexFees = forexFees);
triggerTotal(calculateTotal());
firePropertyChange(Properties.calculationStatus.name(), this.calculationStatus,
this.calculationStatus = calculateStatus());
}
public long getTaxes()
{
return taxes;
}
public void setTaxes(long taxes)
{
firePropertyChange(Properties.taxes.name(), this.taxes, this.taxes = taxes);
triggerTotal(calculateTotal());
firePropertyChange(Properties.calculationStatus.name(), this.calculationStatus,
this.calculationStatus = calculateStatus());
}
public long getForexTaxes()
{
return forexTaxes;
}
public void setForexTaxes(long forexTaxes)
{
firePropertyChange(Properties.forexTaxes.name(), this.forexTaxes, this.forexTaxes = forexTaxes);
triggerTotal(calculateTotal());
firePropertyChange(Properties.calculationStatus.name(), this.calculationStatus,
this.calculationStatus = calculateStatus());
}
public long getTotal()
{
return total;
}
public void setTotal(long total)
{
triggerTotal(total);
firePropertyChange(Properties.convertedGrossValue.name(), this.convertedGrossValue,
this.convertedGrossValue = calculateConvertedGrossValue());
firePropertyChange(Properties.grossValue.name(), this.grossValue,
this.grossValue = Math.round(convertedGrossValue / exchangeRate.doubleValue()));
if (shares != 0)
firePropertyChange(Properties.quote.name(), this.quote, this.quote = BigDecimal
.valueOf(grossValue * Values.Share.factor() / (shares * Values.Amount.divider())));
firePropertyChange(Properties.calculationStatus.name(), this.calculationStatus,
this.calculationStatus = calculateStatus());
}
public void triggerTotal(long total)
{
firePropertyChange(Properties.total.name(), this.total, this.total = total);
}
public String getNote()
{
return note;
}
public void setNote(String note)
{
firePropertyChange(Properties.note.name(), this.note, this.note = note);
}
public String getSecurityCurrencyCode()
{
return security != null ? security.getCurrencyCode() : ""; //$NON-NLS-1$
}
public abstract String getTransactionCurrencyCode();
/**
* Returns exchange rate label in direct (price) notation.
*/
public String getExchangeRateCurrencies()
{
return String.format("%s/%s", getSecurityCurrencyCode(), getTransactionCurrencyCode()); //$NON-NLS-1$
}
/**
* Returns exchange rate label in indirect (quantity) notation.
*/
public String getInverseExchangeRateCurrencies()
{
return String.format("%s/%s", getTransactionCurrencyCode(), getSecurityCurrencyCode()); //$NON-NLS-1$
}
public PortfolioTransaction.Type getType()
{
return type;
}
protected long calculateConvertedGrossValue()
{
long feesAndTaxes = fees + taxes + Math.round(exchangeRate.doubleValue() * (forexFees + forexTaxes));
switch (type)
{
case BUY:
case COVER:
case DELIVERY_INBOUND:
return Math.max(0, total - feesAndTaxes);
case SELL:
case SHORT:
case DELIVERY_OUTBOUND:
return total + feesAndTaxes;
default:
throw new UnsupportedOperationException();
}
}
private long calculateTotal()
{
long feesAndTaxes = fees + taxes + Math.round(exchangeRate.doubleValue() * (forexFees + forexTaxes));
switch (type)
{
case BUY:
case COVER:
case DELIVERY_INBOUND:
return convertedGrossValue + feesAndTaxes;
case SELL:
case SHORT:
case DELIVERY_OUTBOUND:
return Math.max(0, convertedGrossValue - feesAndTaxes);
default:
throw new UnsupportedOperationException();
}
}
}