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;
|
Hi monks,
Given anonymous structures like:
my $array = [qw(a b c)];
my $hash = { a => 1, b => 2, c => 3 };
Is there a technical reason so many people/books use:
print $array->[2];
print $hash->{a};
Instead of saving 1 character and using:
print @$array[2];
print %$hash{a};
|
Hello monks,
In a Prima application I would like to define a widget which allows to set a bunch of configuration variables. So I have several lines, each consisting of a Prima::Label describing the entry and a Prima::InputLine where users can enter their values.
Is there a decent way to get the InputLine widgets aligned vertically, like in a HTML table?
Counting pixels and specifing the size attribute for the widgets works, sort of, but might break if the text in the Label widgets isn't known in advance, for example when it is pulled from some I18N framework. I would also prefer not to count pixels for the line height, which might differ for different platforms (or fonts).
Any idea or pointer to a code example is welcome!
|
Once again, I have a difficult-to-name module.
Given a fully installed Linux (or BSD?) file tree, this module lets you select the files, directories, symlinks, device nodes, etc which you want to become part of a reduced system image. The purpose is to generate Linux initrd, or docker images, or portable chroots, or embedded system images. The cool features are:
- It inspects the 'ldd' output on ELF binaries to determine which libs need added
- It preserves permissions and mtimes of files and the directories that contain them (unlike mkdir -p)
- It can move executables to new locations (like moving them to /newprefix/bin/)
- It can rewrite interpreter paths of scripts to match their new location
- It can rewrite the lib paths of ELF files so that they can live alongside an incompatible libc ecosystem
I'm actually not even sure it belongs on CPAN, because while I keep writing code like this, it's always fairly specialized and I have no idea if my hacks and special cases will be useful for a different environment.
I'm leaning toward "Linux::SystemSlicer" (i.e. like a slice of an array) but other phrases that come to mind are "Filesystem Subset Extractor", "Root Image Collector", "Initrd Builder", or "System Minifier". I'm only planning to ever use it on Linux, but I think it could theoretically be applied to BSD? There are probably a huge number of additional special cases that would be needed for anything other than Linux.
|