Writing a method that takes an integer, and returns it's factorial
Sometimes, when being interviewed for a job as a software developer, you’ll be asked a question such as “write a method that takes an integer as a parameter, and returns it’s factorial.”
For example, the factorial of 3 is represented as “3!”, which is calculated via 3*2*1, which equals 6. 4! is 4*3*2*1, which is 24, etc.
Putting aside whether these kinds of questions should be asked in an interview, if they’re asking you this, there’s a fairly high likelihood they’re asking for you to show that you understand recursion. If that’s the case, no problem, something like this will calculate the factorial:
let rec factorial n =
match n with
| 0 | 1 -> 1
| n when n > 0 && n <= 12 -> n * factorial (n - 1)
| _ -> failwith "Parameter n is out of the supported range. Must be between 0 and 12."
This can be run in the F# interactive shell via:
> factorial 3;;
val it : int = 6
But what if they want to find out whether you:
- Understand recursion, and…
- Know when you can avoid recursion, and just write simple methods instead.
Creating comparison charts for stocks with FSharp Charting, Deedle and Yahoo Finance
When you want to visualize how a stock or portfolio has performed historically relative to the market as a whole, it is useful to create a comparison chart.
This blog shows how to create a line chart to compare two stocks with Deedle, FSharp Charting and F# Data.
In this example, the chart will show the perfomance of ANZ.AX relative to the ASX ALL ORDINARIES index ([^AORD]5) over a three year period from 2011-1-1 to 2014-1-1.
A Basic Stock Trading Backtesting System for F# using Ta-Lib and FSharp.Data
This article is written for the intermediate F# audience who has a basic familiarity of stock trading and technical analysis, and is intended to show the basics of implementing a backtesting system in F#.
If you’re an F# beginner it may not take too long for you to get up to speed on the concepts if you check out a few resources. In particular:
The backtesting strategy which you will implement is a simple SMA crossover where the fast line is 10 bars and the slow one 25. When the fast line crosses above the slow line, a buy signal will be triggered, and when the fast line cross below the slow line, a sell signal will be triggered. Only one long position will exist at any time, so the system will not trigger another buy order until after the long position is sold.