Complex Strategy samples

The following demonstrates how to develop smart and complex strategies. As you know, our strategy language is Perl.
Why did we choose Perl? Perl is a powerful and robust server-side script language, fun to work with and supports all the buzzwords such as
Object-Oriented Programming (OOP), Functional Programming (FP), Aspect-Oriented Programming etc. while not trying to prevent you from writing
quick and dirty code to get your work done.

Also, there are millions of users out there that know Perl and can help you with Perl coding, and you do not need to reinvent the wheel:
Perl has the Comprehensive Perl Archive Network (CPAN), which is a huge collection of useful re-usable Perl modules,
under open source licences. They allow you to use them as libraries to facilitate writing your code. So instead of starting to write
something yourself, do a CPAN search, or ask someone for a recommendation for a good CPAN module.

Yes, we do not include all the CPAN modules in Strategy Seeker (due to security reasons), but we are happy to add those you need and
ask us for. You need one that is missing, simply Contact us and we will open it for you and everyone else.

Now, lets get some examples:
Example 1: use of date manipulation functions, regex, defining a subroutine
Example 2: searching of 'behavior pattern' in a chart

Example 1: use of date manipulation functions, regex, defining a subroutine


my $avgRSI5;
my $avgRSI2;
my $ratio2;
my $ratio5;
my $deltaROC;
my $x;
$deltaROC = ROC(2,2) - ROC(2);
# Date Manipulation examples:
$dd = ParseDate("1st Thursday in June 1992");
$dtoday = ParseDate("today");
$d2 = DateCalc("today","+ 3hours 12minutes 6 seconds");
#
# find the Monday after easter in 1997-1999.
@d3 = ParseRecur("*1997-1999:0:0:0:0:0:0*EASTER,ND1");
#
$d4 = UnixDate("today","The time is now %T on %b %e, %Y.");
$d5 = strftime("%Y-%m-%d %H:%M:%S %Z", gmtime()) ;
$d6 = DateCalc("today","+ 3 business days");
$date1 = "24 June 2013";
$date2 = "21 June 2013";
$d7 = Delta_Format( DateCalc( $date1 , $date2 ) , 0 , "%dt");
DebugMSG(" The +3 day: ".$d6. " d7=".$d7. " d2=$d2 d3=".convert_array(@d3) ." d4=$d4 d5=$d5 " );
#
# Calculate time between 2 dates:
$date1 = "24 June 2013";
$date2 = "21 June 2013";
$d7 = DateCalc( $date1 , $mdate2 ) ;
#
# Example for Regex:
# find the first character:
$mystring = "[2013/04/13] The date of this article.";
if($mystring =~ m/(\d)/) {
DebugMSG( "The first digit is $1.");
}
#
#
$avgRSI5 = ( RSI(2) + RSI(2,1) + RSI(2,2) + RSI(2,3) + RSI(2,4) ) / 5;
$avgRSI2 = ( RSI(2) + RSI(2,1) ) /2;
$ratio2 = sprintf("%.2f", $avgRSI2 * 100 / RSI(2) );
$ratio5 = sprintf("%.2f", $avgRSI5 * 100 / RSI(2) );
#
# Buy section below
#
if ( MP() != 1 and
#Close() > SMA(200) and
RSI(2) Close() Close(1) Close(2) {
Buy( "RSI2=".RSI(2)." Ratio2=".$ratio2." Ratio5=".$ratio2 );
}
#
# Sell section below:
#
if ( MP() == 1 and
$deltaROC > 4 and
$deltaROC ROC(5) > 1
) {
Sell(" ROC2=".ROC(2)." DeltaROC=".$deltaROC );
}
#
#
# Define a subroutine to convert array elements into a string:
#
sub convert_array {
my @ar = shift;
my $str = "";
# loop:
foreach( @ar ) {
$str .= " ".pop( @ar ) ;
}
return $str;
}

Example 2: searching of 'behavior pattern' in a chart

This sample shows how to use global strategy parameters. These parameters are kept between each run of the strategy so a value calculated in one run (for example for yesterday) can be used
and be referred to in another run (for example today).
The sample scans 252 trading days (a year) using its main loop where $i starts with 0 up to 252. When $i=0 the date is oldest (one year ago) and 252 is today. This is the opposite than the index in a technical indicator, for example Close($i+1) is the close of the day before the $i day.
This sample continues and search for a second pattern (after detection of first pattern), and only if found will store values in global variables and will check conditions to buy.
This startegy sample is also using DebugMSG function to log messages in the log file (can be opened in Chart->debug menu) and DrawEvent that draws arrows and messages within the Chart.


# Declare local parameters:
my $c;
my $i;
my $index;
my $flag = 0;
#
# We are using Global parameter storage to store $params between different strategy runs:
my $params = GetGlobal();
#
#we want to search for hammer starting today Date and not earlier
my $start = Date2index( Date() )+1;
#
#
# Set initial value to -1 so we can detect that there was no pattern detected.
$c = -1 ;
#
# Scan everyday of last 252 trading days (which is a year)
for ( $i = $start ; $i $index = $i;
#
#Search a pattern of Open is Lower than last Close after 2 days of Lower Low:
if ( OpenValue( $i ) LowValue( $i ) LowValue( $i -1 ) LowValue( $i -2 ) ) {
# If pattern is found, select the Lowest value between yesterday Close and Open and store it in $c
$c = ( CloseValue( $i - 1 ) > OpenValue( $i - 1 ) ? CloseValue( $i - 1) : OpenValue( $i - 1 ) ) ;
last; # Stop the for loop, patter found.
}
}
#
#
#
# Search for a new pattern: if we have a pattern that is different than the one we stored in $params, lets print it
# in the Log file
if ( defined $index and $index != $params[0] and $c != -1 ) {
#
# Print debug message to log file :
DebugMSG( "****** Found Pattern on Date =". Index2date($index) );
#
# Draw a black arrow on the Graph with a comment :
DrawEvent( "Date=" . Date() . " Open=" . FormatNumber( Open() ).
" Close1=" . FormatNumber( Close(1) ).
" Low0=" . FormatNumber( Low() ).
" Low1=" . FormatNumber( Low(1) ).
" Low2=" . FormatNumber( Low(2) ) .
" Low3=" . FormatNumber( Low(3)) , "Black" ); # color example: "Blue" , "Yellow", "#66FF33" #CCFF33 or default is black
#
#Save new dropdown value and index in global parameter
$params[0] = $index; # Store the index for next iteration
$params[1] = $c; # Store the value of c for next iteration
SetGlobal( \@params ); # call function to store global parameter between different runs.
#
#Flag we have found the pattern
$flag = 1 ;
}
#
# Buy section
#
if ( MP() == 0 and
# Date2index( Date() ) == $index and # Current date is the pattern day!
$flag == 1 ) {
DebugMSG( "*------BUY------- Close=".Close()." Global Index=".$params[0] ." Global C=". $params[1] );
Buy();
}
#
# Sell section
#
elsif ( MP() ==1 and
Close() > SMA(5) ) {
DebugMSG( "*------SELL------- Close=" . Close() . "Buy price=". Buyprice() );
Sell();
}

X
Enter your Strategy Seeker username.
Enter the password that accompanies your username.
14 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
Secure Login

This login is SSL protected

Loading