Create a Domain Specific Vocabulary in PowerShell for Measuring Portfolio Returns – Part 1

by Doug Finke on June 29, 2009

in DSL, DSV, Domain Specific Vocabulary, Finance, Financial, Portfolio, PowerShell

DSLs and DSVs (Domain Specific Languages and Vocabularies) can be super helpful and are not complicated to build.

Example

The mean return to a portfolio is the weighted average return of the stocks making up the portfolio.

Let’s say a portfolio consists of 30% GOOG shares with a mean daily return of 0.14% and 70% MSFT shares, mean daily return of 0.03%.

The mean daily return is:

(0.3 * .14) + (0.7 * 0.03) = 0.063

Another portfolio of 50% GOOG and 50% MSFT gives

(0.5 * .14) + (0.5 * 0.03) = 0.085

The PowerShell DSV

I’d like to represent the portfolio like the following, legal PowerShell syntax.

image

 

Let’s build up the Domain Specific Vocabulary

Lines 1-3 enable the DSV to work and prints:

image

image

 

Adding lines 4-8 will output the snippet below. Note line 7 uses “&”, the PowerShell call operator, to evaluate the script block.

image

image 

 

Transform the Data into objects

Let’s transform the data into objects with properties so it prints the following table, with the added plus of addressable properties.

image

image
 
 

Calculate the Weighted Average

Adding a hash table lookup in lines 4-7 and the properties MeanDailyReturn and WeightedAverage in lines 18-24.

Lines 20 and 23 look up the MeanDailyReturn by stock name using dot notation.

image

image

In an upcoming post

The transformation of the DSV into objects and properties is finished. In an upcoming post we’ll add some more detail and play with the output.

{ 0 comments… add one now }

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>