среда, 29 апреля 2020 г.

Yang-Zhang

https://ru.tradingview.com/script/Z5qQonM6-Yang-Zhang-Volatility/

//@version=3
study("Yang-Zhang Volatility")
n1 = input(20,"N Value")
averageP = input(200,"Average Length")

No = log(open) - log(close[1])  // normalized open
Nu = log(high) - log(open)      // normalized high
Nd = log(low) - log(open)       // normalized low
Nc = log(close) - log(open)     // normalized close

Vrs = 1 / n1 * sum((Nu * (Nu - Nc) + Nd * (Nd - Nc)),n1)  // RS volatility estimator

Noavg = 1 / n1 * sum(No,n1)
Vo = 1 / (n1 - 1) * sum(pow((No - Noavg),2),21)

Ncavg = 1 / n1 * sum(Nc,n1)
Vc = 1 / (n1 - 1) * sum(pow((Nc - Ncavg),2),21)

k = 0.34 / ( 1.34 + (n1 + 1) / (n1 - 1))

Vyangzhang = Vo + k * Vc + (1 - k) * Vrs

avg = sma(Vyangzhang,averageP)

plot(Vyangzhang,color=fuchsia,linewidth=2,transp=50,style=area,title="Volatility")
plot(avg,color=aqua,linewidth=2,transp=0,title="Avg Volatility")

https://www.atmif.com/papers/range.pdf

понедельник, 27 апреля 2020 г.

StartNew() vs Task.Run

Task Timeout Examples

1.
public static async Task<TResult> WithTimeout<TResult>(this Task<TResult> task, TimeSpan timeout)
{
    if (task == await Task.WhenAny(task, Task.Delay(timeout)))
    {
        return await task;
    }
    throw new TimeoutException();
}
try
{
    await DoStuffAsync().WithTimeout(TimeSpan.FromSeconds(5));
}
catch (TimeoutException)
{
    // Handle timeout.
}
public static Task<TResult> WithTimeout<TResult>(this Task<TResult> task, TimeSpan timeout)
{
    var timeoutTask = Task.Delay(timeout).ContinueWith(_ => default(TResult), TaskContinuationOptions.ExecuteSynchronously);
    return Task.WhenAny(task, timeoutTask).Unwrap();
}
2.
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(3));

var task = Task.Run(() => DoStuff()).WithCancellation(cts.Token);
3.
public Task DoStuffAsync(CancellationToken cancellationToken)

..........

CancellationTokenSource cancellationSource = new CancellationTokenSource();
Task timeoutTask = Task.Delay(3000);

if(await Task.WhenAny(DoStuffAsync(cancellationTokenSource), timeoutTask) == timeoutTask)
{
     cancellationSource.Cancel();

     throw new TimeoutException();
}
4.
var client = new TcpClient();
if (!client.ConnectAsync("remotehost", remotePort).Wait(1000))
{
    // connection failure
}