Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help - big newbie needs help!

Status
Not open for further replies.

altaratz

ISP
Apr 15, 2001
73
US
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, &quot;>logfile.txt&quot;);

##########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, &quot;epz.txt&quot;);

@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 &quot;$date, $time, $price ------INITIATE LONG POSITION = $position\n&quot;;


$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 &quot;$date, $time, $price ------INITIATE SHORT POSITION = $position\n&quot;;


$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 &quot;$date, $time, $price ------SELL PROFITABLE POSITION = $position\n&quot;;

$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 &quot;$date, $time, $price ------SELL AT A LOSS POSITION = $position\n&quot;;

$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 &quot;$date, $time, $price ------COVER PROFITABLE POSITION = $position\n&quot;;

$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 &quot;$date, $time, $price ------COVER AT A LOSS POSITION = $position\n&quot;;

$priceopen = $price;
$totalprofit = $totalprofit - ($profit/100*250);
$losingtrades++;
&parserecord();

}

&parserecord();

}

&parserecord();

}

&parserecord();

}

print LOGFILE &quot;\n\n\n\n\nFor the following parameters:\n&quot;;
print LOGFILE &quot;Reversal amount: $reversal points\n&quot;;
print LOGFILE &quot;Maximum allowable loss: $maxheat points\n&quot;;
print LOGFILE &quot;Profit Objective: $profit points\n\n\n&quot;;
print LOGFILE &quot;The results are:\n&quot;;

print LOGFILE &quot;Number of profitable trades: $profitabletrades\n&quot;;
print LOGFILE &quot;Number of losing trades: $losingtrades\n&quot;;
print LOGFILE &quot;Total Profit or Loss for these parameters: \$$totalprofit\n&quot;;

if ($profitabletrades>0 or $losingtrades>0) {

$success = ($profitabletrades/($profitabletrades+$losingtrades)*100);
print LOGFILE &quot;Rate of success under these parameters: %$success\n\n\n\n\n\n\n\n\n\n\n\n&quot;;

}
else {

print LOGFILE &quot;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&quot;;

}

$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++;

}
 
Let's start with this part:
Code:
@opendata = <TESTFILE>;          
@element=<TESTFILE>;
$lengtharray = @element;
@Open=$element[0];
foreach $record (@Open) {
($nameopen,$dateopen,$miscopen,$timeopen,$priceopen,$miscopena,$miscopenb,$miscopenc)=split(/,/,$record);
}
When a <FILEHANDLE> is used in a LIST context, it returns a list of ALL the input lines of the file. Therefore @opendata gets ALL of TESTFILE, and @element gets UNDEFINED!

Even if @element did have data in it, you assign ONE element of that array to another array, which means @Open would have, at best, only a single element, which sort of makes the following foreach useless.

What are you trying to accomplish here? Maybe we can help. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Hi! Thanks for taking the time!

It's probably easier if I print a sample bit of the data I'm testing:

F.US.EPZ01,20011015,0,1730,109275,T,N,N
F.US.EPZ01,20011015,0,1730,109250,B,N,N
F.US.EPZ01,20011015,0,1730,109275,A,N,N
F.US.EPZ01,20011015,0,1730,109275,A,N,N
F.US.EPZ01,20011015,0,1730,109250,T,N,N
F.US.EPZ01,20011015,0,1730,109225,B,N,N
F.US.EPZ01,20011015,0,1730,109275,A,N,N
F.US.EPZ01,20011015,0,1730,109275,A,N,N
F.US.EPZ01,20011015,0,1730,109250,A,N,N
F.US.EPZ01,20011015,0,1730,109250,A,N,N
F.US.EPZ01,20011015,0,1730,109225,B,N,N
F.US.EPZ01,20011015,0,1730,109225,T,N,N
F.US.EPZ01,20011015,0,1730,109200,B,N,N
F.US.EPZ01,20011015,0,1730,109225,A,N,N
F.US.EPZ01,20011015,0,1730,109200,B,N,N
F.US.EPZ01,20011015,0,1730,109225,T,N,N
F.US.EPZ01,20011015,0,1730,109250,A,N,N
F.US.EPZ01,20011015,0,1730,109225,B,N,N
F.US.EPZ01,20011015,0,1730,109250,T,N,N
F.US.EPZ01,20011015,0,1730,109300,A,N,N
F.US.EPZ01,20011015,0,1730,109300,A,N,N
F.US.EPZ01,20011015,0,1730,109225,T,N,N
F.US.EPZ01,20011015,0,1730,109200,B,N,N
F.US.EPZ01,20011015,0,1730,109225,A,N,N
F.US.EPZ01,20011015,0,1730,109225,T,N,N
F.US.EPZ01,20011015,0,1730,109300,A,N,N
F.US.EPZ01,20011015,0,1730,109300,A,N,N
F.US.EPZ01,20011015,0,1730,109225,B,N,N
F.US.EPZ01,20011015,0,1730,109275,A,N,N
F.US.EPZ01,20011015,0,1730,109250,A,N,N
F.US.EPZ01,20011015,0,1730,109225,T,N,N
F.US.EPZ01,20011015,0,1730,109200,B,N,N
F.US.EPZ01,20011015,0,1730,109275,A,N,N
F.US.EPZ01,20011015,0,1730,109200,T,N,N
F.US.EPZ01,20011015,0,1730,109200,B,N,N
F.US.EPZ01,20011015,0,1730,109275,A,N,N
F.US.EPZ01,20011015,0,1730,109200,B,N,N
F.US.EPZ01,20011015,0,1730,109225,B,N,N
F.US.EPZ01,20011015,0,1730,109225,B,N,N
F.US.EPZ01,20011015,0,1730,109250,A,N,N
F.US.EPZ01,20011015,0,1730,109250,A,N,N
F.US.EPZ01,20011015,0,1730,109250,A,N,N
F.US.EPZ01,20011015,0,1730,109225,T,N,N
F.US.EPZ01,20011015,0,1730,109225,B,N,N
F.US.EPZ01,20011015,0,1730,109225,T,N,N
F.US.EPZ01,20011015,0,1730,109200,B,N,N
F.US.EPZ01,20011015,0,1730,109250,T,N,N
F.US.EPZ01,20011015,0,1730,109250,A,N,N
F.US.EPZ01,20011015,0,1730,109250,A,N,N
F.US.EPZ01,20011015,0,1730,109250,A,N,N
F.US.EPZ01,20011015,0,1730,109200,B,N,N
F.US.EPZ01,20011015,0,1730,109250,A,N,N
F.US.EPZ01,20011015,0,1730,109200,T,N,N
F.US.EPZ01,20011015,0,1730,109200,T,N,N
F.US.EPZ01,20011015,0,1730,109175,B,N,N
F.US.EPZ01,20011016,0,1730,109200,A,N,N
F.US.EPZ01,20011016,0,1730,109225,B,N,N
F.US.EPZ01,20011016,0,1730,109250,A,N,N
F.US.EPZ01,20011016,0,1730,109275,A,N,N
F.US.EPZ01,20011016,0,1730,109300,B,N,N
F.US.EPZ01,20011016,0,1730,109325,A,N,N
F.US.EPZ01,20011016,0,1731,109350,B,N,N
F.US.EPZ01,20011016,0,1731,109375,A,N,N
F.US.EPZ01,20011016,0,1731,109400,A,N,N
F.US.EPZ01,20011016,0,1731,109425,A,N,N
F.US.EPZ01,20011016,0,1731,109450,T,N,N
F.US.EPZ01,20011016,0,1731,109475,B,N,N
F.US.EPZ01,20011016,0,1731,109500,A,N,N
F.US.EPZ01,20011016,0,1731,109525,A,N,N
F.US.EPZ01,20011016,0,1731,109550,A,N,N
F.US.EPZ01,20011016,0,1731,109575,A,N,N
F.US.EPZ01,20011016,0,1731,109600,T,N,N
F.US.EPZ01,20011016,0,1731,109625,B,N,N
F.US.EPZ01,20011016,0,1731,109650,A,N,N
F.US.EPZ01,20011016,0,1731,109675,A,N,N
F.US.EPZ01,20011016,0,1731,109700,T,N,N
F.US.EPZ01,20011016,0,1731,109725,A,N,N
F.US.EPZ01,20011016,0,1731,109750,A,N,N
F.US.EPZ01,20011016,0,1731,109775,B,N,N
F.US.EPZ01,20011016,0,1731,109800,T,N,N
F.US.EPZ01,20011016,0,1731,109825,A,N,N
F.US.EPZ01,20011016,0,1731,109850,T,N,N
F.US.EPZ01,20011016,0,1731,109875,A,N,N
F.US.EPZ01,20011016,0,1731,109900,A,N,N
F.US.EPZ01,20011016,0,1731,109875,B,N,N
F.US.EPZ01,20011016,0,1731,109850,T,N,N


The script is to run through a datafile (TESTFILE) that has stock price data. So, I need to establish an open price, time and date for the day, being the first line of data - that's what the @Open array is for. I split that one line of data to establish this (and redo it, every time I try a new set of values for the 3 testing variables, as dictated by the superceding 3 nested for loops at the top).

Then, the rest of the script runs through the rest of the data (digesting each new line with subroutine parserecord - at the bottom)

I use $lengtharray as the number of lines of data in the datafile to make the testing loop run only a many times as the number of price data that exists.

You're right about equating the 2 different arrays to the datafile, I've just erased the @opendata array, and use the @element array only.

Anyway, I really thank you for your time, again.

@opendata = <TESTFILE>;
@element=<TESTFILE>;
$lengtharray = @element;
@Open=$element[0];
foreach $record (@Open) {
($nameopen,$dateopen,$miscopen,$timeopen,$priceopen,$miscopena,$miscopenb,$miscopenc)=split(/,/,$record);
}

 
I don't know much about stocks, so I'm still a little at a loss as to what you're trying to do, but here's a few more suggestions. First, you only need to read the data file into the array ONCE, before you ever start processing. You can then just reprocess the array as many times as you need to. Second, you might consider using additional subroutines to make the code more understandable. It's hard to follow code when the closing bracket for a loop is so far below where the loop opens, especially since your indentation doesn't help. Third, even if you omit the @opendata array assignment, you still have a problem with this code:
Code:
@element=<TESTFILE>;
$lengtharray = @element;
@Open=$element[0];
foreach $record (@Open) {
($nameopen,$dateopen,$miscopen,$timeopen,$priceopen,$miscopena,$miscopenb,$miscopenc)=split(/,/,$record);
}
Because the array @Open will contain only the FIRST element of @element, which makes it an array of ONE RECORD. So your foreach statement will get executed only ONCE. I'm not sure that's what you intended.

The same type of problem is true of you ParseRecord subroutine: you assign a single element to an array and then do a foreach on the array. The foreach will be executed only ONCE.

That's enough for this iteration. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
OK - I see what you're saying, so is this a legal split statement?

@Open=$element[0];


($nameopen,$dateopen,$miscopen,$timeopen,$priceopen,$miscopena,$miscopenb,$miscopenc)=split(/,/,@Open); # get the open price in the list for comparison


or can I say this?

open (TESTFILE, &quot;epz.txt&quot;);

@element=<TESTFILE>;
@Open=$element[0];


($nameopen,$dateopen,$miscopen,$timeopen,$priceopen,$miscopena,$miscopenb,$miscopenc)=split(/,/,@Open); # get the open price in the list for comparison



or should I say this?

open (TESTFILE, &quot;epz.txt&quot;);

@element=<TESTFILE>;
@Open=$element[0];


($nameopen,$dateopen,$miscopen,$timeopen,$priceopen,$miscopena,$miscopenb,$miscopenc)=split(/,/,@element[0]); # get the open price in the list for comparison


thanks!
 
I wouldn't bother with the @Open array, since it's an array of only one element. I'd do it one of these ways:

Just split the first element of @element
Code:
open (TESTFILE, &quot;epz.txt&quot;);

@element=<TESTFILE>;

($nameopen,$dateopen,$miscopen,$timeopen,$priceopen,$miscopena,$miscopenb,$miscopenc)=split(/,/,$element[0]);

or assign the first element of @element to a SCALAR variable
Code:
open (TESTFILE, &quot;epz.txt&quot;);

@element=<TESTFILE>;
$Open = $element[0];

($nameopen,$dateopen,$miscopen,$timeopen,$priceopen,$miscopena,$miscopenb,$miscopenc)=split(/,/,$Open);

Note that in the last option you gave above you're splitting @element[0] instead of $element[0]. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
I would say:
[tt]
open (TESTFILE, &quot;epz.txt&quot;);
@element=<TESTFILE>;
foreach $record (@element) {
# get the open price in the list for comparison
[tab](
[tab][tab]$nameopen, $dateopen, $miscopen, $timeopen,
[tab][tab]$priceopen,$miscopena, $miscopenb, $miscopenc
[tab])=split(/,/,$record);
[tab]# and the process the data
}
[/tt]

In english this code would read:
read the file epz.txt into the array named @element
work through the array element by element (line by line) and split each element into 8 fields
and then do something with it...

Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top