question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Quantization of Quote history

See original GitHub issue

Adding quantization would be a nice feature:

When you have data with 1min candles, it would be nice to have built-in functionality to quantize them to another timeframe.

This is the code I’m using in F#:

let quantize chunkSize candles =
    candles
    |> List.chunkBySize chunkSize
    |> List.map (
        fun (chunk: Quote list) ->
            Quote(
                Date   = chunk.[0].Date,
                Open   = chunk.[0].Open,
                High   = (chunk |> List.maxBy (fun c -> c.High)).High,
                Low    = (chunk |> List.minBy (fun c -> c.Low)).Low,
                Close  = chunk.[^0].Close,
                Volume = (chunk |> List.sumBy (fun c -> c.Volume))
            )
        )

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
thomasd3commented, May 30, 2021

this would fall under quantization or resampling

1reaction
codebeaulieucommented, May 25, 2021

I had done something like this for a previous library I was working with, I haven’t tested this with this library yet, but I think this is in the ballpark.

public static IEnumerable<Skender.Stock.Indicators.Quote> Agg(this IEnumerable<Skender.Stock.Indicators.Quote> candles, TimeInterval interval = TimeInterval.FiveMin)
 {
        var period = interval.ToTimeSpan();

        return candles
            .GroupBy(x => x.Date.RoundDown(period))
            .Select(
                x => new Skender.Stock.Indicators.Quote {
                    Open = x.First().Open,
                    High = x.Max(t => t.High),
                    Low = x.Min(t => t.Low),
                    Close = x.Last().Close,
                    Volume = x.Sum(t => t.Volume)
            });
    }

   public static DateTime RoundDown(this DateTime dateTime, TimeSpan interval)
   {
        if (interval == TimeSpan.Zero)
        {
            // divide by zero exception
            return dateTime;
        }

        return dateTime.AddTicks(-(dateTime.Ticks % interval.Ticks));
    }
    
    public static TimeSpan ToTimeSpan(this TimeInterval resolution)
        {
            switch (resolution)
            {
                case TimeInterval.OneMin:
                    return TimeSpan.FromMinutes(1);
                case TimeInterval.TwoMin:
                    return TimeSpan.FromMinutes(2);
                case TimeInterval.ThreeMin:
                    return TimeSpan.FromMinutes(3);
                case TimeInterval.FiveMin:
                    return TimeSpan.FromMinutes(5);
                case TimeInterval.FifteenMin:
                    return TimeSpan.FromMinutes(15);
                case TimeInterval.ThirtyMin:
                    return TimeSpan.FromMinutes(30);
                case TimeInterval.OneHour:
                    return TimeSpan.FromMinutes(60);
                case TimeInterval.TwoHour:
                    return TimeSpan.FromMinutes(120);
                case TimeInterval.FourHour:
                    return TimeSpan.FromMinutes(240);
                case TimeInterval.Day:
                    return TimeSpan.FromMinutes(1440);
                case TimeInterval.Week:
                    return TimeSpan.FromMinutes(10080);
                default:
                    throw new ArgumentOutOfRangeException(nameof(resolution));
            }
        }
Read more comments on GitHub >

github_iconTop Results From Across the Web

[2202.07838] Quantization: History and Problems
Abstract: In this work, I explore the concept of quantization as a mapping from classical phase space functions to quantum operators.
Read more >
Quantization: History and problems
Thus quantization is a sort of mapping of classical theories to quantum theories, and quantization establishes a correspondence between classical theories and ...
Read more >
Canonical quantization
In physics, canonical quantization is a procedure for quantizing a classical theory, while attempting to preserve the formal structure, such as symmetries, ...
Read more >
1 quote on Quantization Science Quotes
Who said: “A people without children would face a hopeless future; a country without trees is almost as helpless.” Ronald Reagan George W....
Read more >
The Quasi-History of Early Quantum Theory - Physics
This case study investigates the quasi-history of the discovery of energy quantization and the quantum of radiation (i.e., the “photon”) in the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found