понедельник, 9 июня 2014 г.

Фабрика работающая в отдельном потоке

///
/// Поток независимый от пула потоков для скорости и независимости от алгоримов управления пулом
///

public abstract class ThreadTickObserver : IObserver, IDisposable
{

protected bool finishTickWorker = false;
protected Task tickWorker;

protected ManualResetEventSlim _newTickEvent;
protected ConcurrentQueue _TickQueue;

protected int tickCount = 0;

public ThreadTickObserver()
{
_TickQueue = new ConcurrentQueue();

_newTickEvent = new ManualResetEventSlim();

tickWorker = new Task(TaskFunc);
tickWorker.Start();
// ожидаем запуска , а то бывает не получает время на закуск
while (tickWorker.Status != TaskStatus.Running) Thread.Sleep(100);
}

public void TaskFunc()
{
Tick tick = null;
while (!finishTickWorker)
{
_newTickEvent.Wait();

while(_TickQueue.TryDequeue(out tick))
TickAction(tick);

_newTickEvent.Reset();
}
}

protected abstract void TickAction(Tick tick);

public virtual void OnNext(Tick tick)
{
tickCount++;

_TickQueue.Enqueue(tick);

_newTickEvent.Set();
}

public virtual void OnError(Exception error)
{

}

public virtual void OnCompleted()
{
if (tickWorker != null)
{
finishTickWorker = true;
_newTickEvent.Set();
tickWorker.Wait();

}
}

public void Dispose()
{
OnCompleted();
}
}

суббота, 7 июня 2014 г.

Mean and Variance of the Truncated Cauchy Distribution

The Cauchy distribution has infinite mean, sometimes called un-
defined mean and undefined other moments, but if X is Cauchy
with median u and MAD sigma, symbolized by C(u, sigma), and
we truncate X at u - a sigma and u + b sigma for positive a, b,
then the new truncated random variable Y has mean:

1) E(Y) = u + sigma[log(1 + b^2) - log(1 + a^2)]/2[arctan(b) +
arctan(a)]

The variance of Y is sigma-squared times a rational function of
the above brackets with the denominator bracket of (1) in its
denominator and only a + b added to one numerator. So both
mostly depend on the truncation points which are determined by
a and b and E(Y) also depends on the median u and the trunca-
tion point ratio indicated multiplies sigma for VAR(Y).

The special case a = b is of some interest and is in Johnson and
Kotz (1970, p. 162):

2) E(Y) = u
3) Var(Y) = sigma^2 [ b - arctan(b)]/[arctan(b)]

Since we only need E(Y) for Probable Influence Knowledge or
Information, there is no difficulty in estimating the Median
of X.

So here we have an example of the Cauchy distribution, with no
moments (actually mean infinity), and yet with very nice
truncated Probable Influence Knowledge or Information I. We
can truncate as far out in the tails as we want.

For example, the tangent of an angle in radians chosen randomly
from (-pi/2, pi/2) has the Cauchy distribution.

Hoel, Port, and Stone (1971) in their Introduction to Probability
Theory, Houghton-Mifflin: Boston, which I've cited before,
point out that:

4) Tn = (X1 + X2 + ... + Xn)/n

is Cauchy when the Xi, i = 1 to n, are independent Cauchy, which
may remind some readers of the fact that in probable influence
or its proximity function analog p1(x, y) = 1 + y - x for y < = x,
where both x and y are in [0, 1], we obtain the n-dimensional
generalization pn(x, y) = 1 + Sn - Tn when x is an n-vector
(X1, X2, ..., Xn) or its values (x1, x2, ..., xn) and similarly
for Y = (Y1, Y2, ..., Yn) and values (y1, y2, ..., yn) and Sn
is the mean of y components and Tn is as in (4). No requirement
of independence is placed on pn(x, y), although to converge
there are typically uncorrelated or independence assumptions in
the SLLN (Strong Law of Large Numbers) if asymptotics are
desired.

воскресенье, 18 мая 2014 г.

упаковать все dll в исполняемый exe
1. добавить все dll-файлы в проект как ресурсы (тип файлы)
2. добавить код в конструктор

AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
String resourceName = "AssemblyLoadingAndReflection." + new AssemblyName(args.Name).Name + ".dll";
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
};

вторник, 6 мая 2014 г.

симметричный профиль PL для проданных опционов
коррекция в контрактах (минус дельта):

дельта для проданного кола
k = - T / (F-2T-S)
дельта для проданного пута
k = - T / (F+2T-S)

где Т-цена опциона, F-фьючерс,S-страйк