Musings

A random collection

Archive for February 2011

.NET: C# to Excel

Example of adding a row to an Excel Spreadsheet with C#

public static bool AddARow(string filename, Fields values)
{
	bool success = false;
	Microsoft.Office.Interop.Excel.Workbook workbook = null;
	try
	{
		Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
		excelApp.Interactive = false;
		excelApp.Visible = false;
		excelApp.UserControl = false;
		excelApp.DisplayAlerts = false;
		excelApp.ScreenUpdating = false;

		workbook = excelApp.Workbooks.Open(
				filename,
				Type.Missing,               /* UpdateLinks */
				Type.Missing,               /* ReadOnly    */
				Type.Missing,               /* Format    */
				Type.Missing,               /* Password    */
				Type.Missing,               /* WriteResPassword    */
				Type.Missing,               /* IgnoreReadOnlyRecommended    */
				Type.Missing,               /* Origin    */
				Type.Missing,               /* Delimiter    */
				Type.Missing,               /* Editable    */
				Type.Missing,               /* Notify    */
				Type.Missing,               /* Converter    */
				Type.Missing,               /* AddToMru    */
				Type.Missing,               /* Local    */
				Type.Missing                /* CorruptLoad    */
			);

		int numSheets = workbook.Sheets.Count;
		Microsoft.Office.Interop.Excel.Worksheet worksheet = workbook.Sheets[1];

		Microsoft.Office.Interop.Excel.Range range = worksheet.UsedRange;
		int x = range.Rows.Count;

		System.Console.WriteLine(x);

		string id = range.get_Range("A"+x).Text;
		
		int newId = int.Parse(id)+1;
		++x;

		Microsoft.Office.Interop.Excel.Range r = worksheet.get_Range("A"+x + ":X" + x);
		object[] excelValues = {
				newId,
			};
		r.Value2 = excelValues;

		if (!workbook.Saved)
		{
			workbook.SaveAs(filename);
		}
		workbook.Close();
			// object SaveChanges = Type.Missing,
			// object Filename = Type.Missing,
			// object RouteWorkbook = Type.Missing

		success = true;
	}
	catch (System.Exception e)
	{
		Console.WriteLine("Error: " + e.Message);
	}
	finally
	{
		if (workbook != null)
			System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
	}

	return success;
}

Written by curious

February 11, 2011 at 4:14 pm

Posted in dotNET

QUANT: Important Stochastic Processes

Geometric Brownian Motion

  1. It’s SDE is:
      dY_t = \mu Y_t dt + \sigma Y_t dW_t

    or also as

      d \log (Y_t) = \left(\mu -\sigma^2/2\right) dt + \sigma dW_t
  2. Solution: Y_T = Y_0 e^{(\mu-\sigma^2/2)T+\sigma W_T}

Ornstein-Uhlenbeck Process (aka Vasicek Model)

  1. It is a Mean-Reverting Process, although random, shows a pronounced tendency toward an equilibrium value.
  2. It’s SDE is:
      dY_t = -\alpha(Y_t - \mu) dt + \sigma dW_t

    \mu – long term mean/equilibrium value/mean value
    \sigma – volatility to model random shocks
    \alpha – rate by which the shocks dissipate and variable returns towards equilibrium mean

  3. Solution for this SDE is:
      Y_t = Y_0 e^{-\alpha t} + \sigma e^{-\alpha t} \int_0^t e^{\alpha s} dW_s
  4. Useful for modeling interest rates, currency exchange rates, and commodity prices

Bessel Process

  1. Stochastic Differential Equation:
      dX_t = dW_t + \frac{a}{X_t}dt

Written by curious

February 8, 2011 at 12:59 pm

Posted in quant-finance