Popular recipes tagged "finance"http://code.activestate.com/recipes/tags/finance/2014-04-28T08:41:49-07:00ActiveState Code RecipesMonte Carlo Engine : How to find the optimised wager for next bet, following a recent loss. (Python) 2014-04-28T08:41:49-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578869-monte-carlo-engine-how-to-find-the-optimised-wager/ <p style="color: grey"> Python recipe 578869 by <a href="/recipes/users/4166679/">alexander baker</a> (<a href="/recipes/tags/finance/">finance</a>, <a href="/recipes/tags/optimise/">optimise</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/tree/">tree</a>). </p> <p>Simple Engine to help understand how to best wager your next bet, given that you just made a loss. The engine uses the modified Powell method to optimise the weight to apply to your wager on the next position.</p> <p>{'My Simple Heads And Tails Model': &lt;BackTest.Simulation object at 0x0583D410&gt;} participants [100] survivors [90.0%] losers [10.0%] weight [0.073858] solving for r: [ 0.07385806] simulations 100, trials 100 starting pot 1000 calling initialise {'My Simple Heads And Tails Model': &lt;BackTest.Simulation object at 0x0583D430&gt;} participants [100] survivors [86.0%] losers [14.0%] weight [0.072220] solving for r: [ 0.07221954] Optimization terminated successfully. Current function value: 8.000000 Iterations: 2 Function evaluations: 30 highest survivability following loss, multiply wager by 7.2949 %</p> <h5 id="">.</h5> <p>Ran 2 tests in 25.545s</p> <p>OK</p> Multidimensional Pareto front (Python) 2012-10-13T23:26:12-07:00Jamie Bullhttp://code.activestate.com/recipes/users/4183059/http://code.activestate.com/recipes/578287-multidimensional-pareto-front/ <p style="color: grey"> Python recipe 578287 by <a href="/recipes/users/4183059/">Jamie Bull</a> (<a href="/recipes/tags/engineering/">engineering</a>, <a href="/recipes/tags/finance/">finance</a>, <a href="/recipes/tags/optimal/">optimal</a>, <a href="/recipes/tags/pareto/">pareto</a>, <a href="/recipes/tags/pareto_front/">pareto_front</a>, <a href="/recipes/tags/pareto_frontier/">pareto_frontier</a>, <a href="/recipes/tags/pareto_set/">pareto_set</a>). Revision 2. </p> <p>Method for finding non-dominated options in multi-dimensional data.</p> zero curve bootstrapping and forward curve generation (Python) 2012-09-10T08:07:56-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578257-zero-curve-bootstrapping-and-forward-curve-generat/ <p style="color: grey"> Python recipe 578257 by <a href="/recipes/users/4166679/">alexander baker</a> (<a href="/recipes/tags/finance/">finance</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/sympy/">sympy</a>). </p> <p>example of a bootstrapping and forward curve generation this can be used to build a set of curves for different currencies</p> Pareto front (Python) 2012-08-01T09:04:04-07:00Jamie Bullhttp://code.activestate.com/recipes/users/4183059/http://code.activestate.com/recipes/578230-pareto-front/ <p style="color: grey"> Python recipe 578230 by <a href="/recipes/users/4183059/">Jamie Bull</a> (<a href="/recipes/tags/engineering/">engineering</a>, <a href="/recipes/tags/finance/">finance</a>, <a href="/recipes/tags/optimal/">optimal</a>, <a href="/recipes/tags/pareto_front/">pareto_front</a>, <a href="/recipes/tags/pareto_frontier/">pareto_frontier</a>, <a href="/recipes/tags/pareto_set/">pareto_set</a>). </p> <p>Function for finding a 2D <a href="http://en.wikipedia.org/wiki/Pareto_efficiency#Use_in_engineering">Pareto frontier</a> given two lists of matched length.</p> Script that compares various Interest Rate term structure models. (Python) 2012-09-10T08:02:34-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/578256-script-that-compares-various-interest-rate-term-st/ <p style="color: grey"> Python recipe 578256 by <a href="/recipes/users/4166679/">alexander baker</a> (<a href="/recipes/tags/finance/">finance</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>A common model used in the financial industry for modelling the short rate (think overnight rate, but actually an infinitesimally short amount of time) is the Vasicek model. # Although it is unlikely to perfectly fit the yield curve, it has some nice properties that make it a good model to work with. The dynamics of the Vasicek model are describe below.</p> <p>In this model, the parameters are constants, and the random motion is generated by the Q measure Brownian motion . An important property of the Vasicek model is that the interest rate is mean reverting to , and the tendency to revert is controlled by . Also, this process is a diffusion process, hence Markovian, which will lead to some nice closed form formulas. Finally, the future value of the interest rate is normally distributed with the distribution .</p> Yahoo Stock Information (Python) 2012-08-25T17:16:47-07:00Alexander James Wallarhttp://code.activestate.com/recipes/users/4179768/http://code.activestate.com/recipes/577989-yahoo-stock-information/ <p style="color: grey"> Python recipe 577989 by <a href="/recipes/users/4179768/">Alexander James Wallar</a> (<a href="/recipes/tags/computation/">computation</a>, <a href="/recipes/tags/corey_goldberg/">corey_goldberg</a>, <a href="/recipes/tags/finance/">finance</a>, <a href="/recipes/tags/information/">information</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/realtime/">realtime</a>, <a href="/recipes/tags/stock/">stock</a>, <a href="/recipes/tags/yahoo/">yahoo</a>). Revision 3. </p> <p>This recipe is solely due to the programming of Corey Goldberg (<a href="mailto:corey@goldb.org">corey@goldb.org</a>). The only thing I did to it was make it into a class. I thought that by uploading it here that more people would come across it and more people could use it. It is really good. It gives you stock information.</p> NPV / IRR / Payback Analysis (Python) 2013-09-06T23:04:55-07:00Alia Khourihttp://code.activestate.com/recipes/users/4169084/http://code.activestate.com/recipes/576686-npv-irr-payback-analysis/ <p style="color: grey"> Python recipe 576686 by <a href="/recipes/users/4169084/">Alia Khouri</a> (<a href="/recipes/tags/finance/">finance</a>). Revision 18. </p> <p>A few financial functions for quick analysis of an investment opportunity and a series of associated cashflows.</p> <p>As a module, it currently provides straightforward and easy to understand implementations of the Net Present Value (NPV), Internal Rate of Return (IRR), and Payback Period functions.</p> <p>As a script, it provides a simple command line interface which integrates the above functions into a concise analysis of the investment opportunity.</p> <pre class="prettyprint"><code> usage: invest discount_rate [cashflow0, cashflow1, ..., cashflowN] where discount_rate is the rate used to discount future cashflows to their present values cashflow0 is the investment (always a negative value) cashflow1 .. cashflowN values can be positive (net inflows) or negative (net outflows) </code></pre> <p>Here is an example of actual usage and output:</p> <pre class="prettyprint"><code>$ ./invest 0.05 -10000 6000 6000 6000 ---------------------------------------------------------------------- year 0 1 2 3 cashflow -10,000 6,000 6,000 6,000 Discount Rate: 5.0% Payback: 1.67 years IRR: 36.31% NPV: 6339.49 ==&gt; Approve Investment of 10,000 ---------------------------------------------------------------------- </code></pre> <p><em>Note</em>: A check of the output of the Microsoft Excel NPV function against that of the function implemented here reveals a curious discrepancy/bug in the way Excel calculates its NPV. For further details see: <a href="http://www.tvmcalcs.com/blog/comments/the_npv_function_doesnt_calculate_net_present_value/" rel="nofollow">http://www.tvmcalcs.com/blog/comments/the_npv_function_doesnt_calculate_net_present_value/</a></p> <p>Furthermore, the method used to calculate the IRR is rough to say the least and fails at fewer than 3 entries. Please use the secant method along the lines of the following haskell code from (<a href="http://www.haskell.org/haskellwiki/Haskell_Quiz/Internal_Rate_of_Return/Solution_Dolio" rel="nofollow">http://www.haskell.org/haskellwiki/Haskell_Quiz/Internal_Rate_of_Return/Solution_Dolio</a>) for greater accuracy.</p> <pre class="prettyprint"><code>secant :: (Double -&gt; Double) -&gt; Double -&gt; Double secant f delta = fst $ until err update (0,1) where update (x,y) = (x - (x - y) * f x / (f x - f y), x) err (x,y) = abs (x - y) &lt; delta npv :: Double -&gt; [Double] -&gt; Double npv i = sum . zipWith (\t c -&gt; c / (1 + i)**t) [0..] irr :: [Double] -&gt; Double irr cashflows = secant (`npv` cashflows) (0.1**4) </code></pre> Brownian Motion of Stock (Python) 2009-05-19T05:16:29-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/576760-brownian-motion-of-stock/ <p style="color: grey"> Python recipe 576760 by <a href="/recipes/users/4166679/">alexander baker</a> (<a href="/recipes/tags/finance/">finance</a>). </p> <p>Brownian Motion of a Stock</p> get a stock historical value from google finance (Python) 2008-09-07T12:03:11-07:00bussiere bussierehttp://code.activestate.com/recipes/users/4050557/http://code.activestate.com/recipes/576495-get-a-stock-historical-value-from-google-finance/ <p style="color: grey"> Python recipe 576495 by <a href="/recipes/users/4050557/">bussiere bussiere</a> (<a href="/recipes/tags/finance/">finance</a>, <a href="/recipes/tags/google/">google</a>, <a href="/recipes/tags/historical/">historical</a>, <a href="/recipes/tags/value/">value</a>). </p> <p>how to get a stock historical value from google finance</p>