Forex Feed Perl Data API

Below is a brief example of basic ForexFeed PERL API functions.

Please scroll to the bottom of the page to Download the ForexFeed PERL package.

#!/usr/bin/perl

#
# ForexFeed.Net Data API
#
# Copyright 2009 ForexFeed.Net
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#      This product includes software developed by ForexFeed.Net.
# 4. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
use strict;

## Load the ForexFeed.net API
use ForexFeed;
  
  
  
  ## ------------------------------------------ ##
  ## EDIT THE FOLLOWING VARIABLES
  
  my $access_key = 'YOUR_APP_ID';
  my $symbol = 'EURUSD,GBPUSD,USDCHF,USDCAD,AUDUSD';
  my $interval = 3600;
  my $periods = 1;
  
  ## END VARIABLES
  ## ------------------------------------------ ##
  
  
  
  
  
  
  
  ##
  ## Create the ForexFeed Object
  my $fxfeed = new ForexFeed::feed(
                     '-access_key' => $access_key,
                     '-symbol'     => $symbol,
                     '-interval'   => $interval,
                     '-periods'    => $periods,
                     );
  
  print "Content-type: text/html;\r\n\r\n";
  
  ## Display a Conversion
  &printConversion($fxfeed);

  ## Display the data
  &printData($fxfeed);

  ## Display the available Intervals
  &printIntervals($fxfeed);

  ## Display the available Symbols
  &printSymbols($fxfeed);


##
##  Get a conversion and print it to System.out
##
sub printConversion() {
  my $fxfeed = shift;

  ##  Get the conversion
  my %conversion = $fxfeed->getConversion("EUR", "USD", 1);
  # my %conversion = $fxfeed->getConversion("USD", "EUR", 1);

  print "<br>\r\n"-------- Conversion -------- <br>\r\n"";
  if( $fxfeed->getStatus() eq "OK" ){
   print $conversion{'convert_value'} . " ";
   print $conversion{'convert_from'} . " = ";
   print $conversion{'conversion_value'} . " ";
   print $conversion{'convert_to'} . " ";
   print "(rate: " . $conversion{'conversion_rate'} . ") <br>\r\n"";
   print "<br>\r\n"";
   }
  else {
   print "Status: " . $fxfeed->getStatus() . "<br>\r\n"";
   print "ErrorCode: " . $fxfeed->getErrorCode() . "<br>\r\n"";
   print "ErrorMessage: " . $fxfeed->getErrorMessage() . "<br>\r\n"";
   }
}



##
##  Print data to screen,
##
sub printData {
  my $fxfeed = shift;
  
  ## Request the Data
  $fxfeed->getData();
  
  print "<br>\r\n"-------- Quotes --------<br>\r\n"";
  if( $fxfeed->getStatus() eq "OK" ){
   print "Number of Quotes: " . $fxfeed->getNumQuotes() . "<br>\r\n"\r\n"";
   print "Copyright: " . $fxfeed->getCopyright() . "<br>\r\n"";
   print "Website: " . $fxfeed->getWebsite() . "<br>\r\n"";
   print "License: " . $fxfeed->getLicense() . "<br>\r\n"";
   print "Redistribution: " . $fxfeed->getRedistribution() . "<br>\r\n"";
   print "AccessPeriod: " . $fxfeed->getAccessPeriod() . "<br>\r\n"";
   print "AccessPerPeriod: " . $fxfeed->getAccessPerPeriod() . "<br>\r\n"";
   print "AccessThisPeriod: " . $fxfeed->getAccessThisPeriod() . "<br>\r\n"";
   print "AccessRemainingThisPeriod: " . $fxfeed->getAccessPeriodRemaining() . "<br>\r\n"";
   print "AccessPeriodBegan: " . $fxfeed->getAccessPeriodBegan() . "<br>\r\n"";
   print "NextAccessPeriodStarts: " . $fxfeed->getAccessPeriodStarts() . "<br>\r\n"";
   print "<br>\r\n"";
   
   while( $fxfeed->iterator() ){
     
     print " Quote Symbol: " . $fxfeed->iteratorGetSymbol();
     print " Title: " . $fxfeed->iteratorGetTitle();
     print " Time: " . $fxfeed->iteratorGetTimestamp();
     
     if( $fxfeed->getInterval() == 1 ){
      print " Price: " . $fxfeed->iteratorGetPrice();
      }
     else{
      print " Open: " . $fxfeed->iteratorGetOpen();
      print " High: " . $fxfeed->iteratorGetHigh();
      print " Low: " . $fxfeed->iteratorGetLow();
      print " Close: " . $fxfeed->iteratorGetClose();
      }
     print "<br>\r\n"";
     
     }
   }
  else{
   print "Status: " . $fxfeed->getStatus() . "<br>\r\n"";
   print "ErrorCode: " . $fxfeed->getErrorCode() . "<br>\r\n"";
   print "ErrorMessage: " . $fxfeed->getErrorMessage() . "<br>\r\n"";
   }
  
}


##
##  Print available Intervals to screen,
##
sub printIntervals {
  my $fxfeed = shift;
  
  ##  Get the Intervals
  my %intervals = $fxfeed->getAvailableIntervals();
  
  print "<br>\r\n"-------- Intervals --------<br>\r\n"";
  if( $fxfeed->getStatus() eq "OK" ){
   foreach my $interval ( sort { $a <=> $b } keys(%intervals) ){
     print " Interval: " . $interval;
     print " Title: " . $intervals{$interval} . "<br>\r\n"";
     }
   }
  else {
   print "Status: " . $fxfeed->getStatus() . "<br>\r\n"";
   print "ErrorCode: " . $fxfeed->getErrorCode() . "<br>\r\n"";
   print "ErrorMessage: " . $fxfeed->getErrorMessage() . "<br>\r\n"";
   }
}

##
##  Print available Symbols to screen,
##
sub printSymbols {
  my $fxfeed = shift;
  
  ##  Get the Symbols
  my %symbols = $fxfeed->getAvailableSymbols();
  
  print "<br>\r\n"-------- Symbols --------<br>\r\n"";
  if( $fxfeed->getStatus() eq "OK" ){
   foreach my $symbol ( sort keys(%symbols) ){
     print " Symbol: " . $symbols{$symbol}{'symbol'};
     print " Title: " . $symbols{$symbol}{'title'};
     print " Precision: " . $symbols{$symbol}{'decimals'} . "<br>\r\n"";
     }
   }
  else {
   print "Status: " . $fxfeed->getStatus() . "<br>\r\n"";
   print "ErrorCode: " . $fxfeed->getErrorCode() . "<br>\r\n"";
   print "ErrorMessage: " . $fxfeed->getErrorMessage() . "<br>\r\n"";
   }
}