public static void main(String[] args)throws IOException 
    {        
        File directory = new File("/home/garth/Desktop/stock");                            //directory of program files
        File[] files = directory.listFiles(); 
        List<String> allMatches = new ArrayList<String>();
            for(int i=0;i<files.length;i++){
                Matcher m = Pattern.compile(".*csv.*").matcher(files[i].toString());       //finds all files that match the regex
                while (m.find())
                {
                    allMatches.add(m.group());
                }
            }
            File[] stockFiles = new File[allMatches.toArray().length];
            for(int i =0;i<allMatches.toArray().length;i++)
            {
                File temp=new File(allMatches.toArray()[i].toString());
                stockFiles[i]=temp;
            }   
            
            for(int f=0;f<allMatches.toArray().length;f++){
                double money=100;
                double shares=0;
                double sharePrice=0;
                String fileName=stockFiles[f].getAbsolutePath();
                double finalAnswer=-99;
                final XYSeriesCollection dataset = new XYSeriesCollection();
                XYSeries portfolio = new XYSeries("Portfolio");
                XYSeries base = new XYSeries("Base");
                double[] bestParams= new double[2];
                int lineNumber = findNumDays(fileName);
                String line = new String();
                String[] stringArray =new String[7];
                double[] Prices = new double[lineNumber];
                BufferedReader br = new BufferedReader( new FileReader(fileName));
                line = br.readLine();
                for(int i =0;i<lineNumber-1;i++)
                        {
                            line = br.readLine();
                            stringArray = line.toString().split(",");
                            Prices[lineNumber-1-i]= Double.parseDouble(stringArray[3]);           //initialise array of daily lows
                        }
                double Base=100/Prices[100];
                for(int day=100;day<400;day++){
                    bestParams=getOptimalParams(day,dataset,fileName,lineNumber,Prices);
                    double[] Money=new double[3];
                    Money=takePosition(day+1,bestParams,fileName,lineNumber,Prices,money,shares);
                    money=Money[0];
                    shares=Money[1];
                    sharePrice=Money[2];
                    
                    //System.err.println("money"+money+"  shares"+shares);
                }
                Base=Base*Prices[400];
                System.out.println("Portfolio: "+(money+shares*sharePrice));
                System.out.println("base: "+Base);
                dataset.removeAllSeries();
                dataset.addSeries(portfolio);
                dataset.addSeries(base);
                //JFreeChart chart = createChart(dataset);
                //final ChartPanel chartPanel = new ChartPanel(chart);
                //chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
                //final CalcWithJFreeChart demo = new CalcWithJFreeChart(dataset,"Line Chart Demo 6");
                //demo.pack();
                //RefineryUtilities.centerFrameOnScreen(demo);
                //demo.setVisible(true);

            }
        System.out.println("The best parameters are : ");
    }