File Coverage

lib/Amibroker/OLE/Interface.pm
Criterion Covered Total %
statement 9 11 81.8
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 13 15 86.6


line stmt bran cond sub pod time code
1             package Amibroker::OLE::Interface;
2            
3 1     1   21919 use 5.006;
  1         3  
4 1     1   5 use strict;
  1         2  
  1         18  
5 1     1   5 use warnings;
  1         2  
  1         29  
6 1     1   348 use Win32::OLE;
  0            
  0            
7             use Win32;
8             use Carp;
9            
10             =head1 NAME
11            
12             Amibroker::OLE::Interface - A Simple Perl interface to OLE Automation framework of Amibroker Software.
13            
14             =head1 VERSION
15            
16             Version 0.02
17            
18             =cut
19            
20             our $VERSION = '0.03';
21            
22             =head1 SYNOPSIS
23            
24             use Amibroker::OLE::Interface;
25             my $obj = Amibroker::OLE::Interface->new( { verbose => 1,
26             dbpath => "C:/amibroker/dbpath"
27             } );
28             $obj->start_amibroker_engine();
29             $obj->run_analysis( { action => 2,
30             symbol => 'NIFTY-I',
31             apx_file => 'C:/amibroker/apx/path/nifty.apx',
32             result_file => 'C:/amibroker/result/path/report.csv'} );
33             $obj->shutdown_amibroker_engine();
34            
35             =head1 DESCRIPTION
36            
37             Amibroker is one of the most used Automated trading and charting software in the world.
38             Visit Amibroker website for information : L
39             Amibroker has provided OLE Automation framework, to interact with Amibroker engine from external scripts/programs.
40             You can refer to the AmiBroker's OLE Automation Object Model guide: L
41            
42             This module will help programmers who use Amibroker to access the objects easily in perl.
43            
44             =head1 PREREQUISITE
45            
46             Prerequisite to use this module : You need to have Amibroker v.5 and above (32-bit or 64-bit) installed in your system.
47            
48             =head2 new()
49            
50             Constructor to create the object for interacting with Amibroker engine.
51             The new() class method starts a new instance of an Amibroker OLE Interface object. It returns a reference to this object or undef if the creation failed.
52            
53             Eg: Amibroker::OLE::Interface->new()
54            
55             =over 1
56            
57             =item B
58            
59             =back
60            
61             =over 2
62            
63             =item * Verbose is optional
64            
65             =item * dbpath is compulsory - dbpath should be valid Amibroker database path.
66             For how to create database, please check here : L
67            
68             Code Example
69             $obj = Amibroker::OLE::Interface->new( verbose => 1, dbpath => "C:/amibroker/dbpath");
70            
71             =back
72            
73             =cut
74            
75             sub new {
76             my @list = @_;
77             my $class = shift @list;
78             push @list, {} unless @list and _is_arg( $list[-1] );
79             my $self = _check_valid_args(@list);
80             croak("No Database path supplied to Amibroker : $!\n")
81             unless $self->{dbpath};
82             croak("Invalid Database path to Amibroker : $!\n")
83             unless -d $self->{dbpath};
84            
85             bless $self, $class if defined $self;
86             return $self;
87             }
88            
89             =head2 start_amibroker_engine()
90            
91             Starts the Amibroker Engine and Loads the database
92            
93             Code Example
94            
95             $obj->start_amibroker_engine();
96            
97             =cut
98            
99             sub start_amibroker_engine {
100             my $self = shift;
101             $self->{broker} = Win32::OLE->new('Broker.Application')
102             or croak("Can't load Broker.Application : $!\n");
103             print "Amibroker Started\n" if $self->{verbose};
104             $self->{broker}->LoadDatabase( $self->{dbpath} )
105             or croak(
106             "Can't load Database, seems like the format is not supported: $!\n");
107             print "Database loaded to Amibroker\n" if $self->{verbose};
108             return 1;
109             }
110            
111             =head2 run_analysis()
112            
113             You can run various analysis based on the action supplied.
114             But before that you need to pass APX file,
115            
116             APX file is an important file to Amibroker engine. It is like the rule book to the amibroker.
117             The analysis project file (.apx extension) is human-readable self-explanatory XML-format file that can be written/edited/modified from any language / any text editor.
118             APX file includes all settings and formula needed in single file that is required to run analysis.
119             APX file instructs what the amibroker engine has to do.
120            
121             NOTE: Be very careful in creating the apx file.
122            
123             You can either manually create the apx file or by automatically by a script.
124             There should be no errors in the content of APX file, else the analysis of the Amibroker fails.
125            
126             How to create apx file manually:
127            
128             =over 3
129            
130             =item * Open Amibroker -> Analysis window -> Settings
131            
132             =item * Edit settings as per your requirement
133            
134             =item * Menu-> File-> Save_AS -> select (.apx extenstion)
135            
136             For more infor on apx file, check this forum : L
137            
138             $obj->run_analysis( action => 2 ) method allows to run asynchronously scan/explorations/backtest/optimizations.
139             Action parameter can be one of the following values:
140             0 : Scan
141             1 : Exploration
142             2 : Portfolio Backtest
143             3 : Individual Backtest
144             4 : Portfolio Optimization
145             5 : Individual Optimization (supported starting from v5.69)
146             6 : Walk Forward Test
147            
148             Symbol would be appended with '-I' => for current month contract future or '-II' for next month contract future
149             Eg: for Nifty, Symbol would be 'Nifty-I'.
150             This is just an example, you should check your database for the exact symbol name.
151             If the Symbol passed to this function is not present in your amibroker database, then the analysis is bound to fail.
152            
153             result_file is the path where you want to dump the result of the Amibroker analysis.
154             result_file file should NOT be created manually, it will be created automatically by the Amibroker engine when the analysis runs. You have to just pass the filename with the path.
155            
156             =back
157            
158             =over 1
159            
160             =item B
161            
162             =back
163            
164             =over 4
165            
166             =item * action is compulsory
167            
168             =item * symbol is compulsory
169            
170             =item * apx_file is compulsory
171            
172             =item * result_file is Optional - Default will be stored in C:/result.csv
173            
174             Code Example
175            
176             $obj->run_analysis( { action => 2,
177             symbol => 'NIFTY-I',
178             apx_file => "C:/amibroker/apx/path/nifty.apx",
179             result_file => "C:/amibroker/result/path/report.csv"
180             } );
181            
182             =back
183            
184             =cut
185            
186             sub run_analysis {
187             my @list = @_;
188             my $self = shift @list;
189             my $args = _check_valid_args(@list);
190             croak("Invalid apx file : $!\n") unless -e $args->{apx_file};
191             $args->{apx_file} =~ s/\//\\\\/g;
192             $args->{result_file} =~ s/\//\\\\/g if ( $args->{result_file} );
193             $self->{broker}->ActiveDocument->{Name} = $args->{symbol};
194             my $analysis = $self->{broker}->AnalysisDocs->Open( $args->{apx_file} )
195             or croak("Could'nt open analysis in Amibroker: $!\n");
196            
197             if ($analysis) {
198             $analysis->Run( $args->{action} );
199            
200             # Keep waiting till Amibroker completes - There is no end to wait.
201             Win32::Sleep(500) while ( $analysis->IsBusy );
202             $analysis->Export( $args->{result_file} );
203             $analysis->Close();
204             }
205             return 1;
206             }
207            
208             =head2 shutdown_amibroker_engine()
209            
210             Shuts down Amibroker engine.
211            
212             Code Example
213            
214             $obj->shutdown_amibroker_engine();
215            
216             =cut
217            
218             sub shutdown_amibroker_engine {
219             my $self = shift;
220             print "Shutting down Amibroker Engine\n" if $self->{verbose};
221             $self->{broker}->Quit();
222             return 1;
223             }
224            
225             #
226             # Supporting functions to the above useful methods
227             #
228             sub _is_arg {
229             my ($arg) = @_;
230             return ( ref $arg eq 'HASH' );
231             }
232            
233             sub _check_valid_args {
234             my @list = @_;
235             my %args_permitted = map { $_ => 1 } (
236             qw|
237             dbpath
238             verbose
239             broker
240             symbol
241             apx_file
242             action
243             result_file
244             |
245             );
246             my @bad_args = ();
247             my $arg = pop @list;
248             for my $k ( sort keys %{$arg} ) {
249             push @bad_args, $k unless $args_permitted{$k};
250             }
251             croak("Unrecognized option(s) passed to Amibroker OLE: @bad_args")
252             if @bad_args;
253             return $arg;
254             }
255            
256             =head1 AUTHOR
257            
258             Babu Prasad HP, C<< >>
259            
260             =head1 BUGS
261            
262             Please report any bugs or feature requests to C, or through
263             the web interface at L. I will be notified, and then you'll
264             automatically be notified of progress on your bug as I make changes.
265            
266            
267             =head1 SUPPORT
268            
269             You can find documentation for this module with the perldoc command.
270            
271             perldoc Amibroker::OLE::Interface
272            
273            
274             You can also look for information at:
275            
276             =over 4
277            
278             =item * RT: CPAN's request tracker (report bugs here)
279            
280             L
281            
282             =item * AnnoCPAN: Annotated CPAN documentation
283            
284             L
285            
286             =item * CPAN Ratings
287            
288             L
289            
290             =item * Search CPAN
291            
292             L
293            
294             =back
295            
296            
297             =head1 ACKNOWLEDGEMENTS
298            
299             I would like to thank Mr.Pannag M for supporting me in writing this module.
300            
301             =head1 LICENSE AND COPYRIGHT
302            
303             Copyright 2015 Babu Prasad HP.
304            
305             This program is free software; you can redistribute it and/or modify it
306             under the terms of the the Artistic License (2.0). You may obtain a
307             copy of the full license at:
308            
309             L
310            
311             Any use, modification, and distribution of the Standard or Modified
312             Versions is governed by this Artistic License. By using, modifying or
313             distributing the Package, you accept this license. Do not use, modify,
314             or distribute the Package, if you do not accept this license.
315            
316             If your Modified Version has been derived from a Modified Version made
317             by someone other than you, you are nevertheless required to ensure that
318             your Modified Version complies with the requirements of this license.
319            
320             This license does not grant you the right to use any trademark, service
321             mark, tradename, or logo of the Copyright Holder.
322            
323             This license includes the non-exclusive, worldwide, free-of-charge
324             patent license to make, have made, use, offer to sell, sell, import and
325             otherwise transfer the Package with respect to any patent claims
326             licensable by the Copyright Holder that are necessarily infringed by the
327             Package. If you institute patent litigation (including a cross-claim or
328             counterclaim) against any party alleging that the Package constitutes
329             direct or contributory patent infringement, then this Artistic License
330             to you shall terminate on the date that such litigation is filed.
331            
332             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
333             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
334             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
335             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
336             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
337             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
338             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
339             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
340            
341            
342             =cut
343            
344             1; # End of Amibroker::OLE::Interface