This example trading system allows the backtesting of newsletter “buy” 0

Posted on 17, June 2013

in Category SID


Description for Backtest_Newsle SID 584

This example trading system allows the backtesting of newsletter “buy” and “sell” signals. The signals are stored in a comma separated variable (CSV) file that can be edited in a text editor or in a spreadsheet.

This example trading system allows the backtesting of newsletter buy/sid Notes for this Trading Account:

This example trading system allows the backtesting of newsletter “buy” and “sell” signals. The signals are stored in a comma separated variable (CSV) file that can be edited in a text editor or in a spreadsheet.

The file is loaded in the account begin event with the signals loaded into global hash references. During backtesting these hashes are tested to see if a signal is valid for “today” and appropriate action taken. This example supports multiple symbols, and is suitable for full portfolio backtesting. Symbols and actions can appear in any order in the file.

See the account begin event for more details on the file format and for an example file.

This example trading system allows the backtesting of newsletter buy/sid Rules for this Trading Account:

This example trading system allows the backtesting of newsletter buy/sid Begin event logic:

#The file format for the newsletter file should be:
#Signal(Buy or Sell,Symbol,Date (in the format of YYYYMMDD)
#for example:

##An example file 
#Buy,MSFT,20051004
#Sell,MSFT,20051027
#Buy,BA,20051130
#Sell,BA,20051230
#Buy,GM,20050815
#Sell,GM,20051010

#To use this file, the leading '#' should be removed. Note
#that you can use '#' - the line will be ignored when it's
#the first character.


#The path to the newsletter file
my $filepath="C:/newsletter.csv";
open(FILE,"< $filepath") || die("can't open $filepath: $! .See instructions in the Account Begin Event");
#load the file into an array
my @lines=<FILE>;
close(FILE);

#A reference to a hash structure

$buy={}; 
$sell={}; 

foreach my $line (@lines) {
  #ignore the line if the first character is a '#'
  next if substr($line,0,1) eq '#';
  #split the line
  my ($action,$symbol,$date)=split(',',$line);
  #We need to convert the date into a valid Seer date, then validate the date just to be sure
  $date*=10000000;
  die "invalid date $date in line $line" unless ValidateDateTime($date);
  #we now populate the buy/sell hash with the symbol and the date, setting the entry to true
  if ($action eq 'Buy') {
    $buy->{$symbol}{$date}=1;
  }
  elsif ($action eq 'Sell') {
    $sell->{$symbol}{$date}=1;
  }
  else {
    die "unknown action $action in line $line";
  }
}

This example trading system allows the backtesting of newsletter buy/sid Notes for this Trading System:

This example trading system allows the backtesting of newsletter buy/sid Rules for this Trading System:

This example trading system allows the backtesting of newsletter buy/sid Bar event logic:

#fetch the current symbol and the datetime
#we will use these items to test the buy/sell hashes for signals
my $symbol = Symbol;
my $date   = Now(DateTime);

unless (Position) {
  #Buy logic
  if ($buy->{$symbol}{$date}) {
    Output "we have a buy signal for $symbol on $date";
    BuyOpen;
  }
}
else {
  #Sell logic
  if ($sell->{$symbol}{$date}) {
    Output "we have a buy signal for $symbol on $date";
    SellOpen;
  }
}

0 Comments

Leave a Message