Get data from CSV

A strategy sample, which demonstrates CSV file parsing. When strategy starts, it takes file by a given path and parses it. It looks in each line and search for second occurrence of "," symbol. It takes all characters to the left from "," symbol till space symbol accrues. It converts value to Double and stores it in Array. After that it prints out all gathered values on console.

EURUSD_history.csv

2009.09.24 00:00:00,1.4729,1.47275,4000000,1600000
2009.09.24 00:00:00,
2009.09.24 00:00:01,1.4729,1.47275,2400000,4000000
2009.09.24 00:00:04,1.4729,1.47275,2400000,1600000
2009.09.24 00:00:04,1.47295,1.4729,8800000,1600000
2009.09.24 00:00:04,1.47295,1.47285,3200000,6400000
2009.09.24 00:00:05,1.47305,1.47285,7600000,9300000
2009.09.24 00:00:05,1.47305,1.47285,2400000,9300000
2009.09.24 00:00:05,1.47305,1.473,1000000,1000000
2009.09.24 00:00:06,1.47315,1.473,12500000,1000000
package jforex;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

import com.dukascopy.api.*;

/**
 * This is strategy sample, which demonstrates CSV file parsing.
 * When strategy starts, it takes file by a given path and parses it. It looks in 
 * each line and search for second occurrence of "," symbol. It takes all characters
 * to the left from "," symbol till space symbol accrues. Converts value to Double 
 * and stores it in Array. After that it takes all gathered values prints out on console.  
 * 
**/

/*
 * When strategy loader sees this annotation for strategy class it asks user if he wants 
 * to allow the strategy full access. If such acceptance receives strategy classes, they 
 * are loaded with full access policy allowing to read/write any files, use sockets etc.
 * Always try to avoid using this annotation, if only need to read/write to file - you 
 * have full access to "~/My Documents/My Strategies/files" folder. In this folder you 
 * can read or write any file without requesting the full access.
 * 
 * Because "~\My Documents\My Strategies\files" folder dosen't requires any additional access. 
 * In this case you should put your files in directory similar to this on 
 * "C:\Documents and Settings\john.smith\My Documents\My Strategies\files". 
 * Thus there is no needs to use @RequiresFullAccess annotation.
 *    
 */
@RequiresFullAccess
public class GetCSVDataStartegy implements IStrategy {

    private IConsole console;

    public void onStart(IContext context) throws JFException {
        this.console = context.getConsole();

        //putting out all data to console
        try {
           //Iterating thru parsed array
            for(Double d : parse("C:/tmp/EURUSD_history.csv")){
                this.console.getOut().println(d);
            }
        } catch (Exception e) {            
          this.console.getErr().println(e.getMessage());
        }

    }

    public void onAccount(IAccount account) throws JFException {        
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
    }

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }

    /**
     * Takes StringTokenizer as input and returns second token
     * If token dosen't exists return String "0"
     * 
     * @param StringTokenizer st
     * @return String token
     */
    private static String secondToken(StringTokenizer st){
        try{
            st.nextToken();
            return st.nextToken();
        }catch (NoSuchElementException nsee){
           //if in token String is no token symbols 
           //we just return 0   
            return "0";
        }        
    }

    /**
     * Parse file by given path in directory tree. Takes from file each strokes 
     * second token and place it as Double into ArrayList 
     * 
     * @param path - file path
     * @return ArrayList of Doubles  
     * @throws IOException
     */
    private static List<Double> parse(String filePath) throws IOException{

        String path = filePath;  

        List<Double> prices = new ArrayList<Double>();
        File file = new File(path);

        BufferedReader bufRdr  = new BufferedReader(new FileReader(file));
        String line = null;

        //read each line of text file        
        while((line = bufRdr.readLine()) != null)
        {
           StringTokenizer st = new StringTokenizer(line,",");

            //get token and store it as Double in the array
            prices.add(Double.valueOf(secondToken(st)));

        }
        //close the file
        bufRdr.close();

        return prices;
    }
}

GetCSVDataStartegy.java

The information on this web site is provided only as general information, which may be incomplete or outdated. Click here for full disclaimer.