App/Chart/Suffix/MGEX.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 82 CC 3 SCAR 14.2
CC
3
Cov
0%
barchart_customer_resp_to_url
12.0
barchart_customer_intraday
2.0
barchart_customer_url
2.0
CRAP 4.1
1 # Minneapolis Grain Exchange (MGEX) setups.
2  
3 # Copyright 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2017 Kevin Ryde
4  
5 # This file is part of Chart.
6 #
7 # Chart is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3, or (at your option) any later version.
10 #
11 # Chart is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 # details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Chart. If not, see <http://www.gnu.org/licenses/>.
18  
19 package App::Chart::Suffix::MGEX;
20 use 5.010;
21 use strict;
22 use warnings;
23 use Locale::TextDomain 'App-Chart';
24  
25 use App::Chart;
26 use App::Chart::Sympred;
27 use App::Chart::TZ;
28 use App::Chart::Weblink;
29  
30  
31 my $timezone_minneapolis = App::Chart::TZ->new
32 (name => __('Minneapolis'),
33 # no separate Minneapolis in Olson database
34 choose => [ 'America/Minneapolis', 'America/Chicago' ],
35 fallback => 'CST+6');
36  
37 my $pred = App::Chart::Sympred::Suffix->new ('.MGEX');
38 $timezone_minneapolis->setup_for_symbol ($pred);
39  
40 # (source-help! mgex-symbol?
41 # __p('manual-node','Minneapolis Grain Exchange'))
42  
43  
44 #------------------------------------------------------------------------------
45 # weblink - product info
46 #
47 # Believe these overview pages are more helpful than the contracts specs
48 # page.
49  
50 App::Chart::Weblink->new
51 (pred => $pred,
52 name => __('MGEX _Product Information'),
53 desc => __('Open web browser at the Minneapolis Grain Exchange product information page for this company'),
54 proc => sub {
55 my ($symbol) = @_;
56 my $commodity = App::Chart::symbol_commodity ($symbol);
57 if ($commodity eq 'MW' || $commodity eq 'MWE') {
58 return 'http://www.mgex.com/spring_wheat.html';
59 } else {
60 return 'http://www.mgex.com/indexes_index.html';
61 }
62 });
63  
64  
65 #-----------------------------------------------------------------------------
66 # intraday
67 #
68 # Alternately to do it with the main App::Chart::Barchart
69 # $App::Chart::Barchart::intraday_pred->add ($pred);
70 #
71  
72 barchart_customer_intraday
73 ($pred,
74 'http://customer1.barchart.com/cgi-bin/mri/mgexchart.htx?page=chart&code=mfo&org=com&crea=Y');
75  
76 # (lambda (symbol)
77 # (let ((date-time (mgex-quote-adate-time symbol)))
78 # (set-first! date-time (adate->tdate (first date-time)))))
79  
80 #------------------------------------------------------------------------------
81 # The charts at KCBT and MGEX come from barchart.com and are the same style
82 # as barchart itself, ie. download a page which contains a generated
83 # 4-digit numbered .gif file. The links from www.kcbt.com and www.mgex.com
84 # are preferred though, since the pages to get that gif file are only
85 # 26kbytes instead of 50k from barchart.com's native links.
86 #
87 # Z10 MEDHI gives 7 days, Z05 MED gives 1.5 or 2, Z10 HIGH gives about 10 days
88  
89 sub barchart_customer_intraday {
90 my ($pred, $base_url, $tdate_time_proc) = @_;
91  
92 require App::Chart::IntradayHandler;
93 App::Chart::IntradayHandler->new
94 (pred => $pred,
95 proc => \&barchart_customer_url,
96 mode => '1.5d',
97 name => __('_1 1/2 Days'),
98 base_url => $base_url);
99 App::Chart::IntradayHandler->new
100 (pred => $pred,
101 proc => \&barchart_customer_url,
102 mode => '7d',
103 name => __nx('_{n} Day',
104 '_{n} Days',
105 7,
106 n => 7),
107 base_url => $base_url);
108 }
109  
110 my %intraday_mode_to_data = ('1.5d' => '&data=Z05', # 5 minute, linear scale
111 '7d' => '&data=Z10&den=MEDHI');
112  
113 sub barchart_customer_url {
114 my ($self, $symbol, $mode) = @_;
115  
116 # my $mdate = latest_symbol_mdate ($symbol, ($tdate_time_proc->())[0]);
117 # mdate_to_MYY ($mdate);
118  
119 App::Chart::Download::status
120 (__x('Intraday page {symbol} {mode}',
121 symbol => $symbol,
122 mode => $mode));
123  
124 my $url = $self->{'base_url'}
125 . '&sly=N&sym='
126 . URI::Escape::uri_escape (App::Chart::symbol_sans_suffix ($symbol))
127 . $intraday_mode_to_data{$mode};
128 App::Chart::Download::verbose_message ("Intraday page", $url);
129  
130 my $resp = App::Chart::Download->get ($url);
131 return barchart_customer_resp_to_url ($resp, $symbol);
132 }
133 # separate func for offline testing ...
134 sub barchart_customer_resp_to_url {
135 my ($resp, $symbol) = @_;
136 my $content = $resp->decoded_content (raise_error => 1);
137  
138 # This doesn't use java_document_write() (in Finance::Quote::MGEX) and
139 # HTML::LinkExtor because the width/height in the document.write() in the
140 # <img> bit is computed, which java_document_write() can't handle. The
141 # src="" part of it is constant though. There's only a single <img> to
142 # pick out.
143 if ($content =~ /<img src="([^"]+)"/i) {
144 return $1; # url
145 }
146 if ($content =~ /no chart available/i) {
147 die __x("No chart available for {symbol}\n",
148 symbol => $symbol);
149 }
150 die 'Barchart Customer: Intraday page not matched';
151 }
152  
153 1;
154 __END__