Skip to content

Instantly share code, notes, and snippets.

@alex-groshev
alex-groshev / gist:f433f0e0972da24c2106
Last active May 27, 2019 11:31
C# Moving averages extensions (Cumulative, Simple, Exponential).
public static class MovingAverageExtensions
{
public static IEnumerable<decimal> CumulativeMovingAverage(this IEnumerable<decimal> source)
{
ulong count = 0;
decimal sum = 0;
foreach (var d in source)
{
yield return (sum += d) / ++count;
@alex-groshev
alex-groshev / gist:f1dc24fde2c7aa9f1ff3
Created February 24, 2015 13:43
How to use the LowLatency mode
private static void LowLatencyDemo()
{
GCLatencyMode oldMode = GCSettings.LatencyMode;
System.Runtime.CompilerServices.RuntimeHelpers.PrepareConstrainedRegions();
try
{
GCSettings.LatencyMode = GCLatencyMode.LowLatency;
// Run your code here...
}
finally
@alex-groshev
alex-groshev / gist:efcc2c1a958df0c41e00
Last active August 29, 2015 14:17
Moving Averages / MS SQL 2012
-- Cumulative Moving Average (CMA) and Simple Moving Average (SMA)
SELECT
d.Value,
AVG(d.Value) OVER (PARTITION BY d.MetricId ORDER BY d.Id) AS 'CMA',
AVG(d.Value) OVER (PARTITION BY d.MetricId ORDER BY d.Id ROWS 2 PRECEDING) AS 'SMA(3)',
AVG(d.Value) OVER (PARTITION BY d.MetricId ORDER BY d.Id ROWS 6 PRECEDING) AS 'SMA(7)'
FROM DailyTotals AS d
WHERE
d.DateId >= '20150101'
AND d.MetricId = 2;
@alex-groshev
alex-groshev / gist:06b1b0c55c2eb21e5a74
Created April 22, 2015 08:15
Daily Returns / SQL Server 2012+
SELECT
Date
,Value
,CAST(ROUND(100 * (Value / LAG(Value, 1, Value) OVER (ORDER BY Date) - 1), 2) AS NUMERIC(16, 2)) AS 'Return'
FROM Totals
ORDER BY Date;
@alex-groshev
alex-groshev / gist:73deba021028ac6642c8
Created May 8, 2015 16:33
Cumulative (or expanding) and moving averages in C# Deedle
// insert into the same Frame
f.AddColumn("Column_CMA", Stats.expandingMean(f.GetColumn<double>("Column")));
f.AddColumn("Column_MVA3", Stats.movingMean(3, f.GetColumn<double>("Column")));
f.AddColumn("Column_MVA7", Stats.movingMean(7, f.GetColumn<double>("Column")));
// or as extension method
public static void AddMovingAverage(this Frame<DateTime, string> source, string column, int length)
{
if (length < 1)
{
@alex-groshev
alex-groshev / gist:fbb61d65fd201a2ec8f7
Created May 11, 2015 17:08
Invoking multiple handlers periodically from Topshelf Windows Service (using System.Timers.Timer)
using System;
using System.Collections.Generic;
using System.Timers;
using Topshelf;
namespace ConsoleApplication
{
public interface IProcess
{
void ProcessHandler(object o, ElapsedEventArgs args);
@alex-groshev
alex-groshev / gist:1e047950cb9b2d12c212
Created May 15, 2015 11:43
Invoking multiple handlers periodically from Topshelf Windows Service (using System.Threading.Timer)
using System;
using System.Collections.Generic;
using System.Threading;
using Topshelf;
namespace ConsoleApplication
{
public interface IProcess
{
void Perform();
@alex-groshev
alex-groshev / gist:545826470ed4f174dfd8
Created May 18, 2015 11:52
Invoking multiple handlers periodically from Topshelf Windows Service (using System.Threading.Timer) using Reflection
using System;
using System.Collections.Generic;
using System.Threading;
using Topshelf;
namespace ConsoleApplication
{
public interface IProcess
{
void Perform();
@alex-groshev
alex-groshev / CustomerController.cs
Created October 15, 2015 06:42 — forked from vkhorikov/CustomerController.cs
Handling failures and input errors in a functional way
[HttpPost]
public HttpResponseMessage CreateCustomer(string name, string billingInfo)
{
Result<BillingInfo> billingInfoResult = BillingInfo.Create(billingInfo);
Result<CustomerName> customerNameResult = CustomerName.Create(name);
return Result.Combine(billingInfoResult, customerNameResult)
.OnSuccess(() => _paymentGateway.ChargeCommission(billingInfoResult.Value))
.OnSuccess(() => new Customer(customerNameResult.Value))
.OnSuccess(