Below is the code for a perl script I wrote, but when I run perl -w it gives me the following:
Use of uninitialized values at the script lines 38, <TESTFILE> chunk 86.
Does anyone have an idea what could be wrong, and what this error means. I've been trying to figure it out for a couple of days now, and the script logic seems fine to me - I really appreciate any help! I emboldened line 40, and below the script is the datafile I'm working with - thanks!
#!/usr/local/bin/perl
##define the scalars to be used: $reversal, $heat, $profit, $position, $counter, $profitabletrades, $losingtrades
##$price, $date, $time, $misc1-4, $priceinitiate, $priceopen
$position = 0;
$counter = 1;
$profitabletrades = 0;
$losingtrades = 0;
$totalprofit = 0;
$counter2 = 0;
#############open log file file#####################
open (LOGFILE, ">logfile.txt"
;
##########start loop and open test file to get opening date###############3
for ($reversal=300;$reversal<=500;$reversal=$reversal+50) {
for ($profit=300;$profit<=500;$profit=$profit+50) {
for ($maxheat=250;$maxheat<=500;$maxheat=$maxheat+50) {
open (TESTFILE, "epz.txt"
;
@opendata = <TESTFILE>;
@element=<TESTFILE>;
$lengtharray = @element;
@Open=$element[0];
foreach $record (@Open) {
($nameopen,$dateopen,$miscopen,$timeopen,$priceopen,$miscopena,$miscopenb,$miscopenc)=split(/,/,$record); # get the open price in the list for comparison</b></i>
}
################start test loop####################
for ($a=0; $a<$lengtharray; $a=$a+$counter) {
&parserecord();
################test flat position for reversal move#####################
if ($position==0) {
while ($position==0) { ##if flat, look for first reversal amount
#####################initiate long position on break###########################
if ($price == $priceopen - $reversal) { #see if new price is the reversal amount away from the open price
$position++;
print LOGFILE "$date, $time, $price ------INITIATE LONG POSITION = $position\n";
$priceinitiate = $price;
$priceopen = $price;
&parserecord();
}
#####################initiate short position on rally###########################
elsif ($price == $priceopen + $reversal) { #see if new price is the reversal amount away from the open price
$position--;
print LOGFILE "$date, $time, $price ------INITIATE SHORT POSITION = $position\n";
$priceinitiate = $price;
$priceopen = $price;
&parserecord();
}
&parserecord();
}
&parserecord();
}
#######################check for sell conditions on a long position###########################
if ($position==1) {
while ($position==1) { ##if long, look for exit point
if ($price == $priceinitiate + $profit) { #see if new price is the profit goal away from the initiated price
$position--;
print LOGFILE "$date, $time, $price ------SELL PROFITABLE POSITION = $position\n";
$totalprofit = $totalprofit + ($profit/100*250);
$profitabletrades++;
&parserecord();
}
elsif ($price == $priceinitiate - $maxheat) { #see if new price is the maximum loss from the initiated price
$position--;
print LOGFILE "$date, $time, $price ------SELL AT A LOSS POSITION = $position\n";
$priceopen = $price;
$totalprofit = $totalprofit - ($profit/100*250);
$losingtrades++;
&parserecord();
}
&parserecord();
}
&parserecord();
}
#######################check for cover conditions on a short position###########################
if ($position==-1) {
while ($position==-1) { ##if long, look for exit point
if ($price == $priceinitiate - $profit) { #see if new price is the profit goal away from the initiated price
$position++;
print LOGFILE "$date, $time, $price ------COVER PROFITABLE POSITION = $position\n";
$totalprofit = $totalprofit + ($profit/100*250);
$profitabletrades++;
&parserecord();
}
elsif ($price == $priceinitiate + $maxheat) { #see if new price is the maximum loss from the initiated price
$position++;
print LOGFILE "$date, $time, $price ------COVER AT A LOSS POSITION = $position\n";
$priceopen = $price;
$totalprofit = $totalprofit - ($profit/100*250);
$losingtrades++;
&parserecord();
}
&parserecord();
}
&parserecord();
}
&parserecord();
}
print LOGFILE "\n\n\n\n\nFor the following parameters:\n";
print LOGFILE "Reversal amount: $reversal points\n";
print LOGFILE "Maximum allowable loss: $maxheat points\n";
print LOGFILE "Profit Objective: $profit points\n\n\n";
print LOGFILE "The results are:\n";
print LOGFILE "Number of profitable trades: $profitabletrades\n";
print LOGFILE "Number of losing trades: $losingtrades\n";
print LOGFILE "Total Profit or Loss for these parameters: \$$totalprofit\n";
if ($profitabletrades>0 or $losingtrades>0) {
$success = ($profitabletrades/($profitabletrades+$losingtrades)*100);
print LOGFILE "Rate of success under these parameters: %$success\n\n\n\n\n\n\n\n\n\n\n\n";
}
else {
print LOGFILE "No rate of success under these parameters, as no trades would have been executed.\n\n\n\n\n\n\n\n\n\n\n\n";
}
$counter=1;
$losingtrades = 0;
$profitabletrades = 0;
$totalprofit = 0;
close(TESTFILE);
}
$counter=1;
}
$counter=1;
}
close(LOGFILE);
##subroutine to parse each record as it comes through from the textfile
sub parserecord {
@element3=$opendata[$counter];
foreach $record2 (@opendata) {
($name, $date, $misc, $time, $price, $misc2, $misc3, $misc4)=split(/,/, $record2); #split each record into an array
}
$counter++;
}
Use of uninitialized values at the script lines 38, <TESTFILE> chunk 86.
Does anyone have an idea what could be wrong, and what this error means. I've been trying to figure it out for a couple of days now, and the script logic seems fine to me - I really appreciate any help! I emboldened line 40, and below the script is the datafile I'm working with - thanks!
#!/usr/local/bin/perl
##define the scalars to be used: $reversal, $heat, $profit, $position, $counter, $profitabletrades, $losingtrades
##$price, $date, $time, $misc1-4, $priceinitiate, $priceopen
$position = 0;
$counter = 1;
$profitabletrades = 0;
$losingtrades = 0;
$totalprofit = 0;
$counter2 = 0;
#############open log file file#####################
open (LOGFILE, ">logfile.txt"
##########start loop and open test file to get opening date###############3
for ($reversal=300;$reversal<=500;$reversal=$reversal+50) {
for ($profit=300;$profit<=500;$profit=$profit+50) {
for ($maxheat=250;$maxheat<=500;$maxheat=$maxheat+50) {
open (TESTFILE, "epz.txt"
@opendata = <TESTFILE>;
@element=<TESTFILE>;
$lengtharray = @element;
@Open=$element[0];
foreach $record (@Open) {
($nameopen,$dateopen,$miscopen,$timeopen,$priceopen,$miscopena,$miscopenb,$miscopenc)=split(/,/,$record); # get the open price in the list for comparison</b></i>
}
################start test loop####################
for ($a=0; $a<$lengtharray; $a=$a+$counter) {
&parserecord();
################test flat position for reversal move#####################
if ($position==0) {
while ($position==0) { ##if flat, look for first reversal amount
#####################initiate long position on break###########################
if ($price == $priceopen - $reversal) { #see if new price is the reversal amount away from the open price
$position++;
print LOGFILE "$date, $time, $price ------INITIATE LONG POSITION = $position\n";
$priceinitiate = $price;
$priceopen = $price;
&parserecord();
}
#####################initiate short position on rally###########################
elsif ($price == $priceopen + $reversal) { #see if new price is the reversal amount away from the open price
$position--;
print LOGFILE "$date, $time, $price ------INITIATE SHORT POSITION = $position\n";
$priceinitiate = $price;
$priceopen = $price;
&parserecord();
}
&parserecord();
}
&parserecord();
}
#######################check for sell conditions on a long position###########################
if ($position==1) {
while ($position==1) { ##if long, look for exit point
if ($price == $priceinitiate + $profit) { #see if new price is the profit goal away from the initiated price
$position--;
print LOGFILE "$date, $time, $price ------SELL PROFITABLE POSITION = $position\n";
$totalprofit = $totalprofit + ($profit/100*250);
$profitabletrades++;
&parserecord();
}
elsif ($price == $priceinitiate - $maxheat) { #see if new price is the maximum loss from the initiated price
$position--;
print LOGFILE "$date, $time, $price ------SELL AT A LOSS POSITION = $position\n";
$priceopen = $price;
$totalprofit = $totalprofit - ($profit/100*250);
$losingtrades++;
&parserecord();
}
&parserecord();
}
&parserecord();
}
#######################check for cover conditions on a short position###########################
if ($position==-1) {
while ($position==-1) { ##if long, look for exit point
if ($price == $priceinitiate - $profit) { #see if new price is the profit goal away from the initiated price
$position++;
print LOGFILE "$date, $time, $price ------COVER PROFITABLE POSITION = $position\n";
$totalprofit = $totalprofit + ($profit/100*250);
$profitabletrades++;
&parserecord();
}
elsif ($price == $priceinitiate + $maxheat) { #see if new price is the maximum loss from the initiated price
$position++;
print LOGFILE "$date, $time, $price ------COVER AT A LOSS POSITION = $position\n";
$priceopen = $price;
$totalprofit = $totalprofit - ($profit/100*250);
$losingtrades++;
&parserecord();
}
&parserecord();
}
&parserecord();
}
&parserecord();
}
print LOGFILE "\n\n\n\n\nFor the following parameters:\n";
print LOGFILE "Reversal amount: $reversal points\n";
print LOGFILE "Maximum allowable loss: $maxheat points\n";
print LOGFILE "Profit Objective: $profit points\n\n\n";
print LOGFILE "The results are:\n";
print LOGFILE "Number of profitable trades: $profitabletrades\n";
print LOGFILE "Number of losing trades: $losingtrades\n";
print LOGFILE "Total Profit or Loss for these parameters: \$$totalprofit\n";
if ($profitabletrades>0 or $losingtrades>0) {
$success = ($profitabletrades/($profitabletrades+$losingtrades)*100);
print LOGFILE "Rate of success under these parameters: %$success\n\n\n\n\n\n\n\n\n\n\n\n";
}
else {
print LOGFILE "No rate of success under these parameters, as no trades would have been executed.\n\n\n\n\n\n\n\n\n\n\n\n";
}
$counter=1;
$losingtrades = 0;
$profitabletrades = 0;
$totalprofit = 0;
close(TESTFILE);
}
$counter=1;
}
$counter=1;
}
close(LOGFILE);
##subroutine to parse each record as it comes through from the textfile
sub parserecord {
@element3=$opendata[$counter];
foreach $record2 (@opendata) {
($name, $date, $misc, $time, $price, $misc2, $misc3, $misc4)=split(/,/, $record2); #split each record into an array
}
$counter++;
}