Sunday, September 27, 2009

Develop WM Smathphone/Pocket PC App for T-Mobile Dash

Setup Device:

  • Download regEditSTG to edit policy on Dash, HKLM\Security\Policies\Policies\ 00001001 =1 00001005 =40 (from 16)
  • Download SDA_ApplicationUnlock and run
  • run C:\Program Files\Windows Mobile 5.0 SDK R2\Tools\SdkCerts.cab, RapiConfig

VS 2008 Target Platform Pocket PC and Smartphone both works fine, Deploy Solution. Also Tool->device Security Manager would do similar things above but do double check registry manually.

Wednesday, August 12, 2009

Blackberry SpreadSheet does not support Freeze Pane in most cases

For sending Fixed Income OpenXML BreakReport to BlackBerry, It seems that most Blackberry default software does not support "Freeze Pane". The build-in GridMagic (MiniGrid) lacks of this feature but standard+ edition does
http://www.simprit.com/gridmagic/product_comparison.html

Thursday, July 30, 2009

Script related to SQL CLR eviction research

USE master;
GO
EXEC sp_configure 'show advanced option', '1';
RECONFIGURE

Then use
EXEC sp_configure;
to display those options

If you wanted, for example, to set min server memory to 300 use
EXEC sp_configure 'min server memory (MB)', '300';
RECONFIGURE WITH OVERRIDE;

select * from sys.dm_clr_properties


sys.xp_readerrorlog 0,1,2,3...


;WITH VAS_Summary AS

(

SELECT

Size = VAS_Dump.Size,

Reserved = SUM(CASE(CONVERT(INT, VAS_Dump.Base)^0) WHEN 0 THEN 0 ELSE 1 END),

Free = SUM(CASE(CONVERT(INT, VAS_Dump.Base)^0) WHEN 0 THEN 1 ELSE 0 END)

FROM

(

SELECT CONVERT(VARBINARY, SUM(region_size_in_bytes)) [Size],

region_allocation_base_address [Base]

FROM sys.dm_os_virtual_address_dump

WHERE region_allocation_base_address <> 0x0

GROUP BY region_allocation_base_address

UNION

SELECT CONVERT(VARBINARY, region_size_in_bytes) [Size],

region_allocation_base_address [Base]

FROM sys.dm_os_virtual_address_dump

WHERE region_allocation_base_address = 0x0

)

AS VAS_Dump

GROUP BY Size

)

SELECT SUM(CONVERT(BIGINT,Size)*Free)/1024 AS [Total avail mem, KB],

CAST(MAX(Size) AS BIGINT)/1024 AS [Max free size, KB]

FROM VAS_Summary

WHERE Free <> 0

Wednesday, July 29, 2009

SWAP investigation SQL

-- from order to sec_id
select * from ts_order where order_id=1700007815
select 'Parent', SEC_ID, SEC_NAME,* from csm_security where sec_id=1700007812

-- from parent sec_id to leg sec_id
select 'Legs',SEC_ID, SEC_NAME,* from csm_security where parent_sec_id = 1700007812

-- from sec_id to underlying
select 'uPL1L2',* from CRDQT..CSM_UNDERLYING_SECURITY where sec_id in (1700007812,1700007813,1700007814)
select 'up', * from CSM_SECURITY where sec_id=null
select 'u1l', * from CSM_SECURITY where sec_id=null
select 'u2l', * from CSM_SECURITY where sec_id=null

-- from sec_id to orig-sec_id
select 'cust',ORIG_SEC_ID,SEC_ID,* from CSM_SECURITY_CUST where sec_id in (1700007812,1700007813,1700007814)
select 'custP',sec_id, sec_name,* from csm_security where sec_id=null
select 'custLeg1',sec_id, sec_name,sec_typ_Cd, * from csm_security where sec_id=null
select 'custLeg2' sec_id, sec_name,sec_typ_Cd,* from csm_security where sec_id=1700002237

-- list order udf
select * from CRDQT..ts_order_sec_spec where order_id=1700007815

Saturday, May 9, 2009

Excel Converter 2003 XML to OpenXML


Vendors like RIMM Black Berry can render OpenXML correctly in its native. The following are code to read Excel 2003 XML spread sheet and construct OpenXML:

using System;
using System.Collections.Generic;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using System.Xml;
using System.Data;
using System.Collections;
using System.Xml.XPath;

namespace JQD.OpenXMLConverter
{
public class ExcelConverter
{
#region Properties and Fields

string _FilePathExcelXML;
string _FilePathExcelOpenXMLTemplate;
string _FilePathExcelOpenXML;
string _DirectoryOpenXMLZipFiles;
Dictionary<string, int> _SharedStrings = new Dictionary<string, int>();
int _NonUniqueStringsCount = 0;
public string FilePathExcelXML
{
get { return _FilePathExcelXML; }
set { _FilePathExcelXML = value; }
}
public string FilePathExcelOpenXMLTemplate
{
get { return _FilePathExcelOpenXMLTemplate; }
set { _FilePathExcelOpenXMLTemplate = value; }
}
public string FilePathExcelOpenXML
{
get { return _FilePathExcelOpenXML; }
set { _FilePathExcelOpenXML = value; }
}

public string DirectoryOpenXMLZipFiles
{
get { return _DirectoryOpenXMLZipFiles; }
set { _DirectoryOpenXMLZipFiles = value; }
}

#endregion

#region Methods

public void ConvertToOpenXml()
{
StringReader strR;
using (StreamReader sr = new StreamReader(FilePathExcelXML))
{
strR = new StringReader(sr.ReadToEnd().Replace("x:", "")); // due to XML exception
}

using (XmlTextReader r = new XmlTextReader(strR))
{
int sheetNumber = 1;
for (r.MoveToContent(); r.Read(); )
{
if (r.Name == "Worksheet" && r.IsStartElement())
{
MapWorksheetToOpenXmlSheet(r.ReadOuterXml(), sheetNumber);
sheetNumber++;
}
}
}

WriteSharedString();
}

private void WriteSharedString()
{
using (XmlTextWriter writer = new XmlTextWriter(DirectoryOpenXMLZipFiles + @"\xl\sharedStrings.xml", Encoding.UTF8))
{
writer.WriteStartDocument(true);

writer.WriteStartElement("sst");
writer.WriteAttributeString("xmlns", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
writer.WriteAttributeString("count", _NonUniqueStringsCount.ToString());
writer.WriteAttributeString("uniqueCount", _SharedStrings.Count.ToString());

foreach (string str in _SharedStrings.Keys )
{
writer.WriteStartElement("si");
writer.WriteElementString("t", str);
writer.WriteEndElement();
}

writer.WriteEndElement();
}
}

private void MapWorksheetToOpenXmlSheet(string xmlString,int sheetNumber)
{
List<List<string>> RowOfCellList = new List<List<string>>();
StringReader strR = new StringReader(xmlString);
using (XmlTextReader r = new XmlTextReader(strR))
{
for (r.MoveToContent(); r.Read(); )
{
string s1;

if (r.Name == "Row" ) // very strange has to work this way.
{
while ((s1 = r.ReadOuterXml()).StartsWith("<Row"))
{
RowOfCellList.Add(GenerateCellList(s1));
}
}
}
}
AddToSharedString(RowOfCellList);
WriteWorksheet(DirectoryOpenXMLZipFiles + @"\xl\worksheets\sheet"+sheetNumber.ToString()+".xml", RowOfCellList);
}

private void AddToSharedString(List<List<string>> RowOfCellList)
{

for (int row = 0; row < RowOfCellList.Count; row++)
{
for (int column = 0; column < RowOfCellList[row].Count; column++)
{
_NonUniqueStringsCount++;
if (!_SharedStrings.ContainsKey(RowOfCellList[row][column]))
_SharedStrings.Add(RowOfCellList[row][column], _SharedStrings.Count); // 0 based
}
}
}

private List GenerateCellList(string cellString)
{
List<string> CellList = new List<string>();
StringReader strR = new StringReader(cellString);
using (XmlTextReader r = new XmlTextReader(strR))
{
int prevCellIndex = 0;
for (r.MoveToContent(); r.Read(); )
{
if (r.Name == "Cell" && r.IsStartElement())
{
int currCellIndex=Convert.ToInt32( r.GetAttribute("ss:Index"));
for (int i = 0; i < currCellIndex - prevCellIndex - 1; i++) CellList.Add(string.Empty); // could have skipped empty cell by missing a cell element
prevCellIndex=currCellIndex;
CellList.Add(ExtractData(r.ReadOuterXml()));
}
}
}
return CellList;
}

private string ExtractData(string xmlData)
{
// "<Cell ss:Index=\"1\" ss:StyleID=\"BoldFace\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\">
// <Data ss:Type=\"String\">Break</Data></Cell>"
StringReader strR = new StringReader(xmlData);
string ret;
using (XmlTextReader r = new XmlTextReader(strR))
{
ret = r.ReadElementString();
}
strR.Close();
return ret;
}

public void WriteWorksheet(string sheetXmlPath, List<List<string>> RowCellList)
{
using (XmlTextWriter writer = new XmlTextWriter(sheetXmlPath, Encoding.UTF8))
{
int colCountMax = 0;
foreach (List<string> cL in RowCellList) if (cL.Count > colCountMax) colCountMax = cL.Count;

writer.WriteStartDocument(true);
writer.WriteStartElement("worksheet");
writer.WriteAttributeString("xmlns", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
writer.WriteAttributeString("xmlns:r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");

writer.WriteStartElement("dimension");
string lastCell;
if (colCountMax == 0) lastCell = RowColumnToPosition(0, 0); //A1;
else
lastCell = RowColumnToPosition(RowCellList.Count - 1, colCountMax-1);
writer.WriteAttributeString("ref", "A1:" + lastCell);
writer.WriteEndElement();

writer.WriteStartElement("sheetViews");
writer.WriteStartElement("sheetView");
writer.WriteAttributeString("tabSelected", "1");
writer.WriteAttributeString("workbookViewId", "0");
writer.WriteEndElement();
writer.WriteEndElement();

writer.WriteStartElement("sheetFormatPr");
writer.WriteAttributeString("defaultRowHeight", "15");
writer.WriteEndElement();
//writer.WriteStartElement("cols");
//for (int i = 0; i < colCountMax; i++)
//{
// writer.WriteStartElement("col");
// //writer.WriteAttributeString("min", "11");
// //writer.WriteAttributeString("max", "20");
// //writer.WriteAttributeString("width", "11.20");
// //writer.WriteAttributeString("bestFit", "1");
// //writer.WriteAttributeString("customWidth", "1");
// writer.WriteEndElement();
//}
//writer.WriteEndElement();

writer.WriteStartElement("sheetData");
WritesheetData(writer, RowCellList);
writer.WriteEndElement();

writer.WriteStartElement("pageMargins");
writer.WriteAttributeString("left", "0.7");
writer.WriteAttributeString("right", "0.7");
writer.WriteAttributeString("top", "0.75");
writer.WriteAttributeString("bottom", "0.75");
writer.WriteAttributeString("header", "0.3");
writer.WriteAttributeString("footer", "0.3");
writer.WriteEndElement();

writer.WriteEndElement();
}
}


public void WritesheetData(XmlTextWriter writer, List<List<string>> RowOfCellList)
{

for (int row = 0; row < RowOfCellList.Count; row++)
{
writer.WriteStartElement("row");
writer.WriteAttributeString("r", (row+1).ToString());
writer.WriteAttributeString("spans", "1:" + RowOfCellList[row].Count.ToString());

for (int column = 0; column < RowOfCellList[row].Count ; column++)
{
writer.WriteStartElement("c");
writer.WriteAttributeString("r", RowColumnToPosition(row, column));

string key,val;
writer.WriteAttributeString("t", "s");
key = RowOfCellList[row][column];
val = _SharedStrings[key].ToString();

writer.WriteElementString("v", val.ToString());
writer.WriteEndElement();
}

writer.WriteEndElement();
}
}

public void CreateBaseOpenXMLFilesFromTemplate()
{
// clean up
DirectoryInfo di = new DirectoryInfo(_DirectoryOpenXMLZipFiles);
foreach (FileInfo fi in di.GetFiles()) fi.Delete();
foreach (DirectoryInfo sdi in di.GetDirectories()) sdi.Delete(true);

// extract Excel Template into the directory
FastZip fz = new FastZip();
fz.ExtractZip(_FilePathExcelOpenXMLTemplate,_DirectoryOpenXMLZipFiles,null);
}

public void CreateOpenXMLExcelFile()
{
FastZip fz = new FastZip();
fz.CreateZip(_FilePathExcelOpenXML, _DirectoryOpenXMLZipFiles, true, null);
}

#endregion

#region Helper methods

string ColumnIndexToName(int columnIndex)
{
char second = (char)(((int)'A') + columnIndex % 26);

columnIndex /= 26;

if (columnIndex == 0)
return second.ToString();
else
return ((char)(((int)'A') - 1 + columnIndex)).ToString() + second.ToString();
}

string RowIndexToName(int rowIndex)
{
return (rowIndex + 1).ToString();
}

string RowColumnToPosition(int row, int column)
{
return ColumnIndexToName(column) + RowIndexToName(row);
}

#endregion
}
}


Sunday, April 5, 2009

Digital Call Option value = exp(-rt)N(d2)


Black Scholes says C= SN(d1) - Xexp(-rt)N(d2) = exp(-rt){SN(d1)exp(rt)-XN(d2)},
So for risk-neutral, N(d2)= Pr(Price end up above Strike).

Digital Call = Pay M if expired above X. So Digital Call Value =exp(-rt)MN(d2).

In general with Yield,

C=exp(Y-r)tMN(d2)
P=exp(Y-r)tMN(-d2)

Forward Start Call Option = Straight Call Option


This only be true when all of the following are true:
(1) Forward Start is designed to be at-money-call.
(2) Interest Rate and Volatility are constant.

Using Black-Scholes Model, give Forward Start time periods [t1,T1] and calls
are at the money at t=0 and t=t1, we would have call value c, c1 both are proportional to stock price S, S1. Therefore c1= c(S1/S).
Under risk-nerual, S1= Random variable but E[S1]*exp(-rt1]=S or E[S1]=exp(rt1]S
==> value of forward start call at t=0 = exp(-rt1)E[c1]=exp(-rt1)cE[S1]/S=c

That is V-ForwardStart Call(t=0) = C expiring in T1-t1 days.

To really make interest rate and Volality constant, we will need to enter IR swap and Volatility Swap (Pay floating, receiv fixed)