tag:infinitekind.tenderapp.com,2009-01-14:/discussions/moneydance-development/2025-how-to-add-a-security-from-a-python-scriptInfinite Kind: Discussion 2018-12-18T12:40:25Ztag:infinitekind.tenderapp.com,2009-01-14:Comment/445539802018-01-21T20:41:08Z2018-09-18T12:18:51ZHow to add a security from a python script<div><p>I'm trying to add a security to the security master via a python script.</p>
<p>I know the 'New Security" dialog does this, but I want to read my security attributes from a file and add a new security from a python script. The information I would read from the file is the same as what is entered manually in the 'New Security" dialog, namely:<br>
- Security ID - Security Name - Ticker Symbol - Price - Value Prefix - Value Suffix - Max. Decimal Places - Show on summary page</p>
<p>Is there any way I can invoke the java program behind the 'New Security" dialog from python to add a security?</p>
<p>I can see how to delete an item (via the .getCurrencyByID(symbol_id).deleteItem() method), but I can't determine how to add an item.</p>
<p>Can you share an example of how to do this please?</p>
<p>Thank you.</p></div>md_oztag:infinitekind.tenderapp.com,2009-01-14:Comment/445539802018-02-23T00:28:27Z2018-02-23T00:28:27ZHow to add a security from a python script<div><p>Have not actually tried this but I think the pseudo code for this would be</p>
<p>import from com.infinitekind.moneydance.model import CurrencyTable, CurrencyType</p>
<p>book = moneydance.getCurrentAccountBook()<br>
currencies = book.getCurrencies().getAllCurrencies()<br>
newCurrency = CurrencyType(book)<br>
newCurrency.setTickerSymbol("symbol")<br>
newCurrency.setName("name")<br>
newCurrency.setIDString("id")<br>
...</p>
<p>and so forth. Or it seems maybe faster/better to use:</p>
<p>book = moneydance.getCurrentAccountBook()<br>
currencies = book.getCurrencies().getAllCurrencies()<br>
com.infinitekind.moneydance.model.CurrencyType.newCurrency = currencyFromFields(id, id_string, name, rate, decimalPlaces, prefix, suffix, tickerSymbol, effectiveDate, currencyType, currencies)</p>
<p>See the CurrencyType class in those within the com.infinitekind.moneydance.model package documented here <a href="https://infinitekind.com/dev/apidoc/index.html">https://infinitekind.com/dev/apidoc/index.html</a>.</p>
<p>Good luck.</p></div>whitedavidptag:infinitekind.tenderapp.com,2009-01-14:Comment/445539802018-09-18T12:38:14Z2018-09-18T12:43:02ZHow to add a security from a python script<div><p>Sorry for the delay in getting back to this topic, but I wanted to re-open the discussion to share my final solution. I tried both options suggested by whitedavidp, but decided against using the currencyFromFields option as I thought the field by field approach was more readable and flexible. The currencyFromFields sets the security currency as the book BaseType (USD in my case) which doesn't work for non-BaseType securities and it does not allow the setting of the HideInUI, so I'd have to do these functions by field anyway.</p>
<p>A big thank you to whitedavidp and jaraco (<a href="https://github.com/jaraco/moneydance/blob/master/migration.py">https://github.com/jaraco/moneydance/blob/master/migration.py</a>) for sharing their code and expertise, that was the basis of this function, see attachment (I tried pasting it but the formatting is messed up.</p></div>md_oz