Example usage for org.joda.time DateTime DateTime

List of usage examples for org.joda.time DateTime DateTime

Introduction

In this page you can find the example usage for org.joda.time DateTime DateTime.

Prototype

public DateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour) 

Source Link

Document

Constructs an instance from datetime field values using ISOChronology in the default time zone.

Usage

From source file:ch.imetrica.mdfaTradingStrategies.MDFAStrategyEvolution.java

public boolean startStrategyDailyIntradayOneHour() {

    int j, i, l, N, file_count;
    double sum = 0;
    Double D;//from  w  ww  . ja  v a2 s  .  co  m
    String ddelims = "[-]";
    boolean computed = false;
    boolean print_filter = false;
    boolean made_trade = true;
    String date_stamp, strline;
    int daily_size;
    double profit, price_borrowed, price_sold, price_bought;
    double current_price, prev_price;
    double last_price, cur_pnl, stop_loss, lo_pnl, hi_pnl;
    double log_ret = 0;
    signal = new double[trade_obs];
    xt = new double[trade_obs];
    lag_signals = new double[trade_obs];
    prix = new double[trade_obs];
    lo_prix = new double[trade_obs];
    hi_prix = new double[trade_obs];
    total_succ = 0;
    total = 0;
    log_price = 0;
    N = n_obs;
    avg_vol = 0.0;
    b_avg = new double[L * n_rep];
    count = 0;
    trade_succ_ratio = 0;
    double amount = 0;
    double prev_signal;
    reg_trading_hours = false;
    String[] intdates;
    //make sure arraylists empty
    ArrayList<String> perf_dates = new ArrayList<String>();
    ArrayList<Double> perf_returns = new ArrayList<Double>();
    double pnl;
    String[] dates;
    boolean inverse_hours = false;
    String time;
    ArrayList<String> account = new ArrayList<String>();
    ArrayList<String> sunday = new ArrayList<String>();
    ArrayList<String> latestDates = new ArrayList<String>();
    last_trades = new ArrayList<Integer>();
    final_trades = new ArrayList<Double>();
    dailyoutret = new ArrayList<Double>();
    maxIntValue = new ArrayList<Double>();
    avg_volatility = new ArrayList<Double>();
    close_series = new ArrayList<Double>();
    highlow_series = new ArrayList<Double>();
    exp_series_1 = new ArrayList<Double>();
    exp_series_2 = new ArrayList<Double>();
    price = new ArrayList<Double>();
    lo_price = new ArrayList<Double>();
    hi_price = new ArrayList<Double>();
    mid = new ArrayList<Double>();
    bid = new ArrayList<Double>();
    ask = new ArrayList<Double>();
    dates_series = new ArrayList<String>();
    dailyReport = new ArrayList<String>();
    b0_trend = new ArrayList<Double>();
    vol_0 = new ArrayList<Double>();
    vol_1 = new ArrayList<Double>();
    sub_returns = new ArrayList<Double>();
    trade_days = new ArrayList<String>();
    returns = new ArrayList<Double>();
    longreturns = new ArrayList<Double>();
    shortreturns = new ArrayList<Double>();
    dropdowns = new ArrayList<Double>();
    success = new ArrayList<Double>();
    dates_low_high = new ArrayList<String>();
    crits = new ArrayList<String>();
    svm = new ArrayList<String>();
    filters = new ArrayList<Filter>();
    date_returns = new ArrayList<String>();

    live_series = new ArrayList<Double>(); //the data to be applied out of sample
    ib_data_hash = new ibHash();

    fridayROI = 0;
    fridayROI_pos = 0;
    fridays = 0;
    int end_hour;
    lookback_returns = new ArrayList<Double>();
    num_pos_returns = 0;
    deg_0 = new ArrayList<Double>();
    deg_1 = new ArrayList<Double>();
    crit_0 = new ArrayList<Double>();
    crit_1 = new ArrayList<Double>();
    full_returns_array = new ArrayList<double[]>();
    morning_returns = new ArrayList<double[]>();

    morning_buy = true; //enter transaction at morning open
    morning_optimize = false; //optimize in the morning trading hours
    num_full_positive_returns = 0;
    //--- Now get historical interp values ------
    //uploadInterpParams("max_int.dat"); 
    //-------------------------------------------
    forex24 = true;
    ret_dist = new double[trade_obs];
    pos_ret_dist = new int[trade_obs];
    neg_ret_dist = new int[trade_obs];
    neg_trades_started = new int[trade_obs];
    pos_trades_started = new int[trade_obs];
    neg_trades_started_mean = new double[trade_obs];
    pos_trades_started_mean = new double[trade_obs];
    diff_account = new double[trade_obs];
    pos_ret_mean_time = new double[trade_obs];
    neg_ret_mean_time = new double[trade_obs];

    mdfaTrades = new ArrayList<MDFATrade>();
    fmt = DateTimeFormat.forPattern("y-MM-dd HH:mm:ss");
    formatter = new DecimalFormat("#0.000000");
    formatter3 = new DecimalFormat("#0.00000");
    formatter2 = new DecimalFormat("#0.00");
    histo_stat = new int[100];
    interp_vals = new ArrayList<Double>();
    max_ranks = new ArrayList<Double>();
    profit_baby = 0;
    //setForecastDFAParameters();
    bad_starts = 0;
    n_out_samp = 0;

    //take_profit = true;
    //take_profit_thresh = .0020;
    current_signal = 0;
    prev_price = 0;
    cur_pnl = 0;
    stop_loss = stop_loss_thresh;
    out_transaction = 0;
    in_transaction = 0;
    red_zone = false;
    global_stop_loss = stop_loss_thresh;
    profitable_stop = .0005;
    count = 0;
    short_sell = true;
    long_buy = true;
    day_count = 0;
    ArrayList<String> trade_times = new ArrayList<String>();

    lo_pnl = 0;
    hi_pnl = 0;
    price_borrowed = 0;
    price_sold = 0;
    price_bought = 0;
    last_price = 0;

    binary_rule = true;
    signal_strength_rule = true;
    downtick_strategy = false;
    signal_profit = false;
    friday_closing = false;
    //asian_close = true;

    //----- Fill up with times here -------------------------
    for (i = 0; i < 10; i++) {
        trade_times.add("0" + i + ":00:00");
    }
    for (i = 10; i < 24; i++) {
        trade_times.add(i + ":00:00");
    }
    //       trade_times.add("00:00:00");
    //       trade_times.add("06:00:00");
    //       trade_times.add("12:00:00");
    //       trade_times.add("18:00:00");     

    String[] hourToks = startingTime.split("[:]+");
    start_hour = (new Integer(hourToks[0])).intValue();

    hourToks = endingTime.split("[:]+");
    end_hour = (new Integer(hourToks[0])).intValue();

    inverse_hours = false;
    if (start_hour > end_hour) //must switch hours here
    {
        int temphour = end_hour;

        inverse_hours = true;
        end_hour = start_hour;
        start_hour = temphour;
    }

    if (ib_data && ib_data_file != null) {
        try {

            fin = new FileInputStream(ib_data_file);
            din = new DataInputStream(fin);
            br = new BufferedReader(new InputStreamReader(din));

            while ((strline = br.readLine()) != null) {
                String[] sp = strline.split("[,]+");
                ib_data_hash.put(sp[0], new String(
                        sp[1] + " " + sp[2] + " " + sp[3] + " " + sp[4] + " " + sp[5] + " " + sp[6]));
                //System.out.println(sp[1] + " " + sp[2] + " " + sp[3] + " " + sp[4] + " " + sp[5]  + " " + sp[6]);
            }
        } catch (FileNotFoundException fe) {
            System.out.println("File not found..." + fe);
        } catch (IOException ioe) {
            System.out.println("IO procedure faulty..." + ioe);
        }
    }

    String[] myname = dataFiles[0].split("[.]+");

    try {

        PrintWriter b0_coeff = new PrintWriter(new FileWriter("b0_coeff.dat"));
        PrintWriter perform = new PrintWriter(new FileWriter("intraday_performance_" + n_files + ".dat"));
        PrintWriter dailyout = new PrintWriter(new FileWriter("daily_nasdaq.dat"));
        PrintWriter out = new PrintWriter(new FileWriter("strategy_results_" + myname[0] + ".dat"));
        PrintWriter svmout = new PrintWriter(new FileWriter("neural.dat"));

        for (file_count = 0; file_count < 1; file_count++) {

            if (dataFiles[file_count].indexOf("JPY") != -1 || dataFiles[file_count].indexOf("Y") != -1) {
                //change_time_zone = true; System.out.println("Changed time zone to Tokyo");
                jpy = true;
                stop_loss_thresh = stop_loss_thresh * 100;
                take_profit_thresh = take_profit_thresh * 100;
                global_stop_loss = global_stop_loss * 100;
                stop_loss = stop_loss_thresh;
            } else if (futures_data) {
                //change_time_zone = true; System.out.println("Changed time zone to Tokyo");

                stop_loss_thresh = stop_loss_thresh * 10000;
                stop_loss = stop_loss_thresh;
                take_profit_thresh = take_profit_thresh * 10000;
                global_stop_loss = global_stop_loss * 10000;
            } else if (dataFiles[file_count].indexOf("MXN") != -1) {
                stop_loss_thresh = stop_loss_thresh * 10;
                take_profit_thresh = take_profit_thresh * 10;
                global_stop_loss = global_stop_loss * 10;
                stop_loss = stop_loss_thresh;
            } else if (dataFiles[file_count].indexOf("HKD") != -1) {
                stop_loss_thresh = stop_loss_thresh * 10;
                take_profit_thresh = take_profit_thresh * 10;
                global_stop_loss = global_stop_loss * 10;
                stop_loss = stop_loss_thresh;
            } else if (dataFiles[file_count].indexOf("XAU") != -1) {
                stop_loss_thresh = stop_loss_thresh * 1000;
                take_profit_thresh = take_profit_thresh * 1000;
                global_stop_loss = global_stop_loss * 1000;
                stop_loss = stop_loss_thresh;
            } else if (dataFiles[file_count].indexOf("GC") != -1) {
                stop_loss_thresh = stop_loss_thresh * 1000;
                take_profit_thresh = take_profit_thresh * 1000;
                global_stop_loss = global_stop_loss * 1000;
                stop_loss = stop_loss_thresh;
            } else if (dataFiles[file_count].indexOf("JNJ") != -1) {
                stop_loss_thresh = stop_loss_thresh * 100;
                take_profit_thresh = take_profit_thresh * 100;
                global_stop_loss = global_stop_loss * 100;
                stop_loss = stop_loss_thresh;
            }

            setTimeStandards(new File(dataFiles[file_count]));
            System.out.println("opening " + dataFiles[file_count]);
            fin = new FileInputStream(dataFiles[file_count]);
            din = new DataInputStream(fin);
            br = new BufferedReader(new InputStreamReader(din));
            lookback_ready = false;
            spread = new PrintWriter(new FileWriter("spread_" + dataFiles[file_count] + ".dat"));
            //if(print_debug)System.out.println("Entering loop...");
            trading_hours = false;
            computed = false;
            while ((strline = br.readLine()) != null) {

                //System.out.println(strline);
                tokens = strline.split(delims);
                n_toks = tokens.length; //System.out.println("Number of toks = "+n_toks);
                if (n_toks == 0) {
                    System.out.println("End of file");
                    break;
                }

                if (n_toks >= 6) {
                    bid_ask_data = true;
                } else {
                    bid_ask_data = false;
                }

                date_stamp = tokens[0];
                date_tokens = date_stamp.split(date_delims);
                intdates = date_tokens[0].split(ddelims);
                DateTime weekend = new DateTime((new Integer(intdates[0])).intValue(),
                        (new Integer(intdates[1])).intValue(), (new Integer(intdates[2])).intValue(), 14, 0);
                time = date_tokens[1];

                //insampStart is the time we collect daily data 

                //if(date_stamp.indexOf(insampStart) != -1)
                if (trade_times.contains(time)) {

                    //get bid/mid/ask data
                    if (ib_data && ib_data_hash.containsKey(tokens[0])) {
                        String[] hashed = ib_data_hash.get(tokens[0]).split("[ ]+");
                        for (i = 1; i < hashed.length; i++) {
                            tokens[i] = hashed[i];
                        } // System.out.print(tokens[i] + " ");} 
                    }

                    daily_price.add(new Double(tokens[1]));
                    current_price = (new Double(tokens[1])).doubleValue();

                    if (daily_price.size() == 1) {
                        daily_returns.add(new Double(0.0));
                        prev_price = current_price;
                    } else {
                        daily_returns.add(log(current_price) - log(prev_price));
                        prev_price = current_price;
                    }

                    daily_dates.add(date_stamp);

                }

                print_filter = false;
                latestDates.add(date_stamp);
                D = new Double(tokens[4]);
                close_series.add(D);
                if (ib_data && ib_data_hash.containsKey(tokens[0])) {

                    String[] hashed = ib_data_hash.get(tokens[0]).split("[ ]+");
                    //System.out.println("Contains " + tokens[0] + ", lengths = " + hashed.length + ", " + tokens.length);
                    for (i = 1; i < hashed.length; i++) {
                        tokens[i] = hashed[i];
                    } // System.out.print(tokens[i] + " ");} 
                    bid.add(new Double(tokens[2]));
                    ask.add(new Double(tokens[3]));
                    mid.add(new Double(tokens[1]));
                } else {
                    bid.add(new Double(tokens[2]));
                    ask.add(new Double(tokens[3]));
                    mid.add(new Double(tokens[1]));
                }

                D = new Double(tokens[1]);
                price.add(D);

                D = new Double(tokens[4]);
                if (ib_data && ib_data_hash.containsKey(tokens[0])) {
                    live_series.add(log(mid.get(mid.size() - 1)) - log(mid.get(mid.size() - 2)));
                } else {
                    live_series.add(D);
                }

                if (ib_data && ib_data_hash.containsKey(tokens[0])) //use as is
                {
                    lo_price.add(new Double(tokens[7]));
                    hi_price.add(new Double(tokens[8]));
                } else {
                    lo_price.add((new Double(tokens[7])));
                    hi_price.add((new Double(tokens[8])));
                }

                //---- start the account ------
                if (account.size() == 0) {
                    account.add(date_stamp + " " + 0);
                    dailyoutret.add(0.0);
                }

                String[] hours = time.split("[:]+");
                cur_hour = (new Integer(hours[0])).intValue();
                (new Integer(hours[1])).intValue();

                trading_closed = false;

                //if currently not in a transaction and between the hours of midnight and start-hour, then no new 
                //positions will be opened

                if (asian_close) //only closed if most recent transaction was closed artificially through SL or TP after end hours
                {
                    if ((in_transaction == 0 && out_transaction == 0) && cur_hour >= start_hour
                            && cur_hour <= end_hour) {
                        trading_closed = true;
                        if (printall)
                            System.out.println(cur_hour + " " + start_hour);
                    }
                } else {
                    if (cur_hour >= start_hour && cur_hour <= end_hour) {
                        trading_closed = true;
                        if (printall) {
                            System.out.println(cur_hour + " " + start_hour);
                        }
                    }
                }

                //           if(cur_hour == 14) {release_first = true;}
                //           else {elease_first = false;}

                if (inverse_hours) {
                    trading_closed = !trading_closed;
                }

                its_closing_time = (weekend.dayOfWeek().getAsText().equals("Friday")
                        && date_stamp.indexOf("17:00:00") != -1);

                made_trade = false;
                if (daily_returns.size() >= n_obs && trade_times.contains(time)) //a new day begineth
                {

                    computed = true;
                    trading_hours = true;

                    tseries = new double[n_rep * n_obs];

                    for (i = 0; i < n_obs; i++) {
                        tseries[n_obs - 1 - i] = daily_returns.get(daily_returns.size() - 1 - i);
                        tseries[n_obs + n_obs - 1 - i] = daily_returns.get(daily_returns.size() - 1 - i);

                        if (n_rep > 2 && exp_series_1.size() > 0) {
                            tseries[n_obs * 2 + n_obs - 1 - i] = exp_series_1.get(exp_series_1.size() - 1 - i);
                        }
                        if (n_rep > 3 && exp_series_2.size() > 0) {
                            tseries[n_obs * 3 + n_obs - 1 - i] = exp_series_2.get(exp_series_2.size() - 1 - i);
                        }
                    }

                    mdfa.set_tseries(tseries, n_obs, n_rep);

                    //if(day_count == 0)  //recompute filter coefficients
                    if (day_count == 0 || weekend.dayOfWeek().getAsText().equals("Sunday")
                            && date_stamp.indexOf("18:00:00") != -1) {

                        if (printall)
                            System.out.println("Recomputing filter...");
                        mdfa.computeFilterGeneral(true, print_filter);
                        b_coeffs = new double[(n_rep - 1) * L]; //System.out.println(b_coeffs.length + " " + L + n_rep); 
                        for (l = 0; l < L; l++) {

                            for (i = 0; i < n_rep - 1; i++) {
                                b_coeffs[L * i + l] = mdfa.b[L * (i + 1) + l];
                            } // System.out.println(b_coeffs[l]);}               
                            //if(date_stamp.indexOf("2013-12-17") != -1) {System.out.println(b_coeffs[l]);}
                        }
                        if (printall)
                            System.out.println(date_stamp + " b_coeffs = " + b_coeffs[0] + " " + b_coeffs[1]
                                    + " " + b_coeffs[2]);

                        b_copy = new double[mdfa.b.length];
                        System.arraycopy(mdfa.b, 0, b_copy, 0, b_copy.length);
                        b0_coeff.println(b_coeffs[0]); // + ", " + b_coeffs[L] + ", " + b_coeffs[2*L]);

                    }

                    sum = 0.0;
                    for (j = 1; j < n_rep; j++) {
                        for (l = 0; l < L; l++) {
                            sum = sum + b_coeffs[L * (j - 1) + l] * tseries[N * j + n_obs - 1 - l];
                        }
                    }

                    prev_signal = current_signal;
                    current_signal = sum;
                    if (sig_inverse) {
                        current_signal = -current_signal;
                    }

                    if (date_stamp.indexOf("16:00:00") != -1) {
                        svmout.println(
                                date_stamp + " " + formatter3.format(daily_price.get(daily_price.size() - 1)));
                    }

                    //----final signal ---
                    daily_signal.add(current_signal);
                    daily_size = daily_price.size();
                    dailyReport.add(
                            "New day " + date_stamp + ", " + formatter3.format(daily_price.get(daily_size - 1))
                                    + ", " + formatter.format(tseries[n_obs - 1]) + ", " + current_signal);
                    //if(printall) System.out.println("New day "+date_stamp + ", " + formatter3.format(daily_price.get(daily_size-1)) + ", " + formatter.format(tseries[n_obs-1]) + ", " + current_signal);

                    //--compute binary trading rule ---

                    //if(printall) System.out.println("Current signal = " + current_signal + " Prev signal = " + prev_signal + ", trading_closed = " + trading_closed);
                    if (friday_closing && its_closing_time) {
                        if (printall)
                            System.out.println("\nIt's Friday at 5pm, time to close shop for week");
                        if (current_signal > 0 && in_transaction == 1) //in a long transaction
                        {
                            price_sold = daily_price.get(daily_size - 1);
                            profit = price_sold - price_bought;
                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            amount = getAmount(account.get(account.size() - 1), 4);
                            //account.add(date_stamp + " " + (amount + profit));
                            account.add(date_stamp + " " + daily_price.get(daily_price.size() - 1) + " "
                                    + formatter3.format(profit) + " " + formatter3.format(amount + profit));
                            amount = getAmount(account.get(account.size() - 1), 4);

                            if (weekend.dayOfWeek().getAsText().equals("Sunday")) {
                                sunday.add(date_stamp + " " + profit);
                            }

                            made_trade = true;
                            dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                            log_ret = daily_price.get(daily_size - 1).doubleValue();

                            in_transaction = 0;

                            if (printall) {
                                if (profit > 0)
                                    System.out.println("Sold for a profit of " + profit);
                                else if (profit < 0)
                                    System.out.println("Sold for a loss of " + profit);
                                System.out.println("profit, price_bought, price_sold = " + profit + ", "
                                        + price_bought + ", " + price_sold);
                            }
                        } else if (current_signal < 0 && out_transaction == 1) //in a short transaction
                        {

                            price_sold = daily_price.get(daily_size - 1);
                            profit = price_borrowed - price_sold;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            amount = getAmount(account.get(account.size() - 1), 4);
                            //account.add(date_stamp + " " + (amount + profit));   
                            account.add(date_stamp + " " + daily_price.get(daily_price.size() - 1) + " "
                                    + formatter3.format(profit) + " " + formatter3.format(amount + profit));
                            amount = getAmount(account.get(account.size() - 1), 4);
                            if (weekend.dayOfWeek().getAsText().equals("Sunday")) {
                                sunday.add(date_stamp + " " + profit);
                            }

                            made_trade = true;
                            dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                            log_ret = daily_price.get(daily_size - 1).doubleValue();

                            out_transaction = 0;

                            if (printall)
                                System.out.println("profit, price_borrowed, price_sold = " + profit + ", "
                                        + price_borrowed + ", " + price_sold);
                        }

                    } else {
                        //if(binary_rule && !trading_closed)
                        if (binary_rule) {

                            made_trade = false;
                            if (current_signal > 0 && prev_signal <= 0) //new point positive, we see momentum, buy
                            {

                                last_price = daily_price.get(daily_size - 1);

                                if (short_sell && out_transaction == 1) //in a short-sell transaction, sell
                                {
                                    price_sold = daily_price.get(daily_size - 1);
                                    profit = price_borrowed - price_sold;

                                    if (profit > 0) {
                                        succ_trades = succ_trades + 1;
                                    }
                                    total_trades = total_trades + 1;

                                    amount = getAmount(account.get(account.size() - 1), 4);
                                    //account.add(date_stamp + " " + (amount + profit));  
                                    account.add(date_stamp + " "
                                            + formatter3.format(daily_price.get(daily_price.size() - 1)) + " "
                                            + formatter3.format(profit) + " "
                                            + formatter3.format(amount + profit));

                                    amount = getAmount(account.get(account.size() - 1), 4);
                                    if (weekend.dayOfWeek().getAsText().equals("Sunday")) {
                                        sunday.add(date_stamp + " " + profit);
                                    }

                                    made_trade = true;
                                    dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                                    log_ret = daily_price.get(daily_size - 1).doubleValue();

                                    out_transaction = 0;

                                    if (printall) {
                                        if (profit > 0)
                                            System.out.println("Sold for a profit of " + profit);
                                        else if (profit < 0)
                                            System.out.println("Sold for a loss of " + profit);

                                        System.out.println("profit, price_borrowed, price_sold = " + profit
                                                + ", " + price_borrowed + ", " + price_sold);
                                    }
                                }

                                if ((long_buy && in_transaction == 0) && !trading_closed) {
                                    profit = 0;
                                    price_bought = daily_price.get(daily_size - 1);
                                    in_transaction = 1;
                                    account.add(date_stamp + " "
                                            + formatter3.format(daily_price.get(daily_price.size() - 1)) + " "
                                            + formatter3.format(profit) + " "
                                            + formatter3.format(amount + profit));
                                    dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                                    log_ret = daily_price.get(daily_size - 1).doubleValue();
                                    if (printall)
                                        System.out.println("Entered long transaction at " + price_bought);
                                }

                            } else if (current_signal < 0 && prev_signal >= 0) //if in transaction and signal goes below, sell
                            {
                                last_price = daily_price.get(daily_size - 1);

                                if (long_buy && in_transaction == 1) {
                                    price_sold = daily_price.get(daily_size - 1);
                                    profit = price_sold - price_bought;
                                    if (profit > 0) {
                                        succ_trades = succ_trades + 1;
                                    }
                                    total_trades = total_trades + 1;

                                    amount = getAmount(account.get(account.size() - 1), 4);
                                    //account.add(date_stamp + " " + (amount + profit));   
                                    account.add(date_stamp + " -"
                                            + formatter3.format(daily_price.get(daily_price.size() - 1)) + " "
                                            + formatter3.format(profit) + " "
                                            + formatter3.format(amount + profit));
                                    amount = getAmount(account.get(account.size() - 1), 4);
                                    if (weekend.dayOfWeek().getAsText().equals("Sunday")) {
                                        sunday.add(date_stamp + " " + profit);
                                    }

                                    made_trade = true;
                                    dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                                    log_ret = daily_price.get(daily_size - 1).doubleValue();

                                    in_transaction = 0;

                                    if (printall) {
                                        if (profit > 0)
                                            System.out.println("Sold for a profit of " + profit);
                                        else if (profit < 0)
                                            System.out.println("Sold for a loss of " + profit);
                                        System.out.println("profit, price_bought, price_sold = " + profit + ", "
                                                + price_bought + ", " + price_sold);
                                    }

                                }

                                if ((short_sell && out_transaction == 0) && !trading_closed) {
                                    profit = 0;
                                    price_borrowed = daily_price.get(daily_size - 1);
                                    out_transaction = 1;
                                    account.add(date_stamp + " -"
                                            + formatter3.format(daily_price.get(daily_price.size() - 1)) + " "
                                            + formatter3.format(profit) + " "
                                            + formatter3.format(amount + profit));

                                    dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                                    log_ret = daily_price.get(daily_size - 1).doubleValue();

                                    if (printall)
                                        System.out.println("Entered short transaction at " + price_borrowed);

                                }
                            }
                        }

                        if (signal_strength_rule
                                && ((in_transaction == 0 && out_transaction == 0) && !trading_closed)) {

                            if (current_signal > 0) //new point positive, we see momentum, buy
                            {
                                last_price = daily_price.get(daily_size - 1);

                                if (short_sell && out_transaction == 1) //in a short-sell transaction, sell
                                {
                                    price_sold = daily_price.get(daily_size - 1);
                                    profit = price_borrowed - price_sold;

                                    if (profit > 0) {
                                        succ_trades = succ_trades + 1;
                                    }
                                    total_trades = total_trades + 1;

                                    amount = getAmount(account.get(account.size() - 1), 4);
                                    //account.add(date_stamp + " " + (amount + profit));   
                                    account.add(date_stamp + " "
                                            + formatter3.format(daily_price.get(daily_price.size() - 1)) + " "
                                            + formatter3.format(profit) + " "
                                            + formatter3.format(amount + profit));
                                    amount = getAmount(account.get(account.size() - 1), 4);
                                    if (weekend.dayOfWeek().getAsText().equals("Sunday")) {
                                        sunday.add(date_stamp + " " + profit);
                                    }

                                    made_trade = true;
                                    dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                                    log_ret = daily_price.get(daily_size - 1).doubleValue();

                                    out_transaction = 0;

                                    if (printall)
                                        System.out.println("profit, price_borrowed, price_sold = " + profit
                                                + ", " + price_borrowed + ", " + price_sold);
                                }

                                if (long_buy && in_transaction == 0) {
                                    profit = 0;
                                    price_bought = daily_price.get(daily_size - 1);
                                    in_transaction = 1;
                                    account.add(date_stamp + " "
                                            + formatter3.format(daily_price.get(daily_price.size() - 1)) + " "
                                            + formatter3.format(profit) + " "
                                            + formatter3.format(amount + profit));
                                    dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                                    log_ret = daily_price.get(daily_size - 1).doubleValue();

                                    if (printall)
                                        System.out.println("Entered long transaction at " + price_bought);
                                }

                            } else if (current_signal < 0) //if in transaction and signal goes below, sell
                            {

                                last_price = daily_price.get(daily_size - 1);

                                if (long_buy && in_transaction == 1) {
                                    price_sold = daily_price.get(daily_size - 1);
                                    profit = price_sold - price_bought;
                                    if (profit > 0) {
                                        succ_trades = succ_trades + 1;
                                    }
                                    total_trades = total_trades + 1;

                                    amount = getAmount(account.get(account.size() - 1), 4);
                                    //account.add(date_stamp + " " + (amount + profit));   
                                    account.add(date_stamp + " -"
                                            + formatter3.format(daily_price.get(daily_price.size() - 1)) + " "
                                            + formatter3.format(profit) + " "
                                            + formatter3.format(amount + profit));
                                    amount = getAmount(account.get(account.size() - 1), 4);
                                    if (weekend.dayOfWeek().getAsText().equals("Sunday")) {
                                        sunday.add(date_stamp + " " + profit);
                                    }

                                    made_trade = true;
                                    dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                                    log_ret = daily_price.get(daily_size - 1).doubleValue();

                                    in_transaction = 0;

                                    if (printall)
                                        System.out.println("Bought for a profit of " + profit);
                                    if (printall)
                                        System.out.println("profit, price_bought, price_sold = " + profit + ", "
                                                + price_bought + ", " + price_sold);

                                }

                                if (short_sell && out_transaction == 0) {
                                    profit = 0;
                                    price_borrowed = daily_price.get(daily_size - 1);
                                    out_transaction = 1;
                                    account.add(date_stamp + " -"
                                            + formatter3.format(daily_price.get(daily_price.size() - 1)) + " "
                                            + formatter3.format(profit) + " "
                                            + formatter3.format(amount + profit));

                                    dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                                    log_ret = daily_price.get(daily_size - 1).doubleValue();

                                    if (printall)
                                        System.out.println("Entered short transaction at " + price_borrowed);
                                }
                            }
                        }

                        if (!made_trade) {
                            profit = 0;
                            //account.add(date_stamp + " " + formatter3.format(daily_price.get(daily_price.size()-1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit));
                            //dailyoutret.add(price.get(price.size()-1) - log_ret);
                            //log_ret = price.get(price.size()-1);   
                        }

                        day_count++;

                        //if(recompute_day == day_count) {y_count=0;}
                        if (printall)
                            System.out.println(
                                    "" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1))
                                            + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", "
                                            + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl));
                        allsignal.add(date_stamp + " " + current_signal);
                    }

                } else if (trading_hours)// && !trading_closed)// && cur_min != 30)
                {

                    if (in_transaction == 1) //in a long transaction 
                    {

                        if (red_zone && price.get(price.size() - 1) > last_price) //check if new high price
                        {
                            last_price = price.get(price.size() - 1);
                        }

                        //cur_pnl = price.get(price.size()-1) - last_price;    
                        cur_pnl = bid.get(bid.size() - 1) - last_price;
                        lo_pnl = lo_price.get(lo_price.size() - 1) - last_price;
                        hi_pnl = hi_price.get(hi_price.size() - 1) - last_price;

                        if (cur_pnl < -stop_loss) {
                            if (printall)
                                System.out.println(
                                        "Stop-loss Activated since lo_pnl = " + cur_pnl + " < -" + stop_loss);
                            //System.out.println("Closing price at bar was " + price.get(price.size()-1) + " but lowest price in bar was " + lo_price.get(lo_price.size()-1));
                            //--------------sell---------- 

                            //price_sold = price.get(price.size()-1);

                            //price_sold = price.get(price.size()-1);
                            price_sold = bid.get(bid.size() - 1);
                            profit = price_sold - price_bought;

                            //                  price_sold = price_bought - stop_loss;
                            //                  profit = -stop_loss;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count), 4);
                            //account.add(date_stamp + " " + (amount - stop_loss));   
                            //account.add(date_stamp + " " + (amount + profit));   
                            //account.add(date_stamp + " " + price.get(price.size()-1) + " " + profit + " " + (amount + profit));
                            account.add(date_stamp + " -" + formatter3.format(bid.get(bid.size() - 1)) + " "
                                    + formatter3.format(profit) + " " + formatter3.format(amount + profit));
                            amount = getAmount(account.get(account.size() - 1), 4);
                            if (weekend.dayOfWeek().getAsText().equals("Sunday")) {
                                sunday.add(date_stamp + " " + profit);
                            }

                            dailyoutret.add(bid.get(bid.size() - 1) - log_ret);
                            log_ret = bid.get(bid.size() - 1);

                            in_transaction = 0;

                            //-- return stop loss to original setting --------
                            if (printall)
                                System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit);
                            stop_loss = global_stop_loss;
                            red_zone = false;
                        } else if (cur_pnl >= take_profit_thresh) {

                            if (printall)
                                System.out.println("Profit zone activated since cur_pnl = " + cur_pnl + " > "
                                        + take_profit_thresh);

                            //price_sold = price.get(price.size()-1);
                            price_sold = bid.get(bid.size() - 1);
                            profit = price_sold - price_bought;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count), 4);
                            //account.add(date_stamp + " " + (amount + profit));   
                            //account.add(date_stamp + " " + price.get(price.size()-1) + " " + profit + " " + (amount + profit));
                            account.add(date_stamp + " -" + formatter3.format(price_sold) + " "
                                    + formatter3.format(profit) + " " + formatter3.format(amount + profit));
                            amount = getAmount(account.get(account.size() - 1), 4);
                            if (weekend.dayOfWeek().getAsText().equals("Sunday")) {
                                sunday.add(date_stamp + " " + profit);
                            }
                            dailyoutret.add(price_sold - log_ret);
                            log_ret = price_sold;

                            in_transaction = 0;

                            //-- return stop loss to original setting --------
                            if (printall)
                                System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit);
                            stop_loss = global_stop_loss;

                            //                  stop_loss = profitable_stop;
                            //                  last_price = price.get(price.size()-1);
                            //                  red_zone = true;
                        }
                    } else if (out_transaction == 1) {

                        if (red_zone && price.get(price.size() - 1) < last_price) //check if new high price
                        {
                            last_price = price.get(price.size() - 1);
                        }

                        //cur_pnl =  last_price - price.get(price.size()-1);               
                        cur_pnl = last_price - ask.get(ask.size() - 1);
                        lo_pnl = last_price - hi_price.get(hi_price.size() - 1);
                        hi_pnl = last_price - lo_price.get(lo_price.size() - 1);

                        if (cur_pnl < -stop_loss) {
                            if (printall)
                                System.out.println(
                                        "Stop-loss Activated since lo_pnl = " + cur_pnl + " < -" + stop_loss);
                            //System.out.println("Closing price at bar was " + price.get(price.size()-1) + " but highest price in bar was " + hi_price.get(hi_price.size()-1));
                            //--------------sell---------- 

                            //price_sold = price.get(price.size()-1);
                            price_sold = ask.get(ask.size() - 1);
                            profit = price_borrowed - price_sold;

                            //                  price_sold = price_borrowed + stop_loss;
                            //                  profit = -stop_loss;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count), 4);

                            //account.add(date_stamp + " " + (amount - stop_loss));  
                            //account.add(date_stamp + " " + (amount + profit));  
                            //account.add(date_stamp + " " + price.get(price.size()-1) + " " + profit + " " + (amount + profit));
                            account.add(date_stamp + " " + formatter3.format(price_sold) + " "
                                    + formatter3.format(profit) + " " + formatter3.format(amount + profit));
                            amount = getAmount(account.get(account.size() - 1), 4);
                            if (weekend.dayOfWeek().getAsText().equals("Sunday")) {
                                sunday.add(date_stamp + " " + profit);
                            }

                            dailyoutret.add(price_sold - log_ret);
                            log_ret = price_sold;

                            out_transaction = 0;

                            //-- return stop loss to original setting --------
                            stop_loss = global_stop_loss;
                            red_zone = false;

                            if (printall)
                                System.out
                                        .println("Bought at " + date_stamp + " for a profit/loss of " + profit);

                        } else if (cur_pnl >= take_profit_thresh) {

                            if (printall)
                                System.out.println("Profit zone activated since cur_pnl = " + cur_pnl + " > "
                                        + take_profit_thresh);
                            //                  stop_loss = profitable_stop;
                            //                  last_price = price.get(price.size()-1);
                            //                  red_zone = true;

                            //price_sold = price.get(price.size()-1);
                            price_sold = ask.get(ask.size() - 1);
                            profit = price_borrowed - price_sold;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count), 4);
                            //account.add(date_stamp + " " + (amount + profit));   
                            account.add(date_stamp + " " + formatter3.format(price_sold) + " "
                                    + formatter3.format(profit) + " " + formatter3.format(amount + profit));
                            amount = getAmount(account.get(account.size() - 1), 4);
                            if (weekend.dayOfWeek().getAsText().equals("Sunday")) {
                                sunday.add(date_stamp + " " + profit);
                            }

                            dailyoutret.add(price_sold - log_ret);
                            log_ret = price_sold;

                            out_transaction = 0;

                            //-- return stop loss to original setting --------
                            stop_loss = global_stop_loss;
                            red_zone = false;

                            if (printall)
                                System.out
                                        .println("Bought at " + date_stamp + " for a profit/loss of " + profit);

                        }
                    } else if (downtick_strategy) {

                        //strategy here is to buy/sell according to signal iff downtick has occurred

                        if (current_signal > 0 && (price_sold > price.get(price.size() - 1))) {

                            //let's buy some more 
                            if (printall)
                                System.out.println("Buying at " + date_stamp + " since last price sold = "
                                        + price_sold + " > " + price.get(price.size() - 1));
                            price_bought = price.get(price.size() - 1);
                            last_price = price.get(price.size() - 1);
                            in_transaction = 1;

                        } else if (current_signal < 0 && (price_sold < price.get(price.size() - 1))) {

                            //let's short some more
                            if (printall)
                                System.out.println(
                                        "Shorting at " + date_stamp + " since last price bought back at = "
                                                + price_sold + " < " + price.get(price.size() - 1));

                            price_borrowed = price.get(price.size() - 1);
                            last_price = price.get(price.size() - 1);
                            out_transaction = 1;

                            if (printall)
                                System.out.println("Entered short transaction at " + price_borrowed);
                        }
                        cur_pnl = 0;
                        lo_pnl = 0;
                        hi_pnl = 0;
                    } else {
                        cur_pnl = 0;
                        lo_pnl = 0;
                        hi_pnl = 0;
                    }

                    if (weekend.dayOfWeek().getAsText().equals("Friday")
                            && date_stamp.indexOf("17:00:00") != -1) {
                        if (printall)
                            System.out.println("End of week");

                    }

                    dailyReport.add("" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1))
                            + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", "
                            + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl));

                    if (printall)
                        System.out
                                .println("" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1))
                                        + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", "
                                        + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl));

                    allsignal.add(date_stamp + " " + current_signal);
                }

            }
        }

        double mean_ntrades = 0;
        computed = true;
        dailyoutret.set(1, 0.0);
        double[] dreturns = new double[account.size()];
        dreturns[0] = 0;

        double mean = 0;
        double sd = 0;
        n_neg_ret = 0;
        n_pos_ret = 0;
        neg_ret_mean = 0;
        pos_ret_mean = 0;
        pnl = 0;

        int n_trades = 0;
        ArrayList<Integer> n_trades_day = new ArrayList<Integer>();

        for (i = 1; i < account.size(); i++) {
            out.println(account.get(i));
            dailyout.println(account.get(i) + " " + dailyoutret.get(i));
            //System.out.println(account.get(i));
            dreturns[i] = getAmount(account.get(i), 4) - getAmount(account.get(i - 1), 4);

            dates = account.get(i).split("[ ]+");
            if (!perf_dates.contains(dates[0])) //first date entry
            {

                if (perf_dates.size() != 0) {
                    perf_returns.add(pnl);
                    n_trades_day.add(n_trades);
                }

                perf_dates.add(dates[0]);
                pnl = dreturns[i];
                if (dreturns[i] != 0) {
                    n_trades = 1;
                } else {
                    n_trades = 0;
                }
            } else //already contains the date, so add on pnl
            {
                pnl = pnl + dreturns[i];
                //System.out.println(dreturns[i]);
                if (dreturns[i] != 0) {
                    n_trades++;
                } // System.out.println(n_trades);}
            }

            if (dreturns[i] > 0) {
                n_pos_ret++;
                pos_ret_mean = pos_ret_mean + dreturns[i];
                mean = mean + dreturns[i];
            } else if (dreturns[i] < 0) {
                n_neg_ret++;
                neg_ret_mean = neg_ret_mean - dreturns[i];
                mean = mean + dreturns[i];
            }

        }
        perf_returns.add(pnl);
        n_trades_day.add(n_trades);

        for (i = 0; i < perf_dates.size(); i++) {
            if (printall)
                System.out.println(perf_dates.get(i) + " " + perf_returns.get(i) + " " + n_trades_day.get(i));
            perform.println(perf_dates.get(i) + " " + perf_returns.get(i));
            mean_ntrades = mean_ntrades + n_trades_day.get(i);
        }
        mean_ntrades = mean_ntrades / n_trades_day.size();

        dates_price = new String[n_obs];
        for (i = 0; i < n_obs; i++) {

            tokens = allsignal.get(allsignal.size() - n_obs + i).split("[ ]+");

            //System.out.println(tokens[0] + " " + latestDates.get(latestDates.size() - n_obs + i - 1));

            //         if(tokens[0].equals(latestDates.get(latestDates.size() - n_obs + i - 1)))
            //         {
            dates_price[i] = new String(latestDates.get(latestDates.size() - n_obs + i) + " "
                    + (mid.get(mid.size() - n_obs + i) - mid.get(mid.size() - n_obs + i - 1)) + " "
                    + mid.get(mid.size() - n_obs + i) + " " + tokens[2]);
            //        }
        }

        double sunday_roi = 0;
        double sunday_trade = 0;
        double sunday_pos_mean = 0;
        double sunday_neg_mean = 0;
        int nsunday_pos_mean = 0;
        int nsunday_neg_mean = 0;

        System.out.println("\nSunday Performance");
        for (i = 0; i < sunday.size(); i++) {
            //System.out.println(sunday.get(i));
            sunday_trade = getAmount(sunday.get(i), 4);
            sunday_roi = sunday_roi + sunday_trade;

            if (sunday_trade > 0) {
                sunday_pos_mean += sunday_trade;
                nsunday_pos_mean++;
            }
            if (sunday_trade < 0) {
                sunday_neg_mean += sunday_trade;
                nsunday_neg_mean++;
            }
        }

        System.out.println("\nSunday Stats");
        System.out.println("Sunday ROI = " + sunday_roi);
        sunday_roi = sunday_roi / (nsunday_pos_mean + nsunday_neg_mean);
        System.out.println("Sunday meantrade = " + sunday_roi);
        System.out.println("Sunday pos mean = " + (sunday_pos_mean / nsunday_pos_mean) + ", npos_trades = "
                + nsunday_pos_mean);
        System.out.println("Sunday neg mean = " + (sunday_neg_mean / nsunday_neg_mean) + ", nneg_trades = "
                + nsunday_neg_mean);

        mean = mean / (n_pos_ret + n_neg_ret);

        System.out.println("ROI = " + account.get(account.size() - 1));
        //--- compute stats---------------
        double risk = neg_ret_mean / (double) n_neg_ret;
        System.out.println("neg_ret_mean = " + (-neg_ret_mean) + ", " + n_neg_ret);

        double reward = pos_ret_mean / (double) n_pos_ret;
        System.out.println("pos_ret_mean = " + pos_ret_mean + ", " + n_pos_ret);

        double win_ratio = (double) (n_pos_ret) / (n_pos_ret + n_neg_ret);

        kellyPerc = win_ratio - (1.0 - win_ratio) * (risk / reward);
        ulcer_index = ulcerIndex(dreturns);

        System.out.println("win ratio = " + win_ratio + ", risk = " + risk + ", reward = " + reward);
        System.out.println("kelly and ulcer = " + kellyPerc + " " + ulcer_index);

        for (i = 0; i < dreturns.length; i++) {
            sd = sd + (dreturns[i] - mean) * (dreturns[i] - mean) / ((double) dreturns.length);
        }

        standard_deviation = Math.sqrt(sd);

        sharpeRatio = Math.sqrt(250) * mean / standard_deviation;
        maxdraw = computeDrawdown(dreturns);
        rank_coeff = segmentRankCorrelation(30, dreturns);

        System.out.println("MeanRet = " + mean + ", Sharpe = " + sharpeRatio + ", MaxDD = " + maxdraw
                + ", Rank = " + rank_coeff + ", avg_n_trades = " + mean_ntrades);

        out.close();
        dailyout.close();
        perform.close();
        svmout.close();
        b0_coeff.close();

    } catch (FileNotFoundException fe) {
        System.out.println("File not found..." + fe);
    } catch (IOException ioe) {
        System.out.println("IO procedure faulty..." + ioe);
    }

    n_files++;
    return computed;

}

From source file:ch.imetrica.mdfaTradingStrategies.MDFAStrategyEvolution.java

public boolean startStrategyDailyIntradayStocks() {

    int j, i, l, N, file_count;
    double sum = 0;
    Double D;/*from www .j  a  v  a 2s.  c o  m*/
    String ddelims = "[-]";
    boolean computed = false;
    boolean print_filter = false;
    boolean made_trade = true;
    String date_stamp, strline;
    int daily_size;
    double profit, price_borrowed, price_sold, price_bought;
    double current_price, prev_price;
    double last_price, cur_pnl, stop_loss, lo_pnl, hi_pnl;
    double log_ret = 0;
    signal = new double[trade_obs];
    xt = new double[trade_obs];
    lag_signals = new double[trade_obs];
    prix = new double[trade_obs];
    lo_prix = new double[trade_obs];
    hi_prix = new double[trade_obs];
    total_succ = 0;
    total = 0;
    log_price = 0;
    N = n_obs;
    avg_vol = 0.0;
    b_avg = new double[L * n_rep];
    count = 0;
    trade_succ_ratio = 0;
    double amount = 0;
    double prev_signal;
    reg_trading_hours = false;
    String[] intdates;
    //make sure arraylists empty
    ArrayList<String> perf_dates = new ArrayList<String>();
    ArrayList<Double> perf_returns = new ArrayList<Double>();
    double pnl;
    String[] dates;
    boolean inverse_hours = false;
    String time;
    ArrayList<String> account = new ArrayList<String>();
    ArrayList<String> sunday = new ArrayList<String>();
    ArrayList<String> latestDates = new ArrayList<String>();
    last_trades = new ArrayList<Integer>();
    final_trades = new ArrayList<Double>();
    dailyoutret = new ArrayList<Double>();
    maxIntValue = new ArrayList<Double>();
    avg_volatility = new ArrayList<Double>();
    close_series = new ArrayList<Double>();
    highlow_series = new ArrayList<Double>();
    exp_series_1 = new ArrayList<Double>();
    exp_series_2 = new ArrayList<Double>();
    price = new ArrayList<Double>();
    lo_price = new ArrayList<Double>();
    hi_price = new ArrayList<Double>();
    mid = new ArrayList<Double>();
    bid = new ArrayList<Double>();
    ask = new ArrayList<Double>();
    dates_series = new ArrayList<String>();
    dailyReport = new ArrayList<String>();
    b0_trend = new ArrayList<Double>();
    vol_0 = new ArrayList<Double>();
    vol_1 = new ArrayList<Double>();
    sub_returns = new ArrayList<Double>();
    trade_days = new ArrayList<String>();
    returns = new ArrayList<Double>();
    longreturns = new ArrayList<Double>();
    shortreturns = new ArrayList<Double>();
    dropdowns = new ArrayList<Double>();
    success = new ArrayList<Double>();
    dates_low_high = new ArrayList<String>();
    crits = new ArrayList<String>();
    svm = new ArrayList<String>();
    filters = new ArrayList<Filter>();
    date_returns = new ArrayList<String>();

    live_series = new ArrayList<Double>(); //the data to be applied out of sample
    ib_data_hash = new ibHash();

    fridayROI = 0;
    fridayROI_pos = 0;
    fridays = 0;
    int end_hour;
    lookback_returns = new ArrayList<Double>();
    num_pos_returns = 0;
    deg_0 = new ArrayList<Double>();
    deg_1 = new ArrayList<Double>();
    crit_0 = new ArrayList<Double>();
    crit_1 = new ArrayList<Double>();
    full_returns_array = new ArrayList<double[]>();
    morning_returns = new ArrayList<double[]>();

    morning_buy = true; //enter transaction at morning open
    morning_optimize = false; //optimize in the morning trading hours
    num_full_positive_returns = 0;
    //--- Now get historical interp values ------
    //uploadInterpParams("max_int.dat"); 
    //-------------------------------------------
    forex24 = true;
    ret_dist = new double[trade_obs];
    pos_ret_dist = new int[trade_obs];
    neg_ret_dist = new int[trade_obs];
    neg_trades_started = new int[trade_obs];
    pos_trades_started = new int[trade_obs];
    neg_trades_started_mean = new double[trade_obs];
    pos_trades_started_mean = new double[trade_obs];
    diff_account = new double[trade_obs];
    pos_ret_mean_time = new double[trade_obs];
    neg_ret_mean_time = new double[trade_obs];

    mdfaTrades = new ArrayList<MDFATrade>();
    fmt = DateTimeFormat.forPattern("y-MM-dd HH:mm:ss");
    formatter = new DecimalFormat("#0.000000");
    formatter3 = new DecimalFormat("#0.00000");
    formatter2 = new DecimalFormat("#0.00");
    histo_stat = new int[100];
    interp_vals = new ArrayList<Double>();
    max_ranks = new ArrayList<Double>();
    profit_baby = 0;
    //setForecastDFAParameters();
    bad_starts = 0;
    n_out_samp = 0;

    //take_profit = true;
    //take_profit_thresh = .0020;
    current_signal = 0;
    prev_price = 0;
    cur_pnl = 0;
    stop_loss = stop_loss_thresh;
    out_transaction = 0;
    in_transaction = 0;
    red_zone = false;
    global_stop_loss = stop_loss_thresh;
    profitable_stop = .0005;
    count = 0;
    short_sell = true;
    long_buy = true;
    day_count = 0;
    ArrayList<String> trade_times = new ArrayList<String>();

    lo_pnl = 0;
    hi_pnl = 0;
    price_borrowed = 0;
    price_sold = 0;
    price_bought = 0;
    last_price = 0;

    binary_rule = true;
    signal_strength_rule = true;
    downtick_strategy = false;
    signal_profit = false;
    friday_closing = true;
    //asian_close = true;

    //----- Fill up with times here -------------------------
    //       for(i=0;i<10;i++) 
    //       {
    //        if(i%4 == 0) {trade_times.add("0"+i+":00:00");}
    //       }
    //       for(i=10;i<24;i++) 
    //       {
    //       trade_times.add(i+":00:00");}
    //       for(i=0;i<10;i++) {trade_times.add("0"+i+":00:00");}
    //       for(i=10;i<24;i++) {trade_times.add(i+":00:00");} 

    trade_times.add("10:00:00");
    trade_times.add("11:00:00");
    trade_times.add("12:00:00");
    trade_times.add("13:00:00");
    trade_times.add("14:00:00");
    trade_times.add("15:00:00");

    String[] hourToks = startingTime.split("[:]+");
    start_hour = (new Integer(hourToks[0])).intValue();

    hourToks = endingTime.split("[:]+");
    end_hour = (new Integer(hourToks[0])).intValue();

    inverse_hours = false;
    if (start_hour > end_hour) //must switch hours here
    {
        int temphour = end_hour;

        inverse_hours = true;
        end_hour = start_hour;
        start_hour = temphour;
    }

    if (ib_data && ib_data_file != null) {
        try {

            fin = new FileInputStream(ib_data_file);
            din = new DataInputStream(fin);
            br = new BufferedReader(new InputStreamReader(din));

            while ((strline = br.readLine()) != null) {
                String[] sp = strline.split("[,]+");
                ib_data_hash.put(sp[0], new String(
                        sp[1] + " " + sp[2] + " " + sp[3] + " " + sp[4] + " " + sp[5] + " " + sp[6]));
                //System.out.println(sp[1] + " " + sp[2] + " " + sp[3] + " " + sp[4] + " " + sp[5]  + " " + sp[6]);
            }
        } catch (FileNotFoundException fe) {
            System.out.println("File not found..." + fe);
        } catch (IOException ioe) {
            System.out.println("IO procedure faulty..." + ioe);
        }
    }

    String[] myname = dataFiles[0].split("[.]+");

    try {

        PrintWriter b0_coeff = new PrintWriter(new FileWriter("b0_coeff.dat"));
        PrintWriter perform = new PrintWriter(new FileWriter("intraday_performance_" + n_files + ".dat"));
        PrintWriter dailyout = new PrintWriter(new FileWriter("daily_nasdaq.dat"));
        PrintWriter out = new PrintWriter(new FileWriter("strategy_results_" + myname[0] + ".dat"));

        for (file_count = 0; file_count < 1; file_count++) {

            stop_loss_thresh = stop_loss_thresh * 100;
            take_profit_thresh = take_profit_thresh * 100;
            global_stop_loss = global_stop_loss * 100;
            stop_loss = stop_loss_thresh;

            setTimeStandards(new File(dataFiles[file_count]));
            System.out.println("opening " + dataFiles[file_count]);
            fin = new FileInputStream(dataFiles[file_count]);
            din = new DataInputStream(fin);
            br = new BufferedReader(new InputStreamReader(din));
            lookback_ready = false;
            spread = new PrintWriter(new FileWriter("spread_" + dataFiles[file_count] + ".dat"));
            //if(print_debug)System.out.println("Entering loop...");
            trading_hours = false;
            computed = false;
            while ((strline = br.readLine()) != null) {

                //System.out.println(strline);
                tokens = strline.split(delims);
                n_toks = tokens.length; //System.out.println("Number of toks = "+n_toks);
                if (n_toks == 0) {
                    System.out.println("End of file");
                    break;
                }

                if (n_toks >= 6) {
                    bid_ask_data = true;
                } else {
                    bid_ask_data = false;
                }

                date_stamp = tokens[0];
                date_tokens = date_stamp.split(date_delims);
                intdates = date_tokens[0].split(ddelims);
                DateTime weekend = new DateTime((new Integer(intdates[0])).intValue(),
                        (new Integer(intdates[1])).intValue(), (new Integer(intdates[2])).intValue(), 14, 0);
                time = date_tokens[1];

                //insampStart is the time we collect daily data 

                //if(date_stamp.indexOf(insampStart) != -1)
                if (trade_times.contains(time)) {

                    //get bid/mid/ask data
                    if (ib_data && ib_data_hash.containsKey(tokens[0])) {
                        String[] hashed = ib_data_hash.get(tokens[0]).split("[ ]+");
                        for (i = 1; i < hashed.length; i++) {
                            tokens[i] = hashed[i];
                        } // System.out.print(tokens[i] + " ");} 
                    }

                    daily_price.add(new Double(tokens[1]));
                    current_price = (new Double(tokens[1])).doubleValue();

                    if (daily_price.size() == 1) {
                        daily_returns.add(new Double(0.0));
                        prev_price = current_price;
                    } else {
                        daily_returns.add(log(current_price) - log(prev_price));
                        prev_price = current_price;
                    }

                    daily_dates.add(date_stamp);

                }

                print_filter = false;
                latestDates.add(date_stamp);
                D = new Double(tokens[4]);
                close_series.add(D);
                if (ib_data && ib_data_hash.containsKey(tokens[0])) {

                    String[] hashed = ib_data_hash.get(tokens[0]).split("[ ]+");
                    //System.out.println("Contains " + tokens[0] + ", lengths = " + hashed.length + ", " + tokens.length);
                    for (i = 1; i < hashed.length; i++) {
                        tokens[i] = hashed[i];
                    } // System.out.print(tokens[i] + " ");} 
                    bid.add(new Double(tokens[2]));
                    ask.add(new Double(tokens[3]));
                    mid.add(new Double(tokens[1]));
                } else {
                    bid.add(new Double(tokens[2]));
                    ask.add(new Double(tokens[3]));
                    mid.add(new Double(tokens[1]));
                }

                D = new Double(tokens[1]);
                price.add(D);

                D = new Double(tokens[4]);
                if (ib_data && ib_data_hash.containsKey(tokens[0])) {
                    live_series.add(log(mid.get(mid.size() - 1)) - log(mid.get(mid.size() - 2)));
                } else {
                    live_series.add(D);
                }

                if (ib_data && ib_data_hash.containsKey(tokens[0])) //use as is
                {
                    lo_price.add(new Double(tokens[7]));
                    hi_price.add(new Double(tokens[8]));
                } else {
                    lo_price.add((new Double(tokens[7])));
                    hi_price.add((new Double(tokens[8])));
                }

                //---- start the account ------
                if (account.size() == 0) {
                    account.add(date_stamp + " " + 0);
                    dailyoutret.add(0.0);
                }

                String[] hours = time.split("[:]+");
                cur_hour = (new Integer(hours[0])).intValue();
                (new Integer(hours[1])).intValue();

                trading_closed = false;

                //if currently not in a transaction and between the hours of midnight and start-hour, then no new 
                //positions will be opened

                if (asian_close) //only closed if most recent transaction was closed artificially through SL or TP after end hours
                {
                    if ((in_transaction == 0 && out_transaction == 0) && cur_hour >= start_hour
                            && cur_hour <= end_hour) {
                        trading_closed = true;
                        if (printall)
                            System.out.println(cur_hour + " " + start_hour);
                    }
                } else {
                    if (cur_hour >= start_hour && cur_hour <= end_hour) {
                        trading_closed = true;
                        if (printall) {
                            System.out.println(cur_hour + " " + start_hour);
                        }
                    }
                }

                //           if(cur_hour == 14) {release_first = true;}
                //           else {elease_first = false;}

                if (inverse_hours) {
                    trading_closed = !trading_closed;
                }

                its_closing_time = false;
                //its_closing_time = (date_stamp.indexOf("16:00:00") != -1);

                made_trade = false;
                if (daily_returns.size() >= n_obs && trade_times.contains(time)) //a new day begineth
                {

                    computed = true;
                    trading_hours = true;

                    tseries = new double[n_rep * n_obs];

                    for (i = 0; i < n_obs; i++) {
                        tseries[n_obs - 1 - i] = daily_returns.get(daily_returns.size() - 1 - i);
                        tseries[n_obs + n_obs - 1 - i] = daily_returns.get(daily_returns.size() - 1 - i);

                        if (n_rep > 2 && exp_series_1.size() > 0) {
                            tseries[n_obs * 2 + n_obs - 1 - i] = exp_series_1.get(exp_series_1.size() - 1 - i);
                        }
                        if (n_rep > 3 && exp_series_2.size() > 0) {
                            tseries[n_obs * 3 + n_obs - 1 - i] = exp_series_2.get(exp_series_2.size() - 1 - i);
                        }
                    }

                    mdfa.set_tseries(tseries, n_obs, n_rep);

                    //if(day_count == 0)  //recompute filter coefficients
                    if (day_count == 0 || weekend.dayOfWeek().getAsText().equals("Sunday")
                            && date_stamp.indexOf("18:00:00") != -1) {

                        if (printall)
                            System.out.println("Recomputing filter...");
                        mdfa.computeFilterGeneral(true, print_filter);
                        b_coeffs = new double[(n_rep - 1) * L]; //System.out.println(b_coeffs.length + " " + L + n_rep); 
                        for (l = 0; l < L; l++) {

                            for (i = 0; i < n_rep - 1; i++) {
                                b_coeffs[L * i + l] = mdfa.b[L * (i + 1) + l];
                            } // System.out.println(b_coeffs[l]);}               
                            //if(date_stamp.indexOf("2013-12-17") != -1) {System.out.println(b_coeffs[l]);}
                        }
                        if (printall)
                            System.out.println(date_stamp + " b_coeffs = " + b_coeffs[0] + " " + b_coeffs[1]
                                    + " " + b_coeffs[2]);

                        b_copy = new double[mdfa.b.length];
                        System.arraycopy(mdfa.b, 0, b_copy, 0, b_copy.length);
                        b0_coeff.println(b_coeffs[0]); // + ", " + b_coeffs[L] + ", " + b_coeffs[2*L]);

                    }

                    sum = 0.0;
                    for (j = 1; j < n_rep; j++) {
                        for (l = 0; l < L; l++) {
                            sum = sum + b_coeffs[L * (j - 1) + l] * tseries[N * j + n_obs - 1 - l];
                        }
                    }

                    prev_signal = current_signal;
                    current_signal = sum;
                    if (sig_inverse) {
                        current_signal = -current_signal;
                    }

                    //----final signal ---
                    daily_signal.add(current_signal);
                    daily_size = daily_price.size();
                    dailyReport.add(
                            "New day " + date_stamp + ", " + formatter3.format(daily_price.get(daily_size - 1))
                                    + ", " + formatter.format(tseries[n_obs - 1]) + ", " + current_signal);
                    //if(printall) System.out.println("New day "+date_stamp + ", " + formatter3.format(daily_price.get(daily_size-1)) + ", " + formatter.format(tseries[n_obs-1]) + ", " + current_signal);

                    //--compute binary trading rule ---

                    //if(printall) System.out.println("Current signal = " + current_signal + " Prev signal = " + prev_signal + ", trading_closed = " + trading_closed);
                    if (friday_closing && its_closing_time) {
                        if (printall)
                            System.out.println("\nIt's Friday at 5pm, time to close shop for week");
                        if (current_signal > 0 && in_transaction == 1) //in a long transaction
                        {
                            price_sold = daily_price.get(daily_size - 1);
                            profit = price_sold - price_bought;
                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            amount = getAmount(account.get(account.size() - 1), 4);
                            //account.add(date_stamp + " " + (amount + profit));
                            account.add(date_stamp + " " + daily_price.get(daily_price.size() - 1) + " "
                                    + formatter3.format(profit) + " " + formatter3.format(amount + profit));
                            amount = getAmount(account.get(account.size() - 1), 4);

                            if (weekend.dayOfWeek().getAsText().equals("Sunday")) {
                                sunday.add(date_stamp + " " + profit);
                            }

                            made_trade = true;
                            dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                            log_ret = daily_price.get(daily_size - 1).doubleValue();

                            in_transaction = 0;

                            if (printall) {
                                if (profit > 0)
                                    System.out.println("Sold for a profit of " + profit);
                                else if (profit < 0)
                                    System.out.println("Sold for a loss of " + profit);
                                System.out.println("profit, price_bought, price_sold = " + profit + ", "
                                        + price_bought + ", " + price_sold);
                            }
                        } else if (current_signal < 0 && out_transaction == 1) //in a short transaction
                        {

                            price_sold = daily_price.get(daily_size - 1);
                            profit = price_borrowed - price_sold;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            amount = getAmount(account.get(account.size() - 1), 4);
                            //account.add(date_stamp + " " + (amount + profit));   
                            account.add(date_stamp + " " + daily_price.get(daily_price.size() - 1) + " "
                                    + formatter3.format(profit) + " " + formatter3.format(amount + profit));
                            amount = getAmount(account.get(account.size() - 1), 4);
                            if (weekend.dayOfWeek().getAsText().equals("Sunday")) {
                                sunday.add(date_stamp + " " + profit);
                            }

                            made_trade = true;
                            dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                            log_ret = daily_price.get(daily_size - 1).doubleValue();

                            out_transaction = 0;

                            if (printall)
                                System.out.println("profit, price_borrowed, price_sold = " + profit + ", "
                                        + price_borrowed + ", " + price_sold);
                        }

                    } else {
                        //if(binary_rule && !trading_closed)
                        if (binary_rule && (cur_hour <= 16 && cur_hour >= 10)) {

                            //                  if(signal_profit)
                            //                  {
                            //                    //if(printall) System.out.println("Trading_closed = " + trading_closed);
                            //                    if((current_signal > 0 && in_transaction == 1) && (daily_price.get(daily_size-1) > last_price))
                            //                    {
                            //                    
                            //                    
                            //                      price_sold = daily_price.get(daily_size-1);
                            //                 profit = price_sold - price_bought;
                            //                 if(profit > 0) {succ_trades=succ_trades+1;}
                            //                 total_trades=total_trades+1; 
                            //     
                            //                 amount = getAmount(account.get(account.size()-1));
                            //                 account.add(date_stamp + " " + (amount + profit));   
                            //                 amount = getAmount(account.get(account.size()-1));
                            //                
                            //                 made_trade = true;
                            //                 dailyoutret.add(daily_price.get(daily_size-1) - log_ret);
                            //                      log_ret = daily_price.get(daily_size-1).doubleValue();
                            //     
                            //                 in_transaction = 0;
                            //               
                            //                 if(printall){
                            //                      if(profit>0) System.out.println("Sold for a profit of " + profit);
                            //                      else if(profit<0) System.out.println("Sold for a loss of " + profit);
                            //                       System.out.println("profit, price_bought, price_sold = " + profit + ", " + price_bought + ", " + price_sold);
                            //                      }
                            //                    
                            //                      if(!trading_closed)
                            //                      {price_bought = daily_price.get(daily_size-1);
                            //                 //in_transaction = 1; 
                            //                 out_transaction = 1;
                            //                 last_price = daily_price.get(daily_size-1); 
                            //                 if(printall) System.out.println("Entered long transaction at " + price_bought);
                            //                 } 
                            //                    }
                            //                    else if((current_signal < 0 && out_transaction == 1) && (daily_price.get(daily_size-1) < last_price))
                            //                    {
                            //                    
                            //                     price_sold = daily_price.get(daily_size-1);
                            //                     profit = price_borrowed - price_sold;
                            // 
                            //                if(profit > 0) {succ_trades=succ_trades+1;}
                            //                total_trades=total_trades+1; 
                            //      
                            //                amount = getAmount(account.get(account.size()-1));
                            //                account.add(date_stamp + " " + (amount + profit));   
                            //                amount = getAmount(account.get(account.size()-1));
                            //                     
                            //                     made_trade = true;
                            //                     dailyoutret.add(daily_price.get(daily_size-1) - log_ret);
                            //                     log_ret = daily_price.get(daily_size-1).doubleValue();
                            //                     
                            //                     
                            //                     out_transaction = 0;
                            //                     
                            //                     if(printall)
                            //                     {
                            //                     if(profit>0) System.out.println("Sold for a profit of " + profit);
                            //                     else if(profit<0) System.out.println("Sold for a loss of " + profit);
                            //                     }
                            //                     if(printall) System.out.println("profit, price_borrowed, price_sold = " + profit + ", " + price_borrowed + ", " + price_sold);                   
                            //                    
                            //                     if(!trading_closed)
                            //                     {
                            //                     price_borrowed = daily_price.get(daily_size-1);
                            //                     //out_transaction = 1;    
                            //                     in_transaction = 1;
                            //                     if(printall)System.out.println("Entered short transaction at " + price_borrowed);
                            //                     }
                            //                    }
                            //                  
                            //                  }

                            made_trade = false;
                            if (current_signal > 0 && prev_signal <= 0) //new point positive, we see momentum, buy
                            {

                                last_price = daily_price.get(daily_size - 1);

                                if (short_sell && out_transaction == 1) //in a short-sell transaction, sell
                                {
                                    price_sold = daily_price.get(daily_size - 1);
                                    profit = price_borrowed - price_sold;

                                    if (profit > 0) {
                                        succ_trades = succ_trades + 1;
                                    }
                                    total_trades = total_trades + 1;

                                    amount = getAmount(account.get(account.size() - 1), 4);
                                    //account.add(date_stamp + " " + (amount + profit));  
                                    account.add(date_stamp + " "
                                            + formatter3.format(daily_price.get(daily_price.size() - 1)) + " "
                                            + formatter3.format(profit) + " "
                                            + formatter3.format(amount + profit));

                                    amount = getAmount(account.get(account.size() - 1), 4);
                                    if (weekend.dayOfWeek().getAsText().equals("Sunday")) {
                                        sunday.add(date_stamp + " " + profit);
                                    }

                                    made_trade = true;
                                    dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                                    log_ret = daily_price.get(daily_size - 1).doubleValue();

                                    out_transaction = 0;

                                    if (printall) {
                                        if (profit > 0)
                                            System.out.println("Sold for a profit of " + profit);
                                        else if (profit < 0)
                                            System.out.println("Sold for a loss of " + profit);

                                        System.out.println("profit, price_borrowed, price_sold = " + profit
                                                + ", " + price_borrowed + ", " + price_sold);
                                    }
                                }

                                if ((long_buy && in_transaction == 0) && !trading_closed) {
                                    profit = 0;
                                    price_bought = daily_price.get(daily_size - 1);
                                    in_transaction = 1;
                                    account.add(date_stamp + " "
                                            + formatter3.format(daily_price.get(daily_price.size() - 1)) + " "
                                            + formatter3.format(profit) + " "
                                            + formatter3.format(amount + profit));
                                    dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                                    log_ret = daily_price.get(daily_size - 1).doubleValue();
                                    if (printall)
                                        System.out.println("Entered long transaction at " + price_bought);
                                }

                            } else if (current_signal < 0 && prev_signal >= 0) //if in transaction and signal goes below, sell
                            {
                                last_price = daily_price.get(daily_size - 1);

                                if (long_buy && in_transaction == 1) {
                                    price_sold = daily_price.get(daily_size - 1);
                                    profit = price_sold - price_bought;
                                    if (profit > 0) {
                                        succ_trades = succ_trades + 1;
                                    }
                                    total_trades = total_trades + 1;

                                    amount = getAmount(account.get(account.size() - 1), 4);
                                    //account.add(date_stamp + " " + (amount + profit));   
                                    account.add(date_stamp + " -"
                                            + formatter3.format(daily_price.get(daily_price.size() - 1)) + " "
                                            + formatter3.format(profit) + " "
                                            + formatter3.format(amount + profit));
                                    amount = getAmount(account.get(account.size() - 1), 4);
                                    if (weekend.dayOfWeek().getAsText().equals("Sunday")) {
                                        sunday.add(date_stamp + " " + profit);
                                    }

                                    made_trade = true;
                                    dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                                    log_ret = daily_price.get(daily_size - 1).doubleValue();

                                    in_transaction = 0;

                                    if (printall) {
                                        if (profit > 0)
                                            System.out.println("Sold for a profit of " + profit);
                                        else if (profit < 0)
                                            System.out.println("Sold for a loss of " + profit);
                                        System.out.println("profit, price_bought, price_sold = " + profit + ", "
                                                + price_bought + ", " + price_sold);
                                    }

                                }

                                if ((short_sell && out_transaction == 0) && !trading_closed) {
                                    profit = 0;
                                    price_borrowed = daily_price.get(daily_size - 1);
                                    out_transaction = 1;
                                    account.add(date_stamp + " -"
                                            + formatter3.format(daily_price.get(daily_price.size() - 1)) + " "
                                            + formatter3.format(profit) + " "
                                            + formatter3.format(amount + profit));

                                    dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                                    log_ret = daily_price.get(daily_size - 1).doubleValue();

                                    if (printall)
                                        System.out.println("Entered short transaction at " + price_borrowed);

                                }
                            }
                        }

                        if (signal_strength_rule && ((in_transaction == 0 && out_transaction == 0)
                                && !trading_closed && (cur_hour <= 16 && cur_hour >= 10))) {

                            if (current_signal > 0) //new point positive, we see momentum, buy
                            {
                                last_price = daily_price.get(daily_size - 1);

                                if (short_sell && out_transaction == 1) //in a short-sell transaction, sell
                                {
                                    price_sold = daily_price.get(daily_size - 1);
                                    profit = price_borrowed - price_sold;

                                    if (profit > 0) {
                                        succ_trades = succ_trades + 1;
                                    }
                                    total_trades = total_trades + 1;

                                    amount = getAmount(account.get(account.size() - 1), 4);
                                    //account.add(date_stamp + " " + (amount + profit));   
                                    account.add(date_stamp + " "
                                            + formatter3.format(daily_price.get(daily_price.size() - 1)) + " "
                                            + formatter3.format(profit) + " "
                                            + formatter3.format(amount + profit));
                                    amount = getAmount(account.get(account.size() - 1), 4);
                                    if (weekend.dayOfWeek().getAsText().equals("Sunday")) {
                                        sunday.add(date_stamp + " " + profit);
                                    }

                                    made_trade = true;
                                    dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                                    log_ret = daily_price.get(daily_size - 1).doubleValue();

                                    out_transaction = 0;

                                    if (printall)
                                        System.out.println("profit, price_borrowed, price_sold = " + profit
                                                + ", " + price_borrowed + ", " + price_sold);
                                }

                                if (long_buy && in_transaction == 0) {
                                    profit = 0;
                                    price_bought = daily_price.get(daily_size - 1);
                                    in_transaction = 1;
                                    account.add(date_stamp + " "
                                            + formatter3.format(daily_price.get(daily_price.size() - 1)) + " "
                                            + formatter3.format(profit) + " "
                                            + formatter3.format(amount + profit));
                                    dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                                    log_ret = daily_price.get(daily_size - 1).doubleValue();

                                    if (printall)
                                        System.out.println("Entered long transaction at " + price_bought);
                                }

                            } else if (current_signal < 0) //if in transaction and signal goes below, sell
                            {

                                last_price = daily_price.get(daily_size - 1);

                                if (long_buy && in_transaction == 1) {
                                    price_sold = daily_price.get(daily_size - 1);
                                    profit = price_sold - price_bought;
                                    if (profit > 0) {
                                        succ_trades = succ_trades + 1;
                                    }
                                    total_trades = total_trades + 1;

                                    amount = getAmount(account.get(account.size() - 1), 4);
                                    //account.add(date_stamp + " " + (amount + profit));   
                                    account.add(date_stamp + " -"
                                            + formatter3.format(daily_price.get(daily_price.size() - 1)) + " "
                                            + formatter3.format(profit) + " "
                                            + formatter3.format(amount + profit));
                                    amount = getAmount(account.get(account.size() - 1), 4);
                                    if (weekend.dayOfWeek().getAsText().equals("Sunday")) {
                                        sunday.add(date_stamp + " " + profit);
                                    }

                                    made_trade = true;
                                    dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                                    log_ret = daily_price.get(daily_size - 1).doubleValue();

                                    in_transaction = 0;

                                    if (printall)
                                        System.out.println("Bought for a profit of " + profit);
                                    if (printall)
                                        System.out.println("profit, price_bought, price_sold = " + profit + ", "
                                                + price_bought + ", " + price_sold);

                                }

                                if (short_sell && out_transaction == 0) {
                                    profit = 0;
                                    price_borrowed = daily_price.get(daily_size - 1);
                                    out_transaction = 1;
                                    account.add(date_stamp + " -"
                                            + formatter3.format(daily_price.get(daily_price.size() - 1)) + " "
                                            + formatter3.format(profit) + " "
                                            + formatter3.format(amount + profit));

                                    dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                                    log_ret = daily_price.get(daily_size - 1).doubleValue();

                                    if (printall)
                                        System.out.println("Entered short transaction at " + price_borrowed);
                                }
                            }
                        }

                        if (!made_trade) {
                            profit = 0;
                            //account.add(date_stamp + " " + formatter3.format(daily_price.get(daily_price.size()-1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit));
                            //dailyoutret.add(price.get(price.size()-1) - log_ret);
                            //log_ret = price.get(price.size()-1);   
                        }

                        day_count++;

                        //if(recompute_day == day_count) {y_count=0;}
                        if (printall)
                            System.out.println(
                                    "" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1))
                                            + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", "
                                            + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl));
                        allsignal.add(date_stamp + " " + current_signal);
                    }

                } else if (trading_hours && (cur_hour <= 16 && cur_hour >= 10))// && !trading_closed)// && cur_min != 30)
                {

                    if (in_transaction == 1) //in a long transaction 
                    {

                        if (red_zone && price.get(price.size() - 1) > last_price) //check if new high price
                        {
                            last_price = price.get(price.size() - 1);
                        }

                        //cur_pnl = price.get(price.size()-1) - last_price;    
                        cur_pnl = bid.get(bid.size() - 1) - last_price;
                        lo_pnl = lo_price.get(lo_price.size() - 1) - last_price;
                        hi_pnl = hi_price.get(hi_price.size() - 1) - last_price;

                        if (cur_pnl < -stop_loss) {
                            if (printall)
                                System.out.println(
                                        "Stop-loss Activated since lo_pnl = " + cur_pnl + " < -" + stop_loss);
                            //System.out.println("Closing price at bar was " + price.get(price.size()-1) + " but lowest price in bar was " + lo_price.get(lo_price.size()-1));
                            //--------------sell---------- 

                            //price_sold = price.get(price.size()-1);

                            //price_sold = price.get(price.size()-1);
                            price_sold = bid.get(bid.size() - 1);
                            profit = price_sold - price_bought;

                            //                  price_sold = price_bought - stop_loss;
                            //                  profit = -stop_loss;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count), 4);
                            //account.add(date_stamp + " " + (amount - stop_loss));   
                            //account.add(date_stamp + " " + (amount + profit));   
                            //account.add(date_stamp + " " + price.get(price.size()-1) + " " + profit + " " + (amount + profit));
                            account.add(date_stamp + " -" + formatter3.format(bid.get(bid.size() - 1)) + " "
                                    + formatter3.format(profit) + " " + formatter3.format(amount + profit));
                            amount = getAmount(account.get(account.size() - 1), 4);
                            if (weekend.dayOfWeek().getAsText().equals("Sunday")) {
                                sunday.add(date_stamp + " " + profit);
                            }

                            dailyoutret.add(bid.get(bid.size() - 1) - log_ret);
                            log_ret = bid.get(bid.size() - 1);

                            in_transaction = 0;

                            //-- return stop loss to original setting --------
                            if (printall)
                                System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit);
                            stop_loss = global_stop_loss;
                            red_zone = false;
                        } else if (cur_pnl >= take_profit_thresh) {

                            if (printall)
                                System.out.println("Profit zone activated since cur_pnl = " + cur_pnl + " > "
                                        + take_profit_thresh);

                            //price_sold = price.get(price.size()-1);
                            price_sold = bid.get(bid.size() - 1);
                            profit = price_sold - price_bought;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count), 4);
                            //account.add(date_stamp + " " + (amount + profit));   
                            //account.add(date_stamp + " " + price.get(price.size()-1) + " " + profit + " " + (amount + profit));
                            account.add(date_stamp + " -" + formatter3.format(price_sold) + " "
                                    + formatter3.format(profit) + " " + formatter3.format(amount + profit));
                            amount = getAmount(account.get(account.size() - 1), 4);
                            if (weekend.dayOfWeek().getAsText().equals("Sunday")) {
                                sunday.add(date_stamp + " " + profit);
                            }
                            dailyoutret.add(price_sold - log_ret);
                            log_ret = price_sold;

                            in_transaction = 0;

                            //-- return stop loss to original setting --------
                            if (printall)
                                System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit);
                            stop_loss = global_stop_loss;

                            //                  stop_loss = profitable_stop;
                            //                  last_price = price.get(price.size()-1);
                            //                  red_zone = true;
                        }
                    } else if (out_transaction == 1) {

                        if (red_zone && price.get(price.size() - 1) < last_price) //check if new high price
                        {
                            last_price = price.get(price.size() - 1);
                        }

                        //cur_pnl =  last_price - price.get(price.size()-1);               
                        cur_pnl = last_price - ask.get(ask.size() - 1);
                        lo_pnl = last_price - hi_price.get(hi_price.size() - 1);
                        hi_pnl = last_price - lo_price.get(lo_price.size() - 1);

                        if (cur_pnl < -stop_loss) {
                            if (printall)
                                System.out.println(
                                        "Stop-loss Activated since lo_pnl = " + cur_pnl + " < -" + stop_loss);
                            //System.out.println("Closing price at bar was " + price.get(price.size()-1) + " but highest price in bar was " + hi_price.get(hi_price.size()-1));
                            //--------------sell---------- 

                            //price_sold = price.get(price.size()-1);
                            price_sold = ask.get(ask.size() - 1);
                            profit = price_borrowed - price_sold;

                            //                  price_sold = price_borrowed + stop_loss;
                            //                  profit = -stop_loss;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count), 4);

                            //account.add(date_stamp + " " + (amount - stop_loss));  
                            //account.add(date_stamp + " " + (amount + profit));  
                            //account.add(date_stamp + " " + price.get(price.size()-1) + " " + profit + " " + (amount + profit));
                            account.add(date_stamp + " " + formatter3.format(price_sold) + " "
                                    + formatter3.format(profit) + " " + formatter3.format(amount + profit));
                            amount = getAmount(account.get(account.size() - 1), 4);
                            if (weekend.dayOfWeek().getAsText().equals("Sunday")) {
                                sunday.add(date_stamp + " " + profit);
                            }

                            dailyoutret.add(price_sold - log_ret);
                            log_ret = price_sold;

                            out_transaction = 0;

                            //-- return stop loss to original setting --------
                            stop_loss = global_stop_loss;
                            red_zone = false;

                            if (printall)
                                System.out
                                        .println("Bought at " + date_stamp + " for a profit/loss of " + profit);

                        } else if (cur_pnl >= take_profit_thresh) {

                            if (printall)
                                System.out.println("Profit zone activated since cur_pnl = " + cur_pnl + " > "
                                        + take_profit_thresh);
                            //                  stop_loss = profitable_stop;
                            //                  last_price = price.get(price.size()-1);
                            //                  red_zone = true;

                            //price_sold = price.get(price.size()-1);
                            price_sold = ask.get(ask.size() - 1);
                            profit = price_borrowed - price_sold;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count), 4);
                            //account.add(date_stamp + " " + (amount + profit));   
                            account.add(date_stamp + " " + formatter3.format(price_sold) + " "
                                    + formatter3.format(profit) + " " + formatter3.format(amount + profit));
                            amount = getAmount(account.get(account.size() - 1), 4);
                            if (weekend.dayOfWeek().getAsText().equals("Sunday")) {
                                sunday.add(date_stamp + " " + profit);
                            }

                            dailyoutret.add(price_sold - log_ret);
                            log_ret = price_sold;

                            out_transaction = 0;

                            //-- return stop loss to original setting --------
                            stop_loss = global_stop_loss;
                            red_zone = false;

                            if (printall)
                                System.out
                                        .println("Bought at " + date_stamp + " for a profit/loss of " + profit);

                        }
                    } else if (downtick_strategy) {

                        //strategy here is to buy/sell according to signal iff downtick has occurred

                        if (current_signal > 0 && (price_sold > price.get(price.size() - 1))) {

                            //let's buy some more 
                            if (printall)
                                System.out.println("Buying at " + date_stamp + " since last price sold = "
                                        + price_sold + " > " + price.get(price.size() - 1));
                            price_bought = price.get(price.size() - 1);
                            last_price = price.get(price.size() - 1);
                            in_transaction = 1;

                        } else if (current_signal < 0 && (price_sold < price.get(price.size() - 1))) {

                            //let's short some more
                            if (printall)
                                System.out.println(
                                        "Shorting at " + date_stamp + " since last price bought back at = "
                                                + price_sold + " < " + price.get(price.size() - 1));

                            price_borrowed = price.get(price.size() - 1);
                            last_price = price.get(price.size() - 1);
                            out_transaction = 1;

                            if (printall)
                                System.out.println("Entered short transaction at " + price_borrowed);
                        }
                        cur_pnl = 0;
                        lo_pnl = 0;
                        hi_pnl = 0;
                    } else {
                        cur_pnl = 0;
                        lo_pnl = 0;
                        hi_pnl = 0;
                    }

                    if (weekend.dayOfWeek().getAsText().equals("Friday")
                            && date_stamp.indexOf("17:00:00") != -1) {
                        if (printall)
                            System.out.println("End of week");

                    }

                    dailyReport.add("" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1))
                            + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", "
                            + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl));

                    if (printall)
                        System.out
                                .println("" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1))
                                        + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", "
                                        + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl));

                    allsignal.add(date_stamp + " " + current_signal);
                }

            }
        }

        double mean_ntrades = 0;
        computed = true;
        dailyoutret.set(1, 0.0);
        double[] dreturns = new double[account.size()];
        dreturns[0] = 0;

        double mean = 0;
        double sd = 0;
        n_neg_ret = 0;
        n_pos_ret = 0;
        neg_ret_mean = 0;
        pos_ret_mean = 0;
        pnl = 0;

        int n_trades = 0;
        ArrayList<Integer> n_trades_day = new ArrayList<Integer>();

        for (i = 1; i < account.size(); i++) {
            out.println(account.get(i));
            dailyout.println(account.get(i) + " " + dailyoutret.get(i));
            //System.out.println(account.get(i));
            dreturns[i] = getAmount(account.get(i), 4) - getAmount(account.get(i - 1), 4);

            dates = account.get(i).split("[ ]+");
            if (!perf_dates.contains(dates[0])) //first date entry
            {

                if (perf_dates.size() != 0) {
                    perf_returns.add(pnl);
                    n_trades_day.add(n_trades);
                }

                perf_dates.add(dates[0]);
                pnl = dreturns[i];
                if (dreturns[i] != 0) {
                    n_trades = 1;
                } else {
                    n_trades = 0;
                }
            } else //already contains the date, so add on pnl
            {
                pnl = pnl + dreturns[i];
                //System.out.println(dreturns[i]);
                if (dreturns[i] != 0) {
                    n_trades++;
                } // System.out.println(n_trades);}
            }

            if (dreturns[i] > 0) {
                n_pos_ret++;
                pos_ret_mean = pos_ret_mean + dreturns[i];
                mean = mean + dreturns[i];
            } else if (dreturns[i] < 0) {
                n_neg_ret++;
                neg_ret_mean = neg_ret_mean - dreturns[i];
                mean = mean + dreturns[i];
            }

        }
        perf_returns.add(pnl);
        n_trades_day.add(n_trades);

        for (i = 0; i < perf_dates.size(); i++) {
            if (printall)
                System.out.println(perf_dates.get(i) + " " + perf_returns.get(i) + " " + n_trades_day.get(i));
            perform.println(perf_dates.get(i) + " " + perf_returns.get(i));
            mean_ntrades = mean_ntrades + n_trades_day.get(i);
        }
        mean_ntrades = mean_ntrades / n_trades_day.size();

        dates_price = new String[n_obs];
        for (i = 0; i < n_obs; i++) {

            tokens = allsignal.get(allsignal.size() - n_obs + i).split("[ ]+");

            //System.out.println(tokens[0] + " " + latestDates.get(latestDates.size() - n_obs + i - 1));

            //         if(tokens[0].equals(latestDates.get(latestDates.size() - n_obs + i - 1)))
            //         {
            dates_price[i] = new String(latestDates.get(latestDates.size() - n_obs + i) + " "
                    + (mid.get(mid.size() - n_obs + i) - mid.get(mid.size() - n_obs + i - 1)) + " "
                    + mid.get(mid.size() - n_obs + i) + " " + tokens[2]);
            //        }
        }

        double sunday_roi = 0;
        double sunday_trade = 0;
        double sunday_pos_mean = 0;
        double sunday_neg_mean = 0;
        int nsunday_pos_mean = 0;
        int nsunday_neg_mean = 0;

        System.out.println("\nSunday Performance");
        for (i = 0; i < sunday.size(); i++) {
            //System.out.println(sunday.get(i));
            sunday_trade = getAmount(sunday.get(i), 4);
            sunday_roi = sunday_roi + sunday_trade;

            if (sunday_trade > 0) {
                sunday_pos_mean += sunday_trade;
                nsunday_pos_mean++;
            }
            if (sunday_trade < 0) {
                sunday_neg_mean += sunday_trade;
                nsunday_neg_mean++;
            }
        }

        System.out.println("\nSunday Stats");
        System.out.println("Sunday ROI = " + sunday_roi);
        sunday_roi = sunday_roi / (nsunday_pos_mean + nsunday_neg_mean);
        System.out.println("Sunday meantrade = " + sunday_roi);
        System.out.println("Sunday pos mean = " + (sunday_pos_mean / nsunday_pos_mean) + ", npos_trades = "
                + nsunday_pos_mean);
        System.out.println("Sunday neg mean = " + (sunday_neg_mean / nsunday_neg_mean) + ", nneg_trades = "
                + nsunday_neg_mean);

        mean = mean / (n_pos_ret + n_neg_ret);

        System.out.println("ROI = " + account.get(account.size() - 1));
        //--- compute stats---------------
        double risk = neg_ret_mean / (double) n_neg_ret;
        System.out.println("neg_ret_mean = " + (-neg_ret_mean) + ", " + n_neg_ret);

        double reward = pos_ret_mean / (double) n_pos_ret;
        System.out.println("pos_ret_mean = " + pos_ret_mean + ", " + n_pos_ret);

        double win_ratio = (double) (n_pos_ret) / (n_pos_ret + n_neg_ret);

        kellyPerc = win_ratio - (1.0 - win_ratio) * (risk / reward);
        ulcer_index = ulcerIndex(dreturns);

        System.out.println("win ratio = " + win_ratio + ", risk = " + risk + ", reward = " + reward);
        System.out.println("kelly and ulcer = " + kellyPerc + " " + ulcer_index);

        for (i = 0; i < dreturns.length; i++) {
            sd = sd + (dreturns[i] - mean) * (dreturns[i] - mean) / ((double) dreturns.length);
        }

        standard_deviation = Math.sqrt(sd);

        sharpeRatio = Math.sqrt(250) * mean / standard_deviation;
        maxdraw = computeDrawdown(dreturns);
        rank_coeff = segmentRankCorrelation(30, dreturns);

        System.out.println("MeanRet = " + mean + ", Sharpe = " + sharpeRatio + ", MaxDD = " + maxdraw
                + ", Rank = " + rank_coeff + ", avg_n_trades = " + mean_ntrades);

        out.close();
        dailyout.close();
        perform.close();
        b0_coeff.close();

    } catch (FileNotFoundException fe) {
        System.out.println("File not found..." + fe);
    } catch (IOException ioe) {
        System.out.println("IO procedure faulty..." + ioe);
    }

    n_files++;
    return computed;

}

From source file:ch.imetrica.mdfaTradingStrategies.MDFAStrategyEvolution.java

public boolean startStrategyDailyIntradayOneHourBidAsk() {

    int j, i, l, N, file_count;
    double sum = 0;
    Double D;/* www .  ja  v a 2  s  .c  o  m*/
    String ddelims = "[-]";
    boolean computed = false;
    boolean print_filter = false;
    boolean made_trade = true;
    String date_stamp, strline;
    int daily_size;
    double profit, price_borrowed, price_sold, price_bought;
    double current_price, prev_price;
    double last_price, cur_pnl, stop_loss, lo_pnl, hi_pnl;
    double log_ret = 0;
    signal = new double[trade_obs];
    xt = new double[trade_obs];
    lag_signals = new double[trade_obs];
    prix = new double[trade_obs];
    lo_prix = new double[trade_obs];
    hi_prix = new double[trade_obs];
    total_succ = 0;
    total = 0;
    log_price = 0;
    N = n_obs;
    avg_vol = 0.0;
    b_avg = new double[L * n_rep];
    count = 0;
    trade_succ_ratio = 0;
    double amount = 0;
    double prev_signal;
    reg_trading_hours = false;
    String[] intdates;
    //make sure arraylists empty
    ArrayList<String> perf_dates = new ArrayList<String>();
    ArrayList<Double> perf_returns = new ArrayList<Double>();
    double pnl;
    String[] dates;
    boolean inverse_hours = false;
    String time;
    ArrayList<String> account = new ArrayList<String>();
    ArrayList<String> latestDates = new ArrayList<String>();
    last_trades = new ArrayList<Integer>();
    final_trades = new ArrayList<Double>();
    dailyoutret = new ArrayList<Double>();
    maxIntValue = new ArrayList<Double>();
    avg_volatility = new ArrayList<Double>();
    close_series = new ArrayList<Double>();
    highlow_series = new ArrayList<Double>();
    exp_series_1 = new ArrayList<Double>();
    exp_series_2 = new ArrayList<Double>();
    price = new ArrayList<Double>();
    lo_price = new ArrayList<Double>();
    hi_price = new ArrayList<Double>();
    mid = new ArrayList<Double>();
    bid = new ArrayList<Double>();
    ask = new ArrayList<Double>();
    dates_series = new ArrayList<String>();
    dailyReport = new ArrayList<String>();
    b0_trend = new ArrayList<Double>();
    vol_0 = new ArrayList<Double>();
    vol_1 = new ArrayList<Double>();
    sub_returns = new ArrayList<Double>();
    trade_days = new ArrayList<String>();
    returns = new ArrayList<Double>();
    longreturns = new ArrayList<Double>();
    shortreturns = new ArrayList<Double>();
    dropdowns = new ArrayList<Double>();
    success = new ArrayList<Double>();
    dates_low_high = new ArrayList<String>();
    crits = new ArrayList<String>();
    svm = new ArrayList<String>();
    filters = new ArrayList<Filter>();
    date_returns = new ArrayList<String>();

    live_series = new ArrayList<Double>(); //the data to be applied out of sample
    ib_data_hash = new ibHash();

    fridayROI = 0;
    fridayROI_pos = 0;
    fridays = 0;
    int end_hour;
    lookback_returns = new ArrayList<Double>();
    num_pos_returns = 0;
    deg_0 = new ArrayList<Double>();
    deg_1 = new ArrayList<Double>();
    crit_0 = new ArrayList<Double>();
    crit_1 = new ArrayList<Double>();
    full_returns_array = new ArrayList<double[]>();
    morning_returns = new ArrayList<double[]>();

    morning_buy = true; //enter transaction at morning open
    morning_optimize = false; //optimize in the morning trading hours
    num_full_positive_returns = 0;
    //--- Now get historical interp values ------
    //uploadInterpParams("max_int.dat"); 
    //-------------------------------------------
    forex24 = true;
    ret_dist = new double[trade_obs];
    pos_ret_dist = new int[trade_obs];
    neg_ret_dist = new int[trade_obs];
    neg_trades_started = new int[trade_obs];
    pos_trades_started = new int[trade_obs];
    neg_trades_started_mean = new double[trade_obs];
    pos_trades_started_mean = new double[trade_obs];
    diff_account = new double[trade_obs];
    pos_ret_mean_time = new double[trade_obs];
    neg_ret_mean_time = new double[trade_obs];

    mdfaTrades = new ArrayList<MDFATrade>();
    fmt = DateTimeFormat.forPattern("y-MM-dd HH:mm:ss");
    formatter = new DecimalFormat("#0.000000");
    formatter3 = new DecimalFormat("#0.00000");
    formatter2 = new DecimalFormat("#0.00");
    histo_stat = new int[100];
    interp_vals = new ArrayList<Double>();
    max_ranks = new ArrayList<Double>();
    profit_baby = 0;
    //setForecastDFAParameters();
    bad_starts = 0;
    n_out_samp = 0;

    //take_profit = true;
    //take_profit_thresh = .0020;
    current_signal = 0;
    prev_price = 0;
    cur_pnl = 0;
    stop_loss = stop_loss_thresh;
    out_transaction = 0;
    in_transaction = 0;
    red_zone = false;
    global_stop_loss = stop_loss_thresh;
    profitable_stop = .0005;
    count = 0;
    short_sell = true;
    long_buy = true;
    day_count = 0;
    ArrayList<String> trade_times = new ArrayList<String>();
    ArrayList<String> fxcm_dates = new ArrayList<String>();

    lo_pnl = 0;
    hi_pnl = 0;
    price_borrowed = 0;
    price_sold = 0;
    price_bought = 0;
    last_price = 0;

    binary_rule = true;
    signal_strength_rule = true;
    downtick_strategy = false;
    signal_profit = false;
    friday_closing = false;
    //asian_close = true;

    //----- Fill up with times here -------------------------
    for (i = 0; i < 10; i++) {
        trade_times.add("0" + i + ":00:00");
    }
    for (i = 10; i < 24; i++) {
        trade_times.add(i + ":00:00");
    }
    //       trade_times.add("00:00:00");
    //       trade_times.add("06:00:00");
    //       trade_times.add("12:00:00");
    //       trade_times.add("18:00:00");     

    String[] hourToks = startingTime.split("[:]+");
    start_hour = (new Integer(hourToks[0])).intValue();

    hourToks = endingTime.split("[:]+");
    end_hour = (new Integer(hourToks[0])).intValue();

    inverse_hours = false;
    if (start_hour > end_hour) //must switch hours here
    {
        int temphour = end_hour;

        inverse_hours = true;
        end_hour = start_hour;
        start_hour = temphour;
    }

    if (ib_data && ib_data_file != null) {
        try {

            fin = new FileInputStream(ib_data_file);
            din = new DataInputStream(fin);
            br = new BufferedReader(new InputStreamReader(din));

            while ((strline = br.readLine()) != null) {
                String[] sp = strline.split("[,]+");
                ib_data_hash.put(sp[0], new String(
                        sp[1] + " " + sp[2] + " " + sp[3] + " " + sp[4] + " " + sp[5] + " " + sp[6]));
                //System.out.println(sp[1] + " " + sp[2] + " " + sp[3] + " " + sp[4] + " " + sp[5]  + " " + sp[6]);
            }
        } catch (FileNotFoundException fe) {
            System.out.println("File not found..." + fe);
        } catch (IOException ioe) {
            System.out.println("IO procedure faulty..." + ioe);
        }
    }

    jpy = false;
    try {

        PrintWriter b0_coeff = new PrintWriter(new FileWriter("b0_coeff.dat"));
        PrintWriter perform = new PrintWriter(new FileWriter("intraday_performance_" + n_files + ".dat"));
        PrintWriter dailyout = new PrintWriter(new FileWriter("daily_nasdaq.dat"));
        PrintWriter out = new PrintWriter(new FileWriter("strategy_results.dat"));

        for (file_count = 0; file_count < 1; file_count++) {

            if (dataFiles[file_count].indexOf("JPY") != -1 && dataFiles[file_count].indexOf("NOKJPY") == -1) {
                //change_time_zone = true; System.out.println("Changed time zone to Tokyo");
                jpy = true;
                stop_loss_thresh = stop_loss_thresh * 100;
                take_profit_thresh = take_profit_thresh * 100;
                global_stop_loss = global_stop_loss * 100;
                stop_loss = stop_loss_thresh;
            } else if (futures_data) {
                //change_time_zone = true; System.out.println("Changed time zone to Tokyo");

                stop_loss_thresh = stop_loss_thresh * 10000;
                stop_loss = stop_loss_thresh;
                take_profit_thresh = take_profit_thresh * 10000;
                global_stop_loss = global_stop_loss * 10000;
            }

            ArrayList<String> dayData = new ArrayList<String>();
            setTimeStandards(new File(dataFiles[file_count]));
            System.out.println("opening " + dataFiles[file_count]);
            fin = new FileInputStream(dataFiles[file_count]);
            din = new DataInputStream(fin);
            br = new BufferedReader(new InputStreamReader(din));
            lookback_ready = false;
            spread = new PrintWriter(new FileWriter("spread_" + dataFiles[file_count] + ".dat"));
            //if(print_debug)System.out.println("Entering loop...");
            trading_hours = false;
            computed = false;

            //---- add the latest FXCM data -
            while ((strline = br.readLine()) != null) {
                dayData.add(strline);

                //add the date
                tokens = strline.split("[,]+");
                fxcm_dates.add(tokens[0]);

            }

            //----- now add the lates IB data ------------
            System.out.println("opening " + dataFiles[file_count] + " IB data");
            String[] fxpair = dataFiles[file_count].split("[.]+");

            if ((new File(fxpair[0] + ".IB.dat")).exists()) {
                fin = new FileInputStream(fxpair[0] + ".IB.dat");
                din = new DataInputStream(fin);
                br = new BufferedReader(new InputStreamReader(din));

                String lastFXCMdate = fxcm_dates.get(fxcm_dates.size() - 1);

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
                Date date1 = sdf.parse(lastFXCMdate);

                while ((strline = br.readLine()) != null) {
                    tokens = strline.split("[,]+");

                    Date date2 = sdf.parse(tokens[0]);

                    if (!fxcm_dates.contains(tokens[0]) && date1.before(date2)) {
                        System.out.println("New time and observation " + strline);
                        dayData.add(strline);
                    }
                }
            }

            for (int ts = 0; ts < dayData.size(); ts++) {
                strline = dayData.get(ts);

                //System.out.println(strline);
                tokens = strline.split(delims);
                n_toks = tokens.length; //System.out.println("Number of toks = "+n_toks);
                if (n_toks == 0) {
                    System.out.println("End of file");
                    break;
                }

                if (n_toks >= 6) {
                    bid_ask_data = true;
                } else {
                    bid_ask_data = false;
                }

                date_stamp = tokens[0];
                date_tokens = date_stamp.split(date_delims);
                intdates = date_tokens[0].split(ddelims);
                DateTime weekend = new DateTime((new Integer(intdates[0])).intValue(),
                        (new Integer(intdates[1])).intValue(), (new Integer(intdates[2])).intValue(), 14, 0);
                time = date_tokens[1];

                //insampStart is the time we collect daily data 

                //if(date_stamp.indexOf(insampStart) != -1)
                if (trade_times.contains(time)) {

                    //get bid/mid/ask data
                    if (ib_data && ib_data_hash.containsKey(tokens[0])) {
                        String[] hashed = ib_data_hash.get(tokens[0]).split("[ ]+");
                        for (i = 1; i < hashed.length; i++) {
                            tokens[i] = hashed[i];
                        } // System.out.print(tokens[i] + " ");} 
                    }

                    daily_price.add(new Double(tokens[1]));
                    current_price = (new Double(tokens[1])).doubleValue();

                    if (daily_price.size() == 1) {
                        daily_returns.add(new Double(0.0));
                        prev_price = current_price;
                    } else {
                        daily_returns.add(log(current_price) - log(prev_price));
                        prev_price = current_price;
                    }

                    daily_dates.add(date_stamp);

                }

                print_filter = false;
                latestDates.add(date_stamp);
                D = new Double(tokens[4]);
                close_series.add(D);
                if (ib_data && ib_data_hash.containsKey(tokens[0])) {

                    String[] hashed = ib_data_hash.get(tokens[0]).split("[ ]+");
                    //System.out.println("Contains " + tokens[0] + ", lengths = " + hashed.length + ", " + tokens.length);
                    for (i = 1; i < hashed.length; i++) {
                        tokens[i] = hashed[i];
                    } // System.out.print(tokens[i] + " ");} 
                    bid.add(new Double(tokens[2]));
                    ask.add(new Double(tokens[3]));
                    mid.add(new Double(tokens[1]));
                } else {
                    bid.add(new Double(tokens[2]));
                    ask.add(new Double(tokens[3]));
                    mid.add(new Double(tokens[1]));
                }

                D = new Double(tokens[1]);
                price.add(D);

                D = new Double(tokens[4]);
                if (ib_data && ib_data_hash.containsKey(tokens[0])) {
                    live_series.add(log(mid.get(mid.size() - 1)) - log(mid.get(mid.size() - 2)));
                } else {
                    live_series.add(D);
                }

                //           if(ib_data && ib_data_hash.containsKey(tokens[0])) //use as is
                //           {lo_price.add(new Double(tokens[7])); hi_price.add(new Double(tokens[8]));}
                //           else
                //           {lo_price.add((new Double(tokens[7]))); hi_price.add((new Double(tokens[8])));}
                lo_price.add((new Double(tokens[2])));
                hi_price.add((new Double(tokens[3])));

                //---- start the account ------
                if (account.size() == 0) {
                    account.add(date_stamp + " " + 0);
                    dailyoutret.add(0.0);
                }

                String[] hours = time.split("[:]+");
                cur_hour = (new Integer(hours[0])).intValue();
                (new Integer(hours[1])).intValue();

                trading_closed = false;

                //if currently not in a transaction and between the hours of midnight and start-hour, then no new 
                //positions will be opened

                if (asian_close) //only closed if most recent transaction was closed artificially through SL or TP after end hours
                {
                    if ((in_transaction == 0 && out_transaction == 0) && cur_hour >= start_hour
                            && cur_hour <= end_hour) {
                        trading_closed = true;
                    } // if(printall) System.out.println(cur_hour + " " + start_hour);}
                } else {
                    if (cur_hour >= start_hour && cur_hour <= end_hour) {
                        trading_closed = true;
                    } // if(printall) {System.out.println(cur_hour + " " + start_hour);}}          
                }

                if (inverse_hours) {
                    trading_closed = !trading_closed;
                }

                its_closing_time = (weekend.dayOfWeek().getAsText().equals("Friday")
                        && date_stamp.indexOf("17:00:00") != -1);

                made_trade = false;
                if (daily_returns.size() >= n_obs && trade_times.contains(time)) //a new day begineth
                {

                    computed = true;
                    trading_hours = true;

                    tseries = new double[n_rep * n_obs];

                    for (i = 0; i < n_obs; i++) {
                        tseries[n_obs - 1 - i] = daily_returns.get(daily_returns.size() - 1 - i);
                        tseries[n_obs + n_obs - 1 - i] = daily_returns.get(daily_returns.size() - 1 - i);

                        if (n_rep > 2 && exp_series_1.size() > 0) {
                            tseries[n_obs * 2 + n_obs - 1 - i] = exp_series_1.get(exp_series_1.size() - 1 - i);
                        }
                        if (n_rep > 3 && exp_series_2.size() > 0) {
                            tseries[n_obs * 3 + n_obs - 1 - i] = exp_series_2.get(exp_series_2.size() - 1 - i);
                        }
                    }

                    mdfa.set_tseries(tseries, n_obs, n_rep);

                    //if(day_count == 0)  //recompute filter coefficients
                    if (day_count == 0 || weekend.dayOfWeek().getAsText().equals("Sunday")
                            && date_stamp.indexOf("18:00:00") != -1) {

                        if (printall)
                            System.out.println("Recomputing filter...");
                        mdfa.computeFilterGeneral(true, print_filter);
                        b_coeffs = new double[(n_rep - 1) * L]; //System.out.println(b_coeffs.length + " " + L + n_rep); 
                        for (l = 0; l < L; l++) {

                            for (i = 0; i < n_rep - 1; i++) {
                                b_coeffs[L * i + l] = mdfa.b[L * (i + 1) + l];
                            } // System.out.println(b_coeffs[l]);}               
                            //if(date_stamp.indexOf("2013-12-17") != -1) {System.out.println(b_coeffs[l]);}
                        }
                        if (printall)
                            System.out.println(date_stamp + " b_coeffs = " + b_coeffs[0] + " " + b_coeffs[1]
                                    + " " + b_coeffs[2]);

                        b_copy = new double[mdfa.b.length];
                        System.arraycopy(mdfa.b, 0, b_copy, 0, b_copy.length);
                        b0_coeff.println(b_coeffs[0]); // + ", " + b_coeffs[L] + ", " + b_coeffs[2*L]);

                    }

                    sum = 0.0;
                    for (j = 1; j < n_rep; j++) {
                        for (l = 0; l < L; l++) {
                            sum = sum + b_coeffs[L * (j - 1) + l] * tseries[N * j + n_obs - 1 - l];
                        }
                    }

                    prev_signal = current_signal;
                    current_signal = sum;
                    if (sig_inverse) {
                        current_signal = -current_signal;
                    }

                    //----final signal ---
                    daily_signal.add(current_signal);
                    daily_size = daily_price.size();
                    dailyReport.add(
                            "New day " + date_stamp + ", " + formatter3.format(daily_price.get(daily_size - 1))
                                    + ", " + formatter.format(tseries[n_obs - 1]) + ", " + current_signal);
                    //if(printall) System.out.println("New day "+date_stamp + ", " + formatter3.format(daily_price.get(daily_size-1)) + ", " + formatter.format(tseries[n_obs-1]) + ", " + current_signal);

                    //--compute binary trading rule ---

                    //if(printall) System.out.println("Current signal = " + current_signal + " Prev signal = " + prev_signal + ", trading_closed = " + trading_closed);
                    if (friday_closing && its_closing_time) {
                        if (printall)
                            System.out.println("\nIt's Friday at 5pm, time to close shop for week");
                        if (current_signal > 0 && in_transaction == 1) //in a long transaction
                        {
                            price_sold = daily_price.get(daily_size - 1);
                            profit = price_sold - price_bought;
                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            amount = getAmount(account.get(account.size() - 1));
                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));

                            made_trade = true;
                            dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                            log_ret = daily_price.get(daily_size - 1).doubleValue();

                            in_transaction = 0;

                            if (printall) {
                                if (profit > 0)
                                    System.out.println("Sold for a profit of " + profit);
                                else if (profit < 0)
                                    System.out.println("Sold for a loss of " + profit);
                                System.out.println("profit, price_bought, price_sold = " + profit + ", "
                                        + price_bought + ", " + price_sold);
                            }
                        } else if (current_signal < 0 && out_transaction == 1) //in a short transaction
                        {

                            price_sold = daily_price.get(daily_size - 1);
                            profit = price_borrowed - price_sold;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            amount = getAmount(account.get(account.size() - 1));
                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));

                            made_trade = true;
                            dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                            log_ret = daily_price.get(daily_size - 1).doubleValue();

                            out_transaction = 0;

                            if (printall)
                                System.out.println("profit, price_borrowed, price_sold = " + profit + ", "
                                        + price_borrowed + ", " + price_sold);
                        }

                    } else {
                        //if(binary_rule && !trading_closed)
                        if (binary_rule) {

                            //                  if(signal_profit)
                            //                  {
                            //                    //if(printall) System.out.println("Trading_closed = " + trading_closed);
                            //                    if((current_signal > 0 && in_transaction == 1) && (daily_price.get(daily_size-1) > last_price))
                            //                    {
                            //                    
                            //                    
                            //                      price_sold = daily_price.get(daily_size-1);
                            //                 profit = price_sold - price_bought;
                            //                 if(profit > 0) {succ_trades=succ_trades+1;}
                            //                 total_trades=total_trades+1; 
                            //     
                            //                 amount = getAmount(account.get(account.size()-1));
                            //                 account.add(date_stamp + " " + (amount + profit));   
                            //                 amount = getAmount(account.get(account.size()-1));
                            //                
                            //                 made_trade = true;
                            //                 dailyoutret.add(daily_price.get(daily_size-1) - log_ret);
                            //                      log_ret = daily_price.get(daily_size-1).doubleValue();
                            //     
                            //                 in_transaction = 0;
                            //               
                            //                 if(printall){
                            //                      if(profit>0) System.out.println("Sold for a profit of " + profit);
                            //                      else if(profit<0) System.out.println("Sold for a loss of " + profit);
                            //                       System.out.println("profit, price_bought, price_sold = " + profit + ", " + price_bought + ", " + price_sold);
                            //                      }
                            //                    
                            //                      if(!trading_closed)
                            //                      {price_bought = daily_price.get(daily_size-1);
                            //                 //in_transaction = 1; 
                            //                 out_transaction = 1;
                            //                 last_price = daily_price.get(daily_size-1); 
                            //                 if(printall) System.out.println("Entered long transaction at " + price_bought);
                            //                 } 
                            //                    }
                            //                    else if((current_signal < 0 && out_transaction == 1) && (daily_price.get(daily_size-1) < last_price))
                            //                    {
                            //                    
                            //                     price_sold = daily_price.get(daily_size-1);
                            //                     profit = price_borrowed - price_sold;
                            // 
                            //                if(profit > 0) {succ_trades=succ_trades+1;}
                            //                total_trades=total_trades+1; 
                            //      
                            //                amount = getAmount(account.get(account.size()-1));
                            //                account.add(date_stamp + " " + (amount + profit));   
                            //                amount = getAmount(account.get(account.size()-1));
                            //                     
                            //                     made_trade = true;
                            //                     dailyoutret.add(daily_price.get(daily_size-1) - log_ret);
                            //                     log_ret = daily_price.get(daily_size-1).doubleValue();
                            //                     
                            //                     
                            //                     out_transaction = 0;
                            //                     
                            //                     if(printall)
                            //                     {
                            //                     if(profit>0) System.out.println("Sold for a profit of " + profit);
                            //                     else if(profit<0) System.out.println("Sold for a loss of " + profit);
                            //                     }
                            //                     if(printall) System.out.println("profit, price_borrowed, price_sold = " + profit + ", " + price_borrowed + ", " + price_sold);                   
                            //                    
                            //                     if(!trading_closed)
                            //                     {
                            //                     price_borrowed = daily_price.get(daily_size-1);
                            //                     //out_transaction = 1;    
                            //                     in_transaction = 1;
                            //                     if(printall)System.out.println("Entered short transaction at " + price_borrowed);
                            //                     }
                            //                    }
                            //                  
                            //                  }

                            made_trade = false;
                            if (current_signal > 0 && prev_signal <= 0) //new point positive, we see momentum, buy
                            {

                                //last_price = daily_price.get(daily_size-1); 
                                last_price = ask.get(ask.size() - 1);

                                if (short_sell && out_transaction == 1) //in a short-sell transaction, sell
                                {
                                    //price_sold = daily_price.get(daily_size-1);
                                    price_sold = ask.get(ask.size() - 1);
                                    profit = price_borrowed - price_sold;

                                    if (profit > 0) {
                                        succ_trades = succ_trades + 1;
                                    }
                                    total_trades = total_trades + 1;

                                    amount = getAmount(account.get(account.size() - 1));
                                    account.add(date_stamp + " " + (amount + profit));
                                    amount = getAmount(account.get(account.size() - 1));

                                    made_trade = true;
                                    dailyoutret.add(ask.get(ask.size() - 1) - log_ret);
                                    log_ret = ask.get(ask.size() - 1).doubleValue();

                                    out_transaction = 0;

                                    if (printall) {
                                        if (profit > 0)
                                            System.out.println("Sold for a profit of " + profit);
                                        else if (profit < 0)
                                            System.out.println("Sold for a loss of " + profit);

                                        System.out.println("profit, price_borrowed, price_sold = " + profit
                                                + ", " + price_borrowed + ", " + price_sold);
                                    }
                                }

                                if ((long_buy && in_transaction == 0) && !trading_closed) {
                                    price_bought = ask.get(ask.size() - 1);
                                    in_transaction = 1;

                                    if (printall)
                                        System.out.println("Entered long transaction at " + price_bought);
                                }

                            } else if (current_signal < 0 && prev_signal >= 0) //if in transaction and signal goes below, sell
                            {
                                //last_price = daily_price.get(daily_size-1); 
                                last_price = bid.get(bid.size() - 1);

                                if (long_buy && in_transaction == 1) {
                                    price_sold = bid.get(bid.size() - 1);
                                    profit = price_sold - price_bought;
                                    if (profit > 0) {
                                        succ_trades = succ_trades + 1;
                                    }
                                    total_trades = total_trades + 1;

                                    amount = getAmount(account.get(account.size() - 1));
                                    account.add(date_stamp + " " + (amount + profit));
                                    amount = getAmount(account.get(account.size() - 1));

                                    made_trade = true;
                                    dailyoutret.add(bid.get(bid.size() - 1) - log_ret);
                                    log_ret = bid.get(bid.size() - 1).doubleValue();

                                    in_transaction = 0;

                                    if (printall) {
                                        if (profit > 0)
                                            System.out.println("Sold for a profit of " + profit);
                                        else if (profit < 0)
                                            System.out.println("Sold for a loss of " + profit);
                                        System.out.println("profit, price_bought, price_sold = " + profit + ", "
                                                + price_bought + ", " + price_sold);
                                    }

                                }

                                if ((short_sell && out_transaction == 0) && !trading_closed) {
                                    price_borrowed = bid.get(bid.size() - 1);
                                    out_transaction = 1;

                                    if (printall)
                                        System.out.println("Entered short transaction at " + price_borrowed);

                                }
                            }
                        }

                        if (signal_strength_rule
                                && ((in_transaction == 0 && out_transaction == 0) && !trading_closed)) {

                            if (current_signal > 0) //new point positive, we see momentum, buy
                            {
                                //last_price = daily_price.get(daily_size-1); 
                                last_price = ask.get(ask.size() - 1);

                                if (short_sell && out_transaction == 1) //in a short-sell transaction, sell
                                {
                                    price_sold = ask.get(ask.size() - 1);
                                    profit = price_borrowed - price_sold;

                                    if (profit > 0) {
                                        succ_trades = succ_trades + 1;
                                    }
                                    total_trades = total_trades + 1;

                                    amount = getAmount(account.get(account.size() - 1));
                                    account.add(date_stamp + " " + (amount + profit));
                                    amount = getAmount(account.get(account.size() - 1));

                                    made_trade = true;
                                    dailyoutret.add(ask.get(ask.size() - 1) - log_ret);
                                    log_ret = ask.get(ask.size() - 1).doubleValue();

                                    out_transaction = 0;

                                    if (printall)
                                        System.out.println("profit, price_borrowed, price_sold = " + profit
                                                + ", " + price_borrowed + ", " + price_sold);
                                }

                                if (long_buy && in_transaction == 0) {
                                    price_bought = ask.get(ask.size() - 1);
                                    in_transaction = 1;

                                    if (printall)
                                        System.out.println("Entered long transaction at " + price_bought);
                                }

                            } else if (current_signal < 0) //if in transaction and signal goes below, sell
                            {

                                last_price = bid.get(bid.size() - 1);

                                if (long_buy && in_transaction == 1) {
                                    price_sold = bid.get(bid.size() - 1);
                                    profit = price_sold - price_bought;
                                    if (profit > 0) {
                                        succ_trades = succ_trades + 1;
                                    }
                                    total_trades = total_trades + 1;

                                    amount = getAmount(account.get(account.size() - 1));
                                    account.add(date_stamp + " " + (amount + profit));
                                    amount = getAmount(account.get(account.size() - 1));

                                    made_trade = true;
                                    dailyoutret.add(bid.get(bid.size() - 1) - log_ret);
                                    log_ret = bid.get(bid.size() - 1).doubleValue();

                                    in_transaction = 0;

                                    if (printall)
                                        System.out.println("Bought for a profit of " + profit);
                                    if (printall)
                                        System.out.println("profit, price_bought, price_sold = " + profit + ", "
                                                + price_bought + ", " + price_sold);

                                }

                                if (short_sell && out_transaction == 0) {
                                    price_borrowed = bid.get(bid.size() - 1);
                                    out_transaction = 1;

                                    if (printall)
                                        System.out.println("Entered short transaction at " + price_borrowed);
                                }
                            }
                        }

                        if (!made_trade) {
                            account.add(date_stamp + " " + amount);
                            dailyoutret.add(price.get(price.size() - 1) - log_ret);
                            log_ret = price.get(price.size() - 1);
                        }

                        day_count++;

                        //if(recompute_day == day_count) {day_count=0;}
                        //if(printall) System.out.println(""+date_stamp + ", " + formatter3.format(price.get(price.size()-1)) + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", " + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl));             
                        allsignal.add(date_stamp + " " + current_signal);
                    }

                }
                if (trading_hours)// && !trading_closed)// && cur_min != 30)
                {

                    if (cur_pnl != 0 && date_stamp.indexOf("22:30:00") != -1) //close out position
                    {
                        if (in_transaction == 1) {

                            //System.out.println("Ending trading for next startup");

                            price_sold = price.get(price.size() - 1);
                            profit = price_sold - price_bought;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count));

                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));

                            dailyoutret.add(price.get(price.size() - 1) - log_ret);
                            log_ret = price.get(price.size() - 1);

                            in_transaction = 0;

                            //-- return stop loss to original setting --------
                            //System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit);
                            stop_loss = global_stop_loss;
                            red_zone = false;

                        } else if (out_transaction == 1) {

                            //System.out.println("Ending trading for next startup");

                            price_sold = price.get(price.size() - 1);
                            profit = price_borrowed - price_sold;

                            //                  price_sold = price_borrowed + stop_loss;
                            //                  profit = -stop_loss;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count));

                            //account.add(date_stamp + " " + (amount - stop_loss));  
                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));

                            dailyoutret.add(price.get(price.size() - 1) - log_ret);
                            log_ret = price.get(price.size() - 1);

                            out_transaction = 0;

                            //-- return stop loss to original setting --------
                            stop_loss = global_stop_loss;
                            red_zone = false;

                        }
                    }

                    if (in_transaction == 1) //in a long transaction 
                    {

                        if (red_zone && bid.get(bid.size() - 1) > last_price) //check if new high price
                        {
                            last_price = bid.get(bid.size() - 1);
                        }

                        //cur_pnl = price.get(price.size()-1) - last_price;    
                        cur_pnl = bid.get(bid.size() - 1) - last_price;
                        lo_pnl = lo_price.get(lo_price.size() - 1) - last_price;
                        hi_pnl = hi_price.get(hi_price.size() - 1) - last_price;

                        if (cur_pnl < -stop_loss) {
                            if (printall)
                                System.out.println(
                                        "Stop-loss Activated since lo_pnl = " + cur_pnl + " < -" + stop_loss);
                            //System.out.println("Closing price at bar was " + price.get(price.size()-1) + " but lowest price in bar was " + lo_price.get(lo_price.size()-1));
                            //--------------sell---------- 

                            //price_sold = price.get(price.size()-1);

                            //price_sold = price.get(price.size()-1);
                            price_sold = bid.get(bid.size() - 1);
                            profit = price_sold - price_bought;

                            //                  price_sold = price_bought - stop_loss;
                            //                  profit = -stop_loss;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count));
                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));
                            //if(weekend.dayOfWeek().getAsText().equals("Sunday")) {sunday.add(date_stamp + " " + profit);}

                            dailyoutret.add(bid.get(bid.size() - 1) - log_ret);
                            log_ret = bid.get(bid.size() - 1);

                            in_transaction = 0;

                            //-- return stop loss to original setting --------
                            if (printall)
                                System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit);
                            stop_loss = global_stop_loss;
                            red_zone = false;
                        } else if (cur_pnl >= take_profit_thresh) {

                            if (printall)
                                System.out.println("Profit zone activated since cur_pnl = " + cur_pnl + " > "
                                        + take_profit_thresh);

                            //price_sold = price.get(price.size()-1);
                            price_sold = bid.get(bid.size() - 1);
                            profit = price_sold - price_bought;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count));
                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));
                            //if(weekend.dayOfWeek().getAsText().equals("Sunday")) {sunday.add(date_stamp + " " + profit);}
                            dailyoutret.add(price_sold - log_ret);
                            log_ret = price_sold;

                            in_transaction = 0;

                            //-- return stop loss to original setting --------
                            if (printall)
                                System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit);
                            stop_loss = global_stop_loss;

                            //                  stop_loss = profitable_stop;
                            //                  last_price = price.get(price.size()-1);
                            //                  red_zone = true;
                        }
                    } else if (out_transaction == 1) {

                        if (red_zone && ask.get(ask.size() - 1) < last_price) //check if new high price
                        {
                            last_price = ask.get(ask.size() - 1);
                        }

                        //cur_pnl =  last_price - price.get(price.size()-1);               
                        cur_pnl = last_price - ask.get(ask.size() - 1);
                        lo_pnl = last_price - hi_price.get(hi_price.size() - 1);
                        hi_pnl = last_price - lo_price.get(lo_price.size() - 1);

                        if (cur_pnl < -stop_loss) {
                            if (printall)
                                System.out.println(
                                        "Stop-loss Activated since lo_pnl = " + cur_pnl + " < -" + stop_loss);
                            //System.out.println("Closing price at bar was " + price.get(price.size()-1) + " but highest price in bar was " + hi_price.get(hi_price.size()-1));
                            //--------------sell---------- 

                            //price_sold = price.get(price.size()-1);
                            price_sold = ask.get(ask.size() - 1);
                            profit = price_borrowed - price_sold;

                            //                  price_sold = price_borrowed + stop_loss;
                            //                  profit = -stop_loss;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count));
                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));
                            //if(weekend.dayOfWeek().getAsText().equals("Sunday")) {sunday.add(date_stamp + " " + profit);}

                            dailyoutret.add(price_sold - log_ret);
                            log_ret = price_sold;

                            out_transaction = 0;

                            //-- return stop loss to original setting --------
                            stop_loss = global_stop_loss;
                            red_zone = false;

                            if (printall)
                                System.out
                                        .println("Bought at " + date_stamp + " for a profit/loss of " + profit);

                        } else if (cur_pnl >= take_profit_thresh) {

                            if (printall)
                                System.out.println("Profit zone activated since cur_pnl = " + cur_pnl + " > "
                                        + take_profit_thresh);
                            //                  stop_loss = profitable_stop;
                            //                  last_price = price.get(price.size()-1);
                            //                  red_zone = true;

                            //price_sold = price.get(price.size()-1);
                            price_sold = ask.get(ask.size() - 1);
                            profit = price_borrowed - price_sold;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count));
                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));

                            //if(weekend.dayOfWeek().getAsText().equals("Sunday")) {sunday.add(date_stamp + " " + profit);}

                            dailyoutret.add(price_sold - log_ret);
                            log_ret = price_sold;

                            out_transaction = 0;

                            //-- return stop loss to original setting --------
                            stop_loss = global_stop_loss;
                            red_zone = false;

                            if (printall)
                                System.out
                                        .println("Bought at " + date_stamp + " for a profit/loss of " + profit);

                        }
                    } else if (downtick_strategy) {

                        //strategy here is to buy/sell according to signal iff downtick has occurred

                        if (current_signal > 0 && (price_sold > price.get(price.size() - 1))) {

                            //let's buy some more 
                            if (printall)
                                System.out.println("Buying at " + date_stamp + " since last price sold = "
                                        + price_sold + " > " + price.get(price.size() - 1));
                            price_bought = price.get(price.size() - 1);
                            last_price = price.get(price.size() - 1);
                            in_transaction = 1;

                        } else if (current_signal < 0 && (price_sold < price.get(price.size() - 1))) {

                            //let's short some more
                            if (printall)
                                System.out.println(
                                        "Shorting at " + date_stamp + " since last price bought back at = "
                                                + price_sold + " < " + price.get(price.size() - 1));

                            price_borrowed = price.get(price.size() - 1);
                            last_price = price.get(price.size() - 1);
                            out_transaction = 1;

                            if (printall)
                                System.out.println("Entered short transaction at " + price_borrowed);
                        }
                        cur_pnl = 0;
                        lo_pnl = 0;
                        hi_pnl = 0;
                    } else {
                        cur_pnl = 0;
                        lo_pnl = 0;
                        hi_pnl = 0;
                    }

                    if (weekend.dayOfWeek().getAsText().equals("Friday")
                            && date_stamp.indexOf("17:00:00") != -1) {
                        if (printall)
                            System.out.println("End of week");

                    }

                    dailyReport.add("" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1))
                            + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", "
                            + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl));

                    if (printall) {
                        if (current_signal > 0) {
                            System.out
                                    .println("" + date_stamp + ", " + formatter3.format(bid.get(bid.size() - 1))
                                            + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", "
                                            + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl));
                        } else {
                            System.out
                                    .println("" + date_stamp + ", " + formatter3.format(ask.get(ask.size() - 1))
                                            + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", "
                                            + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl));
                        }
                    }

                    allsignal.add(date_stamp + " " + current_signal);
                }

            }
        }

        double mean_ntrades = 0;
        computed = true;
        dailyoutret.set(1, 0.0);
        double[] dreturns = new double[account.size()];
        dreturns[0] = 0;

        double mean = 0;
        double sd = 0;
        n_neg_ret = 0;
        n_pos_ret = 0;
        neg_ret_mean = 0;
        pos_ret_mean = 0;
        pnl = 0;

        int n_trades = 0;
        ArrayList<Integer> n_trades_day = new ArrayList<Integer>();
        double pret;
        ArrayList<Double> perf_rets = new ArrayList<Double>();
        ArrayList<String> perf_datestimes = new ArrayList<String>();

        for (i = 1; i < account.size(); i++) {
            out.println(account.get(i));
            dailyout.println(account.get(i) + " " + dailyoutret.get(i));
            //System.out.println(account.get(i));
            dreturns[i] = getAmount(account.get(i)) - getAmount(account.get(i - 1));

            if (jpy) {
                dreturns[i] = dreturns[i] * .01;
            }

            dates = account.get(i).split("[ ]+");
            if (!perf_dates.contains(dates[0])) //first date entry
            {

                if (perf_dates.size() != 0) {
                    perf_returns.add(pnl);
                    n_trades_day.add(n_trades);
                }

                perf_dates.add(dates[0]);
                if (dreturns[i] != 0) {
                    perf_datestimes.add(dates[0] + " " + dates[1]);
                    perf_rets.add(dreturns[i]);
                }
                pnl = dreturns[i];
                if (dreturns[i] != 0) {
                    n_trades = 1;
                } else {
                    n_trades = 0;
                }
            } else //already contains the date, so add on pnl
            {
                pnl = pnl + dreturns[i];
                //System.out.println(dreturns[i]);
                if (dreturns[i] != 0) {
                    n_trades++;
                    perf_datestimes.add(dates[0] + " " + dates[1]);
                    perf_rets.add(dreturns[i]);
                } // System.out.println(n_trades);}
            }

            if (dreturns[i] > 0) {
                n_pos_ret++;
                pos_ret_mean = pos_ret_mean + dreturns[i];
                mean = mean + dreturns[i];
            } else if (dreturns[i] < 0) {
                n_neg_ret++;
                neg_ret_mean = neg_ret_mean - dreturns[i];
                mean = mean + dreturns[i];
            }

        }
        perf_returns.add(pnl);
        n_trades_day.add(n_trades);

        for (i = 0; i < perf_dates.size(); i++) {
            if (printall)
                System.out.println(perf_dates.get(i) + " " + perf_returns.get(i) + " " + n_trades_day.get(i));
            perform.println(perf_dates.get(i) + " " + perf_returns.get(i));
            date_returns.add(perf_dates.get(i) + " " + perf_returns.get(i));
            mean_ntrades = mean_ntrades + n_trades_day.get(i);
        }
        mean_ntrades = mean_ntrades / n_trades_day.size();

        dates_price = new String[n_obs];

        for (i = 1; i < n_obs; i++) {

            tokens = allsignal.get(allsignal.size() - n_obs + i).split("[ ]+");

            if (perf_datestimes.contains(latestDates.get(latestDates.size() - n_obs + i))) {
                pret = perf_rets.get(perf_datestimes.indexOf(latestDates.get(latestDates.size() - n_obs + i)));
                System.out.println(latestDates.get(latestDates.size() - n_obs + i) + " " + pret);
            } else {
                pret = 0;
            }

            //System.out.println(tokens[0] + " " + latestDates.get(latestDates.size() - n_obs + i - 1));

            //         if(tokens[0].equals(latestDates.get(latestDates.size() - n_obs + i - 1)))
            //         {
            dates_price[i] = new String(latestDates.get(latestDates.size() - n_obs + i) + " "
                    + (mid.get(mid.size() - n_obs + i) - mid.get(mid.size() - n_obs + i - 1)) + " "
                    + mid.get(mid.size() - n_obs + i) + " " + tokens[2] + " " + pret);
            //        }
        }
        dates_price[0] = dates_price[1];

        mean = mean / (n_pos_ret + n_neg_ret);

        System.out.println("ROI = " + account.get(account.size() - 1));
        //--- compute stats---------------
        double risk = neg_ret_mean / (double) n_neg_ret;
        System.out.println("neg_ret_mean = " + (-neg_ret_mean) + ", " + n_neg_ret);

        double reward = pos_ret_mean / (double) n_pos_ret;
        System.out.println("pos_ret_mean = " + pos_ret_mean + ", " + n_pos_ret);

        double win_ratio = (double) (n_pos_ret) / (n_pos_ret + n_neg_ret);

        kellyPerc = win_ratio - (1.0 - win_ratio) * (risk / reward);
        ulcer_index = ulcerIndex(dreturns);

        System.out.println("win ratio = " + win_ratio + ", risk = " + risk + ", reward = " + reward);
        System.out.println("kelly and ulcer = " + kellyPerc + " " + ulcer_index);

        for (i = 0; i < dreturns.length; i++) {
            sd = sd + (dreturns[i] - mean) * (dreturns[i] - mean) / ((double) dreturns.length);
        }

        standard_deviation = Math.sqrt(sd);

        sharpeRatio = Math.sqrt(250) * mean / standard_deviation;
        maxdraw = computeDrawdown(dreturns);
        rank_coeff = segmentRankCorrelation(30, dreturns);

        System.out.println("MeanRet = " + mean + ", Sharpe = " + sharpeRatio + ", MaxDD = " + maxdraw
                + ", Rank = " + rank_coeff + ", avg_n_trades = " + mean_ntrades);

        out.close();
        dailyout.close();
        perform.close();
        b0_coeff.close();

    } catch (ParseException fe) {
        System.out.println("ParseException");
    } catch (NullPointerException fe) {
        System.out.println("Null pointer");
    } catch (IllegalArgumentException fe) {
        System.out.println("IllegalArgument");
    } catch (FileNotFoundException fe) {
        System.out.println("File not found..." + fe);
    } catch (IOException ioe) {
        System.out.println("IO procedure faulty..." + ioe);
    }

    n_files++;

    return computed;

}

From source file:ch.imetrica.mdfaTradingStrategies.MDFAStrategyEvolution.java

public boolean startStrategyDailyIntradayOneHourBidAsk15Min(double sprd, boolean spreadon) {

    int j, i, l, N, file_count;
    double sum = 0;
    Double D;/*ww  w.ja va  2  s.c  om*/
    String ddelims = "[-]";
    boolean computed = false;
    boolean print_filter = false;
    boolean made_trade = true;
    String date_stamp, strline;
    int daily_size;
    double profit, price_borrowed, price_sold, price_bought;
    double current_price, prev_price;
    double last_price, cur_pnl, stop_loss, lo_pnl, hi_pnl;
    double log_ret = 0;
    signal = new double[trade_obs];
    xt = new double[trade_obs];
    lag_signals = new double[trade_obs];
    prix = new double[trade_obs];
    lo_prix = new double[trade_obs];
    hi_prix = new double[trade_obs];
    total_succ = 0;
    total = 0;
    log_price = 0;
    N = n_obs;
    avg_vol = 0.0;
    b_avg = new double[L * n_rep];
    count = 0;
    trade_succ_ratio = 0;
    double amount = 0;
    double prev_signal;
    reg_trading_hours = false;
    String[] intdates;
    //make sure arraylists empty
    ArrayList<String> perf_dates = new ArrayList<String>();
    ArrayList<Double> perf_returns = new ArrayList<Double>();
    double pnl;
    String[] dates;
    boolean inverse_hours = false;
    String time;
    ArrayList<String> account = new ArrayList<String>();
    ArrayList<String> latestDates = new ArrayList<String>();
    last_trades = new ArrayList<Integer>();
    final_trades = new ArrayList<Double>();
    dailyoutret = new ArrayList<Double>();
    maxIntValue = new ArrayList<Double>();
    avg_volatility = new ArrayList<Double>();
    close_series = new ArrayList<Double>();
    highlow_series = new ArrayList<Double>();
    exp_series_1 = new ArrayList<Double>();
    exp_series_2 = new ArrayList<Double>();
    price = new ArrayList<Double>();
    lo_price = new ArrayList<Double>();
    hi_price = new ArrayList<Double>();
    mid = new ArrayList<Double>();
    bid = new ArrayList<Double>();
    ask = new ArrayList<Double>();
    dates_series = new ArrayList<String>();
    dailyReport = new ArrayList<String>();
    b0_trend = new ArrayList<Double>();
    vol_0 = new ArrayList<Double>();
    vol_1 = new ArrayList<Double>();
    sub_returns = new ArrayList<Double>();
    trade_days = new ArrayList<String>();
    returns = new ArrayList<Double>();
    longreturns = new ArrayList<Double>();
    shortreturns = new ArrayList<Double>();
    dropdowns = new ArrayList<Double>();
    success = new ArrayList<Double>();
    dates_low_high = new ArrayList<String>();
    crits = new ArrayList<String>();
    svm = new ArrayList<String>();
    filters = new ArrayList<Filter>();
    date_returns = new ArrayList<String>();

    live_series = new ArrayList<Double>(); //the data to be applied out of sample
    ib_data_hash = new ibHash();

    fridayROI = 0;
    fridayROI_pos = 0;
    fridays = 0;
    int end_hour;
    lookback_returns = new ArrayList<Double>();
    num_pos_returns = 0;
    deg_0 = new ArrayList<Double>();
    deg_1 = new ArrayList<Double>();
    crit_0 = new ArrayList<Double>();
    crit_1 = new ArrayList<Double>();
    full_returns_array = new ArrayList<double[]>();
    morning_returns = new ArrayList<double[]>();

    morning_buy = true; //enter transaction at morning open
    morning_optimize = false; //optimize in the morning trading hours
    num_full_positive_returns = 0;
    //--- Now get historical interp values ------
    //uploadInterpParams("max_int.dat"); 
    //-------------------------------------------
    forex24 = true;
    ret_dist = new double[trade_obs];
    pos_ret_dist = new int[trade_obs];
    neg_ret_dist = new int[trade_obs];
    neg_trades_started = new int[trade_obs];
    pos_trades_started = new int[trade_obs];
    neg_trades_started_mean = new double[trade_obs];
    pos_trades_started_mean = new double[trade_obs];
    diff_account = new double[trade_obs];
    pos_ret_mean_time = new double[trade_obs];
    neg_ret_mean_time = new double[trade_obs];

    mdfaTrades = new ArrayList<MDFATrade>();
    fmt = DateTimeFormat.forPattern("y-MM-dd HH:mm:ss");
    formatter = new DecimalFormat("#0.000000");
    formatter3 = new DecimalFormat("#0.00000");
    formatter2 = new DecimalFormat("#0.00");
    histo_stat = new int[100];
    interp_vals = new ArrayList<Double>();
    max_ranks = new ArrayList<Double>();
    profit_baby = 0;
    //setForecastDFAParameters();
    bad_starts = 0;
    n_out_samp = 0;

    //take_profit = true;
    //take_profit_thresh = .0020;
    current_signal = 0;
    prev_price = 0;
    cur_pnl = 0;
    stop_loss = stop_loss_thresh;
    out_transaction = 0;
    in_transaction = 0;
    red_zone = false;
    global_stop_loss = stop_loss_thresh;
    profitable_stop = .0005;
    count = 0;
    short_sell = true;
    long_buy = true;
    day_count = 0;
    ArrayList<String> trade_times = new ArrayList<String>();
    ArrayList<String> fxcm_dates = new ArrayList<String>();

    lo_pnl = 0;
    hi_pnl = 0;
    price_borrowed = 0;
    price_sold = 0;
    price_bought = 0;
    last_price = 0;

    binary_rule = true;
    signal_strength_rule = true;
    downtick_strategy = false;
    signal_profit = false;
    friday_closing = false;
    //asian_close = true;

    boolean spread_on = spreadon;

    //----- Fill up with times here -------------------------
    for (i = 0; i < 10; i++) {
        trade_times.add("0" + i + ":00:00");
    }
    for (i = 10; i < 24; i++) {
        trade_times.add(i + ":00:00");
    }
    for (i = 0; i < 10; i++) {
        trade_times.add("0" + i + ":15:00");
    }
    for (i = 10; i < 24; i++) {
        trade_times.add(i + ":15:00");
    }
    for (i = 0; i < 10; i++) {
        trade_times.add("0" + i + ":30:00");
    }
    for (i = 10; i < 24; i++) {
        trade_times.add(i + ":30:00");
    }
    for (i = 0; i < 10; i++) {
        trade_times.add("0" + i + ":45:00");
    }
    for (i = 10; i < 24; i++) {
        trade_times.add(i + ":45:00");
    }

    //       trade_times.add("00:00:00");
    //       trade_times.add("06:00:00");
    //       trade_times.add("12:00:00");
    //       trade_times.add("18:00:00");     

    String[] hourToks = startingTime.split("[:]+");
    start_hour = (new Integer(hourToks[0])).intValue();

    hourToks = endingTime.split("[:]+");
    end_hour = (new Integer(hourToks[0])).intValue();

    inverse_hours = false;
    if (start_hour > end_hour) //must switch hours here
    {
        int temphour = end_hour;

        inverse_hours = true;
        end_hour = start_hour;
        start_hour = temphour;
    }

    if (ib_data && ib_data_file != null) {
        try {

            fin = new FileInputStream(ib_data_file);
            din = new DataInputStream(fin);
            br = new BufferedReader(new InputStreamReader(din));

            while ((strline = br.readLine()) != null) {
                String[] sp = strline.split("[,]+");
                ib_data_hash.put(sp[0], new String(
                        sp[1] + " " + sp[2] + " " + sp[3] + " " + sp[4] + " " + sp[5] + " " + sp[6]));
                //System.out.println(sp[1] + " " + sp[2] + " " + sp[3] + " " + sp[4] + " " + sp[5]  + " " + sp[6]);
            }
        } catch (FileNotFoundException fe) {
            System.out.println("File not found..." + fe);
        } catch (IOException ioe) {
            System.out.println("IO procedure faulty..." + ioe);
        }
    }

    jpy = false;
    try {

        PrintWriter b0_coeff = new PrintWriter(new FileWriter("b0_coeff.dat"));
        PrintWriter perform = new PrintWriter(new FileWriter("intraday_performance_" + n_files + ".dat"));
        PrintWriter dailyout = new PrintWriter(new FileWriter("daily_nasdaq.dat"));
        PrintWriter out = new PrintWriter(new FileWriter("strategy_results.dat"));

        for (file_count = 0; file_count < 1; file_count++) {

            if (dataFiles[file_count].indexOf("JPY") != -1 && dataFiles[file_count].indexOf("NOKJPY") == -1) {
                //change_time_zone = true; System.out.println("Changed time zone to Tokyo");
                jpy = true;
                stop_loss_thresh = stop_loss_thresh * 100;
                take_profit_thresh = take_profit_thresh * 100;
                global_stop_loss = global_stop_loss * 100;
                stop_loss = stop_loss_thresh;
            } else if (futures_data) {
                //change_time_zone = true; System.out.println("Changed time zone to Tokyo");

                stop_loss_thresh = stop_loss_thresh * 10000;
                stop_loss = stop_loss_thresh;
                take_profit_thresh = take_profit_thresh * 10000;
                global_stop_loss = global_stop_loss * 10000;
            }

            ArrayList<String> dayData = new ArrayList<String>();
            setTimeStandards(new File(dataFiles[file_count]));
            System.out.println("opening " + dataFiles[file_count]);
            fin = new FileInputStream(dataFiles[file_count]);
            din = new DataInputStream(fin);
            br = new BufferedReader(new InputStreamReader(din));
            lookback_ready = false;
            spread = new PrintWriter(new FileWriter("spread_" + dataFiles[file_count] + ".dat"));
            //if(print_debug)System.out.println("Entering loop...");
            trading_hours = false;
            computed = false;

            //---- add the latest FXCM data -
            while ((strline = br.readLine()) != null) {
                dayData.add(strline);

                //add the date
                tokens = strline.split("[,]+");
                fxcm_dates.add(tokens[0]);

            }

            //----- now add the lates IB data ------------
            System.out.println("opening " + dataFiles[file_count] + " IB data");
            String[] fxpair = dataFiles[file_count].split("[.]+");

            if ((new File(fxpair[0] + ".IB.dat")).exists()) {
                fin = new FileInputStream(fxpair[0] + ".IB.dat");
                din = new DataInputStream(fin);
                br = new BufferedReader(new InputStreamReader(din));

                String lastFXCMdate = fxcm_dates.get(fxcm_dates.size() - 1);

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
                Date date1 = sdf.parse(lastFXCMdate);

                while ((strline = br.readLine()) != null) {
                    tokens = strline.split("[,]+");

                    Date date2 = sdf.parse(tokens[0]);

                    if (!fxcm_dates.contains(tokens[0]) && date1.before(date2)) {
                        System.out.println("New time and observation " + strline);
                        dayData.add(strline);
                    }
                }
            }

            for (int ts = 0; ts < dayData.size(); ts++) {
                strline = dayData.get(ts);

                //System.out.println(strline);
                tokens = strline.split(delims);
                n_toks = tokens.length; //System.out.println("Number of toks = "+n_toks);
                if (n_toks == 0) {
                    System.out.println("End of file");
                    break;
                }

                if (n_toks >= 6) {
                    bid_ask_data = true;
                } else {
                    bid_ask_data = false;
                }

                date_stamp = tokens[0];
                date_tokens = date_stamp.split(date_delims);
                intdates = date_tokens[0].split(ddelims);
                DateTime weekend = new DateTime((new Integer(intdates[0])).intValue(),
                        (new Integer(intdates[1])).intValue(), (new Integer(intdates[2])).intValue(), 14, 0);
                time = date_tokens[1];

                //insampStart is the time we collect daily data 

                //if(date_stamp.indexOf(insampStart) != -1)
                if (trade_times.contains(time)) {

                    //get bid/mid/ask data
                    if (ib_data && ib_data_hash.containsKey(tokens[0])) {
                        String[] hashed = ib_data_hash.get(tokens[0]).split("[ ]+");
                        for (i = 1; i < hashed.length; i++) {
                            tokens[i] = hashed[i];
                        } // System.out.print(tokens[i] + " ");} 
                    }

                    daily_price.add(new Double(tokens[1]));
                    current_price = (new Double(tokens[1])).doubleValue();

                    if (daily_price.size() == 1) {
                        daily_returns.add(new Double(0.0));
                        prev_price = current_price;
                    } else {
                        daily_returns.add(log(current_price) - log(prev_price));
                        prev_price = current_price;
                    }

                    daily_dates.add(date_stamp);

                }

                print_filter = false;
                latestDates.add(date_stamp);
                D = new Double(tokens[4]);
                close_series.add(D);
                if (ib_data && ib_data_hash.containsKey(tokens[0])) {

                    String[] hashed = ib_data_hash.get(tokens[0]).split("[ ]+");
                    //System.out.println("Contains " + tokens[0] + ", lengths = " + hashed.length + ", " + tokens.length);
                    for (i = 1; i < hashed.length; i++) {
                        tokens[i] = hashed[i];
                    } // System.out.print(tokens[i] + " ");} 
                    bid.add((new Double(tokens[2])) - sprd);
                    ask.add((new Double(tokens[3])) + sprd);
                    mid.add(new Double(tokens[1]));
                } else {
                    bid.add((new Double(tokens[2])) - sprd);
                    ask.add((new Double(tokens[3])) + sprd);
                    mid.add(new Double(tokens[1]));
                }

                D = new Double(tokens[1]);
                //price.add(D); 

                if (spread_on) {
                    if (current_signal > 0) {
                        price.add(ask.get(ask.size() - 1));
                    } else {
                        price.add(bid.get(bid.size() - 1));
                    }

                    //              if(current_signal > 0)
                    //              {price.add(mid.get(mid.size()-1) - sprd );}
                    //              else
                    //              {price.add(mid.get(mid.size()-1) + sprd );}

                } else {
                    price.add(mid.get(mid.size() - 1));
                }

                D = new Double(tokens[4]);
                if (ib_data && ib_data_hash.containsKey(tokens[0])) {
                    live_series.add(log(mid.get(mid.size() - 1)) - log(mid.get(mid.size() - 2)));
                } else {
                    live_series.add(D);
                }

                //           if(ib_data && ib_data_hash.containsKey(tokens[0])) //use as is
                //           {lo_price.add(new Double(tokens[7])); hi_price.add(new Double(tokens[8]));}
                //           else
                //           {lo_price.add((new Double(tokens[7]))); hi_price.add((new Double(tokens[8])));}
                lo_price.add((new Double(tokens[2])));
                hi_price.add((new Double(tokens[3])));

                //---- start the account ------
                if (account.size() == 0) {
                    account.add(date_stamp + " " + 0);
                    dailyoutret.add(0.0);
                }

                String[] hours = time.split("[:]+");
                cur_hour = (new Integer(hours[0])).intValue();
                (new Integer(hours[1])).intValue();

                trading_closed = false;

                //if currently not in a transaction and between the hours of midnight and start-hour, then no new 
                //positions will be opened

                if (asian_close) //only closed if most recent transaction was closed artificially through SL or TP after end hours
                {
                    if ((in_transaction == 0 && out_transaction == 0) && cur_hour >= start_hour
                            && cur_hour <= end_hour) {
                        trading_closed = true;
                    } // if(printall) System.out.println(cur_hour + " " + start_hour);}
                } else {
                    if (cur_hour >= start_hour && cur_hour <= end_hour) {
                        trading_closed = true;
                    } // if(printall) {System.out.println(cur_hour + " " + start_hour);}}          
                }

                if (inverse_hours) {
                    trading_closed = !trading_closed;
                }

                its_closing_time = (weekend.dayOfWeek().getAsText().equals("Friday")
                        && date_stamp.indexOf("17:00:00") != -1);

                made_trade = false;
                if (daily_returns.size() >= n_obs && trade_times.contains(time)) //a new day begineth
                {

                    computed = true;
                    trading_hours = true;

                    tseries = new double[n_rep * n_obs];

                    for (i = 0; i < n_obs; i++) {
                        tseries[n_obs - 1 - i] = daily_returns.get(daily_returns.size() - 1 - i);
                        tseries[n_obs + n_obs - 1 - i] = daily_returns.get(daily_returns.size() - 1 - i);

                        if (n_rep > 2 && exp_series_1.size() > 0) {
                            tseries[n_obs * 2 + n_obs - 1 - i] = exp_series_1.get(exp_series_1.size() - 1 - i);
                        }
                        if (n_rep > 3 && exp_series_2.size() > 0) {
                            tseries[n_obs * 3 + n_obs - 1 - i] = exp_series_2.get(exp_series_2.size() - 1 - i);
                        }
                    }

                    mdfa.set_tseries(tseries, n_obs, n_rep);

                    //if(day_count == 0)  //recompute filter coefficients
                    if (day_count == 0 || weekend.dayOfWeek().getAsText().equals("Sunday")
                            && date_stamp.indexOf("18:00:00") != -1) {

                        if (printall)
                            System.out.println("Recomputing filter...");
                        mdfa.computeFilterGeneral(true, print_filter);
                        b_coeffs = new double[(n_rep - 1) * L]; //System.out.println(b_coeffs.length + " " + L + n_rep); 
                        for (l = 0; l < L; l++) {

                            for (i = 0; i < n_rep - 1; i++) {
                                b_coeffs[L * i + l] = mdfa.b[L * (i + 1) + l];
                            } // System.out.println(b_coeffs[l]);}               
                            //if(date_stamp.indexOf("2013-12-17") != -1) {System.out.println(b_coeffs[l]);}
                        }
                        if (printall)
                            System.out.println(date_stamp + " b_coeffs = " + b_coeffs[0] + " " + b_coeffs[1]
                                    + " " + b_coeffs[2]);

                        b_copy = new double[mdfa.b.length];
                        System.arraycopy(mdfa.b, 0, b_copy, 0, b_copy.length);
                        b0_coeff.println(b_coeffs[0]); // + ", " + b_coeffs[L] + ", " + b_coeffs[2*L]);

                    }

                    sum = 0.0;
                    for (j = 1; j < n_rep; j++) {
                        for (l = 0; l < L; l++) {
                            sum = sum + b_coeffs[L * (j - 1) + l] * tseries[N * j + n_obs - 1 - l];
                        }
                    }

                    prev_signal = current_signal;
                    current_signal = sum;
                    if (sig_inverse) {
                        current_signal = -current_signal;
                    }

                    //----final signal ---
                    daily_signal.add(current_signal);
                    daily_size = daily_price.size();
                    dailyReport.add(
                            "New day " + date_stamp + ", " + formatter3.format(daily_price.get(daily_size - 1))
                                    + ", " + formatter.format(tseries[n_obs - 1]) + ", " + current_signal);
                    //if(printall) System.out.println("New day "+date_stamp + ", " + formatter3.format(daily_price.get(daily_size-1)) + ", " + formatter.format(tseries[n_obs-1]) + ", " + current_signal);

                    //--compute binary trading rule ---

                    //if(printall) System.out.println("Current signal = " + current_signal + " Prev signal = " + prev_signal + ", trading_closed = " + trading_closed);
                    if (friday_closing && its_closing_time) {
                        if (printall)
                            System.out.println("\nIt's Friday at 5pm, time to close shop for week");
                        if (current_signal > 0 && in_transaction == 1) //in a long transaction
                        {
                            price_sold = daily_price.get(daily_size - 1);
                            profit = price_sold - price_bought;
                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            amount = getAmount(account.get(account.size() - 1));
                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));

                            made_trade = true;
                            dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                            log_ret = daily_price.get(daily_size - 1).doubleValue();

                            in_transaction = 0;

                            if (printall) {
                                if (profit > 0)
                                    System.out.println("Sold for a profit of " + profit);
                                else if (profit < 0)
                                    System.out.println("Sold for a loss of " + profit);
                                System.out.println("profit, price_bought, price_sold = " + profit + ", "
                                        + price_bought + ", " + price_sold);
                            }
                        } else if (current_signal < 0 && out_transaction == 1) //in a short transaction
                        {

                            price_sold = daily_price.get(daily_size - 1);
                            profit = price_borrowed - price_sold;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            amount = getAmount(account.get(account.size() - 1));
                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));

                            made_trade = true;
                            dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                            log_ret = daily_price.get(daily_size - 1).doubleValue();

                            out_transaction = 0;

                            if (printall)
                                System.out.println("profit, price_borrowed, price_sold = " + profit + ", "
                                        + price_borrowed + ", " + price_sold);
                        }

                    } else {
                        //if(binary_rule && !trading_closed)
                        if (binary_rule) {

                            //                  if(signal_profit)
                            //                  {
                            //                    //if(printall) System.out.println("Trading_closed = " + trading_closed);
                            //                    if((current_signal > 0 && in_transaction == 1) && (daily_price.get(daily_size-1) > last_price))
                            //                    {
                            //                    
                            //                    
                            //                      price_sold = daily_price.get(daily_size-1);
                            //                 profit = price_sold - price_bought;
                            //                 if(profit > 0) {succ_trades=succ_trades+1;}
                            //                 total_trades=total_trades+1; 
                            //     
                            //                 amount = getAmount(account.get(account.size()-1));
                            //                 account.add(date_stamp + " " + (amount + profit));   
                            //                 amount = getAmount(account.get(account.size()-1));
                            //                
                            //                 made_trade = true;
                            //                 dailyoutret.add(daily_price.get(daily_size-1) - log_ret);
                            //                      log_ret = daily_price.get(daily_size-1).doubleValue();
                            //     
                            //                 in_transaction = 0;
                            //               
                            //                 if(printall){
                            //                      if(profit>0) System.out.println("Sold for a profit of " + profit);
                            //                      else if(profit<0) System.out.println("Sold for a loss of " + profit);
                            //                       System.out.println("profit, price_bought, price_sold = " + profit + ", " + price_bought + ", " + price_sold);
                            //                      }
                            //                    
                            //                      if(!trading_closed)
                            //                      {price_bought = daily_price.get(daily_size-1);
                            //                 //in_transaction = 1; 
                            //                 out_transaction = 1;
                            //                 last_price = daily_price.get(daily_size-1); 
                            //                 if(printall) System.out.println("Entered long transaction at " + price_bought);
                            //                 } 
                            //                    }
                            //                    else if((current_signal < 0 && out_transaction == 1) && (daily_price.get(daily_size-1) < last_price))
                            //                    {
                            //                    
                            //                     price_sold = daily_price.get(daily_size-1);
                            //                     profit = price_borrowed - price_sold;
                            // 
                            //                if(profit > 0) {succ_trades=succ_trades+1;}
                            //                total_trades=total_trades+1; 
                            //      
                            //                amount = getAmount(account.get(account.size()-1));
                            //                account.add(date_stamp + " " + (amount + profit));   
                            //                amount = getAmount(account.get(account.size()-1));
                            //                     
                            //                     made_trade = true;
                            //                     dailyoutret.add(daily_price.get(daily_size-1) - log_ret);
                            //                     log_ret = daily_price.get(daily_size-1).doubleValue();
                            //                     
                            //                     
                            //                     out_transaction = 0;
                            //                     
                            //                     if(printall)
                            //                     {
                            //                     if(profit>0) System.out.println("Sold for a profit of " + profit);
                            //                     else if(profit<0) System.out.println("Sold for a loss of " + profit);
                            //                     }
                            //                     if(printall) System.out.println("profit, price_borrowed, price_sold = " + profit + ", " + price_borrowed + ", " + price_sold);                   
                            //                    
                            //                     if(!trading_closed)
                            //                     {
                            //                     price_borrowed = daily_price.get(daily_size-1);
                            //                     //out_transaction = 1;    
                            //                     in_transaction = 1;
                            //                     if(printall)System.out.println("Entered short transaction at " + price_borrowed);
                            //                     }
                            //                    }
                            //                  
                            //                  }

                            made_trade = false;
                            if (current_signal > 0 && prev_signal <= 0) //new point positive, we see momentum, buy
                            {

                                //last_price = daily_price.get(daily_size-1); 
                                last_price = ask.get(ask.size() - 1);

                                if (short_sell && out_transaction == 1) //in a short-sell transaction, buy
                                {
                                    //price_sold = daily_price.get(daily_size-1);
                                    price_sold = ask.get(ask.size() - 1);
                                    profit = price_borrowed - price_sold;

                                    if (profit > 0) {
                                        succ_trades = succ_trades + 1;
                                    }
                                    total_trades = total_trades + 1;

                                    amount = getAmount(account.get(account.size() - 1));
                                    account.add(date_stamp + " " + (amount + profit));
                                    amount = getAmount(account.get(account.size() - 1));

                                    made_trade = true;
                                    dailyoutret.add(ask.get(ask.size() - 1) - log_ret);
                                    log_ret = ask.get(ask.size() - 1).doubleValue();

                                    out_transaction = 0;

                                    if (printall) {
                                        if (profit > 0)
                                            System.out.println("Sold for a profit of " + profit);
                                        else if (profit < 0)
                                            System.out.println("Sold for a loss of " + profit);

                                        System.out.println("profit, price_borrowed, price_sold = " + profit
                                                + ", " + price_borrowed + ", " + price_sold);
                                    }
                                }

                                if ((long_buy && in_transaction == 0) && !trading_closed) {
                                    price_bought = ask.get(ask.size() - 1);
                                    in_transaction = 1;

                                    if (printall)
                                        System.out.println("Entered long transaction at " + price_bought);
                                }

                            } else if (current_signal < 0 && prev_signal >= 0) //if in transaction and signal goes below, sell
                            {
                                //last_price = daily_price.get(daily_size-1); 
                                last_price = bid.get(bid.size() - 1);

                                if (long_buy && in_transaction == 1) {
                                    price_sold = bid.get(bid.size() - 1);
                                    profit = price_sold - price_bought;
                                    if (profit > 0) {
                                        succ_trades = succ_trades + 1;
                                    }
                                    total_trades = total_trades + 1;

                                    amount = getAmount(account.get(account.size() - 1));
                                    account.add(date_stamp + " " + (amount + profit));
                                    amount = getAmount(account.get(account.size() - 1));

                                    made_trade = true;
                                    dailyoutret.add(bid.get(bid.size() - 1) - log_ret);
                                    log_ret = bid.get(bid.size() - 1).doubleValue();

                                    in_transaction = 0;

                                    if (printall) {
                                        if (profit > 0)
                                            System.out.println("Sold for a profit of " + profit);
                                        else if (profit < 0)
                                            System.out.println("Sold for a loss of " + profit);
                                        System.out.println("profit, price_bought, price_sold = " + profit + ", "
                                                + price_bought + ", " + price_sold);
                                    }

                                }

                                if ((short_sell && out_transaction == 0) && !trading_closed) {
                                    price_borrowed = bid.get(bid.size() - 1);
                                    out_transaction = 1;

                                    if (printall)
                                        System.out.println("Entered short transaction at " + price_borrowed);

                                }
                            }
                        }

                        if (signal_strength_rule
                                && ((in_transaction == 0 && out_transaction == 0) && !trading_closed)) {

                            if (current_signal > 0) //new point positive, we see momentum, buy
                            {
                                //last_price = daily_price.get(daily_size-1); 
                                last_price = ask.get(ask.size() - 1);

                                if (short_sell && out_transaction == 1) //in a short-sell transaction, sell
                                {
                                    price_sold = ask.get(ask.size() - 1);
                                    profit = price_borrowed - price_sold;

                                    if (profit > 0) {
                                        succ_trades = succ_trades + 1;
                                    }
                                    total_trades = total_trades + 1;

                                    amount = getAmount(account.get(account.size() - 1));
                                    account.add(date_stamp + " " + (amount + profit));
                                    amount = getAmount(account.get(account.size() - 1));

                                    made_trade = true;
                                    dailyoutret.add(ask.get(ask.size() - 1) - log_ret);
                                    log_ret = ask.get(ask.size() - 1).doubleValue();

                                    out_transaction = 0;

                                    if (printall)
                                        System.out.println("profit, price_borrowed, price_sold = " + profit
                                                + ", " + price_borrowed + ", " + price_sold);
                                }

                                if (long_buy && in_transaction == 0) {
                                    price_bought = ask.get(ask.size() - 1);
                                    in_transaction = 1;

                                    if (printall)
                                        System.out.println("Entered long transaction at " + price_bought);
                                }

                            } else if (current_signal < 0) //if in transaction and signal goes below, sell
                            {

                                last_price = bid.get(bid.size() - 1);

                                if (long_buy && in_transaction == 1) {
                                    price_sold = bid.get(bid.size() - 1);
                                    profit = price_sold - price_bought;
                                    if (profit > 0) {
                                        succ_trades = succ_trades + 1;
                                    }
                                    total_trades = total_trades + 1;

                                    amount = getAmount(account.get(account.size() - 1));
                                    account.add(date_stamp + " " + (amount + profit));
                                    amount = getAmount(account.get(account.size() - 1));

                                    made_trade = true;
                                    dailyoutret.add(bid.get(bid.size() - 1) - log_ret);
                                    log_ret = bid.get(bid.size() - 1).doubleValue();

                                    in_transaction = 0;

                                    if (printall)
                                        System.out.println("Bought for a profit of " + profit);
                                    if (printall)
                                        System.out.println("profit, price_bought, price_sold = " + profit + ", "
                                                + price_bought + ", " + price_sold);

                                }

                                if (short_sell && out_transaction == 0) {
                                    price_borrowed = bid.get(bid.size() - 1);
                                    out_transaction = 1;

                                    if (printall)
                                        System.out.println("Entered short transaction at " + price_borrowed);
                                }
                            }
                        }

                        if (!made_trade) {
                            account.add(date_stamp + " " + amount);
                            dailyoutret.add(price.get(price.size() - 1) - log_ret);
                            log_ret = price.get(price.size() - 1);
                        }

                        day_count++;

                        //if(recompute_day == day_count) {day_count=0;}
                        //if(printall) System.out.println(""+date_stamp + ", " + formatter3.format(price.get(price.size()-1)) + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", " + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl));             
                        allsignal.add(date_stamp + " " + current_signal);
                    }

                }
                if (trading_hours)// && !trading_closed)// && cur_min != 30)
                {

                    //              if(cur_pnl != 0 && date_stamp.indexOf("22:30:00") != -1) //close out position
                    //              {
                    //                if(in_transaction == 1)
                    //                {
                    //                
                    //                  //System.out.println("Ending trading for next startup");
                    // 
                    //                  
                    //                  price_sold = price.get(price.size()-1);
                    //             profit = price_sold - price_bought;                 
                    //                  
                    //                  if(profit > 0) {succ_trades=succ_trades+1;} 
                    //                  total_trades=total_trades+1;
                    //                  
                    //                  count = account.size()-1;
                    //             amount = getAmount(account.get(count));
                    // 
                    //             account.add(date_stamp + " " + (amount + profit));   
                    //             amount = getAmount(account.get(account.size()-1));
                    //                  
                    //                  
                    //                  dailyoutret.add(price.get(price.size()-1) - log_ret);
                    //                  log_ret = price.get(price.size()-1);                 
                    //  
                    //                  in_transaction = 0;
                    //             
                    //             //-- return stop loss to original setting --------
                    //             //System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit);
                    //                  stop_loss = global_stop_loss;
                    //                  red_zone = false;           
                    //                
                    //                }
                    //                else if(out_transaction == 1)
                    //                {
                    //                
                    //                  //System.out.println("Ending trading for next startup");
                    //                
                    //                
                    //                  price_sold = price.get(price.size()-1);
                    //                  profit = price_borrowed - price_sold;                 
                    //                  
                    //                  
                    // //                  price_sold = price_borrowed + stop_loss;
                    // //                  profit = -stop_loss;
                    //                  
                    //             if(profit > 0) {succ_trades=succ_trades+1;}
                    //             total_trades=total_trades+1; 
                    //      
                    //                  count = account.size()-1;
                    //             amount = getAmount(account.get(count));
                    //               
                    //             //account.add(date_stamp + " " + (amount - stop_loss));  
                    //             account.add(date_stamp + " " + (amount + profit));  
                    //             amount = getAmount(account.get(account.size()-1));
                    // 
                    //                  dailyoutret.add(price.get(price.size()-1) - log_ret);
                    //                  log_ret = price.get(price.size()-1);                
                    //             
                    //             
                    //             out_transaction = 0;
                    // 
                    //             //-- return stop loss to original setting --------
                    //                  stop_loss = global_stop_loss;
                    //                  red_zone = false;
                    //                
                    //                }
                    //              }

                    if (in_transaction == 1) //in a long transaction 
                    {

                        if (red_zone && bid.get(bid.size() - 1) > last_price) //check if new high price
                        {
                            last_price = bid.get(bid.size() - 1);
                        }

                        //cur_pnl = price.get(price.size()-1) - last_price;    
                        cur_pnl = bid.get(bid.size() - 1) - last_price;
                        lo_pnl = lo_price.get(lo_price.size() - 1) - last_price;
                        hi_pnl = hi_price.get(hi_price.size() - 1) - last_price;

                        if (cur_pnl < -stop_loss) {
                            if (printall)
                                System.out.println(
                                        "Stop-loss Activated since lo_pnl = " + cur_pnl + " < -" + stop_loss);
                            //System.out.println("Closing price at bar was " + price.get(price.size()-1) + " but lowest price in bar was " + lo_price.get(lo_price.size()-1));
                            //--------------sell---------- 

                            //price_sold = price.get(price.size()-1);

                            //price_sold = price.get(price.size()-1);
                            price_sold = bid.get(bid.size() - 1);
                            profit = price_sold - price_bought;

                            //                  price_sold = price_bought - stop_loss;
                            //                  profit = -stop_loss;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count));
                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));
                            //if(weekend.dayOfWeek().getAsText().equals("Sunday")) {sunday.add(date_stamp + " " + profit);}

                            dailyoutret.add(bid.get(bid.size() - 1) - log_ret);
                            log_ret = bid.get(bid.size() - 1);

                            in_transaction = 0;

                            //-- return stop loss to original setting --------
                            if (printall)
                                System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit);
                            stop_loss = global_stop_loss;
                            red_zone = false;
                        } else if (cur_pnl >= take_profit_thresh) {

                            if (printall)
                                System.out.println("Profit zone activated since cur_pnl = " + cur_pnl + " > "
                                        + take_profit_thresh);

                            //price_sold = price.get(price.size()-1);
                            price_sold = bid.get(bid.size() - 1);
                            profit = price_sold - price_bought;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count));
                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));
                            //if(weekend.dayOfWeek().getAsText().equals("Sunday")) {sunday.add(date_stamp + " " + profit);}
                            dailyoutret.add(price_sold - log_ret);
                            log_ret = price_sold;

                            in_transaction = 0;

                            //-- return stop loss to original setting --------
                            if (printall)
                                System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit);
                            stop_loss = global_stop_loss;

                            //                  stop_loss = profitable_stop;
                            //                  last_price = price.get(price.size()-1);
                            //                  red_zone = true;
                        }
                    } else if (out_transaction == 1) {

                        if (red_zone && ask.get(ask.size() - 1) < last_price) //check if new high price
                        {
                            last_price = ask.get(ask.size() - 1);
                        }

                        //cur_pnl =  last_price - price.get(price.size()-1);               
                        cur_pnl = last_price - ask.get(ask.size() - 1);
                        lo_pnl = last_price - hi_price.get(hi_price.size() - 1);
                        hi_pnl = last_price - lo_price.get(lo_price.size() - 1);

                        if (cur_pnl < -stop_loss) {
                            if (printall)
                                System.out.println(
                                        "Stop-loss Activated since lo_pnl = " + cur_pnl + " < -" + stop_loss);
                            //System.out.println("Closing price at bar was " + price.get(price.size()-1) + " but highest price in bar was " + hi_price.get(hi_price.size()-1));
                            //--------------sell---------- 

                            //price_sold = price.get(price.size()-1);
                            price_sold = ask.get(ask.size() - 1);
                            profit = price_borrowed - price_sold;

                            //                  price_sold = price_borrowed + stop_loss;
                            //                  profit = -stop_loss;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count));
                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));
                            //if(weekend.dayOfWeek().getAsText().equals("Sunday")) {sunday.add(date_stamp + " " + profit);}

                            dailyoutret.add(price_sold - log_ret);
                            log_ret = price_sold;

                            out_transaction = 0;

                            //-- return stop loss to original setting --------
                            stop_loss = global_stop_loss;
                            red_zone = false;

                            if (printall)
                                System.out
                                        .println("Bought at " + date_stamp + " for a profit/loss of " + profit);

                        } else if (cur_pnl >= take_profit_thresh) {

                            if (printall)
                                System.out.println("Profit zone activated since cur_pnl = " + cur_pnl + " > "
                                        + take_profit_thresh);
                            //                  stop_loss = profitable_stop;
                            //                  last_price = price.get(price.size()-1);
                            //                  red_zone = true;

                            //price_sold = price.get(price.size()-1);
                            price_sold = ask.get(ask.size() - 1);
                            profit = price_borrowed - price_sold;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count));
                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));

                            //if(weekend.dayOfWeek().getAsText().equals("Sunday")) {sunday.add(date_stamp + " " + profit);}

                            dailyoutret.add(price_sold - log_ret);
                            log_ret = price_sold;

                            out_transaction = 0;

                            //-- return stop loss to original setting --------
                            stop_loss = global_stop_loss;
                            red_zone = false;

                            if (printall)
                                System.out
                                        .println("Bought at " + date_stamp + " for a profit/loss of " + profit);

                        }
                    } else if (downtick_strategy) {

                        //strategy here is to buy/sell according to signal iff downtick has occurred

                        if (current_signal > 0 && (price_sold > price.get(price.size() - 1))) {

                            //let's buy some more 
                            if (printall)
                                System.out.println("Buying at " + date_stamp + " since last price sold = "
                                        + price_sold + " > " + price.get(price.size() - 1));
                            price_bought = price.get(price.size() - 1);
                            last_price = price.get(price.size() - 1);
                            in_transaction = 1;

                        } else if (current_signal < 0 && (price_sold < price.get(price.size() - 1))) {

                            //let's short some more
                            if (printall)
                                System.out.println(
                                        "Shorting at " + date_stamp + " since last price bought back at = "
                                                + price_sold + " < " + price.get(price.size() - 1));

                            price_borrowed = price.get(price.size() - 1);
                            last_price = price.get(price.size() - 1);
                            out_transaction = 1;

                            if (printall)
                                System.out.println("Entered short transaction at " + price_borrowed);
                        }
                        cur_pnl = 0;
                        lo_pnl = 0;
                        hi_pnl = 0;
                    } else {
                        cur_pnl = 0;
                        lo_pnl = 0;
                        hi_pnl = 0;
                    }

                    if (weekend.dayOfWeek().getAsText().equals("Friday")
                            && date_stamp.indexOf("17:00:00") != -1) {
                        if (printall)
                            System.out.println("End of week");

                    }

                    dailyReport.add("" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1))
                            + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", "
                            + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl));

                    if (printall) {
                        if (current_signal > 0) {
                            System.out.println(
                                    "" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1))
                                            + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", "
                                            + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl));
                        } else {
                            System.out.println(
                                    "" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1))
                                            + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", "
                                            + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl));
                        }
                    }

                    allsignal.add(date_stamp + " " + current_signal);
                }

            }
        }

        double mean_ntrades = 0;
        computed = true;
        dailyoutret.set(1, 0.0);
        double[] dreturns = new double[account.size()];
        dreturns[0] = 0;

        double mean = 0;
        double sd = 0;
        n_neg_ret = 0;
        n_pos_ret = 0;
        neg_ret_mean = 0;
        pos_ret_mean = 0;
        pnl = 0;

        int n_trades = 0;
        ArrayList<Integer> n_trades_day = new ArrayList<Integer>();
        double pret;
        ArrayList<Double> perf_rets = new ArrayList<Double>();
        ArrayList<String> perf_datestimes = new ArrayList<String>();

        for (i = 1; i < account.size(); i++) {
            out.println(account.get(i));
            dailyout.println(account.get(i) + " " + dailyoutret.get(i));
            //System.out.println(account.get(i));
            dreturns[i] = getAmount(account.get(i)) - getAmount(account.get(i - 1));

            if (jpy) {
                dreturns[i] = dreturns[i] * .01;
            }

            dates = account.get(i).split("[ ]+");
            if (!perf_dates.contains(dates[0])) //first date entry
            {

                if (perf_dates.size() != 0) {
                    perf_returns.add(pnl);
                    n_trades_day.add(n_trades);
                }

                perf_dates.add(dates[0]);
                if (dreturns[i] != 0) {
                    perf_datestimes.add(dates[0] + " " + dates[1]);
                    perf_rets.add(dreturns[i]);
                }
                pnl = dreturns[i];
                if (dreturns[i] != 0) {
                    n_trades = 1;
                } else {
                    n_trades = 0;
                }
            } else //already contains the date, so add on pnl
            {
                pnl = pnl + dreturns[i];
                //System.out.println(dreturns[i]);
                if (dreturns[i] != 0) {
                    n_trades++;
                    perf_datestimes.add(dates[0] + " " + dates[1]);
                    perf_rets.add(dreturns[i]);
                } // System.out.println(n_trades);}
            }

            if (dreturns[i] > 0) {
                n_pos_ret++;
                pos_ret_mean = pos_ret_mean + dreturns[i];
                mean = mean + dreturns[i];
            } else if (dreturns[i] < 0) {
                n_neg_ret++;
                neg_ret_mean = neg_ret_mean - dreturns[i];
                mean = mean + dreturns[i];
            }

        }
        perf_returns.add(pnl);
        n_trades_day.add(n_trades);

        for (i = 0; i < perf_dates.size(); i++) {
            if (printall)
                System.out.println(perf_dates.get(i) + " " + perf_returns.get(i) + " " + n_trades_day.get(i));
            perform.println(perf_dates.get(i) + " " + perf_returns.get(i));
            date_returns.add(perf_dates.get(i) + " " + perf_returns.get(i));
            mean_ntrades = mean_ntrades + n_trades_day.get(i);
        }
        mean_ntrades = mean_ntrades / n_trades_day.size();

        dates_price = new String[n_obs];

        for (i = 1; i < n_obs; i++) {

            tokens = allsignal.get(allsignal.size() - n_obs + i).split("[ ]+");

            if (perf_datestimes.contains(latestDates.get(latestDates.size() - n_obs + i))) {
                pret = perf_rets.get(perf_datestimes.indexOf(latestDates.get(latestDates.size() - n_obs + i)));
                System.out.println(latestDates.get(latestDates.size() - n_obs + i) + " " + pret);
            } else {
                pret = 0;
            }

            //System.out.println(tokens[0] + " " + latestDates.get(latestDates.size() - n_obs + i - 1));

            //         if(tokens[0].equals(latestDates.get(latestDates.size() - n_obs + i - 1)))
            //         {
            dates_price[i] = new String(latestDates.get(latestDates.size() - n_obs + i) + " "
                    + (mid.get(mid.size() - n_obs + i) - mid.get(mid.size() - n_obs + i - 1)) + " "
                    + mid.get(mid.size() - n_obs + i) + " " + tokens[2] + " " + pret);
            //        }
        }
        dates_price[0] = dates_price[1];

        mean = mean / (n_pos_ret + n_neg_ret);

        System.out.println("ROI = " + account.get(account.size() - 1));
        //--- compute stats---------------
        double risk = neg_ret_mean / (double) n_neg_ret;
        System.out.println("neg_ret_mean = " + (-neg_ret_mean) + ", " + n_neg_ret);

        double reward = pos_ret_mean / (double) n_pos_ret;
        System.out.println("pos_ret_mean = " + pos_ret_mean + ", " + n_pos_ret);

        double win_ratio = (double) (n_pos_ret) / (n_pos_ret + n_neg_ret);

        kellyPerc = win_ratio - (1.0 - win_ratio) * (risk / reward);
        ulcer_index = ulcerIndex(dreturns);

        System.out.println("win ratio = " + win_ratio + ", risk = " + risk + ", reward = " + reward);
        System.out.println("kelly and ulcer = " + kellyPerc + " " + ulcer_index);

        for (i = 0; i < dreturns.length; i++) {
            sd = sd + (dreturns[i] - mean) * (dreturns[i] - mean) / ((double) dreturns.length);
        }

        standard_deviation = Math.sqrt(sd);

        sharpeRatio = Math.sqrt(250) * mean / standard_deviation;
        maxdraw = computeDrawdown(dreturns);
        rank_coeff = segmentRankCorrelation(30, dreturns);

        System.out.println("MeanRet = " + mean + ", Sharpe = " + sharpeRatio + ", MaxDD = " + maxdraw
                + ", Rank = " + rank_coeff + ", avg_n_trades = " + mean_ntrades);

        out.close();
        dailyout.close();
        perform.close();
        b0_coeff.close();

    } catch (ParseException fe) {
        System.out.println("ParseException");
    } catch (NullPointerException fe) {
        System.out.println("Null pointer");
    } catch (IllegalArgumentException fe) {
        System.out.println("IllegalArgument");
    } catch (FileNotFoundException fe) {
        System.out.println("File not found..." + fe);
    } catch (IOException ioe) {
        System.out.println("IO procedure faulty..." + ioe);
    }

    n_files++;

    return computed;

}

From source file:ch.imetrica.mdfaTradingStrategies.MDFAStrategyEvolution.java

public boolean startStrategyDailyIntradayOneHourBidAsk_MR(StrategyParameters strat) {

    int j, i, l, N, file_count;
    double sum = 0;
    Double D;/*  w w w.j a  va2  s.  co  m*/
    String ddelims = "[-]";
    boolean computed = false;
    boolean print_filter = false;
    boolean made_trade = true;
    String date_stamp, strline;
    int daily_size;
    double profit, price_borrowed, price_sold, price_bought;
    double current_price, prev_price;
    double last_price, cur_pnl, stop_loss, lo_pnl, hi_pnl;
    double log_ret = 0;
    signal = new double[trade_obs];
    xt = new double[trade_obs];
    lag_signals = new double[trade_obs];
    prix = new double[trade_obs];
    lo_prix = new double[trade_obs];
    hi_prix = new double[trade_obs];
    total_succ = 0;
    total = 0;
    log_price = 0;
    N = n_obs;
    avg_vol = 0.0;
    b_avg = new double[L * n_rep];
    count = 0;
    trade_succ_ratio = 0;
    double amount = 0;
    double prev_signal;
    reg_trading_hours = false;
    String[] intdates;
    //make sure arraylists empty
    ArrayList<String> perf_dates = new ArrayList<String>();
    ArrayList<Double> perf_returns = new ArrayList<Double>();
    double pnl;
    String[] dates;
    boolean inverse_hours = false;
    String time;
    ArrayList<String> account = new ArrayList<String>();
    ArrayList<String> latestDates = new ArrayList<String>();
    last_trades = new ArrayList<Integer>();
    final_trades = new ArrayList<Double>();
    dailyoutret = new ArrayList<Double>();
    maxIntValue = new ArrayList<Double>();
    avg_volatility = new ArrayList<Double>();
    close_series = new ArrayList<Double>();
    highlow_series = new ArrayList<Double>();
    exp_series_1 = new ArrayList<Double>();
    exp_series_2 = new ArrayList<Double>();
    price = new ArrayList<Double>();
    lo_price = new ArrayList<Double>();
    hi_price = new ArrayList<Double>();
    mid = new ArrayList<Double>();
    bid = new ArrayList<Double>();
    ask = new ArrayList<Double>();
    dates_series = new ArrayList<String>();
    dailyReport = new ArrayList<String>();
    b0_trend = new ArrayList<Double>();
    vol_0 = new ArrayList<Double>();
    vol_1 = new ArrayList<Double>();
    sub_returns = new ArrayList<Double>();
    trade_days = new ArrayList<String>();
    returns = new ArrayList<Double>();
    longreturns = new ArrayList<Double>();
    shortreturns = new ArrayList<Double>();
    dropdowns = new ArrayList<Double>();
    success = new ArrayList<Double>();
    dates_low_high = new ArrayList<String>();
    crits = new ArrayList<String>();
    svm = new ArrayList<String>();
    filters = new ArrayList<Filter>();
    date_returns = new ArrayList<String>();

    live_series = new ArrayList<Double>(); //the data to be applied out of sample
    ib_data_hash = new ibHash();

    fridayROI = 0;
    fridayROI_pos = 0;
    fridays = 0;
    int end_hour;
    lookback_returns = new ArrayList<Double>();
    num_pos_returns = 0;
    deg_0 = new ArrayList<Double>();
    deg_1 = new ArrayList<Double>();
    crit_0 = new ArrayList<Double>();
    crit_1 = new ArrayList<Double>();
    full_returns_array = new ArrayList<double[]>();
    morning_returns = new ArrayList<double[]>();

    boolean waiting_meanrev_down = false;
    boolean waiting_meanrev_up = false;
    double mean_rev_amnt = .0005;

    morning_buy = true; //enter transaction at morning open
    morning_optimize = false; //optimize in the morning trading hours
    num_full_positive_returns = 0;
    //--- Now get historical interp values ------
    //uploadInterpParams("max_int.dat"); 
    //-------------------------------------------
    forex24 = true;
    ret_dist = new double[trade_obs];
    pos_ret_dist = new int[trade_obs];
    neg_ret_dist = new int[trade_obs];
    neg_trades_started = new int[trade_obs];
    pos_trades_started = new int[trade_obs];
    neg_trades_started_mean = new double[trade_obs];
    pos_trades_started_mean = new double[trade_obs];
    diff_account = new double[trade_obs];
    pos_ret_mean_time = new double[trade_obs];
    neg_ret_mean_time = new double[trade_obs];

    mdfaTrades = new ArrayList<MDFATrade>();
    fmt = DateTimeFormat.forPattern("y-MM-dd HH:mm:ss");
    formatter = new DecimalFormat("#0.000000");
    formatter3 = new DecimalFormat("#0.00000");
    formatter2 = new DecimalFormat("#0.00");
    histo_stat = new int[100];
    interp_vals = new ArrayList<Double>();
    max_ranks = new ArrayList<Double>();
    profit_baby = 0;
    //setForecastDFAParameters();
    bad_starts = 0;
    n_out_samp = 0;

    //take_profit = true;
    //take_profit_thresh = .0020;
    current_signal = 0;
    prev_price = 0;
    cur_pnl = 0;
    stop_loss = stop_loss_thresh;
    out_transaction = 0;
    in_transaction = 0;
    red_zone = false;
    global_stop_loss = stop_loss_thresh;
    profitable_stop = .0005;
    count = 0;
    short_sell = true;
    long_buy = true;
    day_count = 0;
    ArrayList<String> trade_times = new ArrayList<String>();
    ArrayList<String> fxcm_dates = new ArrayList<String>();

    lo_pnl = 0;
    hi_pnl = 0;
    price_borrowed = 0;
    price_sold = 0;
    price_bought = 0;
    last_price = 0;

    binary_rule = true;
    signal_strength_rule = true;
    downtick_strategy = false;
    signal_profit = false;
    friday_closing = false;
    //asian_close = true;

    //----- Fill up with times here -------------------------
    for (i = 0; i < 10; i++) {
        trade_times.add("0" + i + ":00:00");
    }
    for (i = 10; i < 24; i++) {
        trade_times.add(i + ":00:00");
    }
    //       trade_times.add("00:00:00");
    //       trade_times.add("06:00:00");
    //       trade_times.add("12:00:00");
    //       trade_times.add("18:00:00");     

    String[] hourToks = startingTime.split("[:]+");
    start_hour = (new Integer(hourToks[0])).intValue();

    hourToks = endingTime.split("[:]+");
    end_hour = (new Integer(hourToks[0])).intValue();

    inverse_hours = false;
    if (start_hour > end_hour) //must switch hours here
    {
        int temphour = end_hour;

        inverse_hours = true;
        end_hour = start_hour;
        start_hour = temphour;
    }

    if (ib_data && ib_data_file != null) {
        try {

            fin = new FileInputStream(ib_data_file);
            din = new DataInputStream(fin);
            br = new BufferedReader(new InputStreamReader(din));

            while ((strline = br.readLine()) != null) {
                String[] sp = strline.split("[,]+");
                ib_data_hash.put(sp[0], new String(
                        sp[1] + " " + sp[2] + " " + sp[3] + " " + sp[4] + " " + sp[5] + " " + sp[6]));
                //System.out.println(sp[1] + " " + sp[2] + " " + sp[3] + " " + sp[4] + " " + sp[5]  + " " + sp[6]);
            }
        } catch (FileNotFoundException fe) {
            System.out.println("File not found..." + fe);
        } catch (IOException ioe) {
            System.out.println("IO procedure faulty..." + ioe);
        }
    }

    jpy = false;
    try {

        PrintWriter b0_coeff = new PrintWriter(new FileWriter("b0_coeff.dat"));
        PrintWriter perform = new PrintWriter(new FileWriter("intraday_performance_" + n_files + ".dat"));
        PrintWriter dailyout = new PrintWriter(new FileWriter("daily_nasdaq.dat"));
        PrintWriter out = new PrintWriter(new FileWriter("strategy_results.dat"));

        for (file_count = 0; file_count < 1; file_count++) {

            if (dataFiles[file_count].indexOf("JPY") != -1 && dataFiles[file_count].indexOf("NOKJPY") == -1) {
                //change_time_zone = true; System.out.println("Changed time zone to Tokyo");
                jpy = true;
                stop_loss_thresh = stop_loss_thresh * 100;
                take_profit_thresh = take_profit_thresh * 100;
                global_stop_loss = global_stop_loss * 100;
                stop_loss = stop_loss_thresh;
            } else if (futures_data) {
                //change_time_zone = true; System.out.println("Changed time zone to Tokyo");

                stop_loss_thresh = stop_loss_thresh * 10000;
                stop_loss = stop_loss_thresh;
                take_profit_thresh = take_profit_thresh * 10000;
                global_stop_loss = global_stop_loss * 10000;
            }

            ArrayList<String> dayData = new ArrayList<String>();
            setTimeStandards(new File(dataFiles[file_count]));
            System.out.println("opening " + dataFiles[file_count]);
            fin = new FileInputStream(dataFiles[file_count]);
            din = new DataInputStream(fin);
            br = new BufferedReader(new InputStreamReader(din));
            lookback_ready = false;
            spread = new PrintWriter(new FileWriter("spread_" + dataFiles[file_count] + ".dat"));
            //if(print_debug)System.out.println("Entering loop...");
            trading_hours = false;
            computed = false;

            //---- add the latest FXCM data -
            while ((strline = br.readLine()) != null) {
                dayData.add(strline);

                //add the date
                tokens = strline.split("[,]+");
                fxcm_dates.add(tokens[0]);

            }

            //----- now add the lates IB data ------------
            System.out.println("opening " + dataFiles[file_count] + " IB data");
            String[] fxpair = dataFiles[file_count].split("[.]+");

            if ((new File(fxpair[0] + ".IB.dat")).exists()) {
                fin = new FileInputStream(fxpair[0] + ".IB.dat");
                din = new DataInputStream(fin);
                br = new BufferedReader(new InputStreamReader(din));

                String lastFXCMdate = fxcm_dates.get(fxcm_dates.size() - 1);

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
                Date date1 = sdf.parse(lastFXCMdate);

                while ((strline = br.readLine()) != null) {
                    tokens = strline.split("[,]+");

                    Date date2 = sdf.parse(tokens[0]);

                    if (!fxcm_dates.contains(tokens[0]) && date1.before(date2)) {
                        System.out.println("New time and observation " + strline);
                        dayData.add(strline);
                    }
                }
            }

            for (int ts = 0; ts < dayData.size(); ts++) {
                strline = dayData.get(ts);

                //System.out.println(strline);
                tokens = strline.split(delims);
                n_toks = tokens.length; //System.out.println("Number of toks = "+n_toks);
                if (n_toks == 0) {
                    System.out.println("End of file");
                    break;
                }

                if (n_toks >= 6) {
                    bid_ask_data = true;
                } else {
                    bid_ask_data = false;
                }

                date_stamp = tokens[0];
                date_tokens = date_stamp.split(date_delims);
                intdates = date_tokens[0].split(ddelims);
                DateTime weekend = new DateTime((new Integer(intdates[0])).intValue(),
                        (new Integer(intdates[1])).intValue(), (new Integer(intdates[2])).intValue(), 14, 0);
                time = date_tokens[1];

                //insampStart is the time we collect daily data 

                //if(date_stamp.indexOf(insampStart) != -1)
                if (trade_times.contains(time)) {

                    //get bid/mid/ask data
                    if (ib_data && ib_data_hash.containsKey(tokens[0])) {
                        String[] hashed = ib_data_hash.get(tokens[0]).split("[ ]+");
                        for (i = 1; i < hashed.length; i++) {
                            tokens[i] = hashed[i];
                        } // System.out.print(tokens[i] + " ");} 
                    }

                    daily_price.add(log(new Double(tokens[1])));
                    current_price = (new Double(tokens[1])).doubleValue();

                    if (daily_price.size() == 1) {
                        daily_returns.add(new Double(0.0));
                        prev_price = current_price;
                    } else {
                        daily_returns.add(log(current_price) - log(prev_price));
                        prev_price = current_price;
                    }

                    daily_dates.add(date_stamp);

                }

                print_filter = false;
                latestDates.add(date_stamp);
                D = new Double(tokens[4]);
                close_series.add(D);
                if (ib_data && ib_data_hash.containsKey(tokens[0])) {

                    String[] hashed = ib_data_hash.get(tokens[0]).split("[ ]+");
                    //System.out.println("Contains " + tokens[0] + ", lengths = " + hashed.length + ", " + tokens.length);
                    for (i = 1; i < hashed.length; i++) {
                        tokens[i] = hashed[i];
                    } // System.out.print(tokens[i] + " ");} 
                    bid.add(log(new Double(tokens[2])));
                    ask.add(log(new Double(tokens[3])));
                    mid.add(log(new Double(tokens[1])));
                } else {
                    bid.add(log(new Double(tokens[2])));
                    ask.add(log(new Double(tokens[3])));
                    mid.add(log(new Double(tokens[1])));
                }

                D = new Double(tokens[1]);
                price.add(log(D));

                D = new Double(tokens[4]);
                if (ib_data && ib_data_hash.containsKey(tokens[0])) {
                    live_series.add(log(mid.get(mid.size() - 1)) - log(mid.get(mid.size() - 2)));
                } else {
                    live_series.add(D);
                }

                //           if(ib_data && ib_data_hash.containsKey(tokens[0])) //use as is
                //           {lo_price.add(new Double(tokens[7])); hi_price.add(new Double(tokens[8]));}
                //           else
                //           {lo_price.add((new Double(tokens[7]))); hi_price.add((new Double(tokens[8])));}
                lo_price.add((new Double(tokens[2])));
                hi_price.add((new Double(tokens[3])));

                //---- start the account ------
                if (account.size() == 0) {
                    account.add(date_stamp + " " + 0);
                    dailyoutret.add(0.0);
                }

                String[] hours = time.split("[:]+");
                cur_hour = (new Integer(hours[0])).intValue();
                (new Integer(hours[1])).intValue();

                trading_closed = false;

                //if currently not in a transaction and between the hours of midnight and start-hour, then no new 
                //positions will be opened

                if (asian_close) //only closed if most recent transaction was closed artificially through SL or TP after end hours
                {
                    if ((in_transaction == 0 && out_transaction == 0) && cur_hour >= start_hour
                            && cur_hour <= end_hour) {
                        trading_closed = true;
                    } // if(printall) System.out.println(cur_hour + " " + start_hour);}
                } else {
                    if (cur_hour >= start_hour && cur_hour <= end_hour) {
                        trading_closed = true;
                    } //if(printall) {System.out.println(cur_hour + " " + start_hour);}}          
                }

                if (inverse_hours) {
                    trading_closed = !trading_closed;
                }

                its_closing_time = (weekend.dayOfWeek().getAsText().equals("Friday")
                        && date_stamp.indexOf("17:00:00") != -1);

                made_trade = false;
                if (daily_returns.size() >= n_obs && trade_times.contains(time)) //a new day begineth
                {

                    computed = true;
                    trading_hours = true;

                    tseries = new double[n_rep * n_obs];

                    for (i = 0; i < n_obs; i++) {
                        tseries[n_obs - 1 - i] = daily_returns.get(daily_returns.size() - 1 - i);
                        tseries[n_obs + n_obs - 1 - i] = daily_returns.get(daily_returns.size() - 1 - i);

                        if (n_rep > 2 && exp_series_1.size() > 0) {
                            tseries[n_obs * 2 + n_obs - 1 - i] = exp_series_1.get(exp_series_1.size() - 1 - i);
                        }
                        if (n_rep > 3 && exp_series_2.size() > 0) {
                            tseries[n_obs * 3 + n_obs - 1 - i] = exp_series_2.get(exp_series_2.size() - 1 - i);
                        }
                    }

                    mdfa.set_tseries(tseries, n_obs, n_rep);

                    //if(day_count == 0)  //recompute filter coefficients
                    if (day_count == 0 || weekend.dayOfWeek().getAsText().equals("Sunday")
                            && date_stamp.indexOf("18:00:00") != -1) {

                        if (printall)
                            System.out.println("Recomputing filter...");
                        mdfa.computeFilterGeneral(true, print_filter);
                        b_coeffs = new double[(n_rep - 1) * L]; //System.out.println(b_coeffs.length + " " + L + n_rep); 
                        for (l = 0; l < L; l++) {

                            for (i = 0; i < n_rep - 1; i++) {
                                b_coeffs[L * i + l] = mdfa.b[L * (i + 1) + l];
                            } // System.out.println(b_coeffs[l]);}               
                            //if(date_stamp.indexOf("2013-12-17") != -1) {System.out.println(b_coeffs[l]);}
                        }
                        if (printall)
                            System.out.println(date_stamp + " b_coeffs = " + b_coeffs[0] + " " + b_coeffs[1]
                                    + " " + b_coeffs[2]);

                        b_copy = new double[mdfa.b.length];
                        System.arraycopy(mdfa.b, 0, b_copy, 0, b_copy.length);
                        b0_coeff.println(b_coeffs[0]); // + ", " + b_coeffs[L] + ", " + b_coeffs[2*L]);

                    }

                    sum = 0.0;
                    for (j = 1; j < n_rep; j++) {
                        for (l = 0; l < L; l++) {
                            sum = sum + b_coeffs[L * (j - 1) + l] * tseries[N * j + n_obs - 1 - l];
                        }
                    }

                    prev_signal = current_signal;
                    current_signal = sum;
                    if (sig_inverse) {
                        current_signal = -current_signal;
                    }

                    //----final signal ---
                    daily_signal.add(current_signal);
                    daily_size = daily_price.size();
                    dailyReport.add(
                            "New day " + date_stamp + ", " + formatter3.format(daily_price.get(daily_size - 1))
                                    + ", " + formatter.format(tseries[n_obs - 1]) + ", " + current_signal);
                    //if(printall) System.out.println("New day "+date_stamp + ", " + formatter3.format(daily_price.get(daily_size-1)) + ", " + formatter.format(tseries[n_obs-1]) + ", " + current_signal);

                    //--compute binary trading rule ---

                    //if(printall) System.out.println("Current signal = " + current_signal + " Prev signal = " + prev_signal + ", trading_closed = " + trading_closed);
                    if (friday_closing && its_closing_time) {
                        if (printall)
                            System.out.println("\nIt's Friday at 5pm, time to close shop for week");
                        if (current_signal > 0 && in_transaction == 1) //in a long transaction
                        {
                            price_sold = daily_price.get(daily_size - 1);
                            profit = price_sold - price_bought;
                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            amount = getAmount(account.get(account.size() - 1));
                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));

                            made_trade = true;
                            dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                            log_ret = daily_price.get(daily_size - 1).doubleValue();

                            in_transaction = 0;

                            if (printall) {
                                if (profit > 0)
                                    System.out.println("Sold for a profit of " + profit);
                                else if (profit < 0)
                                    System.out.println("Sold for a loss of " + profit);
                                System.out.println("profit, price_bought, price_sold = " + profit + ", "
                                        + price_bought + ", " + price_sold);
                            }
                        } else if (current_signal < 0 && out_transaction == 1) //in a short transaction
                        {

                            price_sold = daily_price.get(daily_size - 1);
                            profit = price_borrowed - price_sold;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            amount = getAmount(account.get(account.size() - 1));
                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));

                            made_trade = true;
                            dailyoutret.add(daily_price.get(daily_size - 1) - log_ret);
                            log_ret = daily_price.get(daily_size - 1).doubleValue();

                            out_transaction = 0;

                            if (printall)
                                System.out.println("profit, price_borrowed, price_sold = " + profit + ", "
                                        + price_borrowed + ", " + price_sold);
                        }

                    } else {
                        //if(binary_rule && !trading_closed)
                        if (binary_rule) {

                            made_trade = false;
                            if (current_signal > 0 && prev_signal <= 0) //new point positive, we see momentum, buy
                            {

                                waiting_meanrev_down = false;
                                waiting_meanrev_up = false;
                                //last_price = daily_price.get(daily_size-1); 
                                last_price = ask.get(ask.size() - 1);

                                if (short_sell && out_transaction == 1) //in a short-sell transaction, sell
                                {
                                    //price_sold = daily_price.get(daily_size-1);
                                    price_sold = ask.get(ask.size() - 1);
                                    profit = price_borrowed - price_sold;

                                    if (profit > 0) {
                                        succ_trades = succ_trades + 1;
                                    }
                                    total_trades = total_trades + 1;

                                    amount = getAmount(account.get(account.size() - 1));
                                    account.add(date_stamp + " " + (amount + profit));
                                    amount = getAmount(account.get(account.size() - 1));

                                    made_trade = true;
                                    dailyoutret.add(ask.get(ask.size() - 1) - log_ret);
                                    log_ret = ask.get(ask.size() - 1).doubleValue();

                                    out_transaction = 0;

                                    if (printall) {
                                        if (profit > 0)
                                            System.out.println("Sold for a profit of " + profit);
                                        else if (profit < 0)
                                            System.out.println("Sold for a loss of " + profit);

                                        System.out.println("profit, price_borrowed, price_sold = " + profit
                                                + ", " + price_borrowed + ", " + price_sold);
                                    }
                                }

                                waiting_meanrev_down = true;

                                //                   if((long_buy && in_transaction == 0) && !trading_closed)
                                //              {
                                //                    price_bought = ask.get(ask.size()-1);
                                //               in_transaction = 1; 
                                //               
                                //               if(printall) System.out.println("Entered long transaction at " + price_bought);
                                //              } 

                            } else if (current_signal < 0 && prev_signal >= 0) //if in transaction and signal goes below, sell
                            {
                                //last_price = daily_price.get(daily_size-1); 

                                waiting_meanrev_down = false;
                                waiting_meanrev_up = false;

                                last_price = bid.get(bid.size() - 1);

                                if (long_buy && in_transaction == 1) {
                                    price_sold = bid.get(bid.size() - 1);
                                    profit = price_sold - price_bought;
                                    if (profit > 0) {
                                        succ_trades = succ_trades + 1;
                                    }
                                    total_trades = total_trades + 1;

                                    amount = getAmount(account.get(account.size() - 1));
                                    account.add(date_stamp + " " + (amount + profit));
                                    amount = getAmount(account.get(account.size() - 1));

                                    made_trade = true;
                                    dailyoutret.add(bid.get(bid.size() - 1) - log_ret);
                                    log_ret = bid.get(bid.size() - 1).doubleValue();

                                    in_transaction = 0;

                                    if (printall) {
                                        if (profit > 0)
                                            System.out.println("Sold for a profit of " + profit);
                                        else if (profit < 0)
                                            System.out.println("Sold for a loss of " + profit);
                                        System.out.println("profit, price_bought, price_sold = " + profit + ", "
                                                + price_bought + ", " + price_sold);
                                    }

                                }

                                waiting_meanrev_up = true;
                                //              if((short_sell && out_transaction == 0) && !trading_closed)
                                //              {
                                //               price_borrowed = bid.get(bid.size()-1);
                                //                    out_transaction = 1;    
                                //                   
                                //                    if(printall) System.out.println("Entered short transaction at " + price_borrowed);
                                //                   
                                //              }
                            }
                        }

                        if (signal_strength_rule
                                && ((in_transaction == 0 && out_transaction == 0) && !trading_closed)) {

                            if (current_signal > 0) //new point positive, we see momentum, buy
                            {
                                //last_price = daily_price.get(daily_size-1); 
                                last_price = ask.get(ask.size() - 1);
                                waiting_meanrev_down = true;
                                //                   if(short_sell && out_transaction == 1) //in a short-sell transaction, sell
                                //                   { 
                                //                     price_sold = ask.get(ask.size()-1);
                                //                     profit = price_borrowed - price_sold;
                                // 
                                //                if(profit > 0) {succ_trades=succ_trades+1;}
                                //                total_trades=total_trades+1; 
                                //      
                                //                amount = getAmount(account.get(account.size()-1));
                                //                account.add(date_stamp + " " + (amount + profit));   
                                //                amount = getAmount(account.get(account.size()-1));
                                // 
                                //                made_trade = true;
                                //                     dailyoutret.add(ask.get(ask.size()-1) - log_ret);
                                //                     log_ret = ask.get(ask.size()-1).doubleValue();               
                                //                
                                //                     out_transaction = 0;
                                //                   
                                // 
                                //                      if(printall) System.out.println("profit, price_borrowed, price_sold = " + profit + ", " + price_borrowed + ", " + price_sold);
                                //                   }

                                // //                   if(long_buy && in_transaction == 0)
                                // //              {
                                // //                    price_bought = ask.get(ask.size()-1);
                                // //               in_transaction = 1; 
                                // //               
                                // //               if(printall) System.out.println("Entered long transaction at " + price_bought);
                                // //              } 

                            } else if (current_signal < 0) //if in transaction and signal goes below, sell
                            {

                                last_price = bid.get(bid.size() - 1);
                                waiting_meanrev_up = true;
                                //                   if(long_buy && in_transaction == 1)
                                //                   {
                                //               price_sold = bid.get(bid.size()-1);
                                //               profit = price_sold - price_bought;
                                //               if(profit > 0) {succ_trades=succ_trades+1;}
                                //               total_trades=total_trades+1; 
                                //     
                                //                amount = getAmount(account.get(account.size()-1));
                                //                account.add(date_stamp + " " + (amount + profit));   
                                //                amount = getAmount(account.get(account.size()-1));
                                // 
                                //                made_trade = true;
                                //                     dailyoutret.add(bid.get(bid.size()-1) - log_ret);
                                //                     log_ret = bid.get(bid.size()-1).doubleValue();               
                                //                
                                //               in_transaction = 0;
                                //               
                                //                    if(printall)System.out.println("Bought for a profit of " + profit);
                                //                    if(printall)System.out.println("profit, price_bought, price_sold = " + profit + ", " + price_bought + ", " + price_sold);              
                                //               
                                //               
                                //              }

                                //              if(short_sell && out_transaction == 0)
                                //              {
                                //               price_borrowed = bid.get(bid.size()-1);
                                //                    out_transaction = 1;    
                                //                   
                                //                    if(printall)System.out.println("Entered short transaction at " + price_borrowed);
                                //              }
                            }
                        }

                        if (!made_trade) {
                            account.add(date_stamp + " " + amount);
                            dailyoutret.add(price.get(price.size() - 1) - log_ret);
                            log_ret = price.get(price.size() - 1);
                        }

                        day_count++;

                        //if(recompute_day == day_count) {day_count=0;}
                        if (printall)
                            System.out.println(
                                    "" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1))
                                            + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", "
                                            + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl));
                        allsignal.add(date_stamp + " " + current_signal);
                    }

                }
                if (trading_hours)// && !trading_closed)// && cur_min != 30)
                {

                    if (cur_pnl != 0 && date_stamp.indexOf("22:30:00") != -1) //close out position
                    {
                        if (in_transaction == 1) {

                            //System.out.println("Ending trading for next startup");

                            price_sold = price.get(price.size() - 1);
                            profit = price_sold - price_bought;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count));

                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));

                            dailyoutret.add(price.get(price.size() - 1) - log_ret);
                            log_ret = price.get(price.size() - 1);

                            in_transaction = 0;

                            //-- return stop loss to original setting --------
                            //System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit);
                            stop_loss = global_stop_loss;
                            red_zone = false;

                        } else if (out_transaction == 1) {

                            //System.out.println("Ending trading for next startup");

                            price_sold = price.get(price.size() - 1);
                            profit = price_borrowed - price_sold;

                            //                  price_sold = price_borrowed + stop_loss;
                            //                  profit = -stop_loss;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count));

                            //account.add(date_stamp + " " + (amount - stop_loss));  
                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));

                            dailyoutret.add(price.get(price.size() - 1) - log_ret);
                            log_ret = price.get(price.size() - 1);

                            out_transaction = 0;

                            //-- return stop loss to original setting --------
                            stop_loss = global_stop_loss;
                            red_zone = false;

                            waiting_meanrev_up = false;
                            waiting_meanrev_down = false;

                        }
                    }

                    if (waiting_meanrev_up) //waiting for tick price to go up in order to sell the bid
                    {

                        if ((bid.get(bid.size() - 1) - last_price) > mean_rev_amnt) {

                            if ((short_sell && out_transaction == 0) && !trading_closed) {
                                price_borrowed = bid.get(bid.size() - 1);
                                out_transaction = 1;

                                last_price = price_borrowed;
                                if (printall)
                                    System.out.println("Waiting over Entered short transaction at "
                                            + price_borrowed + " after "
                                            + (bid.get(bid.size() - 1) - last_price) + " reversion");
                            }
                            waiting_meanrev_up = false;
                        }
                    } else if (waiting_meanrev_down) {
                        if ((last_price - ask.get(ask.size() - 1)) > mean_rev_amnt) {
                            if ((long_buy && in_transaction == 0) && !trading_closed) {
                                price_bought = ask.get(ask.size() - 1);
                                in_transaction = 1;

                                last_price = price_bought;
                                if (printall)
                                    System.out.println("Waiting over, Entered long transaction at "
                                            + price_bought + " after " + (last_price - ask.get(ask.size() - 1))
                                            + " reversion");
                            }
                            waiting_meanrev_down = false;
                        }
                    }

                    if (in_transaction == 1) //in a long transaction 
                    {

                        if (red_zone && bid.get(bid.size() - 1) > last_price) //check if new high price
                        {
                            last_price = bid.get(bid.size() - 1);
                        }

                        //cur_pnl = price.get(price.size()-1) - last_price;    
                        cur_pnl = bid.get(bid.size() - 1) - last_price;
                        lo_pnl = lo_price.get(lo_price.size() - 1) - last_price;
                        hi_pnl = hi_price.get(hi_price.size() - 1) - last_price;

                        if (cur_pnl < -stop_loss) {
                            if (printall)
                                System.out.println(
                                        "Stop-loss Activated since lo_pnl = " + cur_pnl + " < -" + stop_loss);
                            //System.out.println("Closing price at bar was " + price.get(price.size()-1) + " but lowest price in bar was " + lo_price.get(lo_price.size()-1));
                            //--------------sell---------- 

                            //price_sold = price.get(price.size()-1);

                            //price_sold = price.get(price.size()-1);
                            price_sold = bid.get(bid.size() - 1);
                            profit = price_sold - price_bought;

                            //                  price_sold = price_bought - stop_loss;
                            //                  profit = -stop_loss;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count));
                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));
                            //if(weekend.dayOfWeek().getAsText().equals("Sunday")) {sunday.add(date_stamp + " " + profit);}

                            dailyoutret.add(bid.get(bid.size() - 1) - log_ret);
                            log_ret = bid.get(bid.size() - 1);

                            in_transaction = 0;

                            //-- return stop loss to original setting --------
                            if (printall)
                                System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit);
                            stop_loss = global_stop_loss;
                            red_zone = false;
                        } else if (cur_pnl >= take_profit_thresh) {

                            if (printall)
                                System.out.println("Profit zone activated since cur_pnl = " + cur_pnl + " > "
                                        + take_profit_thresh);

                            //price_sold = price.get(price.size()-1);
                            price_sold = bid.get(bid.size() - 1);
                            profit = price_sold - price_bought;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count));
                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));
                            //if(weekend.dayOfWeek().getAsText().equals("Sunday")) {sunday.add(date_stamp + " " + profit);}
                            dailyoutret.add(price_sold - log_ret);
                            log_ret = price_sold;

                            in_transaction = 0;

                            //-- return stop loss to original setting --------
                            if (printall)
                                System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit);
                            stop_loss = global_stop_loss;

                            //                  stop_loss = profitable_stop;
                            //                  last_price = price.get(price.size()-1);
                            //                  red_zone = true;
                        }
                    } else if (out_transaction == 1) {

                        if (red_zone && ask.get(ask.size() - 1) < last_price) //check if new high price
                        {
                            last_price = ask.get(ask.size() - 1);
                        }

                        //cur_pnl =  last_price - price.get(price.size()-1);               
                        cur_pnl = last_price - ask.get(ask.size() - 1);
                        lo_pnl = last_price - hi_price.get(hi_price.size() - 1);
                        hi_pnl = last_price - lo_price.get(lo_price.size() - 1);

                        if (cur_pnl < -stop_loss) {
                            if (printall)
                                System.out.println(
                                        "Stop-loss Activated since lo_pnl = " + cur_pnl + " < -" + stop_loss);
                            //System.out.println("Closing price at bar was " + price.get(price.size()-1) + " but highest price in bar was " + hi_price.get(hi_price.size()-1));
                            //--------------sell---------- 

                            //price_sold = price.get(price.size()-1);
                            price_sold = ask.get(ask.size() - 1);
                            profit = price_borrowed - price_sold;

                            //                  price_sold = price_borrowed + stop_loss;
                            //                  profit = -stop_loss;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count));
                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));
                            //if(weekend.dayOfWeek().getAsText().equals("Sunday")) {sunday.add(date_stamp + " " + profit);}

                            dailyoutret.add(price_sold - log_ret);
                            log_ret = price_sold;

                            out_transaction = 0;

                            //-- return stop loss to original setting --------
                            stop_loss = global_stop_loss;
                            red_zone = false;

                            if (printall)
                                System.out
                                        .println("Bought at " + date_stamp + " for a profit/loss of " + profit);

                        } else if (cur_pnl >= take_profit_thresh) {

                            if (printall)
                                System.out.println("Profit zone activated since cur_pnl = " + cur_pnl + " > "
                                        + take_profit_thresh);
                            //                  stop_loss = profitable_stop;
                            //                  last_price = price.get(price.size()-1);
                            //                  red_zone = true;

                            //price_sold = price.get(price.size()-1);
                            price_sold = ask.get(ask.size() - 1);
                            profit = price_borrowed - price_sold;

                            if (profit > 0) {
                                succ_trades = succ_trades + 1;
                            }
                            total_trades = total_trades + 1;

                            count = account.size() - 1;
                            amount = getAmount(account.get(count));
                            account.add(date_stamp + " " + (amount + profit));
                            amount = getAmount(account.get(account.size() - 1));

                            //if(weekend.dayOfWeek().getAsText().equals("Sunday")) {sunday.add(date_stamp + " " + profit);}

                            dailyoutret.add(price_sold - log_ret);
                            log_ret = price_sold;

                            out_transaction = 0;

                            //-- return stop loss to original setting --------
                            stop_loss = global_stop_loss;
                            red_zone = false;

                            if (printall)
                                System.out
                                        .println("Bought at " + date_stamp + " for a profit/loss of " + profit);

                        }
                    } else if (downtick_strategy) {

                        //strategy here is to buy/sell according to signal iff downtick has occurred

                        if (current_signal > 0 && (price_sold > price.get(price.size() - 1))) {

                            //let's buy some more 
                            if (printall)
                                System.out.println("Buying at " + date_stamp + " since last price sold = "
                                        + price_sold + " > " + price.get(price.size() - 1));
                            price_bought = price.get(price.size() - 1);
                            last_price = price.get(price.size() - 1);
                            in_transaction = 1;

                        } else if (current_signal < 0 && (price_sold < price.get(price.size() - 1))) {

                            //let's short some more
                            if (printall)
                                System.out.println(
                                        "Shorting at " + date_stamp + " since last price bought back at = "
                                                + price_sold + " < " + price.get(price.size() - 1));

                            price_borrowed = price.get(price.size() - 1);
                            last_price = price.get(price.size() - 1);
                            out_transaction = 1;

                            if (printall)
                                System.out.println("Entered short transaction at " + price_borrowed);
                        }
                        cur_pnl = 0;
                        lo_pnl = 0;
                        hi_pnl = 0;
                    } else {
                        cur_pnl = 0;
                        lo_pnl = 0;
                        hi_pnl = 0;
                    }

                    if (weekend.dayOfWeek().getAsText().equals("Friday")
                            && date_stamp.indexOf("17:00:00") != -1) {
                        if (printall)
                            System.out.println("End of week");

                    }

                    dailyReport.add("" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1))
                            + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", "
                            + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl));

                    if (printall) {
                        if (current_signal > 0) {
                            System.out
                                    .println("" + date_stamp + ", " + formatter3.format(bid.get(bid.size() - 1))
                                            + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", "
                                            + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl));
                        } else {
                            System.out
                                    .println("" + date_stamp + ", " + formatter3.format(ask.get(ask.size() - 1))
                                            + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", "
                                            + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl));
                        }
                    }

                    allsignal.add(date_stamp + " " + current_signal);
                }

            }
        }

        double mean_ntrades = 0;
        computed = true;
        dailyoutret.set(1, 0.0);
        double[] dreturns = new double[account.size()];
        dreturns[0] = 0;

        double mean = 0;
        double sd = 0;
        n_neg_ret = 0;
        n_pos_ret = 0;
        neg_ret_mean = 0;
        pos_ret_mean = 0;
        pnl = 0;

        int n_trades = 0;
        ArrayList<Integer> n_trades_day = new ArrayList<Integer>();
        double pret;
        ArrayList<Double> perf_rets = new ArrayList<Double>();
        ArrayList<String> perf_datestimes = new ArrayList<String>();

        for (i = 1; i < account.size(); i++) {
            out.println(account.get(i));
            dailyout.println(account.get(i) + " " + dailyoutret.get(i));
            //System.out.println(account.get(i));
            dreturns[i] = getAmount(account.get(i)) - getAmount(account.get(i - 1));

            if (jpy) {
                dreturns[i] = dreturns[i] * .01;
            }

            dates = account.get(i).split("[ ]+");
            if (!perf_dates.contains(dates[0])) //first date entry
            {

                if (perf_dates.size() != 0) {
                    perf_returns.add(pnl);
                    n_trades_day.add(n_trades);
                }

                perf_dates.add(dates[0]);
                if (dreturns[i] != 0) {
                    perf_datestimes.add(dates[0] + " " + dates[1]);
                    perf_rets.add(dreturns[i]);
                }
                pnl = dreturns[i];
                if (dreturns[i] != 0) {
                    n_trades = 1;
                } else {
                    n_trades = 0;
                }
            } else //already contains the date, so add on pnl
            {
                pnl = pnl + dreturns[i];
                //System.out.println(dreturns[i]);
                if (dreturns[i] != 0) {
                    n_trades++;
                    perf_datestimes.add(dates[0] + " " + dates[1]);
                    perf_rets.add(dreturns[i]);
                } // System.out.println(n_trades);}
            }

            if (dreturns[i] > 0) {
                n_pos_ret++;
                pos_ret_mean = pos_ret_mean + dreturns[i];
                mean = mean + dreturns[i];
            } else if (dreturns[i] < 0) {
                n_neg_ret++;
                neg_ret_mean = neg_ret_mean - dreturns[i];
                mean = mean + dreturns[i];
            }

        }
        perf_returns.add(pnl);
        n_trades_day.add(n_trades);

        for (i = 0; i < perf_dates.size(); i++) {
            if (printall)
                System.out.println(perf_dates.get(i) + " " + perf_returns.get(i) + " " + n_trades_day.get(i));
            perform.println(perf_dates.get(i) + " " + perf_returns.get(i));
            date_returns.add(perf_dates.get(i) + " " + perf_returns.get(i));
            mean_ntrades = mean_ntrades + n_trades_day.get(i);
        }
        mean_ntrades = mean_ntrades / n_trades_day.size();

        dates_price = new String[n_obs];

        for (i = 1; i < n_obs; i++) {

            tokens = allsignal.get(allsignal.size() - n_obs + i).split("[ ]+");

            if (perf_datestimes.contains(latestDates.get(latestDates.size() - n_obs + i))) {
                pret = perf_rets.get(perf_datestimes.indexOf(latestDates.get(latestDates.size() - n_obs + i)));
                System.out.println(latestDates.get(latestDates.size() - n_obs + i) + " " + pret);
            } else {
                pret = 0;
            }

            //System.out.println(tokens[0] + " " + latestDates.get(latestDates.size() - n_obs + i - 1));

            //         if(tokens[0].equals(latestDates.get(latestDates.size() - n_obs + i - 1)))
            //         {
            dates_price[i] = new String(latestDates.get(latestDates.size() - n_obs + i) + " "
                    + (mid.get(mid.size() - n_obs + i) - mid.get(mid.size() - n_obs + i - 1)) + " "
                    + mid.get(mid.size() - n_obs + i) + " " + tokens[2] + " " + pret);
            //        }
        }
        dates_price[0] = dates_price[1];

        mean = mean / (n_pos_ret + n_neg_ret);

        System.out.println("ROI = " + account.get(account.size() - 1));
        //--- compute stats---------------
        double risk = neg_ret_mean / (double) n_neg_ret;
        System.out.println("neg_ret_mean = " + (-neg_ret_mean) + ", " + n_neg_ret);

        double reward = pos_ret_mean / (double) n_pos_ret;
        System.out.println("pos_ret_mean = " + pos_ret_mean + ", " + n_pos_ret);

        double win_ratio = (double) (n_pos_ret) / (n_pos_ret + n_neg_ret);

        kellyPerc = win_ratio - (1.0 - win_ratio) * (risk / reward);
        ulcer_index = ulcerIndex(dreturns);

        System.out.println("win ratio = " + win_ratio + ", risk = " + risk + ", reward = " + reward);
        System.out.println("kelly and ulcer = " + kellyPerc + " " + ulcer_index);

        for (i = 0; i < dreturns.length; i++) {
            sd = sd + (dreturns[i] - mean) * (dreturns[i] - mean) / ((double) dreturns.length);
        }

        standard_deviation = Math.sqrt(sd);

        sharpeRatio = Math.sqrt(250) * mean / standard_deviation;
        maxdraw = computeDrawdown(dreturns);
        rank_coeff = segmentRankCorrelation(30, dreturns);

        System.out.println("MeanRet = " + mean + ", Sharpe = " + sharpeRatio + ", MaxDD = " + maxdraw
                + ", Rank = " + rank_coeff + ", avg_n_trades = " + mean_ntrades);

        out.close();
        dailyout.close();
        perform.close();
        b0_coeff.close();

    } catch (ParseException fe) {
        System.out.println("ParseException");
    } catch (NullPointerException fe) {
        System.out.println("Null pointer");
    } catch (IllegalArgumentException fe) {
        System.out.println("IllegalArgument");
    } catch (FileNotFoundException fe) {
        System.out.println("File not found..." + fe);
    } catch (IOException ioe) {
        System.out.println("IO procedure faulty..." + ioe);
    }

    n_files++;

    return computed;

}

From source file:ch.imetrica.mdfaTradingStrategies.MDFAStrategyEvolution.java

public void updateEndTimeAsia(String time, int start_hour, int close_hour, int liquide_hour, int liquide_min) {
    String date_delims = "[ ]+";
    String ddelims = "[-]";

    String[] date_tokens = time.split(date_delims);
    String[] intdates = date_tokens[0].split(ddelims);

    int signal_min = liquide_min;

    try {// w w  w  .  j  av  a  2 s  .c o  m
        startTimeDT = new DateTime((new Integer(intdates[0])).intValue(), (new Integer(intdates[1])).intValue(),
                (new Integer(intdates[2])).intValue(), start_hour, signal_min);
    } catch (IllegalInstantException ie) {
        startTimeDT = new DateTime((new Integer(intdates[0])).intValue(), (new Integer(intdates[1])).intValue(),
                (new Integer(intdates[2])).intValue(), start_hour + 1, signal_min);
    }

    endTimeDT = new DateTime((new Integer(intdates[0])).intValue(), (new Integer(intdates[1])).intValue(),
            (new Integer(intdates[2])).intValue(), close_hour, signal_min);
    liquidateTimeDT = new DateTime((new Integer(intdates[0])).intValue(), (new Integer(intdates[1])).intValue(),
            (new Integer(intdates[2])).intValue(), liquide_hour, liquide_min);

    if (liquide_hour > start_hour && liquidateTimeDT.dayOfWeek().getAsText().equals("Friday")) {
        liquidateTimeDT = liquidateTimeDT.plusDays(2);
    }

    //--- assume starting after 10am HST (16.00 EST or 4pm EST) produces new system setup for next day
    //System.out.println(cur_hour);

    if (close_hour < start_hour) {
        endTimeDT = endTimeDT.plusDays(1);

        if (endTimeDT.dayOfWeek().getAsText().equals("Saturday")) {
            endTimeDT = endTimeDT.plusDays(2);
        }
    } else if (endTimeDT.dayOfWeek().getAsText().equals("Friday")) {
        endTimeDT = endTimeDT.plusDays(2);
    }

    if (liquide_hour < start_hour) {
        liquidateTimeDT = liquidateTimeDT.plusDays(1);
    }

    if (liquidateTimeDT.dayOfWeek().getAsText().equals("Saturday")) {
        liquidateTimeDT = liquidateTimeDT.plusDays(2);
    }

    //System.out.println("Updated start/close at " + startTimeDT + " " + endTimeDT + " " + liquidateTimeDT);

}

From source file:ch.imetrica.mdfaTradingStrategies.MDFAStrategyEvolution.java

public void updateEndTime(String time, int start_hour, int close_hour, int liquide_hour, int liquide_min) {
    String date_delims = "[ ]+";
    String ddelims = "[-]";

    String[] date_tokens = time.split(date_delims);
    String[] intdates = date_tokens[0].split(ddelims);

    int signal_min = liquide_min;

    try {//from w w w.  j a  v  a 2  s  . c o  m
        startTimeDT = new DateTime((new Integer(intdates[0])).intValue(), (new Integer(intdates[1])).intValue(),
                (new Integer(intdates[2])).intValue(), start_hour, signal_min);
    } catch (IllegalArgumentException ie) {

        startTimeDT = new DateTime((new Integer(intdates[0])).intValue(), (new Integer(intdates[1])).intValue(),
                (new Integer(intdates[2])).intValue(), start_hour + 1, signal_min);
    }

    try {
        endTimeDT = new DateTime((new Integer(intdates[0])).intValue(), (new Integer(intdates[1])).intValue(),
                (new Integer(intdates[2])).intValue(), close_hour, signal_min);
    } catch (IllegalArgumentException ie) {
        endTimeDT = new DateTime((new Integer(intdates[0])).intValue(), (new Integer(intdates[1])).intValue(),
                (new Integer(intdates[2])).intValue(), close_hour + 1, signal_min);
    }

    liquidateTimeDT = new DateTime((new Integer(intdates[0])).intValue(), (new Integer(intdates[1])).intValue(),
            (new Integer(intdates[2])).intValue(), liquide_hour, liquide_min);
    //System.out.println("here2??");

    if (close_hour < start_hour) {
        endTimeDT = endTimeDT.plusDays(1);

        if (endTimeDT.dayOfWeek().getAsText().equals("Saturday")) {
            endTimeDT = endTimeDT.plusDays(2);
        }
    }

    //System.out.println("here3??");
    if (liquide_hour < start_hour) {
        liquidateTimeDT = liquidateTimeDT.plusDays(1);
    }

    if (liquidateTimeDT.dayOfWeek().getAsText().equals("Saturday")) {
        liquidateTimeDT = liquidateTimeDT.plusDays(2);
    }

    if (start_hour > 17 && startTimeDT.dayOfWeek().getAsText().equals("Friday")) {
        startTimeDT = startTimeDT.plusDays(2);
    }

    if (liquide_hour > 17 && liquidateTimeDT.dayOfWeek().getAsText().equals("Friday")) {
        liquidateTimeDT = liquidateTimeDT.plusDays(2);
    }

    if (close_hour > 17 && endTimeDT.dayOfWeek().getAsText().equals("Friday")) {
        endTimeDT = endTimeDT.plusDays(2);
    }

}

From source file:ch.imetrica.mdfaTradingStrategies.MDFAStrategyEvolution.java

public void setFilterFilesiMetrica(File file, String name, int sl, int sigFreq, int pnlFreq,
        double spreadFilter, int shift) {

    String[] tokens;/*from  ww w  .j  av  a2  s  .  c o  m*/
    String strline;
    int i, m;
    int n_files = 0;

    ArrayList<String> params = new ArrayList<String>();
    String header;
    try {

        FileInputStream fin = new FileInputStream(file);
        DataInputStream din = new DataInputStream(fin);
        BufferedReader br = new BufferedReader(new InputStreamReader(din));

        //----- first get the frequencies --------------
        //names = new ArrayList<String>();
        while ((strline = br.readLine()) != null) {

            tokens = strline.split("[ ]+");

            String[] liquidHour = tokens[0].split("[:]+");
            String[] endHour = tokens[1].split("[:]+");
            String[] startHour = tokens[3].split("[:]+");

            DateTime liquidTime = new DateTime(2015, 11, 17, (new Integer(liquidHour[0])).intValue(),
                    (new Integer(liquidHour[1])).intValue());
            DateTime endTime = new DateTime(2015, 11, 17, (new Integer(endHour[0])).intValue(),
                    (new Integer(liquidHour[1])).intValue());
            DateTime startTime = new DateTime(2015, 11, 17, (new Integer(startHour[0])).intValue(),
                    (new Integer(liquidHour[1])).intValue());

            liquidTime = liquidTime.minusHours(shift);
            endTime = endTime.minusHours(shift);
            startTime = startTime.minusHours(shift);

            int newLiquidhour = liquidTime.getHourOfDay();
            int newEndhour = endTime.getHourOfDay();
            int newStarthour = startTime.getHourOfDay();

            String newLiquid = "" + newLiquidhour + ":" + liquidHour[1] + ":00";
            String newEnd = "" + newEndhour + ":" + liquidHour[1];
            String newStart = "" + newStarthour + ":" + liquidHour[1];

            strline = newLiquid + " " + newEnd + " " + tokens[2] + " " + newStart + " ";

            for (i = 4; i < tokens.length; i++) {
                strline = strline + tokens[i] + " ";
            }

            params.add(strline);

            n_files++;
        }

        header = "String[] " + name + "_parameters = new String[" + n_files + "];";
        System.out.println(header);

        for (m = 0; m < n_files; m++) {
            System.out.println(name + "_parameters[" + m + "] = \"" + params.get(m) + pnlFreq + " " + sigFreq
                    + " " + spreadFilter + " " + name + "-signal-" + m + "\";");
        }

        din.close();
    } catch (FileNotFoundException fe) {
        System.out.println("File not found..." + fe);
    } catch (IOException ioe) {
        System.out.println("IO procedure faulty..." + ioe);
    }

}

From source file:ch.imetrica.mdfaTradingStrategies.MDFAStrategyEvolution.java

public boolean isSunday(String d) {

    String[] intdates = d.split("[-]+");
    DateTime weekend = new DateTime((new Integer(intdates[0])).intValue(),
            (new Integer(intdates[1])).intValue(), (new Integer(intdates[2])).intValue(), 16, 0);

    return (weekend.dayOfWeek().getAsText().equals("Sunday"));

}

From source file:ch.ledcom.log4jtools.utils.ParseUtils.java

License:Apache License

public static DateTime extractDateFromName(File file) {
    Matcher m = DATE_PATTERN.matcher(file.getName());
    if (!m.matches()) {
        return null;
    }/* w w w .  j  a v  a 2s  . c  o  m*/
    int year = Integer.parseInt(m.group(1));
    int month = Integer.parseInt(m.group(2));
    int day = Integer.parseInt(m.group(3));
    return new DateTime(year, month, day, 0, 0);
}