Colorize negative number using jquery in html table

Recently I was working on creating reports for clients on web portal. We are developing web portabl in asp.net MVC3 and using jquery utility functions.

One of the UI requirement was to colorize negative numbers. I can implement in server side in chtml pages but it would not be generic for all pages. So I decided to use jquery functions to do this task.

This is my html page:

<table class=”list”>
<tr>
<td>Date</td>
<td class=”right”>Amount</td>
</tr>
<tr>
<td>2012-05-01</td>
<td class=”right”>-5.156</td>
</tr>
<tr>
<td>2012-05-01</td>
<td class=”right”>-8.25</td>
</tr>

<tr>
<td>2012-05-01</td>
<td class=”right”>55.45</td>
</tr>
</table>​

Date Amount
2012-05-01 -5.156
2012-05-01 -8.25
2012-05-01 55.45

Java Script
On Dom Ready

$(function(){

$(“td.right”).filter(function() {
return $.trim($(this).text()).indexOf(“-“) == 0;
}).addClass(“negative”)​;
});

Css
.right
{
text-align:right;
}
.left
{
text-align:left;
}
.negative
{
color:red;
text-align:right;
}

.list
{
border: 1px solid;
}
.row
{
border: 1px solid;
}

Jquery filter function applied on td element which has class name “right”. this function find index of ‘-‘ and if it on first place it will return true. In case of true, css class “negative” will be applied to td tag.

we can also use contains functions to applied color.

$("td.right"):contains('-').addClass("negative");

Problem with this approach is it will apply negative class to any value which contains ‘-‘ on any place.

You can see live example at JFiddle.

http://jsfiddle.net/neerajkaushik_123/AwCqz/3/

Default Application Domains in CLR

 
When CLR loads first to execute managed code, it creates 3 application domains.  System and Shared domains are singleton for each CLR.

 

Application Domains

 

System Domain

    • It creates and Shared and default application Domains.
    • It Loads Mscorlib.dll (class library for system namespace) into shared domain.
    • Creation of default Exception instances (OutOfMemoryException, StackOverFlowException and FatalExecutionException): the reason to create these exceptions in advance is that in case of above exceptions to be raise system needs to creates instance and memory, but say if it is outofmemory exception then there will be no space to instantiate exception instance.
    • It keeps track all the domains in the process and manage them by loading or unloading AppDomains.

    Shared Domain

     

    • It contains mscorlib.dll
    • It contains basic system types like class, array, struct etc.

    Default Domain

     

  • It contains user code and resources for only this app domain.
  • Name of default domain is name of executable.

 

 

Reference: http://msdn.microsoft.com/en-us/magazine/cc163791.aspx#S1

Programming languages and contribution of Mathematics : An overview

Application of Discrete Mathematics in Computer Science

Discrete Mathematics play a vital role in software Development in Computer science . Almost ever program is based on some Mathematical Model . To explore the application of Discrete mathematics in computer science let us begin from the evolution of computer languages and how mathematics played important role in the evolution of computer languages .

Programming languages and contribution of Mathematics : An overview

Here, we will not explore the history of programming languages in detail but we will only focus on those parts which are concerned and connected to our topic. Computers are electronic devices which are made of registers ,logics & gates etc. All the electronic devices can only understand the ON and OFF logic. Mathematics fertilized logic with the mathematical logic operations and number system. Despite the complex hardware the computer systems operates on a very rudimentary level .10 symbol number system(0,1,2,…9) is too complicated for the computer system to process. At the logic level a computer can understand only two states i.e. ON logic and OFF logic. The ON state is represented by Binary 1 and OFF state is represented by Binary 0.In modern computers the most basic unit of information is a BIT (0 or 1).A processor reads some sequence of Bits and then Performs some binary operation on them. The information which is processed by processor generates some output. There are lot of abstractions involved the whole process.(abstractions like conversion of user input to binary information, than processing the information in the processor and then again converting the processor’s binary output to human readable format).

All the programming languages which are developed till now are Greatly influenced by Mathematics. we can say that the evolution of computer and programming is made possible only due to Mathematics and Mathematical Logic involved in it . Mathematical logic is a subfield of mathematics with close connections to the foundations of mathematics and theoretical computer Science .each and every assignment statements, subroutines, conditional statements, iteration, etc everything follows some mathematical Logic which is rooted from low level binary logic to high level computational logic.

The history of  Binary number system also known as BASE-2-NUMBER is very interesting and worth reading. evidence proves that binary numbers were used in India prior to around 5th–2nd centuries, more than 1500 years before their discovery in the west. The source of this discovery is a text of music by Pingala named “Chhandahshastra” meaning science of meters. Pingala developed mathematical concepts for describing prosody(poem), and in doing so he presented the first known description of a binary number system. The modern Binary numbers were discovered in the west by German mathematician Gottfried Leibniz in 1695 (Suggested Reading Leibniz G., Explication de l’Arithmétique Binaire, Die Mathematische Schriften, ed. C. Gerhardt, Berlin 1879, vol.7, p.223;). Let’s not Deviate focus from our topic .

In 1940’s with the invention of first electronic computer ,the first recognizable programming languages came into existence which is basically a low level assemble language. The Limited speed and limited memory capacity forced Programmers to write hand tuned assemble language programs. These assembly languages includes are very complex instructions. Even the programming of simple task is difficult and complex in assemble languages. These languages generally have binary information which needs to be converted into hex decimal codes to make it more Human Readable.

Between 1943-1945 Konrad Zuse proposed a programming language Plankalkül which is described for engineering purposes. It was the first high-level non-von Neumann programming language to be designed for a computer, Plankalkül was not published at that time due to the circumstances(2nd world war). In 1948 Zuse published a paper about the Plankalkül in the “Archiv der Mathematik” but still did not attract much feedback (Paper : for a long time to come programming a computer would only be thought of as programming with machine code). Plankalkül has drawn comparisons to APL(name after the book : A Programming Language) and relational algebra. It includes assignment statements, subroutines, conditional statements, iteration, floating point arithmetic, arrays, hierarchical record structures, assertions, exception handling, and other advanced features such as goal-directed execution. Plankalkül used Logical notation which was proposed in Begriffsschrift ( “concept-script”) a book on logic by Gottlob Frege, published in 1879. The Begriffsschrift was arguably the most important publication in logic since Aristotle founded the subject. The work of Plankalkül was influenced by Heinz Rutishauser. Heinz Rutishauser (30 January 1918 in Weinfelden, Switzerland – 10 November 1970 in Zürich) was a Swiss mathematician and a pioneer of modern numerical mathematics and computer science.

Between 1950 to 1957 lot of different programming languages like ARITH-MATIC, MATH-MATIC, MATRIX_MATH, FLOW-MATIC, etc are proposed .( Check the programming language timeline at http://en.wikipedia.org/wiki/Timeline_of_programming_languages)

In 1957 Fortran-I was introduced which is developed by IBM at their campus in south San Jose, California for scientific and engineering applications. In 1954-1955 FORTRAN concept was introduced. The Name FORTRAN is derived from The IBM Mathematical Formula Translating System. It is one of the most popular languages in the area of high-performance computing  and is the language used for programs that benchmark and rank the world’s fastest supercomputers. A significant improvement in programming languages and new programming languages which came overtime put more abstractions on internal design of programming and the mathematics involved in it.

Later in  1957 Noam Chomsky published a book Syntactic Structures  in which he developed the idea that each sentence in a language has two levels of representation — a deep structure and a surface structure. In 1959 Noam Chomsky introduced the mathematical Structure of a Grammar. This mathematical structure seeded all the modern programming languages.

In the summary written by ROBERT W. FLOYD in his paper The Syntax of Programming Languages-A Survey says

The syntactic rules for many programming languages have been expressed by formal grammars, generally variants of phrase-structure grammars. The syntactic analysis essential to translation of programming languages can be done entirely mechanically for such languages. Major problems remain in rendering analyzers efficient in use of space and time and in finding fully satisfactory formal grammars for present and future programming languages. “

References and Suggested Readings :

  1. http://home.ica.net/~roymanju/Binary.htm
  2. http://www.leibniz-translations.com/binary.htm
  3. http://en.wikipedia.org/wiki/Begriffsschrift
  4. http://en.wikipedia.org/wiki/Plankalk%C3%BCl
  5. http://www.zib.de/zuse/Inhalt/Programme/Plankalkuel/Plankalkuel-Report/Plankalkuel-Report.htm
  6. http://en.wikipedia.org/wiki/Heinz_Rutishauser
  7. http://www.jsoftware.com/papers/APL.htm
  8. http://www.win.tue.nl/~mvdbrand/courses/seminar/0809/papers/Floyd.pdf
  9. http://en.wikipedia.org/wiki/Fortran
  10. http://en.wikipedia.org/wiki/Timeline_of_programming_languages
  11. http://www.tutorialsdownload.com/application-of-discrete-mathematics-in-computer-science/

Setup connection on SSL with FIX server

 

Some broker support SSL to send message to FIX server. QuickFix engine does not have support for SSL. Some commercial QuickFix engine support it eg. OnixS ultra low latency .NET FIX Engine.

 

Here I am give example how we can connect to FIX server on SSL.

We can install third party network proxy software which can route calls on SSL. We can  Stunnel program to support it. It can be used to add SSL functionality to commonly used in daemons like POP2, POP3, and IMAP servers without any changes in the programs’ code.

QuickFIX Configuration in Stunnel

Add following entry to Stunel.conf config file to connect with FIX Server

;it can be any name

[FIXSERVER]
client = yes
accept = 127.0.0.1:3500
connect=<FIXSERVER Address>: <PORT>

 

image

 

You can change listening port to any free port on local machine.

 

Change in QuickFix config File

[default]
PersistMessages=Y
ConnectionType=initiator
UseDataDictionary=Y

[SESSION]
ConnectionType=initiator
FileStorePath=store
FileLogPath=fixlog
BeginString=FIXT.1.1
DefaultApplVerID=FIX.5.0

TransportDataDictionary=FIXT.1.1.xml

AppDataDictionary=FIX50.xml
SenderCompID=ABC
TargetCompID=FIXSERVER
SocketConnectHost=127.0.0.1
SocketConnectPort=3500
HeartBtInt=20
ReconnectInterval=30
ResetOnLogon=Y
ResetOnLogout=Y
ResetOnDisconnect=Y

 

image

 

I hope, it will help you.

Implementation of FIX messages for Fix 5.0 sp2 and FIXT1.1 specification

 

This post will demonstrate how to connect with FIX5.0 server and FIXT1.1 specification and uses of QuickFix/n (native .net FIX engine).

Introduction

With this release of FIX protocol version 5.0, a new Transport Independence framework (TI) introduced which separates the FIX Session Protocol from the FIX Application Protocol. This gives freedom to send message across different messaging technologies like MSMQ, message bus etc.

Because of different versions of transport and application protocol, we will have to explicitly defines settings in config file.

TransportDataDictionary is used for defining transport protocol version eg. FIXT.1.1.xml

AppDataDictionary is used for defining data dictionary for FIX application protocol version eg. FIX50.xml

You can read more about FIXT1.1 and FIX 5.0 Sp2 specification on fixprotocol.org.

http://fixprotocol.org/specifications/FIXT.1.1

http://fixprotocol.org/specifications/FIX.5.0

 

QuickFix/N

To demonstrate implementation of FIX 5.0 sp2, I’ll use open source FIX engine for .net (QuickFix/N) which is one of open source engine in native .net code.  Code for quickfix.net is available on github and primarily contributed by Connamara System’s developers. These guys are doing commendable job.

 

Implementation

FixInitiator

This is client application which will connect with FIX server to send and receive FIX messages. I am demonstrating implementation of MarketDataRequest and its responses (MarketDataSnapshot & MarketDataIncrementalRefresh).

 

Start with Configuration

First we create configuration file for initiator.

[default]
PersistMessages=Y
ConnectionType=initiator
UseDataDictionary=Y

[SESSION]
ConnectionType=initiator
FileStorePath=store
FileLogPath=fixlog
BeginString=FIXT.1.1
DefaultApplVerID=FIX.5.0

TransportDataDictionary=FIXT.1.1.xml

AppDataDictionary=FIX50.xml
SenderCompID=ABC
TargetCompID=FIXSERVER
SocketConnectHost=127.0.0.1
SocketConnectPort=3500
HeartBtInt=20
ReconnectInterval=30
ResetOnLogon=Y
ResetOnLogout=Y
ResetOnDisconnect=Y

 

*Note- AppDataDictionary is for application protocol eg. FIX 5.0 and TransportDataDictionary is for transport protocol.

You can read more about configuration here.

Create Application Class

Before starting with implementation you would need to have quickFix.dll which is available of github  athttps://github.com/connamara/quickfixn

 

To connect with FIX session, you will have to implement QuickFix.Application interface.

public interface Application
{
void FromAdmin(Message message, SessionID sessionID);
void FromApp(Message message, SessionID sessionID);
void OnCreate(SessionID sessionID);
void OnLogon(SessionID sessionID);
void OnLogout(SessionID sessionID);
void ToAdmin(Message message, SessionID sessionID);
void ToApp(Message message, SessionID sessionId);
}

I create one class named FixClient50Sp2 which implements interface and inherit base class for message cracking and getting message events.

image

 

FIX Application Setup

 

Setting up Initiator

// FIX app settings and related
var settings = new SessionSettings(“C:\initiator.cfg”);

// FIX application setup
MessageStoreFactory storeFactory = new FileStoreFactory(settings);
LogFactory logFactory = new FileLogFactory(settings);
_client = new FixClient50Sp2(settings);

IInitiator initiator = new SocketInitiator(_client, storeFactory, settings, logFactory);
_client.Initiator = initiator;

 

* _client is instance of class FixClient50Sp2.

Starting Initiator

_client.Start();

 

Implementation of QuickFix.Application Interface methods

/// <summary>
/// every inbound admin level message will pass through this method,
/// such as heartbeats, logons, and logouts.
/// </summary>
/// <param name=”message”></param>
/// <param name=”sessionId”></param>
public void FromAdmin(Message message, SessionID sessionId)
{
Log(message.ToString());
}

/// <summary>
/// every inbound application level message will pass through this method,
/// such as orders, executions, secutiry definitions, and market data.
/// </summary>
/// <param name=”message”></param>
/// <param name=”sessionID”></param>
public void FromApp(Message message, SessionID sessionID)
{
Trace.WriteLine(“## FromApp: ” + message);

Crack(message, sessionID);
}

/// <summary>
/// this method is called whenever a new session is created.
/// </summary>
/// <param name=”sessionID”></param>
public void OnCreate(SessionID sessionID)
{
if (OnProgress != null)
Log(string.Format(“Session {0} created”, sessionID));
}

/// <summary>
/// notifies when a successful logon has completed.
/// </summary>
/// <param name=”sessionID”></param>
public void OnLogon(SessionID sessionID)
{
ActiveSessionId = sessionID;
Trace.WriteLine(String.Format(“==OnLogon: {0}==”, ActiveSessionId));

if (LogonEvent != null)
LogonEvent();
}

/// <summary>
/// notifies when a session is offline – either from
/// an exchange of logout messages or network connectivity loss.
/// </summary>
/// <param name=”sessionID”></param>
public void OnLogout(SessionID sessionID)
{
// not sure how ActiveSessionID could ever be null, but it happened.
string a = (ActiveSessionId == null) ? “null” : ActiveSessionId.ToString();
Trace.WriteLine(String.Format(“==OnLogout: {0}==”, a));

if (LogoutEvent != null)
LogoutEvent();
}

/// <summary>
/// all outbound admin level messages pass through this callback.
/// </summary>
/// <param name=”message”></param>
/// <param name=”sessionID”></param>
public void ToAdmin(Message message, SessionID sessionID)
{
Log(“To Admin : ” + message);
}

/// <summary>
/// all outbound application level messages pass through this callback before they are sent.
/// If a tag needs to be added to every outgoing message, this is a good place to do that.
/// </summary>
/// <param name=”message”></param>
/// <param name=”sessionId”></param>
public void ToApp(Message message, SessionID sessionId)
{
Log(“To App : ” + message);
}

 

 

Callback to Subscription

public void OnMessage(MarketDataIncrementalRefresh message, SessionID session)
{
var noMdEntries = message.NoMDEntries;
var listOfMdEntries = noMdEntries.getValue();
//message.GetGroup(1, noMdEntries);
var group = new MarketDataIncrementalRefresh.NoMDEntriesGroup();

Group gr = message.GetGroup(1, group);

string sym = message.MDReqID.getValue();

var price = new MarketPrice();

for (int i = 1; i <= listOfMdEntries; i++)
{
group = (MarketDataIncrementalRefresh.NoMDEntriesGroup)message.GetGroup(i, group);

price.Symbol = group.Symbol.getValue();

MDEntryType mdentrytype = group.MDEntryType;

if (mdentrytype.getValue() == ‘0’) //bid
{
decimal px = group.MDEntryPx.getValue();
price.Bid = px;
}
else if (mdentrytype.getValue() == ‘1’) //offer
{
decimal px = group.MDEntryPx.getValue();
price.Offer = px;
}

price.Date = Constants.AdjustedCurrentUTCDate.ToString(“yyyyMMdd”);
price.Time = group.MDEntryTime.ToString();
}

if (OnMarketDataIncrementalRefresh != null)
{
OnMarketDataIncrementalRefresh(price);
}
}

 

Code can be found at github.

 

https://github.com/neerajkaushik123/Fix50Sp2SampleApp.git

Arithmetic Expression Calculator

 

This post describes implementation of arithmetic expression calculator in C#.

I have below specification in the form of Backus–Naur Form expression. It is mainly used to define syntax of languages in compiler design world. You can read more about it on wikipedia.

There are certain readymade solutions are available to calculate expressions.  Microsoft also introduced Expression tree through which we can compile complex expressions using Lambda expressions

Basically this expression is defining arithmetic calculation with some validations. Following defines list of validations and rule.

  • Expression should contain arithmetic operations and operands only.
  • Expression can only have +, -, * operators.
  • Numbers can be negative or positive.
  • Decimals numbers can have “.” as decimal.
  • Digits should be non zero.
    Expression Example

cmd::= expression* signed_decimal

expresion::= signed_decimal ‘ ‘* operator ‘ ‘* eg. 2.3 * + * 2.3

operator::= ‘+’ | ‘-‘ | ‘*’

signed_decimal::= ‘-‘? decimal_number

decimal_number::= digits | digits ‘.’ digits

digits::= ‘0’ | non_zero_digit digits*

non_zero_digit::= ‘1’|’2’|’3’|’4’|’5’|’6’|’7’|’8’|’9′

Let’s start Implementation

Here I’ll take simple approach to evaluate this expression without use of .Net Expression Tree with possible failure and pass test cases. I tried to put relevant comments in code itself to make it self explanatory.

 

using System;
using System.Linq;
using NUnit.Framework;

namespace Calculator
{
    /// 
    /// Basic calc engine for handling +, -, * operations.
    /// 
    public class CalcEngine
    {

        char[] operatorList = new char[3] { '+', '-', '*' };

        /// 
        /// Calculate arithmatic Expression
        /// 
        /// 
        /// 
        public decimal Calculate(string expression)
        {
            foreach (var oper in operatorList)
            {
                if (expression.Contains(oper))
                {
                    var parts = expression.Split(oper);
                    int i = 1;
                    decimal result = Calculate(parts[0].Trim());

                    while (i < parts.Length)
                    {
                        switch (oper)
                        {
                            case '+':
                                result += Calculate(parts[i].Trim());
                                break;
                            case '-':
                                result -= Calculate(parts[i].Trim());
                                break;
                            case '*':
                                result *= Calculate(parts[i].Trim());
                                break;
                        }
                        i++;
                    }
                    return result;
                }

            }
            decimal value = 0;
   //Note: we can also use decimal.Parse and can catch exception in catch block 
   but it is expensive task to wait for system exception
   //better to use TryParse and then throw custom exception
   if (expression.Trim().Length > 0 && 
         !decimal.TryParse(expression, System.Globalization.NumberStyles.Float, 
	  System.Globalization.CultureInfo.InvariantCulture, out value))
            {
                throw new FormatException("Expression is wrong! 
			Please removed un-allowed charactersn 
			please follow following validations:n" +
                    "Expression should contain arithmetic operations
			 and operands only n" +
                    "Expression can only have +, -, * operators n" +
                    "Numbers can be negative or positive n" +
                    ". as decimal point");
            }

            return value;
        }

    }

Unit Tests

[TestFixture]
    public class CalcEngineTest
    {
        CalcEngine engine;

        [Setup]
        public void Setup()
        {
            engine = new CalcEngine();
        }

        [Test]
        [ExpectedException(typeof(FormatException))]
        public void TestValidationFailur_Nondigit()
        {
            Assert.AreEqual(-14, engine.Calculate("1+2+3-4d"));
        }

        [Test]
        [ExpectedException(typeof(FormatException))]
        public void TestValidationFailure_NonDecimalNotation()
        {
            Assert.AreEqual(0, engine.Calculate("1+2+3,4"));
        }

        [Test]
        public void TestBlankStringShouldZero()
        {
            Assert.AreEqual(0, engine.Calculate(" "));


        }


        [Test]
        public void TestMultiplication()
        {
            Assert.AreEqual(60, engine.Calculate("5*6*2"));

            Assert.AreEqual(469.929812m, engine.Calculate("5.5*2.456*34.789"));
        }

        [Test]
        public void TestSubtraction()
        {

            Assert.AreEqual(45, engine.Calculate("100-35-20"));

            Assert.AreEqual(469.929812m, engine.Calculate("5.5*2.456*34.789"));
        }


    [Test]
    public void TestSummation()
    {
    CalcEngine engine = new CalcEngine();
    Assert.AreEqual(10, engine.Calculate("1+2+3+4"));
    Assert.AreEqual(20.20m, engine.Calculate("1.4+4.5+8.90+5.4"));
    Assert.AreEqual(20 + 20, engine.Calculate("20+20"));
    }

 [Test]
 public void TestAdd_Multiply_Subtraction()
 {
  Assert.AreEqual(-5563.4541m, 
		engine.Calculate("-2*4.5+3+30-20*278.9+30.5459-40"));
  Assert.AreEqual(-5563.4541m, 
		engine.Calculate("-2*4.5+3+30-20*278.9+30.5459-40"));
 }
    [Test]
    public void TestDecimalNumberWithoutOperators()
    {
       Assert.AreEqual(4.5m, engine.Calculate("4.5"));
     }
  }
}

Seasoned IT Professional contributing in designing and developing solutions