| 1 | package Finance::TWS::Simple::HistoricalData; | ||
| 2 | |||
| 3 | # ABSTRACT: request historical quotes | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use Carp qw/croak/; | ||
| 9 | use DateTime; | ||
| 10 | |||
| 11 | sub call { | ||
| 12 | my ($class, $tws, $cv, $arg) = @_; | ||
| 13 | |||
| 14 | # Parameter | ||
| 15 | my $contract = $arg->{contract} or croak "CONTRACT missing"; | ||
| 16 | my $duration = $arg->{duration} or croak "DURATION missing"; | ||
| 17 | my $bar_size = $arg->{bar_size} or croak "BAR_SIZE missing"; | ||
| 18 | my $end_date = $arg->{end_date} || DateTime->today->subtract(seconds => 1); | ||
| 19 | my $bar_type = $arg->{bar_type} || 'TRADES'; | ||
| 20 | my $outside_rth = $arg->{outside_rth} || 0; | ||
| 21 | |||
| 22 | # format conversion | ||
| 23 | $end_date = $end_date->strftime('%Y%m%d %H:%M:%S') if ref $end_date; | ||
| 24 | |||
| 25 | my $request = $tws->request( | ||
| 26 | reqHistoricalData => { | ||
| 27 | id => $tws->next_id, | ||
| 28 | contract => $contract, | ||
| 29 | endDateTime => $end_date, | ||
| 30 | durationStr => $duration, | ||
| 31 | barSizeSetting => $bar_size, | ||
| 32 | whatToShow => $bar_type, | ||
| 33 | useRTH => $outside_rth ? 0 : 1, | ||
| 34 | formatDate => 1, | ||
| 35 | }, | ||
| 36 | ); | ||
| 37 | |||
| 38 | my $self = bless { | ||
| 39 | cv => $cv, | ||
| 40 | }, $class; | ||
| 41 | |||
| 42 | $tws->ae_call($self, $request); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub cb { | ||
| 46 | my ($self, $response) = @_; | ||
| 47 | |||
| 48 | $self->{cv}->send($response->bars); | ||
| 49 | } | ||
| 50 | |||
| 51 | 1; | ||
| 52 | |||
| 53 | |||
| 54 | __END__ |