Is this functional code? Dr.T. Reply ….

 


This would be a functional approach:


CountWithPrevious : [‘a] => (a => a => bool) => int // type of the function
CountWithPrevious [] _                    = 0
CountWithPrevious [_]  _                  = 0
CountWithPrevious [prev, val | tail] pred = CountWithPrevious [val | tail] + (pred val prev ? 1 : 0)


Some observations:


  • _” is used as wildcard argument – matches any value that you don’t need a name for.

  • [ x,y,z | t ] are pattern matching over lists – here x,y and z get bound to the first elements and t is the rest of the list.

  • Both CountWithPrevious and the passed-in pred are curried – they take two arguments but one at a time.

  • The CountWithPrevious function is defined with pattern matching – at runtime it searches through the definitions until one matches.

  • The type declaration is optional – the compiler can figure out the type from the last case of the function.

In C# we don’t have pattern matching and currying, and so would probably need a helper function.

public static int CountWithPrevious<T>(IEnumerable<T> en, PredWithPrevious pred) {

IEnumerator<T> rest = en.GetEnumerator();

if (rest.MoveNext()) return Helper(rest.Current,rest,pred);

else return 0;

}

private static int Helper<T>(T prev, IEnumerator<T> rest, PredWithPrevious pred) {

if (rest.MoveNext()) {

T val = rest.Current;

return Helper(val,rest,pred) + (pred(val,prev) ? 1 : 0);

} else return 0;

}


We could simulate local functions with lambdas so that we don’t need to pass pred, prev and T along:

public static int CountWithPrevious<T>(IEnumerable<T> en, PredWithPrevious pred) {

IEnumerator<T> rest = en.GetEnumerator();

Func<T,int> helper = prev => {

if (rest.MoveNext()) {

T val = rest.Current;

return Helper(val) + (pred(val,prev) ? 1 : 0);

} else return 0;

};

if (rest.MoveNext()) return Helper(rest.Current);

else return 0;

}


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Advertisements

Is this functional code?

 


I’m an Object Oriented (OO) kind of guy, maybe a bigot. I have read a very large number of OO books and I’ve written a fair amount of OO code in my 10+ years in this industry. I’m afraid my mind is wired for OO at this point, for good or bad.

Recently, I’ve been getting interested in functional languages. The most functional guys around here are Wes and Luke, while Dr. T has somehow transcended the whole thing.

Here is the text of one of my newbie emails to Dr.T. I’ll let you think about it for a bit before posting his reply. The phrase: “Is this it?” has to be read as “Is this what functional programming is all about?”.

/// EMAIL TEXT BELOW

Is this it? I have about four of these ‘delegate taking’ functions in my excel addin ….

    delegate bool PredWithPrevious<T>(T value, T previous);

static int CountWithPrevious<T>(IEnumerable<T> en, PredWithPrevious<T> pred) {

int count = 0;
T previous = default(T);
bool isPrevious = false;

foreach (T val in en) {

if (isPrevious)
if (pred(val, previous))
count++;
previous = val;
isPrevious = true;
}
return count;
}


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

A trading/portfolio management Excel Add-in based on the books by Ralph Vince

I like to write code and I still manage to do it even now that it is not my primary job. I never post the things that I write because I don’t want to maintain them. Lately Charlie convinced me that I don’t have to do that. I can just throw the code out, without much preparation or implicit contract of perpetual maintenance.


This one is an Excel add-in that adds functions to Excel to analyze your trading and manage your portfolio. Notice that I’m not a professional trader or statistician, so the whole thing could be wrong, buggy or conceptually absurd. Probably it is all of the above.


I used the extremely good ExcelDna to write the add-in. You need to download it and follow the instructions in HowToInstall.txt on the attached zip file to use it. I based the formulas mainly on the work of Ralph Vince. Please buy and read his books on money management as they are wonderful.


On the statistics side: I made up the “Downside correlation coefficient”, I have no idea if it is statistically sound.
On the technical side:



  1. I haven’t optimized the algos at all. I.E. I’m sure there is a way to calculate the standard deviation without navigating the array twice, but I didn’t bother to look. They end up being fast enough anyhow (apart from the Monte Carlo related functions that I should investigate).

  2. I haven’t organized the code correctly. It needs to be rewritten now that I partially know what I’m doing.

  3. I haven’t used LINQ. It will be a lot of fun for me to rewrite the code to be more OO and to use LINQ in the process.

I plan to implement OptimalF for a normal distribution and Efficient Frontier calculations in the future. I have no idea when.


I also have some other projects that might be interesting to share (I.E. a blackjack simulator that you can use to try the result of different strategies inspired by Blackjack Attack).


This is the list of Excel functions that you get with this add-in. They ends up in a Trading category in the list of functions on the “Insert Function” Excel dialog:


AnnualizeRet(double, double)
AnnualizeStdevp(double, double)
AnnualStdevpParam(double, double)
ArithMean(object[,], double, double, object, double, double)
AutoCorrel(object[,])
AvgLoss(object[,], double, double, object, double, double)
AvgWin(object[,], double, double, object, double, double)
Chebyshev(double)
Correlat(object[,], object[,])
Describe(object)
DownCorrelat(object[,], object[,], double)
DownStdevp(object[,], double)
EGM(double, double)
EGM2(double, double)
GM(object[,], double, double, object, double, double)
Kelly(double, double) TradingLibrary.Losses(object[,])
MaxDD(object[,])
MaxDDFromEq(object[,])
MaxLoss(object[,])
MaxNegRun(object[,])
MaxWin(object[,])
MCMaxDD(object[,], double)
MCOptimalF(object[,], double, double, double)
OptimalF(object[,], double, double)
OptimalFArray(object[,], double, double, double)
OptimalPos(double, double)
PortAnnualGM(double, double, double, double, double, double, double, double)
PortAnnualTaxGM(double, double, double, double, double, double, double)
PortAnnualTaxGMGivenTrades(object[,], double, double, double, double, double, object, double, double)
PortStdevp(double, double, double)
PRR(object[,], double, double, object, double, double)
PRRParam(double, double, double, double)
RealReturn(double, double, double, double)
RunTest(object[,])
SharpeRatio(double, double, double)
SortinoRatio(double, double, double)
Stdevp2(object[,], double, double, object, double, double)
TWR(object[,], double, double, object, double, double)
Wins(object[,])


 

kick it on DotNetKicks.com

TradingLibrary.zip