forked from portfolio-performance/portfolio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Security.java
791 lines (657 loc) · 20.3 KB
/
Security.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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
package name.abuchen.portfolio.model;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import com.google.common.base.Strings;
import name.abuchen.portfolio.money.CurrencyUnit;
import name.abuchen.portfolio.util.Pair;
/**
* A <code>Security</code> is used for assets that have historical prices
* attached.
* </p>
* <strong>Attributes</strong> are managed and edited by the user while
* <strong>properties</strong> are managed by the program.
*/
public final class Security implements Attributable, InvestmentVehicle
{
public static final class ByName implements Comparator<Security>, Serializable
{
private static final long serialVersionUID = 1L;
@Override
public int compare(Security s1, Security s2)
{
if (s1 == null)
return s2 == null ? 0 : -1;
return s1.name.compareToIgnoreCase(s2.name);
}
}
private String uuid;
private String onlineId;
private String name;
private String currencyCode = CurrencyUnit.EUR;
private String targetCurrencyCode;
private String note;
private String isin;
private String tickerSymbol;
private String wkn;
private String calendar;
// feed and feedURL are used to update historical prices
private String feed;
private String feedURL;
private List<SecurityPrice> prices = new ArrayList<>();
// latestFeed and latestFeedURL are used to update the latest (current)
// quote. If null, the values from feed and feedURL are used instead.
private String latestFeed;
private String latestFeedURL;
private LatestSecurityPrice latest;
private Attributes attributes;
private List<SecurityEvent> events;
private List<SecurityProperty> properties;
private boolean isRetired = false;
@Deprecated
private String type;
@Deprecated
private String industryClassification;
public Security()
{
this.uuid = UUID.randomUUID().toString();
}
public Security(String name, String currencyCode)
{
this();
this.name = name;
this.currencyCode = currencyCode;
}
public Security(String name, String isin, String tickerSymbol, String feed)
{
this();
this.name = name;
this.isin = isin;
this.tickerSymbol = tickerSymbol;
this.feed = feed;
}
@Override
public String getUUID()
{
return uuid;
}
/* package */void generateUUID()
{
// needed to assign UUIDs when loading older versions from XML
uuid = UUID.randomUUID().toString();
}
/**
* Generates a UUID only if no UUID exists. For not yet known reasons, some
* securities do miss the UUID. However, we do not want to make the
* #generateUUID function public.
*/
public void fixMissingUUID()
{
if (uuid == null)
generateUUID();
}
public String getOnlineId()
{
return onlineId;
}
public void setOnlineId(String onlineId)
{
this.onlineId = onlineId;
}
@Override
public String getName()
{
return name;
}
@Override
public void setName(String name)
{
this.name = name;
}
@Override
public String getCurrencyCode()
{
return currencyCode;
}
@Override
public void setCurrencyCode(String currencyCode)
{
this.currencyCode = currencyCode;
}
/**
* Gets the target currency for exchange rates (the currency of the exchange
* rate).
*
* @return target currency for exchange rates, else null
*/
public String getTargetCurrencyCode()
{
return this.targetCurrencyCode;
}
/**
* Sets the target currency for exchange rates (defines the currency of the
* exchange rate).
*
* @param targetCurrencyCode
* target currency for exchange rates, else null
*/
public void setTargetCurrencyCode(String targetCurrencyCode)
{
this.targetCurrencyCode = targetCurrencyCode;
}
@Override
public String getNote()
{
return note;
}
@Override
public void setNote(String note)
{
this.note = note;
}
public String getIsin()
{
return isin;
}
public void setIsin(String isin)
{
this.isin = isin;
}
public String getTickerSymbol()
{
return tickerSymbol;
}
public void setTickerSymbol(String tickerSymbol)
{
this.tickerSymbol = tickerSymbol;
}
public String getWkn()
{
return wkn;
}
public void setWkn(String wkn)
{
this.wkn = wkn;
}
public String getCalendar()
{
return calendar;
}
public void setCalendar(String calendar)
{
this.calendar = calendar;
}
/**
* Is this an exchange rate symbol?
*
* @return true for exchange rates, else false
*/
public boolean isExchangeRate()
{
return this.targetCurrencyCode != null;
}
/**
* Returns ISIN, Ticker or WKN - whatever is available.
*/
public String getExternalIdentifier()
{
if (isin != null && isin.length() > 0)
return isin;
else if (tickerSymbol != null && tickerSymbol.length() > 0)
return tickerSymbol;
else if (wkn != null && wkn.length() > 0)
return wkn;
else
return name;
}
@Deprecated
/* package */String getIndustryClassification()
{
return industryClassification;
}
@Deprecated
/* package */void setIndustryClassification(String industryClassification)
{
this.industryClassification = industryClassification;
}
@Deprecated
/* package */String getType()
{
return type;
}
@Deprecated
/* package */void setType(String type)
{
this.type = type;
}
public String getFeed()
{
return feed;
}
public void setFeed(String feed)
{
this.feed = feed;
}
public String getFeedURL()
{
return feedURL;
}
public void setFeedURL(String feedURL)
{
this.feedURL = feedURL;
}
public List<SecurityPrice> getPrices()
{
return Collections.unmodifiableList(prices);
}
/**
* Returns a list of historical security prices that includes the latest
* security price if no history price exists for that date
*/
public List<SecurityPrice> getPricesIncludingLatest()
{
if (latest == null)
return getPrices();
int index = Collections.binarySearch(prices, new SecurityPrice(latest.getDate(), latest.getValue()));
if (index >= 0) // historic quote exists -> use it
return getPrices();
List<SecurityPrice> copy = new ArrayList<>(prices);
copy.add(~index, latest);
return copy;
}
/**
* Adds security price to historical quotes.
*
* @return true if the historical quote was updated.
*/
public boolean addPrice(SecurityPrice price)
{
return addPrice(price, true);
}
/**
* Adds security price to historical quotes.
*
* @param overwriteExisting
* is used to decide on whether to keep or overwrite existing
* prices
* @return true if the historical quote was updated.
*/
public boolean addPrice(SecurityPrice price, boolean overwriteExisting)
{
Objects.requireNonNull(price);
int index = Collections.binarySearch(prices, price);
if (index < 0)
{
prices.add(~index, price);
return true;
}
else
{
SecurityPrice replaced = prices.get(index);
// different prices are replaced only, if the source is manual, csv
// or html import, the value is 0.0
if (!replaced.equals(price) && (overwriteExisting || replaced.getValue() == 0.0))
{
// only replace if necessary -> UI might keep reference!
prices.set(index, price);
return true;
}
else
{
return false;
}
}
}
/**
* Adds all prices to the list of prices unless a security price for that
* date already exists. However, the last historical date is overwritten as
* some quote provider include the latest security price in the list of
* historical prices.
*/
public boolean addAllPrices(List<SecurityPrice> prices)
{
if (prices.isEmpty())
return false;
LocalDate now = LocalDate.now();
LocalDate last = null;
if (!prices.isEmpty())
last = prices.get(prices.size() - 1).getDate();
boolean isUpdated = false;
for (SecurityPrice p : prices)
{
if (p.getDate().isBefore(now))
{
boolean doOverwrite = p.getDate().equals(last);
boolean isAdded = addPrice(p, doOverwrite);
isUpdated = isUpdated || isAdded;
}
}
return isUpdated;
}
public void removePrice(SecurityPrice price)
{
prices.remove(price);
}
public void removeAllPrices()
{
prices.clear();
}
public SecurityPrice getSecurityPrice(LocalDate requestedDate)
{
// assumption: prefer historic quote over latest if there are more
// up-to-date historic quotes
SecurityPrice lastHistoric = prices.isEmpty() ? null : prices.get(prices.size() - 1);
// use latest quote only
// * if one exists
// * and if either no historic quotes exist
// * or
// ** if the requested time is after the latest quote
// ** and the historic quotes are older than the latest quote
if (latest != null //
&& (lastHistoric == null //
|| (!requestedDate.isBefore(latest.getDate()) && //
!latest.getDate().isBefore(lastHistoric.getDate()) //
)))
return latest;
if (lastHistoric == null)
return new SecurityPrice(requestedDate, 0);
// avoid binary search if last historic quote <= requested date
if (!lastHistoric.getDate().isAfter(requestedDate))
return lastHistoric;
SecurityPrice p = new SecurityPrice(requestedDate, 0);
int index = Collections.binarySearch(prices, p);
if (index >= 0)
return prices.get(index);
else if (index == -1) // requested is date before first historic quote
return prices.get(0);
else
return prices.get(-index - 2);
}
/**
* Returns the latest two security prices needed to display the previous
* close as well as to calculate the change on the previous close.
*
* @return a pair of security prices with the <em>left</em> being today's
* price and <em>right</em> being the previous close
*/
public Optional<Pair<SecurityPrice, SecurityPrice>> getLatestTwoSecurityPrices()
{
if (prices.isEmpty())
return Optional.empty();
List<SecurityPrice> list = getPricesIncludingLatest();
if (list.size() < 2)
return Optional.empty();
LocalDate now = LocalDate.now();
SecurityPrice today = null;
int index = list.size() - 1;
while (index >= 0)
{
today = list.get(index);
if (!today.getDate().isAfter(now))
break;
index--;
}
return index > 0 ? Optional.of(new Pair<>(list.get(index), list.get(index - 1))) : Optional.empty();
}
public String getLatestFeed()
{
return latestFeed;
}
public void setLatestFeed(String latestFeed)
{
this.latestFeed = latestFeed;
}
public String getLatestFeedURL()
{
return latestFeedURL;
}
public void setLatestFeedURL(String latestFeedURL)
{
this.latestFeedURL = latestFeedURL;
}
public LatestSecurityPrice getLatest()
{
return latest;
}
/**
* Sets the latest security price.
*
* @return true if the latest security price was updated.
*/
public boolean setLatest(LatestSecurityPrice latest)
{
// only replace if necessary -> UI might keep reference!
if (!Objects.equals(latest, this.latest))
{
this.latest = latest;
return true;
}
else
{
return false;
}
}
@Override
public boolean isRetired()
{
return isRetired;
}
@Override
public void setRetired(boolean isRetired)
{
this.isRetired = isRetired;
}
public List<SecurityEvent> getEvents()
{
if (this.events == null)
this.events = new ArrayList<>();
return events;
}
public void addEvent(SecurityEvent event)
{
if (this.events == null)
this.events = new ArrayList<>();
this.events.add(event);
}
public Stream<SecurityProperty> getProperties()
{
if (properties == null)
properties = new ArrayList<>();
return properties.stream();
}
public Optional<String> getPropertyValue(SecurityProperty.Type type, String name)
{
return getProperties().filter(p -> p.getType() == type && name.equals(p.getName()))
.map(SecurityProperty::getValue).findAny();
}
/**
* Sets the property values. If the value is null, the property is removed
* if it exists. Returns <tt>true</tt> if the property was updated.
*/
public boolean setPropertyValue(SecurityProperty.Type type, String name, String value)
{
if (properties == null)
properties = new ArrayList<>();
Optional<SecurityProperty> prop = properties.stream()
.filter(p -> p.getType() == type && name.equals(p.getName())).findAny();
if (prop.isPresent())
{
boolean isEqual = Objects.equals(prop.get().getValue(), value);
if (!isEqual)
{
properties.remove(prop.get());
if (value != null)
properties.add(new SecurityProperty(type, name, value));
}
return !isEqual;
}
else if (value != null)
{
properties.add(new SecurityProperty(type, name, value));
return true;
}
else
{
return false;
}
}
public void addProperty(SecurityProperty data)
{
if (properties == null)
properties = new ArrayList<>();
this.properties.add(data);
}
/**
* Removes the given property, if it is present. Returns <tt>true</tt> if
* the properties contained the specified property (or equivalently, if this
* list changed as a result of the call).
*/
public boolean removeProperty(SecurityProperty data)
{
if (properties == null)
return false;
return this.properties.remove(data);
}
/**
* Removes all of the SecurityProperties that satisfy the given predicate.
*/
public boolean removePropertyIf(Predicate<SecurityProperty> filter)
{
if (properties != null)
return properties.removeIf(filter);
else
return false;
}
@Override
public Attributes getAttributes()
{
if (attributes == null)
attributes = new Attributes();
return attributes;
}
@Override
public void setAttributes(Attributes attributes)
{
this.attributes = attributes;
}
/**
* Gets all URLs contained in the notes.
*
* @return list of URLs
*/
public Stream<Bookmark> getCustomBookmarks()
{
List<Bookmark> bookmarks = new ArrayList<>();
// extract bookmarks from attributes
getAttributes().getAllValues().filter(v -> v instanceof Bookmark).forEach(v -> bookmarks.add((Bookmark) v));
// extract bookmarks from notes
if (!Strings.isNullOrEmpty(note))
{
Pattern urlPattern = Pattern.compile("(https?\\:\\/\\/[^ \\t\\r\\n]+)", Pattern.CASE_INSENSITIVE); //$NON-NLS-1$
Matcher m = urlPattern.matcher(note);
while (m.find())
bookmarks.add(new Bookmark(m.group()));
}
return bookmarks.stream();
}
public List<TransactionPair<?>> getTransactions(Client client)
{
List<TransactionPair<?>> answer = new ArrayList<>();
for (Account account : client.getAccounts())
{
account.getTransactions().stream() //
.filter(t -> this.equals(t.getSecurity()))
.filter(t -> t.getType() == AccountTransaction.Type.INTEREST
|| t.getType() == AccountTransaction.Type.DIVIDENDS
|| t.getType() == AccountTransaction.Type.TAXES
|| t.getType() == AccountTransaction.Type.TAX_REFUND
|| t.getType() == AccountTransaction.Type.FEES
|| t.getType() == AccountTransaction.Type.FEES_REFUND)
.map(t -> new TransactionPair<AccountTransaction>(account, t)) //
.forEach(answer::add);
}
for (Portfolio portfolio : client.getPortfolios())
{
portfolio.getTransactions().stream() //
.filter(t -> this.equals(t.getSecurity()))
.map(t -> new TransactionPair<PortfolioTransaction>(portfolio, t)) //
.forEach(answer::add);
}
return answer;
}
public boolean hasTransactions(Client client)
{
for (Portfolio portfolio : client.getPortfolios())
{
Optional<PortfolioTransaction> transaction = portfolio.getTransactions().stream()
.filter(t -> this.equals(t.getSecurity())).findAny();
if (transaction.isPresent())
return true;
}
for (Account account : client.getAccounts())
{
Optional<AccountTransaction> transaction = account.getTransactions().stream()
.filter(t -> this.equals(t.getSecurity())).findAny();
if (transaction.isPresent())
return true;
}
return false;
}
public Security deepCopy()
{
Security answer = new Security();
answer.onlineId = onlineId;
answer.name = name;
answer.currencyCode = currencyCode;
answer.targetCurrencyCode = targetCurrencyCode;
answer.note = note;
answer.isin = isin;
answer.tickerSymbol = tickerSymbol;
answer.wkn = wkn;
answer.calendar = calendar;
answer.feed = feed;
answer.feedURL = feedURL;
answer.prices = new ArrayList<>(prices);
answer.latestFeed = latestFeed;
answer.latestFeedURL = latestFeedURL;
answer.latest = latest;
answer.events = new ArrayList<>(getEvents());
if (properties != null)
answer.properties = new ArrayList<>(properties);
answer.isRetired = isRetired;
return answer;
}
@Override
public String toString()
{
return getName();
}
public String toInfoString()
{
StringBuilder b = new StringBuilder();
b.append(name);
if (notEmpty(isin))
b.append('\n').append(isin);
if (notEmpty(wkn))
b.append('\n').append(wkn);
if (notEmpty(tickerSymbol))
b.append('\n').append(tickerSymbol);
if (notEmpty(note))
b.append("\n\n").append(note); //$NON-NLS-1$
return b.toString();
}
private boolean notEmpty(String s)
{
return s != null && s.length() > 0;
}
}