Pages

Showing posts with label Finance. Show all posts
Showing posts with label Finance. Show all posts

Sunday, November 17, 2013

Why F# Type Provider?

F# type provider is a very nice feature. I was asked several times why I need type provider? Just because of the intellisense? I believe there should be something else. In the current team, I found people "hate" type, they prefer to use C# object all the times. Now I realize why type is so important and why type can increase code quality.

I want to use a simple approval process as a sample. For my understanding, a type defines the meaning of a area of memory and certain operations which can be executed on this type.

The sample is simple. You can have something to be approved by lead, manager, and director. If a lead approves, it returns a manager type. If manager disapproves, the process will stop and director won't know it. After director's approval, this process stops. The type provider can have three types, director's approve method returns nothing while other type's approve method return higher level type. Lead's approve method returns manager, and manager's approve method returns director.

I'd bet people can say I can use a single type to do the same job. A general class can be used to create three instances. The director instance's approve method can return NULL or any special value to stop the approval process. It can solve this problem provides you know director's approval will stop this process beforehand. A runtime error is not far away if this information is not known beforehand. The type provider uses type to provide the information. After you invoke director's approve method, which returns NULL, user will know immediately that this process is terminated.

I would say in this case, type is a way to move runtime check/error to compile time. The attached is the demo project for this approval process.

Saturday, September 29, 2012

F# on Algorithms - merge sort

Not every Saturday is a pleasant sunshine blue sky. Today is one of these Saturdays.

The real driving power for F# is business requirements from the community and the success of F# depends on the success of community. I was hoping these materials can  inspires discussions around F# and eventually increase the adoption of F#. Our team, like me, wants to serve the community better, so please tell me, your code has bug, your code can be more concise, or your code is shit. 

So please, together, we can make it.

open System

let rec merge list0 list1 = 
    match list0, list1 with 
    | [], [] -> []
    | [], list
    | list, [] -> list
    | h0::t0, h1::t1 -> 
        if h0 < h1 then
            [h0] @ merge t0 list1
        else
            [h1] @ merge list0 t1

let rec mergeSort list = 
    match list with
    | [] -> []
    | [a] -> [a]
    | [a; b] -> [ min a b; max a b ]
    | _ -> 
        let firstHalf = 
            list 
            | > Seq.take (list.Length / 2)
            | > Seq.toList
        let secondHalf = 
            list
            | > Seq.skip (list.Length / 2)
            | > Seq.toList

        let sortedFirstHalf = mergeSort firstHalf
        let sortedSecondHalf = mergeSort secondHalf

        merge sortedFirstHalf sortedSecondHalf

mergeSort [1;3;9;2;4;7;6;5]


Friday, August 10, 2012

Use Space in F# Namespace and Module

It is Friday's afternoon and sun finally decide to visit Seattle. I decide to have some fun with F#. I tried to use  space in the variable name with ``. It works perfectly and then  I decide to add space to other components like namespace and module.

Result: both namespace and module name can contains space.

But there are some limitation. The C# IDE does not support namespace contains space. As a result, the C# cannot reference the F# function inside a namespace which contains space. Because F# support space, so you can reference function inside F# code.

In File1.fs

namespace A.``My Space``
type MyClass() = class end
In File2.fs, the following code compiles.

module File2
open A.``My Space``
let c = MyClass()
let d = A.``My Space``.MyClass()
Not sure if somebody wants to use this feature to hide the code from C#, but it is do-able.. :-)

Sunday, August 5, 2012

Self Note: Yahoo Finance Tags

Sample to use the following tags:

http://finance.yahoo.com/d/quotes.csv?s=GE+PTR+MSFT&f=snd1l1yr

aAska2Average Daily Volumea5Ask Size
bBidb2Ask (Real-time)b3Bid (Real-time)
b4Book Valueb6Bid SizecChange & Percent Change
c1Changec3Commissionc6Change (Real-time)
c8After Hours Change (Real-time)dDividend/Shared1Last Trade Date
d2Trade DateeEarnings/Sharee1Error Indication (returned for symbol changed / invalid)
e7EPS Estimate Current Yeare8EPS Estimate Next Yeare9EPS Estimate Next Quarter
f6Float SharesgDay’s LowhDay’s High
j52-week Lowk52-week Highg1Holdings Gain Percent
g3Annualized Gaing4Holdings Gaing5Holdings Gain Percent (Real-time)
g6Holdings Gain (Real-time)iMore Infoi5Order Book (Real-time)
j1Market Capitalizationj3Market Cap (Real-time)j4EBITDA
j5Change From 52-week Lowj6Percent Change From 52-week Lowk1Last Trade (Real-time) With Time
k2Change Percent (Real-time)k3Last Trade Sizek4Change From 52-week High
k5Percebt Change From 52-week HighlLast Trade (With Time)l1Last Trade (Price Only)
l2High Limitl3Low LimitmDay’s Range
m2Day’s Range (Real-time)m350-day Moving Averagem4200-day Moving Average
m5Change From 200-day Moving Averagem6Percent Change From 200-day Moving Averagem7Change From 50-day Moving Average
m8Percent Change From 50-day Moving AveragenNamen4Notes
oOpenpPrevious Closep1Price Paid
p2Change in Percentp5Price/Salesp6Price/Book
qEx-Dividend DaterP/E Ratior1Dividend Pay Date
r2P/E Ratio (Real-time)r5PEG Ratior6Price/EPS Estimate Current Year
r7Price/EPS Estimate Next YearsSymbols1Shares Owned
s7Short Ratiot1Last Trade Timet6Trade Links
t7Ticker Trendt81 yr Target PricevVolume
v1Holdings Valuev7Holdings Value (Real-time)w52-week Range
w1Day’s Value Changew4Day’s Value Change (Real-time)xStock Exchange
yDividend Yield