KendoUI Grid filter and sorting not working with Asp.Net MVC.

It took me long to understand why my kendo grid wont fill DataSourceRequest and although the filtering and sorting parameters are send via query string. There is a javascript file required to make this feature works its called ‘kendo.aspnetmvc.min.js’.

Historical Volatility

The code snippet is capable of calculating historical volatility using Close Price, High Low Price and Close High Low Price methods. Simply provide symbol, start date and end date of the specific volatility method and it extracts the market data from the yahoo service and calculated the volatility.

open System
open System.IO
open System.Xml
open System.Text
open System.Net
open System.Globalization

let makeUrl symbol (dfrom:DateTime) (dto:DateTime) =
    //Uses the not-so-known chart-data:
    new Uri("http://ichart.finance.yahoo.com/table.csv?s=" + symbol +
       "&e=" + dto.Day.ToString() + "&d=" + dto.Month.ToString() + "&f=" + dto.Year.ToString() +
       "&g=d&b=" + dfrom.Day.ToString() + "&a=" + dfrom.Month.ToString() + "&c=" + dfrom.Year.ToString() +
       "&ignore=.csv")

let fetch (url : Uri) =
    let req = WebRequest.Create (url) :?> HttpWebRequest
    use stream = req.GetResponse().GetResponseStream()
    use reader = new StreamReader(stream)
    reader.ReadToEnd()

let reformat (response:string) =
    let split (mark:char) (data:string) =
        data.Split(mark) |> Array.toList
    response |> split '\n'
    |> List.filter (fun f -> f<>"")
    |> List.map (split ',') 

let getRequest uri = (fetch >> reformat) uri

type MarketData =
    {Date: DateTime;
     Open: double;
     High: double;
     Low: double;
     Close: double;
     Volume: int;
     AdjClose: double;}

let converter (data:List<List<string>>) =
    [for dataArray in data.Tail do
        yield {Date = DateTime.ParseExact(dataArray.[0],"yyyy-MM-dd",CultureInfo.InvariantCulture);
          Open = System.Convert.ToDouble(dataArray.[1]);
          High = System.Convert.ToDouble(dataArray.[2]);
          Low = System.Convert.ToDouble(dataArray.[3]);
          Close = System.Convert.ToDouble(dataArray.[4]);
          Volume = System.Convert.ToInt32(dataArray.[5]);
          AdjClose = System.Convert.ToDouble(dataArray.[6])}]

//Example: Microsoft, from 2010-03-20 to 2010-04-21
//getMarketData "MSFT" (new DateTime(2010,1,1)) (new DateTime(2010,1,30));;
let getMarketData symbol fromDate toDate = makeUrl symbol fromDate toDate  |> getRequest |> converter

//highLowVolatility "MSFT" (new DateTime(2010,1,1)) (new DateTime(2010,1,30));;
let highLowVolatility symbol (fromDate:DateTime) (toDate:DateTime) =
    let data = getMarketData symbol fromDate toDate
    data
    |> List.fold (fun acc mktdata -> acc + Math.Log(mktdata.High / mktdata.Low) )  0.0
    |> (*) ( 1.0 / ( ( 2.0 * System.Convert.ToDouble(data.Length)) * System.Math.Sqrt(System.Math.Log(2.0)) ) )
    |> (*) (System.Math.Sqrt 252.0)

//closeVolatility "MSFT" (new DateTime(2010,1,1)) (new DateTime(2010,1,30));;
let closeVolatility symbol (fromDate:DateTime) (toDate:DateTime) =
        let data = getMarketData symbol fromDate toDate
        let rec close (dt: MarketData list)  =
            match dt with
            | x::y::[] -> System.Math.Pow(System.Math.Log((y.Close / x.Close)),2.0)
            | x::y::tail -> System.Math.Pow(System.Math.Log((y.Close / x.Close)),2.0)  + (close (List.append [y] tail))
            | [] -> 0.0

        let rec close1 (dt: MarketData list)  =
            match dt with
            | x::y::[] -> System.Math.Log(y.Close / x.Close)
            | x::y::tail -> System.Math.Log(y.Close / x.Close)  + (close1 (List.append [y] tail))
            | [] -> 0.0

        let firstValue = data
                            |> close
                            |> (*) (1.0 / (System.Convert.ToDouble(data.Length - 1) - 1.0))

        let secondValue = (System.Math.Pow((close1 data), 2.0)) * ( 1.0 / (System.Convert.ToDouble(data.Length - 1) *(System.Convert.ToDouble(data.Length - 1) - 1.0 )))

        System.Math.Sqrt (firstValue - secondValue) * System.Math.Sqrt(252.0)     

//highLowCloseVolatility "MSFT" (new DateTime(2010,1,1)) (new DateTime(2010,1,30));;
let highLowCloseVolatility symbol (fromDate:DateTime) (toDate:DateTime) =
        let data = getMarketData symbol fromDate toDate
        let highLow (dt: MarketData list) =
                    dt
                    |> List.fold (fun acc x -> acc + (0.5) * System.Math.Pow(System.Math.Log(x.High / x.Low),2.0))  0.0
                    |> (*) ( 1.0 / System.Convert.ToDouble(data.Length - 1))

        let rec closeCalc (dt: MarketData list)  =
                    match dt with
                    | x::y::[] -> ((2.0 * System.Math.Log(2.0)) - 1.0) * System.Math.Pow(System.Math.Log((y.Close / x.Close)),2.0)
                    | x::y::tail -> ((2.0 * System.Math.Log(2.0)) - 1.0) * System.Math.Pow(System.Math.Log((y.Close / x.Close)),2.0)  + (closeCalc (List.append [y] tail))
                    | [] -> 0.0

        let close (dt: MarketData list) =
                dt
                |> closeCalc
                |> (*) ( 1.0 / System.Convert.ToDouble(data.Length - 1))

        System.Math.Sqrt(252.0) * System.Math.Sqrt((highLow data.Tail) - (close data))

 

Open new window with its own UI thread.

The code below speaks for itself on how to open multiple windows which have there own dispatcher. 
private void CreateNewWindow()
  {
   Thread thread = new Thread(() =>
    {
     Window1 w = new Window1();
     w.Show();

     w.Closed += (sender2, e2) =>
      w.Dispatcher.InvokeShutdown();

     System.Windows.Threading.Dispatcher.Run();
    });

   thread.SetApartmentState(ApartmentState.STA);
   thread.Start();
  }

Powershell – Copyright header generator script

Recently i have been doing pair programming with my colleague Andre and thanks to him, i learned powershell programming. Most of the code files in the project need some kind of copyright information at the top. One way to add these headers is by using code formatting tools like stylecop or resharper. But with my newly acquired skills i believe its better to write a small script to append the header on all code files using powershell.

Problem: (Add copyright information to all the files with a given extension found in the target and its subdirectories.)

  • Read all the files from target directory and its subdirectories with given extension.
  • Append header text at the beginning of the file with copyright information.

Solution:

param($target = "c:\\tmp", $companyname = "Lab49")
$header = "//-----------------------------------------------------------------------
// <copyright file=""{0}"" company=""{1}"">
//     Copyright (c) {1}. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------`r`n"
function Write-Header ($file) {
$content = Get-Content $file
$filename = Split-Path -Leaf $file
$fileheader = $header -f $filename,$companyname
Set-Content $file $fileheader
Add-Content $file $content
}
Get-ChildItem $target -Recurse | ? { $_.Extension -like ".txt" } | % {
Write-Header $_.PSPath.Split(":",3)[2]
}

WPF Dynamic Data Display.

Found this amazing library for dynamic data display developed by Computational Science Laboratory in Microsoft Research. It features efficient binding mechanisms and real-time interactivity capable of charting millions of data points. WPF developers in finance should have a look at this.

http://www.codeplex.com/dynamicdatadisplay

CSLA.Net framework for developing business objects.

I have been fan of CSLA.Net(component-based, scalable, logical architecture) framework since early days of my career had ported it from VB.Net to C# then. This framework comes handy for development of N-Tier architecture. Following are the feature highlights.
  1. Tracking Broken Business Rules
  2. Tracking Whether the Object Has Changed
  3. Strongly Typed Collections of Child Objects
  4. Simple and Abstract Model for the UI Developer
  5. Supporting Data Binding
  6. Object Persistence and Object-Relational Mapping .
  7. Business Object Creation
  8. N-Level Undo Functionality
  9. Data Binding Support
  10. Validation Rules
  11. Data Portal
  12. Custom Authentication
  13. Integrated Authorization

http://www.lhotka.net/

    Posted in .Net, C#. Tags: . 1 Comment »

    Finally WPF/Sliverlight style generator.

    I started this project last week and so far i was able to get Monochromatic, complimentary, Triad , Tetrads and analogous colors. The final goal of this project are as follows.

    1. User should be able to view dark and light style with chosen color on predefined template.
    2. User should be able to generate ResourceDictionary for Silverlight/WPF out of the chosen color. (This would relieve every developer from pain of styling the application :) trust me i know it)

    image

    Coming soon on codeplex…….  Download code.

    Note: Please contribute to the project for the wellbeing of all the developers :)

    Follow

    Get every new post delivered to your Inbox.