Hi,
I'm not a great programmer, but I've spent a lot time with this problem and I would greatly appreciate any help.
So here's the idea:
I run load() from a class called menu. load() is located in a class called astock(). load() collects data from a file and loads it into an array called data of objects called stockdays. stockday is a class I wrote containing some doubles and integers.
Then while loading this data, I examine it to see if price changes on different days meet threshold values set in menu.
The crazy thing is, if I don't include the line: data[i].direction = 0; (marked by *** below) right before I run if-statements to examine price changes I get this behavoir:
each data[i].direction is equal to data[i-1].direction (the previously analyzed piece). For example, if the thresholds are met and the if-statement runs then data.direction is changed and so if every single one afterwards even though in my constructor for stockday I explicitly state direction = 0 (initialization really).
This problem is fixed if I include the ***'d line, but then I arrive at another problem.
In the next method run called posJumpEx() every single data[].direction is equal to zero.
I've written my stockday class with public variables. This may be bad form, I was just looking for a way to easily modify them. Could this be the problem?
I'm going to attach my code below, I apologize it's so long, but I just cant seem to find a way to just post the explanatory parts:
And it looks like all the tabs are screwed up, let me know if an email of the txt files would help. Thanks!
\\_____HERE is the stockday class________ public class stockday { public double high, low, price; public long volume; public int direction; // 0 unassigned, 1 negative, 2 positive public stockday (double fhigh, double flow, double fprice, long fvolume) //Constructor { high = fhigh; low = flow; price = fprice; volume = fvolume; direction = 0; } }\\__________Here is the astock class, it uses the stockday objects ______________ public class astock { public String ticker; public static int numDays; public stockday data[]; public double pthresh, nthresh, pthrlen, nthrlen; public counter //Counts the pos and neg jumps cpos, cneg; public astock(String fTicker, int fNumDays, double fpthresh, double fpthrlen, double fnthresh, double fnthrlen) { //Constructor data = new stockday[fNumDays]; ticker = fTicker; numDays = fNumDays; pthresh = fpthresh; pthrlen = fpthrlen; nthresh = fnthresh; nthrlen = fnthrlen; stockday sd = new stockday(0,0,0,0); for(int i=0; i <numDays; i++) //Need to initialize all stockday objects in array data[] { data[i] = sd; } cpos = new counter(); cneg = new counter(); } public void load() { //Loads and marks jump locations System.out.println(ticker); counter c1 = new counter(); //Creates a counter try { FileInputStream gstream = new FileInputStream("C://Programming//StockData//" + ticker + ".txt"); DataInputStream stkIn = new DataInputStream(gstream); String str; double prevprice = 0; //yesterdays price double pchg = 0; //percentage change from yesterday for (int i = 0; i < numDays; i++) { str = stkIn.readLine(); String fields[] = str.split(","); data[i].price = Double.parseDouble(fields[0]); data[i].high = Double.parseDouble(fields[3]); data[i].low = Double.parseDouble(fields[2]); data[i].volume = Long.parseLong(fields[1]); //System.out.println("price: " + data[i].price + "\tVolume: " + data[i].volume + "\tLow: " + data[i].low + "\tHigh: " + data[i].high); //Determine % chg, then set directions ********data[i].direction = 0; ******** if(i > 0) //Make sure we're not in the first run of the loop { pchg = (data[i].price - prevprice)/prevprice; if( pchg < nthresh && pchg > (nthresh+nthrlen)) { data[i].direction = 1; cneg.click(); //add one to the counter to determine number of jumps } if( pchg > pthresh && pchg < (pthresh+pthrlen)) { data[i].direction = 2; cpos.click(); } } System.out.println("price change: " + pchg + "\tdirection: " + data[i].direction); prevprice = data[i].price; } } catch (IOException e) {} System.out.println("Number of Pos Jumps: " + cpos.get()); } public void posJumpEx() { //Gets jump/correction magnitudes double corrPerc[] = new double[cpos.get()+1]; int n=0; for(int i=0; i < numDays; i++) { //System.out.println("Direction: " + data[i].direction); if(data[i].direction == 2) { corrPerc[n] = (data[i+1].price - data[i].price)/data[i].price; System.out.println("Correction %: " + corrPerc[n]); n++; } } }\\___________Here is the menu class_____________ vary straightforward public class menu { private static double pthresh = .08, pthrlen = .07, nthresh = -.08, nthrlen = -.07; private static int numDays = 1249; public static void main (String args[]) { astock Fdata = new astock("CNX", numDays, pthresh, pthrlen, nthresh, nthrlen); Fdata.load(); Fdata.posJumpEx(); } }