Search in sources :

Example 26 with SecurityNode

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;
}
Also used : CurrencyNode(jgnash.engine.CurrencyNode) Account(jgnash.engine.Account) SecurityNode(jgnash.engine.SecurityNode) IOException(java.io.IOException) Engine(jgnash.engine.Engine)

Example 27 with SecurityNode

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());
}
Also used : IEXParser(jgnash.net.security.iex.IEXParser) SecurityNode(jgnash.engine.SecurityNode) QuoteSource(jgnash.engine.QuoteSource) SecurityParser(jgnash.net.security.SecurityParser) SecurityHistoryEvent(jgnash.engine.SecurityHistoryEvent) AbstractEngineTest(jgnash.engine.AbstractEngineTest) Test(org.junit.jupiter.api.Test) DisabledIfEnvironmentVariable(org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable)

Example 28 with SecurityNode

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()));
}
Also used : SecurityNode(jgnash.engine.SecurityNode) ArrayList(java.util.ArrayList) SecurityHistoryNode(jgnash.engine.SecurityHistoryNode) AbstractEngineTest(jgnash.engine.AbstractEngineTest) Test(org.junit.jupiter.api.Test)

Example 29 with SecurityNode

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();
}
Also used : SecurityNode(jgnash.engine.SecurityNode) Engine(jgnash.engine.Engine)

Example 30 with SecurityNode

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;
}
Also used : Account(jgnash.engine.Account) InvestmentTransaction(jgnash.engine.InvestmentTransaction) SecurityNode(jgnash.engine.SecurityNode) ArrayList(java.util.ArrayList) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) TransactionEntry(jgnash.engine.TransactionEntry)

Aggregations

SecurityNode (jgnash.engine.SecurityNode)39 Engine (jgnash.engine.Engine)12 Account (jgnash.engine.Account)10 SecurityHistoryNode (jgnash.engine.SecurityHistoryNode)8 LocalDate (java.time.LocalDate)7 ArrayList (java.util.ArrayList)6 AbstractEngineTest (jgnash.engine.AbstractEngineTest)6 Test (org.junit.jupiter.api.Test)6 CurrencyNode (jgnash.engine.CurrencyNode)5 DisabledIfEnvironmentVariable (org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable)5 List (java.util.List)4 QuoteSource (jgnash.engine.QuoteSource)4 SecurityParser (jgnash.net.security.SecurityParser)4 FXML (javafx.fxml.FXML)3 InvestmentTransaction (jgnash.engine.InvestmentTransaction)3 SecurityHistoryEvent (jgnash.engine.SecurityHistoryEvent)3 Transaction (jgnash.engine.Transaction)3 IEXParser (jgnash.net.security.iex.IEXParser)3 InjectFXML (jgnash.uifx.util.InjectFXML)3 NotNull (jgnash.util.NotNull)3