use of jgnash.engine.SecurityNode in project jgnash by ccavanaugh.
the class ControlsTest method createEngine.
private Engine createEngine() {
try {
testFile = Files.createTempFile("test", DataStoreType.BINARY_XSTREAM.getDataStore().getFileExt()).toFile().getAbsolutePath();
tempFile = testFile;
} catch (IOException e1) {
LogUtil.logSevere(ControlsTest.class, e1);
}
EngineFactory.deleteDatabase(testFile);
final Engine engine = EngineFactory.bootLocalEngine(testFile, EngineFactory.DEFAULT, EngineFactory.EMPTY_PASSWORD, DataStoreType.BINARY_XSTREAM);
Objects.requireNonNull(engine);
CurrencyNode node = engine.getDefaultCurrency();
if (!node.getSymbol().equals("USD")) {
engine.setDefaultCurrency(DefaultCurrencies.buildNode(Locale.US));
}
node = engine.getCurrency("CAD");
if (node == null) {
node = DefaultCurrencies.buildNode(Locale.CANADA);
assertNotNull(node);
assertTrue(engine.addCurrency(node));
}
Account account = new Account(AccountType.BANK, engine.getDefaultCurrency());
account.setName("Bank Accounts");
engine.addAccount(engine.getRootAccount(), account);
SecurityNode securityNode = new SecurityNode();
securityNode.setSymbol("GGG");
securityNode.setDescription("Google");
securityNode.setReportedCurrencyNode(engine.getDefaultCurrency());
engine.addSecurity(securityNode);
securityNode = new SecurityNode();
securityNode.setSymbol("MSFT");
securityNode.setDescription("Microsoft");
securityNode.setReportedCurrencyNode(engine.getDefaultCurrency());
engine.addSecurity(securityNode);
return engine;
}
use of jgnash.engine.SecurityNode in project jgnash by ccavanaugh.
the class IEXParserTest method testHistoricalDividendsDownload.
@Test
// disable on Travis-CI
@DisabledIfEnvironmentVariable(named = "CI", matches = "true")
void testHistoricalDividendsDownload() throws IOException {
if (System.getenv(GITHUB_ACTION) != null) {
// don't test with Github actions
return;
}
// test env must be configured with a valid token
if (System.getenv(TEST_TOKEN) == null) {
// don't test with Github actions
return;
}
final SecurityNode spy = new SecurityNode(e.getDefaultCurrency());
spy.setSymbol("SPY");
spy.setScale((byte) 2);
spy.setQuoteSource(QuoteSource.IEX_CLOUD);
e.addSecurity(spy);
final QuoteSource quoteSource = spy.getQuoteSource();
assertNotNull(quoteSource);
final SecurityParser securityParser = quoteSource.getParser();
assertNotNull(securityParser);
assertThat(securityParser, instanceOf(IEXParser.class));
((IEXParser) securityParser).setUseSandbox();
securityParser.setTokenSupplier(() -> System.getenv(TEST_TOKEN));
final Set<SecurityHistoryEvent> historicalEvents = securityParser.retrieveHistoricalEvents(spy, LocalDate.of(2015, Month.JANUARY, 1));
assertNotNull(historicalEvents);
assertEquals(1, historicalEvents.size());
}
use of jgnash.engine.SecurityNode in project jgnash by ccavanaugh.
the class ApiTest method testSecurities.
@Test
void testSecurities() {
SecurityNode securityNode = new SecurityNode(e.getDefaultCurrency());
securityNode.setSymbol("GGG");
securityNode.setScale((byte) 2);
e.addSecurity(securityNode);
assertEquals(securityNode, e.getSecurity("GGG"));
assertEquals(0, securityNode.getHistoryNodes().size());
assertFalse(securityNode.getClosestHistoryNode(LocalDate.now()).isPresent());
assertFalse(securityNode.getHistoryNode(LocalDate.now()).isPresent());
assertEquals(BigDecimal.ZERO, securityNode.getMarketPrice(LocalDate.now(), e.getDefaultCurrency()));
// Add the security node to the account
final List<SecurityNode> securitiesList = new ArrayList<>(investAccount.getSecurities());
final int securityCount = securitiesList.size();
securitiesList.add(securityNode);
e.updateAccountSecurities(investAccount, securitiesList);
assertEquals(securityCount + 1, investAccount.getSecurities().size());
// Returned market price should be zero
assertEquals(BigDecimal.ZERO, Engine.getMarketPrice(new ArrayList<>(), securityNode, e.getDefaultCurrency(), LocalDate.now()));
// Create and add some security history
final SecurityHistoryNode historyNode = new SecurityHistoryNode(LocalDate.now(), BigDecimal.TEN, 10000, BigDecimal.TEN, BigDecimal.TEN);
e.addSecurityHistory(securityNode, historyNode);
assertEquals(1, securityNode.getHistoryNodes().size());
// Returned market price should be 10
assertEquals(BigDecimal.TEN, Engine.getMarketPrice(new ArrayList<>(), securityNode, e.getDefaultCurrency(), LocalDate.now()));
// Ensure the security updates correctly with a replacement value
final SecurityHistoryNode historyNodeReplacement = new SecurityHistoryNode(LocalDate.now(), BigDecimal.ONE, 10000, BigDecimal.ONE, BigDecimal.ONE);
e.addSecurityHistory(securityNode, historyNodeReplacement);
assertEquals(1, securityNode.getHistoryNodes().size());
// Returned market price should be 1
assertEquals(BigDecimal.ONE, Engine.getMarketPrice(new ArrayList<>(), securityNode, e.getDefaultCurrency(), LocalDate.now()));
}
use of jgnash.engine.SecurityNode in project jgnash by ccavanaugh.
the class ImportUtils method matchSecurity.
static Optional<SecurityNode> matchSecurity(final ImportSecurity security) {
final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
Objects.requireNonNull(engine);
for (final SecurityNode securityNode : engine.getSecurities()) {
if (securityNode.getSymbol().equals(security.getTicker())) {
return Optional.of(securityNode);
}
}
return Optional.empty();
}
use of jgnash.engine.SecurityNode in project jgnash by ccavanaugh.
the class OfxImport method importInvestmentTransaction.
private static InvestmentTransaction importInvestmentTransaction(final OfxBank ofxBank, final ImportTransaction ofxTransaction, final Account investmentAccount) {
final SecurityNode securityNode = matchSecurity(ofxBank, ofxTransaction.getSecurityId());
final String memo = ofxTransaction.getMemo();
final LocalDate datePosted = ofxTransaction.getDatePosted();
final BigDecimal units = ofxTransaction.getUnits();
final BigDecimal unitPrice = ofxTransaction.getUnitPrice();
final Account incomeAccount = ofxTransaction.getGainsAccount();
final Account fessAccount = ofxTransaction.getFeesAccount();
Account cashAccount = ofxTransaction.getAccount();
// force use of cash balance
if (OfxTags.CASH.equals(ofxTransaction.getSubAccount())) {
cashAccount = investmentAccount;
}
final List<TransactionEntry> fees = new ArrayList<>();
final List<TransactionEntry> gains = new ArrayList<>();
if (!ofxTransaction.getCommission().equals(BigDecimal.ZERO)) {
final TransactionEntry transactionEntry = new TransactionEntry(fessAccount, ofxTransaction.getCommission().negate());
transactionEntry.setTransactionTag(TransactionTag.INVESTMENT_FEE);
fees.add(transactionEntry);
}
if (!ofxTransaction.getFees().equals(BigDecimal.ZERO)) {
final TransactionEntry transactionEntry = new TransactionEntry(fessAccount, ofxTransaction.getFees().negate());
transactionEntry.setTransactionTag(TransactionTag.INVESTMENT_FEE);
fees.add(transactionEntry);
}
InvestmentTransaction transaction = null;
if (securityNode != null) {
switch(ofxTransaction.getTransactionType()) {
case DIVIDEND:
final BigDecimal dividend = ofxTransaction.getAmount();
transaction = TransactionFactory.generateDividendXTransaction(incomeAccount, investmentAccount, cashAccount, securityNode, dividend, dividend, dividend, datePosted, memo);
break;
case REINVESTDIV:
// Create a gains entry of an account other than the investment account has been selected
if (incomeAccount != investmentAccount) {
final TransactionEntry gainsEntry = TransactionFactory.createTransactionEntry(incomeAccount, investmentAccount, ofxTransaction.getAmount().negate(), memo, TransactionTag.GAIN_LOSS);
gains.add(gainsEntry);
}
transaction = TransactionFactory.generateReinvestDividendXTransaction(investmentAccount, securityNode, unitPrice, units, datePosted, memo, fees, gains);
break;
case BUYSHARE:
transaction = TransactionFactory.generateBuyXTransaction(cashAccount, investmentAccount, securityNode, unitPrice, units, BigDecimal.ONE, datePosted, memo, fees);
break;
case SELLSHARE:
transaction = TransactionFactory.generateSellXTransaction(cashAccount, investmentAccount, securityNode, unitPrice, units, BigDecimal.ONE, datePosted, memo, fees, gains);
break;
default:
}
}
return transaction;
}
Aggregations