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