Example usage for org.joda.time DateTime dayOfWeek

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

Introduction

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

Prototype

public Property dayOfWeek() 

Source Link

Document

Get the day of week property which provides access to advanced functionality.

Usage

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;//  www .  ja va2  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[]>();

    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 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:com.chiorichan.plugin.lua.api.OSAPI.java

License:Mozilla Public License

@Override
void initialize() {
    // Push a couple of functions that override original Lua API functions or
    // that add new functionality to it.
    lua.getGlobal("os");

    // Custom os.clock() implementation returning the time the computer has
    // been actively running, instead of the native library...
    lua.pushJavaFunction(new JavaFunction() {
        @Override/*from   w ww  .ja  va 2  s  . c o m*/
        public int invoke(LuaState lua) {
            lua.pushNumber(System.currentTimeMillis());
            return 1;
        }
    });
    lua.setField(-2, "clock");

    lua.pushJavaFunction(new JavaFunction() {
        @Override
        public int invoke(LuaState lua) {
            String format = lua.getTop() > 0 && lua.isString(1) ? lua.toString(1) : "%d/%m/%y %H:%M:%S";
            long time = (long) (lua.getTop() > 1 && lua.isNumber(2) ? lua.toNumber(2) * 1000 / 60 / 60
                    : System.currentTimeMillis());
            DateTime dt = new DateTime(time);
            if (format == "*t") {
                lua.newTable(0, 8);
                lua.pushInteger(dt.year().get());
                lua.setField(-2, "year");
                lua.pushInteger(dt.monthOfYear().get());
                lua.setField(-2, "month");
                lua.pushInteger(dt.dayOfMonth().get());
                lua.setField(-2, "day");
                lua.pushInteger(dt.hourOfDay().get());
                lua.setField(-2, "hour");
                lua.pushInteger(dt.minuteOfHour().get());
                lua.setField(-2, "min");
                lua.pushInteger(dt.secondOfMinute().get());
                lua.setField(-2, "sec");
                lua.pushInteger(dt.dayOfWeek().get());
                lua.setField(-2, "wday");
                lua.pushInteger(dt.dayOfYear().get());
                lua.setField(-2, "yday");
            } else
                lua.pushString(dt.toString(DateTimeFormat.forPattern(format)));
            return 1;
        }
    });
    lua.setField(-2, "date");

    /*
     * // Date formatting function.
     * lua.pushScalaFunction(lua => {
     * val format =
     * if (lua.getTop > 0 && lua.isString(1)) lua.toString(1)
     * else "%d/%m/%y %H:%M:%S"
     * val time =
     * if (lua.getTop > 1 && lua.isNumber(2)) lua.toNumber(2) * 1000 / 60 / 60
     * else machine.worldTime + 5000
     * 
     * val dt = GameTimeFormatter.parse(time)
     * def fmt(format: String) {
     * if (format == "*t") {
     * lua.newTable(0, 8)
     * lua.pushInteger(dt.year)
     * lua.setField(-2, "year")
     * lua.pushInteger(dt.month)
     * lua.setField(-2, "month")
     * lua.pushInteger(dt.day)
     * lua.setField(-2, "day")
     * lua.pushInteger(dt.hour)
     * lua.setField(-2, "hour")
     * lua.pushInteger(dt.minute)
     * lua.setField(-2, "min")
     * lua.pushInteger(dt.second)
     * lua.setField(-2, "sec")
     * lua.pushInteger(dt.weekDay)
     * lua.setField(-2, "wday")
     * lua.pushInteger(dt.yearDay)
     * lua.setField(-2, "yday")
     * }
     * else {
     * lua.pushString(GameTimeFormatter.format(format, dt))
     * }
     * }
     * 
     * // Just ignore the allowed leading '!', Minecraft has no time zones...
     * if (format.startsWith("!"))
     * fmt(format.substring(1))
     * else
     * fmt(format)
     * 1
     * })
     * lua.setField(-2, "date")
     * 
     * // Return ingame time for os.time().
     * lua.pushScalaFunction(lua => {
     * if (lua.isNoneOrNil(1)) {
     * // Game time is in ticks, so that each day has 24000 ticks, meaning
     * // one hour is game time divided by one thousand. Also, Minecraft
     * // starts days at 6 o'clock, versus the 1 o'clock of timestamps so we
     * // add those five hours. Thus:
     * // timestamp = (time + 5000) * 60[kh] * 60[km] / 1000[s]
     * lua.pushNumber((machine.worldTime + 5000) * 60 * 60 / 1000)
     * }
     * else {
     * def getField(key: String, d: Int) = {
     * lua.getField(-1, key)
     * val res = lua.toIntegerX(-1)
     * lua.pop(1)
     * if (res == null)
     * if (d < 0) throw new Exception("field '" + key + "' missing in date table")
     * else d
     * else res: Int
     * }
     * 
     * lua.checkType(1, LuaType.TABLE)
     * lua.setTop(1)
     * 
     * val sec = getField("sec", 0)
     * val min = getField("min", 0)
     * val hour = getField("hour", 12)
     * val mday = getField("day", -1)
     * val mon = getField("month", -1)
     * val year = getField("year", -1)
     * 
     * GameTimeFormatter.mktime(year, mon, mday, hour, min, sec) match {
     * case Some(time) => lua.pushNumber(time)
     * case _ => lua.pushNil()
     * }
     * }
     * 1
     * })
     * lua.setField(-2, "time")
     */
    // Pop the os table.
    lua.pop(1);
}

From source file:com.clicktime.model.service.calendario.Day.java

public Day(DateTime dt) {
    this.day = dt;
    this.dayOfWeekShort = dt.dayOfWeek().getAsShortText();
}

From source file:com.esofthead.mycollab.module.project.view.bug.components.CreatedDateOrderComponent.java

License:Open Source License

@Override
public void insertBugs(List<SimpleBug> bugs) {
    for (SimpleBug bug : bugs) {
        if (bug.getCreatedtime() != null) {
            Date createdDate = bug.getCreatedtime();
            DateTime jodaTime = new DateTime(createdDate, DateTimeZone.UTC);
            DateTime monDay = jodaTime.dayOfWeek().withMinimumValue();
            if (createdDateAvailables.containsKey(monDay)) {
                GroupComponent groupComponent = createdDateAvailables.get(monDay);
                groupComponent.insertTask(bug);
            } else {
                GroupComponent groupComponent = new GroupComponent(monDay);
                createdDateAvailables.put(monDay, groupComponent);
                addComponent(groupComponent);
                groupComponent.insertTask(bug);
            }//w  w w  .j a va 2  s. c  o  m
        } else {
            if (unspecifiedTasks == null) {
                unspecifiedTasks = new GroupComponent();
                addComponent(unspecifiedTasks);
            }
            unspecifiedTasks.insertTask(bug);
        }
    }
}

From source file:com.esofthead.mycollab.module.project.view.bug.components.DueDateOrderComponent.java

License:Open Source License

@Override
public void insertBugs(List<SimpleBug> bugs) {
    for (SimpleBug bug : bugs) {
        if (bug.getDuedate() != null) {
            Date dueDate = bug.getDuedate();
            DateTime jodaTime = new DateTime(dueDate, DateTimeZone.UTC);
            DateTime monDay = jodaTime.dayOfWeek().withMinimumValue();
            if (dueDateAvailables.containsKey(monDay)) {
                GroupComponent groupComponent = dueDateAvailables.get(monDay);
                groupComponent.insertTask(bug);
            } else {
                GroupComponent groupComponent = new GroupComponent(monDay);
                dueDateAvailables.put(monDay, groupComponent);
                int index = dueDateAvailables.getKeyIndex(monDay);
                if (index > -1) {
                    addComponent(groupComponent, index);
                } else {
                    addComponent(groupComponent);
                }//from w w w. j  ava  2 s .c o  m
                groupComponent.insertTask(bug);
            }
        } else {
            if (unspecifiedTasks == null) {
                unspecifiedTasks = new GroupComponent();
                addComponent(unspecifiedTasks);
            }
            unspecifiedTasks.insertTask(bug);
        }
    }
}

From source file:com.esofthead.mycollab.module.project.view.bug.components.StartDateOrderComponent.java

License:Open Source License

@Override
public void insertBugs(List<SimpleBug> bugs) {
    for (SimpleBug bug : bugs) {
        if (bug.getCreatedtime() != null) {
            Date startDate = bug.getCreatedtime();
            DateTime jodaTime = new DateTime(startDate, DateTimeZone.UTC);
            DateTime monDay = jodaTime.dayOfWeek().withMinimumValue();
            if (startDateAvailables.containsKey(monDay)) {
                GroupComponent groupComponent = startDateAvailables.get(monDay);
                groupComponent.insertTask(bug);
            } else {
                GroupComponent groupComponent = new GroupComponent(monDay);
                startDateAvailables.put(monDay, groupComponent);
                addComponent(groupComponent);
                groupComponent.insertTask(bug);
            }//  www .j a  v  a 2 s .c o  m
        } else {
            if (unspecifiedTasks == null) {
                unspecifiedTasks = new GroupComponent();
                addComponent(unspecifiedTasks, 0);
            }
            unspecifiedTasks.insertTask(bug);
        }
    }
}

From source file:com.esofthead.mycollab.module.project.view.task.components.CreatedDateOrderComponent.java

License:Open Source License

@Override
public void insertTasks(List<SimpleTask> tasks) {
    for (SimpleTask task : tasks) {
        if (task.getCreatedtime() != null) {
            Date createdDate = task.getCreatedtime();
            DateTime jodaTime = new DateTime(createdDate, DateTimeZone.UTC);
            DateTime monDay = jodaTime.dayOfWeek().withMinimumValue();
            if (createdDateAvailables.containsKey(monDay)) {
                GroupComponent groupComponent = createdDateAvailables.get(monDay);
                groupComponent.insertTask(task);
            } else {
                GroupComponent groupComponent = new GroupComponent(monDay);
                createdDateAvailables.put(monDay, groupComponent);
                int index = createdDateAvailables.getKeyIndex(monDay);
                if (index > -1) {
                    addComponent(groupComponent, index);
                } else {
                    addComponent(groupComponent);
                }//www  . j  a  v a 2 s .c o  m

                groupComponent.insertTask(task);
            }
        } else {
            if (unspecifiedTasks == null) {
                unspecifiedTasks = new GroupComponent();
                addComponent(unspecifiedTasks, 0);
            }
            unspecifiedTasks.insertTask(task);
        }
    }
}

From source file:com.esofthead.mycollab.module.project.view.task.components.DueDateOrderComponent.java

License:Open Source License

@Override
public void insertTasks(List<SimpleTask> tasks) {
    for (SimpleTask task : tasks) {
        if (task.getDeadline() != null) {
            Date dueDate = task.getDeadline();
            DateTime jodaTime = new DateTime(dueDate, DateTimeZone.UTC);
            DateTime monDay = jodaTime.dayOfWeek().withMinimumValue();
            if (dueDateAvailables.containsKey(monDay)) {
                GroupComponent groupComponent = dueDateAvailables.get(monDay);
                groupComponent.insertTask(task);
            } else {
                GroupComponent groupComponent = new GroupComponent(monDay);
                dueDateAvailables.put(monDay, groupComponent);
                int index = dueDateAvailables.getKeyIndex(monDay);
                if (index > -1) {
                    addComponent(groupComponent, index);
                } else {
                    addComponent(groupComponent);
                }//from w  ww  .j a  va2s  . co  m

                groupComponent.insertTask(task);
            }
        } else {
            if (unspecifiedTasks == null) {
                unspecifiedTasks = new GroupComponent();
                addComponent(unspecifiedTasks);
            }
            unspecifiedTasks.insertTask(task);
        }
    }
}

From source file:com.esofthead.mycollab.module.project.view.task.components.StartDateOrderComponent.java

License:Open Source License

@Override
public void insertTasks(List<SimpleTask> tasks) {
    for (SimpleTask task : tasks) {
        if (task.getStartdate() != null) {
            Date startDate = task.getStartdate();
            DateTime jodaTime = new DateTime(startDate, DateTimeZone.UTC);
            DateTime monDay = jodaTime.dayOfWeek().withMinimumValue();
            if (startDateAvailables.containsKey(monDay)) {
                GroupComponent groupComponent = startDateAvailables.get(monDay);
                groupComponent.insertTask(task);
            } else {
                GroupComponent groupComponent = new GroupComponent(monDay);
                startDateAvailables.put(monDay, groupComponent);
                int index = startDateAvailables.getKeyIndex(monDay);
                if (index > -1) {
                    addComponent(groupComponent, index);
                } else {
                    addComponent(groupComponent);
                }/*from   w  ww.j av a  2 s  .  c  o m*/

                groupComponent.insertTask(task);
            }
        } else {
            if (unspecifiedTasks == null) {
                unspecifiedTasks = new GroupComponent();
                addComponent(unspecifiedTasks, 0);
            }
            unspecifiedTasks.insertTask(task);
        }
    }
}