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:backend.util.FileGroup.java

public static void main(String[] args) throws IOException {
    boolean Done = false;
    do {/*from w  w  w  .  j a v  a  2 s .  c o  m*/
        DateTimeFormatter templFMT = DateTimeFormat.forPattern("yyyy-MM-dd");
        DateTimeFormatter subDirFMT = DateTimeFormat.forPattern("yyMMdd");

        JFileChooser fChooser = new JFileChooser("D:\\CGKStudio\\log");
        fChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        //        fChooser.setFileFilter(new FileNameExtensionFilter("LOG File",""));
        if (fChooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION)
            return;

        File dir = fChooser.getSelectedFile();
        List<File> files = new LinkedList(Arrays.asList(dir.listFiles()));

        DateTime d = new DateTime(2014, 11, 20, 0, 0);
        DateTime now = DateTime.now();
        while (d.compareTo(now) <= 0) {
            String templ = templFMT.print(d);

            List<File> moved = new LinkedList();
            files.stream().filter((file) -> (file.getName().contains(templ))).forEach((file) -> {
                moved.add(file);
            });

            files.removeAll(moved);
            if (moved.size() > 0) {
                String subDir = dir.getAbsolutePath() + "\\" + subDirFMT.print(d);
                File subDirFile = new File(subDir);
                if (!subDirFile.exists())
                    Files.createDirectory(subDirFile.toPath());

                moved.stream().forEach((file) -> {
                    try {
                        File target = new File(subDir + "\\" + file.getName());
                        if (!target.exists()) {
                            Files.copy(new File(dir.getAbsolutePath() + "\\" + file.getName()).toPath(),
                                    target.toPath());
                        }
                    } catch (IOException ex) {
                        Logger.getLogger(FileGroup.class.getName()).log(Level.SEVERE, null, ex);
                    }
                });
            }

            d = d.plusDays(1);
        }

        int sel;
        sel = JOptionPane.showConfirmDialog(fChooser, "Do it again?", "Again", JOptionPane.YES_NO_OPTION);
        if (sel != JOptionPane.YES_OPTION)
            Done = true;
    } while (!Done);
}

From source file:ca.farrelltonsolar.classic.ModbusTask.java

License:Apache License

private void loadBoilerPlateInfo() {
    try {//from   w  ww  .  j a v  a 2s  . co m
        Register[] registers = modbusMaster.readMultipleRegisters(4100, 32);

        if (registers != null && registers.length == 32) {
            short reg1 = (short) registers[0].getValue();
            String model = String.format("Classic %d (rev %d)", reg1 & 0x00ff, reg1 >> 8);
            chargeControllerInfo.setModel(model);
            int buildYear = registers[1].getValue();
            int buildMonthDay = registers[2].getValue();
            DateTime buildDate = new DateTime(buildYear, (buildMonthDay >> 8), (buildMonthDay & 0x00ff), 0, 0);
            chargeControllerInfo.setBuildDate(DateTimeFormat.fullDate().print(buildDate));
            short reg6 = registers[5].toShort();
            short reg7 = registers[6].toShort();
            short reg8 = registers[7].toShort();
            String macAddress = String.format("%02x:%02x:%02x:%02x:%02x:%02x", reg8 >> 8, reg8 & 0x00ff,
                    reg7 >> 8, reg7 & 0x00ff, reg6 >> 8, reg6 & 0x00ff);
            chargeControllerInfo.setMacAddress(macAddress);
            float reg22 = (float) registers[21].getValue();
            chargeControllerInfo.setLastVOC(reg22 / 10);
        }
        registers = modbusMaster.readMultipleRegisters(4244, 2);
        if (registers != null && registers.length == 2) {
            short reg4245 = (short) registers[0].getValue();
            chargeControllerInfo.setNominalBatteryVoltage(reg4245);
        }
        registers = modbusMaster.readMultipleRegisters(16386, 4);
        if (registers != null && registers.length == 4) {
            short reg16387 = registers[0].toShort();
            short reg16388 = registers[1].toShort();
            short reg16389 = registers[2].toShort();
            short reg16390 = registers[3].toShort();
            chargeControllerInfo.setAppVersion(String.format("%d", (reg16388 << 16) + reg16387));
            chargeControllerInfo.setNetVersion(String.format("%d", (reg16390 << 16) + reg16389));
        }
    } catch (Exception e) {
        Log.w(getClass().getName(), "loadBoilerPlateInfo failed ex: %s", e);
    }
}

From source file:ca.phon.session.io.xml.v12.XMLSessionReader_v12.java

License:Open Source License

/**
 * Read session in from given xml {@link SessionType} object.
 * /*from   ww  w .j  a  v  a 2s  . c  om*/
 * @param sessionType
 * 
 * @return session
 */
private Session readSessionType(SessionType sessionType) {
    final SessionFactory factory = SessionFactory.newFactory();
    final Session retVal = factory.createSession();

    // copy header info
    retVal.setName(sessionType.getId());
    retVal.setCorpus(sessionType.getCorpus());

    final HeaderType headerData = sessionType.getHeader();
    if (headerData != null) {
        if (headerData.getMedia() != null && headerData.getMedia().length() > 0) {
            retVal.setMediaLocation(headerData.getMedia());
        }
        if (headerData.getDate() != null) {
            final XMLGregorianCalendar xmlDate = headerData.getDate();
            final DateTime dateTime = new DateTime(xmlDate.getYear(), xmlDate.getMonth(), xmlDate.getDay(), 12,
                    0);
            retVal.setDate(dateTime);
        }
        if (headerData.getLanguage().size() > 0) {
            String langs = "";
            for (String lang : headerData.getLanguage()) {
                langs += (langs.length() > 0 ? " " : "") + lang;
            }
            retVal.setLanguage(langs);
        }
    }

    // copy participant information
    final ParticipantsType participants = sessionType.getParticipants();
    if (participants != null) {
        for (ParticipantType pt : participants.getParticipant()) {
            final Participant p = copyParticipant(factory, pt, retVal.getDate());
            retVal.addParticipant(p);
        }
    }

    // copy transcriber information
    final TranscribersType transcribers = sessionType.getTranscribers();
    if (transcribers != null) {
        for (TranscriberType tt : transcribers.getTranscriber()) {
            final Transcriber t = copyTranscriber(factory, tt);
            retVal.addTranscriber(t);
        }
    }

    // copy tier information
    final UserTiersType userTiers = sessionType.getUserTiers();
    if (userTiers != null) {
        for (UserTierType utt : userTiers.getUserTier()) {
            final TierDescription td = copyTierDescription(factory, utt);
            retVal.addUserTier(td);
        }
    }

    final List<TierViewItem> tierOrder = new ArrayList<TierViewItem>();
    for (TvType tot : sessionType.getTierOrder().getTier()) {
        final TierViewItem toi = copyTierViewItem(factory, tot);
        tierOrder.add(toi);
    }
    retVal.setTierView(tierOrder);

    // copy transcript data
    final List<Comment> recordComments = new ArrayList<Comment>();
    boolean foundFirstRecord = false;
    if (sessionType.getTranscript() != null) {
        for (Object uOrComment : sessionType.getTranscript().getUOrComment()) {
            if (uOrComment instanceof CommentType) {
                final CommentType ct = (CommentType) uOrComment;
                final Comment comment = copyComment(factory, ct);
                recordComments.add(comment);
            } else {
                if (!foundFirstRecord && recordComments.size() > 0) {
                    // add record comments to session metadata
                    for (Comment c : recordComments) {
                        retVal.getMetadata().addComment(c);
                    }
                    recordComments.clear();
                }
                final RecordType rt = (RecordType) uOrComment;

                Record record = null;
                try {
                    record = new LazyRecord(factory, retVal, rt);
                } catch (Exception e) {
                    LOGGER.info(rt.getId());
                    LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);

                }

                retVal.addRecord(record);

                for (Comment comment : recordComments) {
                    record.addComment(comment);
                }
                recordComments.clear();
            }
        }
    }

    return retVal;
}

From source file:ca.phon.session.io.xml.v12.XMLSessionReader_v12.java

License:Open Source License

private Participant copyParticipant(SessionFactory factory, ParticipantType pt, DateTime sessionDate) {
    final Participant retVal = factory.createParticipant();

    retVal.setId(pt.getId());/*from  w  w  w . ja v  a 2s.  co m*/
    retVal.setName(pt.getName());

    final XMLGregorianCalendar bday = pt.getBirthday();
    if (bday != null) {
        final DateTime bdt = new DateTime(bday.getYear(), bday.getMonth(), bday.getDay(), 12, 0);
        retVal.setBirthDate(bdt);

        // calculate age up to the session date
        final Period period = new Period(bdt, sessionDate);
        retVal.setAgeTo(period);
    }

    final Duration ageDuration = pt.getAge();
    if (ageDuration != null) {
        // convert to period
        final Period age = Period.parse(ageDuration.toString());
        retVal.setAge(age);
    }

    retVal.setEducation(pt.getEducation());
    retVal.setGroup(pt.getGroup());

    String langs = "";
    for (String lang : pt.getLanguage())
        langs += (langs.length() > 0 ? ", " : "") + lang;
    retVal.setLanguage(langs);

    if (pt.getSex() == SexType.MALE)
        retVal.setSex(Sex.MALE);
    else if (pt.getSex() == SexType.FEMALE)
        retVal.setSex(Sex.FEMALE);
    else
        retVal.setSex(Sex.UNSPECIFIED);

    ParticipantRole prole = ParticipantRole.fromString(pt.getRole());
    if (prole == null)
        prole = ParticipantRole.TARGET_CHILD;
    retVal.setRole(prole);

    retVal.setSES(pt.getSES());

    return retVal;
}

From source file:cd.education.data.collector.android.widgets.DateWidget.java

License:Apache License

@Override
public IAnswerData getAnswer() {
    if (showCalendar) {
        scrollView.clearChildFocus(mDatePicker);
    }//w  w  w  .  j  a v a 2s  . c o  m
    clearFocus();
    DateTime ldt = new DateTime(mDatePicker.getYear(),
            (!showCalendar && hideMonth) ? 1 : mDatePicker.getMonth() + 1,
            (!showCalendar && (hideMonth || hideDay)) ? 1 : mDatePicker.getDayOfMonth(), 0, 0);
    // DateTime utc = ldt.withZone(DateTimeZone.forID("UTC"));
    return new DateData(ldt.toDate());
}

From source file:ch.bfh.srs.gui.javafx.CalendarView.java

License:Open Source License

private DateTime getDateTime(LocalDate date, LocalTime time) {
    return new DateTime(date.getYear(), date.getMonthValue(), date.getDayOfMonth(), time.getHour(),
            time.getMinute());//from   w ww .ja  va 2s .co  m
}

From source file:ch.bfh.ti.soed.hs16.srs.yellow.controllers.JPARealDataAccessor.java

License:Open Source License

private void generateFakeData() {
    Building building = this.makeBuilding("GEO1");
    Building building1 = this.makeBuilding("NAT2");
    Building building2 = this.makeBuilding("ROLEX3");
    Building building3 = this.makeBuilding("ID4");
    Building building4 = this.makeBuilding("TRN5");
    Equipment equipment = this.makeEquipment("TV");
    Equipment equipment1 = this.makeEquipment("Panoramic view");
    Equipment equipment2 = this.makeEquipment("Douche");
    Equipment equipment3 = this.makeEquipment("Projector");
    Equipment equipment4 = this.makeEquipment("Embedded PC");
    Customer customer = this.makeCustomer("nt4245", "rwutth9428*/&");
    Customer customer1 = this.makeCustomer("test", "twztuzrw85478&/(");
    Customer customer2 = this.makeCustomer("test1", "rwut428*/&");
    Customer customer3 = this.makeCustomer("test2", "fkdsnmfks428*/&");
    Customer customer4 = this.makeCustomer("test3", "rwutt(()7778*/&");
    Room room = this.makeRoom("N1", 15);
    Room room1 = this.makeRoom("N2", 10);
    Room room2 = this.makeRoom("N3", 12);
    Room room3 = this.makeRoom("N4", 13);
    Room room4 = this.makeRoom("N5", 16);
    Booking booking = this.makeBooking(customer, room, DateTime.now(), new DateTime(2017, 6, 2, 5, 17));
    Booking booking1 = this.makeBooking(customer1, room1, DateTime.now(), new DateTime(2017, 6, 2, 5, 13));
    Booking booking2 = this.makeBooking(customer, room, DateTime.now(), new DateTime(2017, 6, 2, 4, 17));
}

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

public boolean startStrategyDaily() {
    int j, i, l, N, file_count, k;
    double sum = 0;
    Double D;/*from w w w  .  j a va2 s . c om*/
    String ddelims = "[-]";
    boolean computed = false;
    int nobs_count = 0;
    String date_stamp, strline;
    date_returns = new ArrayList<String>();
    double mean = 0;
    int dtotal_trades = 0;
    trade_obs = 100;
    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;
    dailyoutret = new ArrayList<Double>();
    mdfaTrades = new ArrayList<MDFATrade>();

    ArrayList<String> datesAll = 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;

    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;

    formatter = new DecimalFormat("#0.000000");
    formatter3 = new DecimalFormat("#0.00000");
    formatter2 = new DecimalFormat("#0.00");
    date_stamp = "";
    ndays = 30;

    stop_loss = true;
    stop_loss_thresh = 50;

    ArrayList<String> dailyData = new ArrayList<String>();
    start_seq = 0;

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

        dailyData = new ArrayList<String>();

        if (dataFiles[file_count].indexOf("JPY") != -1 && dataFiles[file_count].indexOf("SEKJPY") == -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;
        }

        //setTimeStandards(new File(dataFiles[file_count]));
        System.out.println("opening " + dataFiles[file_count]);

        try {

            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;
            nobs_count = 0;
            computed = false;

            trade_count = 0;
            recompute_filter = true;

            while ((strline = br.readLine()) != null) {
                dailyData.add(strline);
            }

            trade_obs = dailyData.size();

            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];
            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];

            for (int day = 0; day < dailyData.size(); day++) {

                strline = dailyData.get(day);

                //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];
                String[] intdates = date_stamp.split(ddelims);
                new DateTime((new Integer(intdates[0])).intValue(), (new Integer(intdates[1])).intValue(),
                        (new Integer(intdates[2])).intValue(), 14, 0);

                if (bid_ask_data) {

                    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]));
                    }

                    if (current_signal > 0) {
                        D = new Double(tokens[2]);
                        price.add(D);
                        if (spread_method == 0) {
                            live_series.add(log(bid.get(bid.size() - 1)) - log(bid.get(bid.size() - 2)));
                        } //low urgency
                        else if (spread_method == 1) {
                            live_series.add(log(bid.get(bid.size() - 1)) - log(mid.get(mid.size() - 2)));
                        } //med urgency
                        else {
                            live_series.add(log(bid.get(bid.size() - 1)) - log(ask.get(ask.size() - 2)));
                        } //high urgency
                    } else if (current_signal < 0) {
                        D = new Double(tokens[3]);
                        price.add(D);
                        if (spread_method == 0) {
                            live_series.add(log(bid.get(bid.size() - 1)) - log(bid.get(bid.size() - 2)));
                        } //low urgency
                        else if (spread_method == 1) {
                            live_series.add(log(ask.get(ask.size() - 1)) - log(mid.get(mid.size() - 2)));
                        } //med urgency
                        else {
                            live_series.add(log(ask.get(ask.size() - 1)) - log(bid.get(bid.size() - 2)));
                        } //high urgency
                    } else {
                        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(new Double(tokens[4]));
                        }
                    }
                    lo_price.add(new Double(tokens[7]));
                    hi_price.add(new Double(tokens[8]));

                    //System.out.println(date_stamp + " " + live_series.get(live_series.size() - 1));
                    //              if(live_series.size() > 2)
                    //              {
                    //               System.out.println("\n" + date_stamp + " " + live_series.get(live_series.size() - 1) + ", ask = " + ask.get(ask.size()-1) + ", bid = " + bid.get(bid.size()-1) + 
                    //               ", ask_t-1 " + ask.get(ask.size()-2) + ", bid_t-1 " + bid.get(bid.size()-2));
                    //              }
                    //spread.println(date_stamp + " " + (new Double(tokens[3]) - new Double(tokens[2])));

                } else {

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

                    //if(print_debug)System.out.println(date_stamp + " " + price.get(price.size()-1) + " " + close_series.get(close_series.size()-1));
                    //System.out.println(date_stamp + " " + price.get(price.size()-1) + " " + close_series.get(close_series.size()-1));
                    if (n_rep > 2 && tokens.length > 3) {
                        D = new Double(tokens[3]);
                        exp_series_1.add(D);
                    }
                    if (n_rep > 3 && tokens.length > 4) {
                        D = new Double(tokens[4]);
                        exp_series_2.add(D);
                    }
                }

                nobs_count++;

                //System.out.println(nobs_count + " " + n_obs);  
                if (nobs_count >= n_obs) {

                    datesAll.add(date_stamp);
                    tseries = new double[n_rep * n_obs];
                    out_series = new double[n_obs];

                    if (classical_data) {
                        for (i = 0; i < n_obs; i++) {
                            tseries[n_obs - 1 - i] = live_series.get(live_series.size() - 1 - i);
                            tseries[n_obs + n_obs - 1 - i] = live_series.get(live_series.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);
                            }
                        }

                        for (i = 0; i < n_obs; i++) {
                            out_series[n_obs - 1 - i] = tseries[n_obs - 1 - i];
                        }

                    } else {
                        for (i = 0; i < n_obs; i++) {
                            tseries[n_obs - 1 - i] = close_series.get(close_series.size() - 1 - i);
                            tseries[n_obs + n_obs - 1 - i] = close_series.get(close_series.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);
                            }
                        }

                        for (i = 0; i < n_out_samp; i++) {
                            out_series[n_obs - 1 - i] = live_series.get(live_series.size() - 1 - i);
                        }
                        for (i = n_out_samp; i < n_obs; i++) {
                            out_series[n_obs - 1 - i] = tseries[n_obs - 1 - i];
                        }
                    }

                    mdfa.set_tseries(tseries, n_obs, n_rep);

                    if (recompute_filter) {

                        //                      System.out.println(date_stamp + " " + tseries[n_obs-1] + " " + tseries[n_obs - 2] + " " + tseries[n_obs-3] + " " + tseries[n_obs-4] + " " 
                        //                         + price.get(price.size()-1));

                        mdfa.computeFilterGeneral(true, false);

                        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(print_debug) System.out.println(date_stamp + " b_coeffs = " + b_coeffs[0] + " " + b_coeffs[1] + " " + b_coeffs[2]);
                        pulse_count = 0;
                        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]);

                    }

                    //---- apply the mother filter------------
                    sum = 0.0;
                    if (!bid_ask_data) {
                        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];
                            }
                        }
                    } else {
                        for (l = 0; l < L; l++) {
                            sum = sum + b_coeffs[l] * out_series[n_obs - 1 - l];
                        } // System.out.println(out_series[n_obs-1-l]);}
                    }
                    //System.out.println("");

                    current_signal = sum;

                    if (sig_inverse) {
                        current_signal = -current_signal;
                    }

                    dailyReport.add("" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1))
                            + ", " + formatter3.format(lo_price.get(lo_price.size() - 1)) + ", "
                            + formatter3.format(hi_price.get(hi_price.size() - 1)) + ", "
                            + formatter.format(out_series[n_obs - 1]) + ", "
                            + formatter.format(current_signal));

                    xt[trade_count] = close_series.get(close_series.size() - 1).doubleValue();
                    signal[trade_count] = current_signal;

                    if (diff_sig_trading && trade_count > 0) {
                        signal[trade_count] = signal[trade_count] - signal[trade_count - 1];
                    }

                    prix[trade_count] = price.get(price.size() - 1).doubleValue();
                    lo_prix[trade_count] = lo_price.get(lo_price.size() - 1).doubleValue();
                    hi_prix[trade_count] = hi_price.get(hi_price.size() - 1).doubleValue();

                    trade_count++;

                    if (trade_count % ndays == 0) {
                        recompute_filter = true;
                    } else {
                        recompute_filter = false;
                    }

                }

            } //finish with running the data

            //---- now run through trading rules and compute stats------

            last_trade_index = prix.length;
            insampleTradingDiff_Cust_SL(prix, lo_prix, hi_prix, signal, prix.length);
            ROI = account[end_time_index];
            if (jpy) {
                ROI = .01 * ROI;
            }

            mean = 0;
            dtotal_trades = 0;
            diff_account = new double[account.length];
            diff_account[0] = 0;

            //System.out.println(date_tokens[0]);
            for (k = 1; k < account.length; k++) {
                diff_account[k] = account[k] - account[k - 1];

                ret_dist[k] = ret_dist[k] + diff_account[k];

                if (diff_account[k] > 0) {
                    pos_ret_dist[k] = pos_ret_dist[k] + 1;
                    pos_ret_mean_time[k] = pos_ret_mean_time[k] + diff_account[k];
                    pos_ret_mean = pos_ret_mean + diff_account[k];
                    n_pos_ret++;
                } else if (diff_account[k] < 0) {
                    neg_ret_dist[k] = neg_ret_dist[k] + 1;
                    neg_ret_mean_time[k] = neg_ret_mean_time[k] + diff_account[k];
                    neg_ret_mean = neg_ret_mean + diff_account[k];
                    n_neg_ret++;
                }
            }

            //----- now do the trade analysis ---------
            trade_started = 0;
            for (k = 1; k < account.length; k++) {

                if (k > end_time_index) {
                    break;
                }

                if (diff_account[k] != 0) {
                }

                if (diff_account[k] > 0) {

                    mean = mean + diff_account[k];
                    dtotal_trades = dtotal_trades + 1;

                    min_pnl_dd = 0;
                    max_pnl_uu = 0;

                    l = k;
                    while (l > 0) {
                        if (pnl[l] < min_pnl_dd) {
                            min_pnl_dd = pnl[l];
                        }
                        if (png[l] > max_pnl_uu) {
                            max_pnl_uu = png[l];
                        }

                        l--;

                        if (diff_account[l] != 0) {
                            break;
                        }
                    }
                    //System.out.println("trade, " + k + " " + trade_started + " " + end_time_index);
                    if (trade_started != end_time_index) {
                        pos_trades_started[trade_started] = pos_trades_started[trade_started] + 1;
                        pos_trades_started_mean[trade_started] = pos_trades_started_mean[trade_started]
                                + diff_account[k];
                        if (jpy) {
                            mdfaTrades.add(new MDFATrade(date_stamp, trade_started, k, .01 * min_pnl_dd,
                                    .01 * max_pnl_uu, .01 * diff_account[k]));
                        } else {
                            mdfaTrades.add(new MDFATrade(date_stamp, trade_started, k, min_pnl_dd, max_pnl_uu,
                                    diff_account[k]));
                        }
                        //System.out.println("trade, " + trade_started + " " + k + " " + min_pnl_dd + " " + diff_account[k]);
                    }
                    trade_started = k;
                }
                if (diff_account[k] < 0) {

                    mean = mean + diff_account[k];
                    dtotal_trades = dtotal_trades + 1;

                    min_pnl_dd = 0;
                    max_pnl_uu = 0;

                    l = k;
                    while (l > 0) {
                        if (pnl[l] < min_pnl_dd) {
                            min_pnl_dd = pnl[l];
                        }
                        if (png[l] > max_pnl_uu) {
                            max_pnl_uu = png[l];
                        }

                        l--;

                        if (diff_account[l] != 0) {
                            break;
                        }
                    }
                    //System.out.println("trade, " + k + " " + trade_started + " " + end_time_index);
                    if (trade_started != end_time_index) {
                        neg_trades_started[trade_started] = neg_trades_started[trade_started] + 1;
                        neg_trades_started_mean[trade_started] = neg_trades_started_mean[trade_started]
                                - diff_account[k];
                        if (jpy) {
                            mdfaTrades.add(new MDFATrade(date_stamp, trade_started, k, .01 * min_pnl_dd,
                                    .01 * max_pnl_uu, .01 * diff_account[k]));
                        } else {
                            mdfaTrades.add(new MDFATrade(date_stamp, trade_started, k, min_pnl_dd, max_pnl_uu,
                                    diff_account[k]));
                        }
                        //System.out.println("trade, " + trade_started + " " + k + " " + min_pnl_dd + " " + diff_account[k]);
                    }

                    trade_started = k;
                }
            }

            mean = mean / (double) dtotal_trades;
            standard_deviation = 0;
            for (k = 0; k < account.length; k++) {
                if (diff_account[k] != 0) {
                    standard_deviation = standard_deviation
                            + (diff_account[k] - mean) * (diff_account[k] - mean);
                }
            }
            standard_deviation = Math.sqrt(standard_deviation) / (double) dtotal_trades;

            System.out.println("size = " + dailyReport.size());

            for (k = 0; k < dailyReport.size(); k++) {
                System.out.println(dailyReport.get(k + start_seq) + ", " + formatter.format(account[k]) + ", "
                        + formatter.format(pnl[k]) + ", " + formatter.format(png[k]));
            }
            longROI = account[end_time_index];
            shortROI = account[end_time_index];

            for (i = 0; i < datesAll.size(); i++) {
                returns.add(diff_account[i]);
                dailyoutret.add(prix[i]);
                date_returns.add(datesAll.get(i) + ", " + diff_account[i]);
                System.out.println(datesAll.get(i) + ", " + diff_account[i]);
            }

            total_ROI = account[account.length - 1];

            mean_perf = mean;
            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(returns.toArray(new Double[0]));

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

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

    }
    return computed;

}

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

public boolean startStrategyDailyIntraday() {

    int j, i, l, N, file_count;
    double sum = 0;
    Double D;/*from   w  ww.j  a  v  a2s .  com*/
    String ddelims = "[-]";
    boolean computed = 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>();

    double pnl;
    String[] dates;
    int cur_month = 0;
    double convers = 1.0;
    ArrayList<String> allsignal = new ArrayList<String>();
    ArrayList<String> account = new ArrayList<String>();
    ArrayList<String> latestDates = new ArrayList<String>();
    perf_returns = new ArrayList<Double>();
    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>();
    daily_returns = new ArrayList<Double>();
    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;

    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];
    daily_price = new ArrayList<Double>();

    daily_dates = new ArrayList<String>();
    ArrayList<String> daily_data = new ArrayList<String>();
    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;
    int line;
    //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;

    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;

    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);
        }
    }

    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"));
        PrintWriter svmout = new PrintWriter(new FileWriter("neural_" + n_files + ".dat"));

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

            if (dataFiles[file_count].indexOf("JPY") != -1) {
                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 (dataFiles[file_count].indexOf("NOK") != -1) {
                jpy = true;
                stop_loss_thresh = stop_loss_thresh * 8;
                take_profit_thresh = take_profit_thresh * 8;
                global_stop_loss = global_stop_loss * 8;
                stop_loss = stop_loss_thresh;
            } else if (dataFiles[file_count].indexOf("XAU") != -1) {
                jpy = true;
                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;
            }

            //          if(dataFiles[file_count].indexOf("JPY") != -1 && (dataFiles[file_count].indexOf("SEKJPY") == -1 && dataFiles[file_count].indexOf("NOKJPY") == -1))
            //          {
            //           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;
            //           convers = .01;
            //          }
            //          else if(dataFiles[file_count].indexOf("SEKJPY") != -1 || dataFiles[file_count].indexOf("NOKJPY") != -1)
            //          {
            //            scandi_jpy = true;
            //            convers = .01;
            //          }
            //          else if(dataFiles[file_count].indexOf("NOK") != -1 || dataFiles[file_count].indexOf("SEK") != -1)
            //          {
            //            scandi = true;
            //            convers = 1/6.0; 
            //          }

            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) {
                daily_data.add(strline);
            }

            for (line = 0; line < daily_data.size() - 1; line++) {

                strline = daily_data.get(line);
                //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);

                String[] hours = date_tokens[1].split("[:]+");

                //--- TIME FILTER----------------------
                if ((!time_filter && !hours[0].equals("17"))
                        || ((date_tokens[1].indexOf(":00:00") != -1 || date_tokens[1].indexOf(":30:00") != -1)
                                && !hours[0].equals("17")))
                //if(!time_filter || ((date_tokens[1].indexOf(":00:00") != -1 || date_tokens[1].indexOf(":30:00")  != -1) && !hours[0].equals("17")))
                {

                    //insampStart is the time we collect daily data 

                    //if(date_stamp.indexOf(insampStart) != -1)
                    if (date_stamp.indexOf(insampStart) != -1)// && weekend.dayOfWeek().getAsText().equals("Wednesday"))
                    {

                        //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);

                    }

                    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[2]));
                        hi_price.add(new Double(tokens[2]));
                    } else {
                        lo_price.add((new Double(tokens[2])));
                        hi_price.add((new Double(tokens[2])));
                    }

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

                    made_trade = false;
                    if (daily_returns.size() >= n_obs && (date_stamp.indexOf(insampStart) != -1))// && weekend.dayOfWeek().getAsText().equals("Tuesday"))) //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 ((new Integer(intdates[1])).intValue() != cur_month || day_count == 0) {
                            cur_month = (new Integer(intdates[1])).intValue();
                            //System.out.println("Recomputing filter..." + intdates[0] + "-" + intdates[1] + "-" + intdates[2]);
                            if (printall)
                                System.out.println("Recomputing filter...");
                            mdfa.computeFilterGeneral(true, false);
                            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]);

                            //System.out.println("\n");
                            //for(l=0;l<L;l++) {System.out.println(b_coeffs[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;
                        }

                        svmout.println(date_stamp + ", " + 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);
                        if (binary_rule) {

                            if (signal_profit) {

                                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);
                                    }

                                    price_bought = daily_price.get(daily_size - 1);
                                    in_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);

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

                                    price_borrowed = daily_price.get(daily_size - 1);
                                    out_transaction = 1;

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

                                }

                            }

                            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));
                                    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);

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

                                if (long_buy && in_transaction == 0) {
                                    price_bought = daily_price.get(daily_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);

                                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));
                                    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 (short_sell && out_transaction == 0) {
                                    price_borrowed = daily_price.get(daily_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)) {

                            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));
                                    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);
                                    }
                                }

                                if (long_buy && in_transaction == 0) {
                                    price_bought = daily_price.get(daily_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 = 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));
                                    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) {
                                        System.out.println("Bought for a profit of " + profit);
                                        System.out.println("profit, price_bought, price_sold = " + profit + ", "
                                                + price_bought + ", " + price_sold);
                                    }

                                }

                                if (short_sell && out_transaction == 0) {
                                    price_borrowed = daily_price.get(daily_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;}

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

                    } else if (trading_hours) {

                        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;
                            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);
                                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 - 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);

                                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);
                                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 --------
                                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);
                            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);
                                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 (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);
                                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));

                                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 (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 
                                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
                                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;

                                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) {
                            //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);

                    }

                }
            }

        }

        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;

        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));

            dreturns[i] = convers * dreturns[i]; //approximate pips in dollars

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

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

                pnl = dreturns[i];
                perf_dates.add(dates[0]);

            } else //already contains the date, so add on pnl
            {
                pnl = pnl + dreturns[i];
            }

            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);

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

        perform.close();
        // load in intraday information Date log-diff price signal - EXCLUDES LATEST OBSERVATION!!! 
        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]);
            //        }
        }

        n_files++;
        mean = mean / (n_pos_ret + n_neg_ret);

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

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

        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);

        out.close();
        dailyout.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);
    }

    return computed;

}

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

public boolean startStrategyDailyIntraday_MR() {

    int j, i, l, N, file_count;
    double sum = 0;
    Double D;//w ww .j  a v  a 2  s  .c  o m
    String ddelims = "[-]";
    boolean computed = 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>();

    boolean waiting_meanrev_down = false;
    boolean waiting_meanrev_up = false;
    double mean_rev_amnt = 0;

    double pnl;
    String[] dates;
    int cur_month = 0;
    double convers = 1.0;
    ArrayList<String> allsignal = new ArrayList<String>();
    ArrayList<String> account = new ArrayList<String>();
    ArrayList<String> latestDates = new ArrayList<String>();
    perf_returns = new ArrayList<Double>();
    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>();
    daily_returns = new ArrayList<Double>();
    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;

    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 = false;
    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];
    daily_price = new ArrayList<Double>();

    daily_dates = new ArrayList<String>();
    ArrayList<String> daily_data = new ArrayList<String>();
    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;
    int line;
    //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;

    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;

    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);
        }
    }

    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"));
        PrintWriter svmout = new PrintWriter(new FileWriter("neural_" + n_files + ".dat"));

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

            if (dataFiles[file_count].indexOf("JPY") != -1) {
                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 (dataFiles[file_count].indexOf("NOK") != -1) {
                jpy = true;
                stop_loss_thresh = stop_loss_thresh * 8;
                take_profit_thresh = take_profit_thresh * 8;
                global_stop_loss = global_stop_loss * 8;
                stop_loss = stop_loss_thresh;
            } else if (dataFiles[file_count].indexOf("XAU") != -1) {
                jpy = true;
                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;
            }

            //          if(dataFiles[file_count].indexOf("JPY") != -1 && (dataFiles[file_count].indexOf("SEKJPY") == -1 && dataFiles[file_count].indexOf("NOKJPY") == -1))
            //          {
            //           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;
            //           convers = .01;
            //          }
            //          else if(dataFiles[file_count].indexOf("SEKJPY") != -1 || dataFiles[file_count].indexOf("NOKJPY") != -1)
            //          {
            //            scandi_jpy = true;
            //            convers = .01;
            //          }
            //          else if(dataFiles[file_count].indexOf("NOK") != -1 || dataFiles[file_count].indexOf("SEK") != -1)
            //          {
            //            scandi = true;
            //            convers = 1/6.0; 
            //          }

            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) {
                daily_data.add(strline);
            }

            for (line = 0; line < daily_data.size() - 1; line++) {

                strline = daily_data.get(line);
                //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);

                //insampStart is the time we collect daily data 

                //if(date_stamp.indexOf(insampStart) != -1)
                if (date_stamp.indexOf(insampStart) != -1)// && weekend.dayOfWeek().getAsText().equals("Wednesday"))
                {

                    //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);

                }

                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[2]));
                    hi_price.add(new Double(tokens[2]));
                } else {
                    lo_price.add((new Double(tokens[2])));
                    hi_price.add((new Double(tokens[2])));
                }

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

                made_trade = false;
                if (daily_returns.size() >= n_obs && (date_stamp.indexOf(insampStart) != -1))// && weekend.dayOfWeek().getAsText().equals("Tuesday"))) //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 ((new Integer(intdates[1])).intValue() != cur_month || day_count == 0) {
                        cur_month = (new Integer(intdates[1])).intValue();
                        //System.out.println("Recomputing filter..." + intdates[0] + "-" + intdates[1] + "-" + intdates[2]);
                        if (printall)
                            System.out.println("Recomputing filter...");
                        mdfa.computeFilterGeneral(true, false);
                        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]);

                        //System.out.println("\n");
                        //for(l=0;l<L;l++) {System.out.println(b_coeffs[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;
                    }

                    svmout.println(date_stamp + ", " + 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);
                    if (binary_rule) {

                        if (signal_profit) {

                            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);
                                }

                                price_bought = daily_price.get(daily_size - 1);
                                in_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);

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

                                price_borrowed = daily_price.get(daily_size - 1);
                                out_transaction = 1;

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

                            }

                        }

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

                            last_price = daily_price.get(daily_size - 1);
                            waiting_meanrev_down = false;
                            waiting_meanrev_up = false;

                            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));
                                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);

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

                            waiting_meanrev_down = true;
                            if (printall) {
                                System.out.println(
                                        "Waiting for better price to buy to enter long at " + last_price);
                            }

                            /*                  if(long_buy && in_transaction == 0)
                                         {
                                               price_bought = daily_price.get(daily_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;

                            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));
                                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);
                                }

                            }

                            waiting_meanrev_up = true;
                            if (printall) {
                                System.out.println(
                                        "Waiting for better price to buy to enter long at " + last_price);
                            }

                            //              if(short_sell && out_transaction == 0)
                            //              {
                            //               price_borrowed = daily_price.get(daily_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)) {

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

                            /*                  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));
                                           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);
                                                }
                                              }
                                                      
                                                      
                                                      
                                                      
                                              if(long_buy && in_transaction == 0)
                                         {
                                               price_bought = daily_price.get(daily_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 = daily_price.get(daily_size - 1);
                            waiting_meanrev_up = true;

                            //                   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));
                            //                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){
                            //                    System.out.println("Bought for a profit of " + profit);
                            //                    System.out.println("profit, price_bought, price_sold = " + profit + ", " + price_bought + ", " + price_sold);}              
                            //               
                            //               
                            //              }
                            //             
                            //              if(short_sell && out_transaction == 0)
                            //              {
                            //               price_borrowed = daily_price.get(daily_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;}

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

                } else if (trading_hours) {

                    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) {
                                price_borrowed = bid.get(bid.size() - 1);
                                out_transaction = 1;

                                last_price = price_borrowed;
                                if (printall)
                                    System.out.println(
                                            "Entered short transaction at " + price_borrowed + " after "
                                                    + (bid.get(bid.size() - 1) - last_price) + " reversion");
                            }
                            waiting_meanrev_up = false;
                            waiting_meanrev_down = false;
                        }
                    } else if (waiting_meanrev_down) {
                        if ((last_price - ask.get(ask.size() - 1)) > mean_rev_amnt) {
                            if (long_buy && in_transaction == 0) {
                                price_bought = ask.get(ask.size() - 1);
                                in_transaction = 1;

                                last_price = price_bought;
                                if (printall)
                                    System.out.println("Entered long transaction at " + price_bought);
                            }
                            waiting_meanrev_down = false;
                            waiting_meanrev_up = false;
                        }
                    }

                    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;
                        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);
                            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 - 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);

                            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);
                            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 --------
                            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);
                        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);
                            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 (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);
                            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));

                            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 (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 
                            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
                            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;

                            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) {
                        //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);

                }

            }
        }

        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;

        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));

            dreturns[i] = convers * dreturns[i]; //approximate pips in dollars

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

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

                pnl = dreturns[i];
                perf_dates.add(dates[0]);

            } else //already contains the date, so add on pnl
            {
                pnl = pnl + dreturns[i];
            }

            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);

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

        perform.close();
        // load in intraday information Date log-diff price signal - EXCLUDES LATEST OBSERVATION!!! 
        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]);
            //        }
        }

        n_files++;
        mean = mean / (n_pos_ret + n_neg_ret);

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

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

        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);

        out.close();
        dailyout.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);
    }

    return computed;

}