I'm trying to get a slice of a 2D PDL array and am having problems finding good examples of this. What I am trying to do is take Tick data and create create a OHLCV array based on a summarization of x number seconds. Any help would be apreciated. My code is below

#!/usr/bin/env perl use strict; use warnings; use PDL; use PDL::NiceSlice; use Time::Local; # Sample tick data: array of arrays [timestamp, price, volume] # Example: ["2025-01-12 09:30:00", 100.5, 200] my $tick_data = [ ["2025-01-12 09:30:00", 100.5, 200], ["2025-01-12 09:30:15", 101.0, 150], ["2025-01-12 09:30:30", 100.8, 100], ["2025-01-12 09:30:45", 101.2, 300], ["2025-01-12 09:31:00", 101.0, 250], ]; # Group data into OHLCV intervals (e.g., 1 minute) my $interval_seconds = 20; # Set interval in seconds # Helper: Convert timestamp to epoch sub timestamp_to_epoch { my ($timestamp) = @_; my ($date, $time) = split(' ', $timestamp); my ($year, $month, $day) = split('-', $date); my ($hour, $min, $sec) = split(':', $time); return timelocal($sec, $min, $hour, $day, $month - 1, $year); } # Pre-process: Add epoch to data my $data = pdl([ map { my $epoch = timestamp_to_epoch($_->[0]); [$epoch, $_->[1], $_->[2]] } @$tick_data ]); for my $i (0..$data->dim(1)-1) { my $ts = $data->at(0,$i); my $p = $data->at(1,$i); my $v = $data->at(2,$i); } # Find unique interval buckets my $start_epoch = $data((0), 0); my $intervals = floor(($data(0, -1) - $start_epoch) / $interval_seco +nds); # Compute OHLCV my ($open, $high, $low, $close, $volume) = ([], [], [], [], []); for my $i (0 .. max($intervals)) { my $group = $data->where(floor(($data - $start_epoch) / $interval_ +seconds)== $i); next if $group->nelem == 0; # Skip empty groups # push @$open, $group(0, 1); # First price # push @$high, max($group(:, 1)); # push @$low, min($group(:, 1)); # push @$close, $group((($group->dim(0) - 1)), 1); # Last price # push @$volume, sum($group(:, 2)); } # Convert OHLCV to PDL for display #my $ohlcv = pdl($open, $high, $low, $close, $volume)->transpose; # Output results #print "OHLCV Format (Open, High, Low, Close, Volume):\n"; #print $ohlcv;

In reply to PDL slice 2D array by paul92

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.