List of usage examples for org.joda.time DateTime dayOfWeek
public Property dayOfWeek()
From source file:DDTDate.java
License:Apache License
/** * This function will populate the varsMap with new copies of values related to display format of date & time stamps. * Those values are based on the class (static) date variable that is currently in use. * Those values can later be used in verification of UI elements that are date-originated but may change in time (say, today's date, month, year - etc.) * The base date that is used is Locale dependent (currently, the local is assumed to be that of the workstation running the software.) * When relevant, values are provided in various styles (SHORT, MEDIUM, LONG, FULL) - not all date elements have all those versions available. * The purpose of this 'exercise' is to set up the facility for the user to verify any component of date / time stamps independently of one another or 'traditional' formatting. * * The variables maintained here are formatted with a prefix to distinguish them from other, user defined variables. * * @TODO: enable Locale modifications (at present the test machine's locale is considered and within a test session only one locale can be tested) * WikiPedia: https://en.wikipedia.org/wiki/Date_format_by_country * Stack Overflow (coes) http://stackoverflow.com/questions/3191664/list-of-all-locales-and-their-short-codes * @param varsMap//from ww w.j a v a2s .co m */ private void maintainDateProperties(Hashtable<String, Object> varsMap) throws Exception { String prefix = "$"; try { // Build formatting objects for each of the output styles DateFormat shortStyleFormatter = DateFormat.getDateInstance(DateFormat.SHORT, getLocale()); DateFormat mediumStyleFormatter = DateFormat.getDateInstance(DateFormat.MEDIUM, getLocale()); DateFormat longStyleFormatter = DateFormat.getDateInstance(DateFormat.LONG, getLocale()); DateFormat fullStyleFormatter = DateFormat.getDateInstance(DateFormat.FULL, getLocale()); // Use a dedicated variable to hold values of formatting results to facilitate debugging // @TODO (maybe) when done debugging - convert to inline calls to maintainDateProperty ... String formatValue; // Examples reflect time around midnight of February 6 2014 - actual values DO NOT include quotes (added here for readability) MutableDateTime theReferenceDate = getReferenceDate(); //getReferenceDateAdjustedForTimeZone(); // Default Date using DDTSettings pattern. formatValue = new SimpleDateFormat(defaultDateFormat()).format(theReferenceDate.toDate()); maintainDateProperty(prefix + "defaultDate", formatValue, varsMap); // Short Date - '2/6/14' formatValue = shortStyleFormatter.format(theReferenceDate.toDate()); maintainDateProperty(prefix + "shortDate", formatValue, varsMap); // Medium Date - 'Feb 6, 2014' formatValue = mediumStyleFormatter.format(theReferenceDate.toDate()); maintainDateProperty(prefix + "mediumDate", formatValue, varsMap); // Long Date - 'February 6, 2014' formatValue = longStyleFormatter.format(theReferenceDate.toDate()); maintainDateProperty(prefix + "longDate", formatValue, varsMap); // Full Date 'Thursday, February 6, 2014' formatValue = fullStyleFormatter.format(theReferenceDate.toDate()); maintainDateProperty(prefix + "fullDate", formatValue, varsMap); // hours : minutes : seconds : milliseconds (broken to separate components) - formatValue = theReferenceDate.toString("hh:mm:ss:SSS"); if (formatValue.toString().contains(":")) { String[] hms = split(formatValue.toString(), ":"); if (hms.length > 3) { // Hours - '12' formatValue = hms[0]; maintainDateProperty(prefix + "hours", formatValue, varsMap); // Minutes - '02' formatValue = hms[1]; maintainDateProperty(prefix + "minutes", formatValue, varsMap); // Seconds - '08' formatValue = hms[2]; maintainDateProperty(prefix + "seconds", formatValue, varsMap); // Milliseconds - '324' formatValue = hms[3]; maintainDateProperty(prefix + "milliseconds", formatValue, varsMap); // Hours in 24 hours format - '23' formatValue = theReferenceDate.toString("HH"); maintainDateProperty(prefix + "hours24", formatValue, varsMap); } else setException("Failed to format reference date to four time units!"); } else { setException("Failed to format reference date to its time units!"); } // hours : minutes : seconds (default timestamp) - '12:34:56' formatValue = theReferenceDate.toString("hh:mm:ss"); maintainDateProperty(prefix + "timeStamp", formatValue, varsMap); // Short Year - '14' formatValue = theReferenceDate.toString("yy"); maintainDateProperty(prefix + "shortYear", formatValue, varsMap); // Long Year - '2014' formatValue = theReferenceDate.toString("yyyy"); maintainDateProperty(prefix + "longYear", formatValue, varsMap); // Short Month - '2' formatValue = theReferenceDate.toString("M"); maintainDateProperty(prefix + "shortMonth", formatValue, varsMap); // Padded Month - '02' formatValue = theReferenceDate.toString("MM"); maintainDateProperty(prefix + "paddedMonth", formatValue, varsMap); // Short Month Name - 'Feb' formatValue = theReferenceDate.toString("MMM"); maintainDateProperty(prefix + "shortMonthName", formatValue, varsMap); // Long Month Name - 'February' formatValue = theReferenceDate.toString("MMMM"); maintainDateProperty(prefix + "longMonthName", formatValue, varsMap); // Week in Year - '2014' (the year in which this week falls) formatValue = String.valueOf(theReferenceDate.getWeekyear()); maintainDateProperty(prefix + "weekYear", formatValue, varsMap); // Short Day in date stamp - '6' formatValue = theReferenceDate.toString("d"); maintainDateProperty(prefix + "shortDay", formatValue, varsMap); // Padded Day in date stamp - possibly with leading 0 - '06' formatValue = theReferenceDate.toString("dd"); maintainDateProperty(prefix + "paddedDay", formatValue, varsMap); // Day of Year - '37' formatValue = theReferenceDate.toString("D"); maintainDateProperty(prefix + "yearDay", formatValue, varsMap); // Short Day Name - 'Thu' formatValue = theReferenceDate.toString("E"); maintainDateProperty(prefix + "shortDayName", formatValue, varsMap); // Long Day Name - 'Thursday' DateTime dt = new DateTime(theReferenceDate.toDate()); DateTime.Property dowDTP = dt.dayOfWeek(); formatValue = dowDTP.getAsText(); maintainDateProperty(prefix + "longDayName", formatValue, varsMap); // AM/PM - 'AM' formatValue = theReferenceDate.toString("a"); maintainDateProperty(prefix + "ampm", formatValue, varsMap); // Era - (BC/AD) formatValue = theReferenceDate.toString("G"); maintainDateProperty(prefix + "era", formatValue, varsMap); // Time Zone - 'EST' formatValue = theReferenceDate.toString("zzz"); maintainDateProperty(prefix + "zone", formatValue, varsMap); addComment( "Date variables replenished for date: " + fullStyleFormatter.format(theReferenceDate.toDate())); addComment(theReferenceDate.toString()); System.out.println(getComments()); } catch (Exception e) { setException(e); } }
From source file:DDTDate.java
License:Apache License
/** * Creates the output the user indicated in the input (outputType component) subject to the requested style (outputStyle) component * @return//from ww w . j a v a 2 s.c o m */ private String createOutput() { String result = ""; try { // If needed, adjust the reference date by the number and type of units specified - as per the time zone if (getUnits() != 0) { //setReferenceDate(getReferenceDateAdjustedForTimeZone()); getReferenceDate().add(getDurationType(), getUnits()); } // Create date formatters to be used for all varieties - the corresponding date variables are always set for convenience purposes DateFormat shortFormatter = DateFormat.getDateInstance(DateFormat.SHORT); DateFormat mediumFormatter = DateFormat.getDateInstance(DateFormat.MEDIUM); DateFormat longFormatter = DateFormat.getDateInstance(DateFormat.LONG); DateFormat fullFormatter = DateFormat.getDateInstance(DateFormat.FULL); // Build the specific formatter specified DateFormat formatter = null; switch (getOutputStyle().toLowerCase()) { case "medium": { formatter = mediumFormatter; break; } case "long": { formatter = longFormatter; break; } case "full": { formatter = fullFormatter; break; } default: formatter = shortFormatter; } // output style switch // construct the specified result - one at a time MutableDateTime theReferenceDate = getReferenceDate(); //getReferenceDateAdjustedForTimeZone(); switch (getOutputType().toLowerCase()) { case "date": { result = formatter.format(theReferenceDate.toDate()); break; } case "time": { switch (getOutputStyle().toLowerCase()) { case "short": { result = theReferenceDate.toString("hh:mm:ss"); break; } default: result = theReferenceDate.toString("hh:mm:ss.SSS"); } break; } // separate time components case "hour": case "minute": case "second": case "hour24": { String tmp = theReferenceDate.toString("hh:mm:ss"); if (tmp.toString().contains(":")) { String[] hms = split(tmp.toString(), ":"); if (hms.length > 2) { switch (getOutputType().toLowerCase()) { case "hour": { // Hour - '12' result = hms[0]; break; } case "minute": { // Minutes - '34' result = hms[1]; break; } case "second": { // Second - '56' result = hms[2]; break; } case "hour24": { // Hour - '23' result = theReferenceDate.toString("HH"); break; } default: result = hms[0]; } // switch for individual time component } // three parts of time components } // timestamp contains separator ":" break; } // Hours, Minutes, Seconds case "year": { switch (getOutputStyle().toLowerCase()) { case "short": { result = theReferenceDate.toString("yy"); break; } default: result = theReferenceDate.toString("yyyy"); } break; } case "month": { switch (getOutputStyle().toLowerCase()) { case "short": { result = theReferenceDate.toString("M"); break; } case "medium": { // padded with 0 result = theReferenceDate.toString("MM"); break; } case "long": { // short name 'Feb' result = theReferenceDate.toString("MMM"); break; } default: // Full name 'September' result = theReferenceDate.toString("MMMM"); } break; } case "day": { switch (getOutputStyle().toLowerCase()) { case "short": { result = theReferenceDate.toString("d"); break; } case "medium": { result = theReferenceDate.toString("dd"); break; } default: result = theReferenceDate.toString("dd"); } } case "doy": { result = theReferenceDate.toString("D"); break; } case "dow": { switch (getOutputStyle().toLowerCase()) { case "short": { result = theReferenceDate.toString("E"); break; } case "medium": { DateTime dt = new DateTime(theReferenceDate.toDate()); DateTime.Property dowDTP = dt.dayOfWeek(); result = dowDTP.getAsText(); break; } default: result = theReferenceDate.toString("E"); } break; } // day of week case "zone": { result = theReferenceDate.toString("zzz"); break; } case "era": { result = theReferenceDate.toString("G"); break; } case "ampm": { result = theReferenceDate.toString("a"); break; } default: { setException("Invalid Output Unit - cannot set output"); } // Create full date variables for the short, medium, long, full styles } // output type switch } // try constructing result catch (Exception e) { setException(e); } finally { return result; } }
From source file:calendario.service.Day.java
public Day(DateTime dt) { this.day = dt.getDayOfMonth() + ""; this.dayOfWeekShort = dt.dayOfWeek().getAsShortText(); }
From source file:ch.imetrica.mdfaTradingStrategies.MDFAStrategyEvolution.java
public boolean startStrategy() { int j, i, l, N, file_count, k; double sum = 0; Double D;/* ww w . ja v a2 s . c o m*/ double spreadnow; String ddelims = "[-]"; boolean computed = false; double b0_sum = 0; int nobs_count = 0; boolean print_filter = false; boolean no_return = true; String date_stamp, strline; 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; int advance = 8; // number of observations afterwhich we optimize double prev_signal; int changed_signs = 0; reg_trading_hours = false; String[] intdates; //make sure arraylists empty ArrayList<String> latestDates = new ArrayList<String>(); last_trades = new ArrayList<Integer>(); final_trades = new ArrayList<Double>(); dailyoutret = new ArrayList<Double>(); maxIntValue = new ArrayList<Double>(); avg_volatility = new ArrayList<Double>(); close_series = new ArrayList<Double>(); highlow_series = new ArrayList<Double>(); exp_series_1 = new ArrayList<Double>(); exp_series_2 = new ArrayList<Double>(); price = new ArrayList<Double>(); lo_price = new ArrayList<Double>(); hi_price = new ArrayList<Double>(); mid = new ArrayList<Double>(); bid = new ArrayList<Double>(); ask = new ArrayList<Double>(); dates_series = new ArrayList<String>(); dailyReport = new ArrayList<String>(); b0_trend = new ArrayList<Double>(); vol_0 = new ArrayList<Double>(); vol_1 = new ArrayList<Double>(); sub_returns = new ArrayList<Double>(); trade_days = new ArrayList<String>(); returns = new ArrayList<Double>(); longreturns = new ArrayList<Double>(); shortreturns = new ArrayList<Double>(); dropdowns = new ArrayList<Double>(); success = new ArrayList<Double>(); dates_low_high = new ArrayList<String>(); crits = new ArrayList<String>(); svm = new ArrayList<String>(); filters = new ArrayList<Filter>(); date_returns = new ArrayList<String>(); live_series = new ArrayList<Double>(); //the data to be applied out of sample ib_data_hash = new ibHash(); fridayROI = 0; fridayROI_pos = 0; fridays = 0; int all_trades, all_succ_trades, tot_all_trades, tot_succ_trades; lookback_returns = new ArrayList<Double>(); num_pos_returns = 0; deg_0 = new ArrayList<Double>(); deg_1 = new ArrayList<Double>(); crit_0 = new ArrayList<Double>(); crit_1 = new ArrayList<Double>(); full_returns_array = new ArrayList<double[]>(); morning_returns = new ArrayList<double[]>(); morning_buy = true; //enter transaction at morning open morning_optimize = false; //optimize in the morning trading hours num_full_positive_returns = 0; //--- Now get historical interp values ------ //uploadInterpParams("max_int.dat"); //------------------------------------------- forex24 = true; ret_dist = new double[trade_obs]; pos_ret_dist = new int[trade_obs]; neg_ret_dist = new int[trade_obs]; neg_trades_started = new int[trade_obs]; pos_trades_started = new int[trade_obs]; neg_trades_started_mean = new double[trade_obs]; pos_trades_started_mean = new double[trade_obs]; diff_account = new double[trade_obs]; pos_ret_mean_time = new double[trade_obs]; neg_ret_mean_time = new double[trade_obs]; mdfaTrades = new ArrayList<MDFATrade>(); fmt = DateTimeFormat.forPattern("y-MM-dd HH:mm:ss"); formatter = new DecimalFormat("#0.000000"); formatter3 = new DecimalFormat("#0.00000"); formatter2 = new DecimalFormat("#0.00"); tot_all_trades = 0; tot_succ_trades = 0; histo_stat = new int[100]; interp_vals = new ArrayList<Double>(); max_ranks = new ArrayList<Double>(); profit_baby = 0; //setForecastDFAParameters(); bad_starts = 0; n_out_samp = 0; //take_profit = true; //take_profit_thresh = .0020; 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); } } for (i = 0; i < trade_obs; i++) { neg_ret_dist[i] = 0; pos_ret_dist[i] = 0; ret_dist[i] = 0; pos_ret_mean_time[i] = 0; neg_ret_mean_time[i] = 0; neg_trades_started[i] = 0; pos_trades_started[i] = 0; neg_trades_started_mean[i] = 0; pos_trades_started_mean[i] = 0; } pos_ret_mean = 0; neg_ret_mean = 0; n_pos_ret = 0; n_neg_ret = 0; try { //PrintWriter overall = new PrintWriter(new FileWriter("performance_total.dat")); PrintWriter max_int = new PrintWriter(new FileWriter("max_interpolation.dat")); PrintWriter b0_coeff = new PrintWriter(new FileWriter("b0_coeff.dat")); PrintWriter perform = new PrintWriter(new FileWriter("intraday_performance.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.dat")); for (file_count = 0; file_count < 1; file_count++) { if (dataFiles[file_count].indexOf("JPY") != -1 && dataFiles[file_count].indexOf("NOKJPY") == -1) { //change_time_zone = true; System.out.println("Changed time zone to Tokyo"); jpy = true; stop_loss_thresh = stop_loss_thresh * 100; take_profit_thresh = take_profit_thresh * 100; } 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; nobs_count = 0; computed = false; while ((strline = br.readLine()) != null) { //System.out.println(strline); tokens = strline.split(delims); n_toks = tokens.length; //System.out.println("Number of toks = "+n_toks); if (n_toks == 0) { System.out.println("End of file"); break; } if (n_toks >= 6) { bid_ask_data = true; } else { bid_ask_data = false; } if (change_time_zone) { String[] dts = tokens[0].split("[ ]+"); String[] ymd = dts[0].split("[-]+"); String[] hmss = dts[1].split("[:]+"); DateTime localTime = new DateTime((new Integer(ymd[0])).intValue(), (new Integer(ymd[1])).intValue(), (new Integer(ymd[2])).intValue(), (new Integer(hmss[0])).intValue(), (new Integer(hmss[1])).intValue(), (new Integer(hmss[2])).intValue(), 0); localTime = localTime.plusHours(13); tokens[0] = localTime.toString(fmt); } 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); //System.out.println(strline + " " + final_trade_time); if (let_shop) { setTimeInterval(insampStart, permEndTrading, final_trade_time); } else { setTimeInterval(insampStart, permEndTrading, permEndTrading); } friday = false; //System.out.println(strline + " " + forex24); if (forex24) //need to change friday trading end time { //System.out.println(weekend.dayOfWeek().getAsText()); if (weekend.dayOfWeek().getAsText().equals("Friday") && !change_time_zone) //change number of trading days { if (min30trade) { setTimeInterval(insampStart, "16:30:00", "16:30:00"); } else { setTimeInterval(insampStart, "16:00:00", "16:00:00"); } friday = true; } else if (weekend.dayOfWeek().getAsText().equals("Saturday") && change_time_zone) { if (min30trade) { setTimeInterval(insampStart, "05:30:00", "05:30:00"); } else { setTimeInterval(insampStart, "05:00:00", "05:00:00"); } friday = true; } else { if (let_shop) { setTimeInterval(insampStart, permEndTrading, final_trade_time); } else { setTimeInterval(insampStart, permEndTrading, permEndTrading); } } //System.out.println(startingTime + " " + insampStart + " " + endingTime + " " + final_trade_time + " " + start_seq + " " + trade_obs); } //System.out.println(startingTime + " " + insampStart + " " + endingTime + " " + start_seq + " " + trade_obs); if (date_stamp.indexOf(insampStart) != -1) { 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]; } //print_filter = true; latestDates.add(date_stamp); //use only iqfeed data for insample, use ib for out-of-sample //strategy is as follows 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])); } spreadnow = ask.get(ask.size() - 1) - bid.get(bid.size() - 1); //System.out.println(date_stamp + " spread = " + spreadnow); if ((date_stamp.indexOf(startingTime) != -1) && 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(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 ((date_stamp.indexOf(startingTime) != -1) && 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(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 if ((date_stamp.indexOf(startingTime) == -1) && 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 ((date_stamp.indexOf(startingTime) == -1) && 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])) + spreadnow/2.0); hi_price.add((new Double(tokens[8])) - spreadnow/2.0); if (ib_data && ib_data_hash.containsKey(tokens[0])) //use as is { lo_price.add(new Double(tokens[7])); hi_price.add(new Double(tokens[8])); } else { lo_price.add((new Double(tokens[7])) + spreadnow); 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); } } //System.out.println(nobs_count + " " + n_obs); if (nobs_count >= n_obs) { computed = true; //---- collect data and put in time series if (daily_strategy) { trading_hours = true; } if (date_stamp.indexOf(insampStart) != -1 || trading_hours) //start at 9:30 { // if(print_debug)System.out.println("Start the day trading"); // System.out.println("Starting TIME = " + startingTime); //System.out.println("ENDING TIME = " + endingTime + " " + finalCall); if (trade_count < trade_obs) { //--- new series //System.out.println("trade count = " + trade_count + " " + trade_obs); n_out_samp++; trading_hours = true; tseries = new double[n_rep * n_obs]; out_series = new double[n_obs]; if (!bid_ask_data) { if (n_rep == 3) { for (i = 0; i < n_obs; i++) { tseries[n_obs - 1 - i] = close_series.get(close_series.size() - 1 - i); if (n_rep > 2 && exp_series_1.size() > 0) { tseries[n_obs + n_obs - 1 - i] = exp_series_1 .get(exp_series_1.size() - 1 - i); } if (exp_series_2.size() > 0) { tseries[n_obs * 2 + n_obs - 1 - i] = exp_series_2 .get(exp_series_2.size() - 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); } } } } else { 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]; } } } //---- set new time series------------ // if((gaussianize || volatility_thresh) || save_volatility) // { // if(date_stamp.indexOf(startingTime) != -1) // { // gaussianizeData(.9,.01,2.0); // morningSeries = new double[n_rep*n_obs]; // System.arraycopy(tseries,0,morningSeries,0,morningSeries.length); // computePeriodogram(); // } // else // { // updateGaussianData(); // gauss_target.add(tseries[n_obs-1]); // if(n_rep > 2) // { // gauss_1.add(tseries[n_obs*2 + n_obs-1]); // gauss_2.add(tseries[n_obs*3 + n_obs-1]); // } // for(i=0;i<trade_count;i++) // { // tseries[n_obs-1-i] = gauss_target.get(gauss_target.size()-1-i); // tseries[n_obs + n_obs - 1 - i] = tseries[n_obs-1-i]; // if(n_rep > 2) // {tseries[n_obs*2 + n_obs-1 - i] = gauss_1.get(gauss_1.size()-1-i);} // if(n_rep > 3) // {tseries[n_obs*3 + n_obs-1 - i] = gauss_2.get(gauss_2.size()-1-i);} // } // // //System.out.println(date_stamp + " " + tseries[n_obs-1] + " " + tseries[n_obs*2 + n_obs-1] + " " + tseries[n_obs*3 + n_obs-1]); // // } // avg_vol = avg_vol + vol[vol.length-1]; // } mdfa.set_tseries(tseries, n_obs, n_rep); //--- compute new filter -------- //if(pulse_count >= recomp_pulse || (date_stamp.indexOf(startingTime) != -1)) if (date_stamp.indexOf(insampStart) != -1 && day_count == 0) { // System.out.println(date_stamp + " " + insampStart + " " + startingTime + " " + tseries[n_obs-1] + " " + tseries[n_obs - 2] + " " + tseries[n_obs-3] + " " + tseries[n_obs-4] + " " // + price.get(price.size()-1)); if (!useH0) //general filtering { //if(print_debug) System.out.println(date_stamp); // if(date_stamp.indexOf("2013-04-03") != -1 || date_stamp.indexOf("2013-04-02") != -1) // { // System.out.println(date_stamp); // System.out.println("AT THE END!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); // } mdfa.computeFilterGeneral(true, print_filter); b_coeffs = new double[(n_rep - 1) * L]; //System.out.println(b_coeffs.length + " " + L + n_rep); for (l = 0; l < L; l++) { for (i = 0; i < n_rep - 1; i++) { b_coeffs[L * i + l] = mdfa.b[L * (i + 1) + l]; } // System.out.println(b_coeffs[l]);} //if(date_stamp.indexOf("2013-12-17") != -1) {System.out.println(b_coeffs[l]);} } //if(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]); b0_sum = 0; for (l = 0; l < L; l++) { b0_sum = b0_sum + b_coeffs[l]; } b0_trend.add(b0_sum); // if(filters.size() > 5) // { // System.out.println("Old BO = " + b_coeffs[0] + " " + b_coeffs[L] + " " + b_coeffs[L+L]); // averageFilters(3,.3); // System.out.println("Avg BO = " + b_coeffs[0] + " " + b_coeffs[L] + " " + b_coeffs[L+L]); // } } else { if (H0set) //H0_Optimization_Lookback2(double delta, double interp_start) { if (!lookback_ready) { H0_Optimization(h0_outsamp, localdecay, h0_delta, interp_start); avg_volatility.add(0.0); System.out.println("First"); } //else{H0_Optimization_Lookback(h0_delta, 0.0); max_int.println(max_interp_value + " " + max_ret_interp + " " + ROI + " " + date_stamp);} else { //if(morning_optimize) //{H0_Optimization(h0_outsamp, localdecay, h0_delta, interp_start);} //else //{ H0_Optimization_Lookback(h0_delta, 0); //H0_Optimization_Lookback_Test(yesterday_series, h0_delta, 0.0, optval, cut); //max_int.println(max_interp_value + " " + optval + " " + max_ret_interp + " " + zero_ret); max_int.println(max_interp_value + " " + max_ret_interp); //System.out.println("New BO = " + b_coeffs[0] + " " + b_coeffs[L] + " " + b_coeffs[L+L]); //} } //H0_Optimization_Morning(localdecay, h0_delta, interp_start); //max_int.println(max_interp_value + " " + max_ret_interp); pulse_count = 0; } else { System.out.println("Problem: Couldn't find H0 filter"); break; } } } //System.out.println("Out of sample " + date_stamp); //else {pulse_count++;} //---- change filter to updated filter -------------------------------- if ((morning_optimize && (date_stamp.indexOf(optimizeTime) != -1)) && lookback_ready) { System.out.println("Optimizing at 12:30"); advance = 13; // !!!! Caution, this value must correspond with the optimizeTime (e.g. 8 = 11:30) //optval = max_interp_value; H0_Optimization_Intraday(h0_delta, 0.0, advance); //find optimized parameter at new time // max_int.println(max_interp_value + " " + optval); } if (date_stamp.indexOf("09:30") != -1) { reg_trading_hours = true; } //start_seq = 10; if (pulse_count < recomp_pulse && reg_trading_hours) { if (H0set && (pulse_count % start_seq == 0) && pulse_count > 0) //5 is best //if(H0set) { H0_Optimization_Update(0, trade_count, true, true); } pulse_count++; } else if (pulse_count < recomp_pulse && date_stamp.indexOf("08:30") != -1) { System.out.println("Starting out for the day"); double[] outs = computeInterpolation(tseries, 0); int lag4 = 2 * (n_obs - L + 1) + 2 * (n_rep + 1) * K1; b_coeffs = new double[(n_rep - 1) * L]; for (l = 0; l < L; l++) { for (i = 0; i < n_rep - 1; i++) { b_coeffs[L * i + l] = outs[lag4 + L * (i + 1) + l]; } } pulse_count = 0; } //---- 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(""); prev_signal = current_signal; current_signal = sum; if (sig_inverse) { current_signal = -current_signal; } if (morning_optimize && (date_stamp.indexOf(optimizeTime) != -1)) { if ((prev_signal > 0 && current_signal < 0) || (prev_signal < 0 && current_signal > 0)) { System.out.println( "Sign of signal changed after optimization, forced to enter transaction."); changed_signs++; } } if (cont_lookback) //compute previous lag { if (compute_lag) { mdfa.set_lag(lag + 1); mdfa.computeFilterGeneral(true, false); b_lag = new double[(n_rep - 1) * L]; for (l = 0; l < L; l++) { for (i = 0; i < n_rep - 1; i++) { b_lag[L * i + l] = mdfa.b[L * (i + 1) + l]; } } sum = 0.0; for (j = 1; j < n_rep; j++) { for (l = 0; l < L; l++) { sum = sum + b_lag[L * (j - 1) + l] * tseries[N * j + n_obs - 1 - l]; } } lag_signal = sum; } else { 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 - 2 - l]; } } lag_signal = sum; } } if (adaptive_filtering) { //------------------- Adaptive steps -------------------------------------------- if (adapt_pulse_count >= adaptive_recomp_pulse || (date_stamp.indexOf(startingTime) != -1)) { adaptiveUpdateFilter(); updateAdaptiveSignal(); adapt_pulse_count = 0; } else { updateAdaptiveSignal(); adapt_pulse_count++; } current_signal = update_signal[update_signal.length - 1]; lag_signal = update_signal[update_signal.length - 2]; } // if(print_debug) // { // System.out.println(date_stamp + ", " + formatter.format(price.get(price.size()-1)) + ", " + formatter.format(tseries[n_obs-1]) + ", " + formatter.format(current_signal)); // } // if(!forex24) {dailyReport.add(""+date_stamp + ", " + formatter2.format(Math.exp(price.get(price.size()-1))) + ", " + formatter.format(tseries[n_obs-1]) + ", " + formatter.format(current_signal));} // else {dailyReport.add(""+date_stamp + ", " + formatter.format(price.get(price.size()-1)) + ", " + formatter.format(tseries[n_obs-1]) + ", " + formatter.format(current_signal));} if (!bid_ask_data) { 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(tseries[n_obs - 1]) + ", " + formatter.format(current_signal)); } else { 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)); } // double binsig = 0; // if(current_signal > 0) binsig = 1; // else binsig = -1; // // dailyReport.add(""+date_stamp + ", " + formatter.format(price.get(price.size()-1)) + ", " + binsig); //System.out.println(dailyReport.size()); 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(); if (trade_count > 0 && cont_lookback) { lag_signals[trade_count - 1] = lag_signal; } trade_count++; } if (date_stamp.indexOf(finalCall) != -1) //if(date_stamp.indexOf(endingTime) != -1) { //System.out.println("ENDING TIME Data at " + endingTime); // for(i=0;i<L;i++) // { // System.out.println(tseries[n_obs-1-i] + " " + tseries[n_obs + n_obs-1 - i] + " " + tseries[2*n_obs + n_obs-1 - i] + " " + tseries[3*n_obs + n_obs-1 - i]); // } if (reset_signal) { current_signal = 0; } current_signal = 0; n_out_samp = 0; reg_trading_hours = false; daily_return = price.get(price.size() - 1).doubleValue() - log_price; log_price = price.get(price.size() - 1).doubleValue(); trade_count = 0; trading_hours = false; pulse_count = 0; // if(startingTime.equals("09:30")) // { actual_price = new double[trade_obs - start_seq]; actual_loprice = new double[trade_obs - start_seq]; actual_hiprice = new double[trade_obs - start_seq]; actual_signal = new double[trade_obs - start_seq]; for (i = 0; i < actual_price.length; i++) { actual_price[i] = prix[i + start_seq]; actual_signal[i] = signal[i + start_seq]; actual_loprice[i] = lo_prix[i + start_seq]; actual_hiprice[i] = hi_prix[i + start_seq]; } /// System.out.println(actual_price[i] + " " + actual_signal[i]);} short_sell = true; long_buy = true; if (!let_shop) { insampleTradingDiff(actual_price, actual_signal, actual_price.length); ROI = account[account.length - 1]; } else { insampleTradingDiff_Cust_SL(actual_price, actual_loprice, actual_hiprice, actual_signal, actual_price.length); ROI = account[end_time_index]; } if (jpy) { ROI = .01 * ROI; } //date_tokens = date_stamp.split("[ ]+"); 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; no_return = true; for (k = 1; k < account.length; k++) { if (k > end_time_index) { break; } if (diff_account[k] != 0) { no_return = false; } if (diff_account[k] > 0) { 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_tokens[0], trade_started, k, .01 * min_pnl_dd, .01 * max_pnl_uu, .01 * diff_account[k])); } else { mdfaTrades.add(new MDFATrade(date_tokens[0], 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) { 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_tokens[0], trade_started, k, .01 * min_pnl_dd, .01 * max_pnl_uu, .01 * diff_account[k])); } else { mdfaTrades.add(new MDFATrade(date_tokens[0], 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; } } //the day had no action, so place an empty trade if (no_return) { mdfaTrades.add(new MDFATrade(date_tokens[0], 0, end_time_index, 0, 0, .00001)); } if (print_debug) { System.out.println(""); for (k = 0; k < account.length; k++) { System.out.println(dailyReport.get(k + start_seq) + ", " + formatter.format(account[k]) + ", " + formatter.format(pnl[k]) + ", " + formatter.format(png[k])); } } all_trades = total_trades; all_succ_trades = succ_trades; tot_all_trades = tot_all_trades + total_trades; tot_succ_trades = tot_succ_trades + succ_trades; short_sell = false; long_buy = true; //insampleTradingDiff_Cust_SL(actual_price, actual_signal, actual_price.length); //longROI = account[account.length-1]; longROI = account[end_time_index]; short_sell = true; long_buy = false; //insampleTradingDiff_Cust_SL(actual_price, actual_signal, actual_price.length); //shortROI = account[account.length-1]; shortROI = account[end_time_index]; final_trades.add(final_trade); last_trades.add(last_trade); sub_returns.add(diff_band); //I_MDFA.plotData(xt, signal, trade_obs); avg_vol = avg_vol / trade_obs; dailyReport.clear(); mnmx = minmax(account); ratio = ((double) succ_trades) / ((double) total_trades); if (ROI < 0) { losses_in_arow++; } else { losses_in_arow = 0; } if (useH0) { //System.out.println("using H0"); if (lookback_ready) { dailyoutret.add(daily_return); maxIntValue.add(max_interp_value); avg_volatility.add(avg_vol - 0.25); returns.add(ROI - all_trades * tradingCost); longreturns.add(longROI); shortreturns.add(shortROI); dropdowns.add(drop_down); success.add(ratio); trade_days.add(date_tokens[0]); dates_low_high.add(date_stamp + ", " + mnmx[0] + ", " + mnmx[1]); } else { lookback_ready = true; } } else { //System.out.println("Not using H0"); dailyoutret.add(daily_return); maxIntValue.add(max_interp_value); avg_volatility.add(100000.0 * (avg_vol - 0.30)); returns.add(ROI - all_trades * tradingCost); longreturns.add(longROI); shortreturns.add(shortROI); dropdowns.add(drop_down); success.add(ratio); trade_days.add(date_tokens[0]); dates_low_high.add(date_stamp + ", " + mnmx[0] + ", " + mnmx[1]); } if (ROI < 0 && first_trade_loss) { bad_starts++; } if (mnmx[1] > .005) { profit_baby++; } //out.println("Result for " + date_stamp + ", ROI = " + ROI + ", " + mnmx[0] + ", " + mnmx[1] + ", " + succ_trades + ", " + total_trades); if (print_debug) System.out.println("Result for " + date_stamp + ", ROI = " + (ROI - all_trades * tradingCost) + ", succ_trades = " + all_succ_trades + ", total_trades = " + all_trades); // if(returns.size() > rolling_length) //take latest 30 days and compute a few stats // { // double[] latRets = new double[rolling_length]; // Double[] latRetsD = new Double[rolling_length]; // for(k=0;k<rolling_length;k++) // { // latRets[rolling_length - 1 - k] = returns.get(returns.size() - 1 - k); // latRetsD[rolling_length - 1 - k] = returns.get(returns.size() - 1 - k); // } // //compute sharpe // double[] mstd = mean_std(latRets); // double sh = Math.sqrt(250)*mstd[0]/mstd[1]; // //compute rank // double rc = rankCoefficient(latRets,rolling_length); // //compute Ulcer // double ui = ulcerIndex(latRetsD); // //computer Kelly // // rolling_ind.add(sh + " " + rc + " " + ui + " " + kp); // } // else // {rolling_ind.add("0 0 0 0");} dailyout.println(daily_return); signal = new double[trade_obs]; xt = new double[trade_obs]; prix = new double[trade_obs]; lag_signals = new double[trade_obs]; avg_vol = 0.0; total_ROI = total_ROI + ROI - all_trades * tradingCost; total_succ = total_succ + succ_trades; total = total + total_trades; //System.out.println("total trades = " + total_succ + "/" + total); if (ROI > 0) { trade_succ_ratio++; } yesterday_vol = 0;//avg_volatility.get(avg_volatility.size() - 2); if (ROI > .006)// && (avg_volatility.get(avg_volatility.size() - 2) < 4.007053e-07 )) { avg_count++; for (k = 1; k < n_rep; k++) { for (l = 0; l < L; l++) { b_avg[L * k + l] = b_avg[L * k + l] + b_coeffs[L * (k - 1) + l]; } } } if (day_count == recompute_day) { day_count = 0; } else { day_count++; } } } } nobs_count++; //System.out.println(nobs_count); } //out.println("Total ROI: " + total_ROI + ", Total successful trades = " + succ_trades + ", total trades = " + total_trades + ", rate = " + (double)succ_trades/(double)total_trades); System.out.println("Total ROI: " + total_ROI + ", Total successful trades = " + tot_succ_trades + ", total trades = " + tot_all_trades + ", avg_n_trades = " + (double) tot_all_trades / returns.size() + ", rate = " + (double) tot_succ_trades / (double) tot_all_trades); //System.out.println("Avg Friday ROI = " + (fridayROI/fridays) + ", friday success = " + ((double)fridayROI_pos/fridays)); avg_n_trades = (double) tot_all_trades / (double) returns.size(); avg_rate = (double) tot_succ_trades / (double) tot_all_trades; dailyoutret.set(0, 0.0); if (!H0set || recomp_pulse > 0) { System.out.println("Summing statistics"); num_trade_days = returns.size(); if (num_trade_days == 0) { for (i = 0; i < 100; i++) returns.add(0.0); } num_trade_days = returns.size(); mean = 0.0; num_gains = 0; num_losses = 0; dreturns = new double[num_trade_days]; //double ret = returns.get(0); double ret = 0; if (num_trade_days > 0) dreturns[0] = 0; for (i = 0; i < num_trade_days; i++) { ret = returns.get(i); if (ret > 0) { num_gains++; } else { num_losses++; } mean = mean + ret; out.println(ret); if (i > 0) dreturns[i] = dreturns[i - 1] + ret; } mean = mean / ((double) returns.size()); 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); sum_sd = 0; for (i = 0; i < returns.size(); i++) { sum_sd = sum_sd + (returns.get(i) - mean) * (returns.get(i) - mean) / ((double) returns.size()); } standard_deviation = Math.sqrt(sum_sd); } else { num_trade_days = lookback_returns.size(); mean = 0.0; num_gains = 0; num_losses = 0; dreturns = new double[num_trade_days]; double ret = lookback_returns.get(0); dreturns[0] = ret; for (i = 0; i < num_trade_days; i++) { ret = lookback_returns.get(i); if (ret > 0) { num_gains++; } else { num_losses++; } mean = mean + ret; out.println(ret); if (i > 0) dreturns[i] = dreturns[i - 1] + ret; } mean = mean / ((double) lookback_returns.size()); sum_sd = 0; for (i = 0; i < lookback_returns.size(); i++) { sum_sd = sum_sd + (lookback_returns.get(i) - mean) * (lookback_returns.get(i) - mean) / ((double) lookback_returns.size()); } standard_deviation = Math.sqrt(sum_sd); } out.println(""); /* for(i=0;i<num_trade_days;i++) { out.println(dreturns[i] + " " + dailyoutret.get(i)); System.out.println(dreturns[i] + " " + avg_volatility.get(i)); }*/ maxdraw = computeDrawdown(dreturns); //rankCoefficient(dreturns, num_trade_days); rank_coeff = segmentRankCorrelation(15, dreturns); out.println("Rank Coefficient = " + rank_coeff + ", mean = " + mean); System.out.println("Rank Coefficient = " + rank_coeff + ", mean = " + mean); mean_perf = mean; rank_perf = rank_coeff; } for (i = 0; i < svm.size(); i++) { svmout.println(svm.get(i)); } out.close(); dailyout.close(); perform.close(); b0_coeff.close(); max_int.close(); svmout.close(); spread.close(); } catch (FileNotFoundException fe) { System.out.println("File not found..." + fe); } catch (IOException ioe) { System.out.println("IO procedure faulty..." + ioe); } total_obs = returns.size(); if (trade_days.size() < total_obs) { total_obs = trade_days.size(); } for (i = 0; i < total_obs; i++) { //System.out.println(trade_days.get(i) + " " + returns.get(i) + " " + avg_volatility.get(i) + " " + maxIntValue.get(i) + " " + interp_vals.get(i) + crits.get(i)); if (!morning_optimize) { System.out.println(trade_days.get(i) + ", " + returns.get(i)); date_returns.add(trade_days.get(i) + ", " + dailyoutret.get(i)); } else { System.out.println(trade_days.get(i) + ", " + returns.get(i) + ", " + interp_vals.get(i) + ", " + max_ranks.get(i)); } } for (i = 0; i < morning_returns.size(); i++) { double[] array = morning_returns.get(i); for (j = 0; j < array.length - 1; j++) { System.out.print(df4.format(array[j]) + " "); } System.out.println(df4.format(array[array.length - 1])); } if (!morning_optimize) { for (i = 0; i < crits.size(); i++) { System.out.println(crits.get(i)); } } System.out.println(avg_count); for (k = 1; k < n_rep; k++) { for (l = 0; l < L; l++) { b_avg[L * k + l] = b_avg[L * k + l] / (double) avg_count; } } try { //PrintWriter overall = new PrintWriter(new FileWriter("performance_total.dat")); PrintWriter b0_coeff = new PrintWriter(new FileWriter("h0b0_filter.dat")); b0_coeff.println(L + " " + n_rep); for (l = 0; l < L; l++) { for (k = 0; k < n_rep - 1; k++) { b0_coeff.print(b_avg[L * k + l] + " "); } b0_coeff.println(b_avg[L * (n_rep - 1) + l]); } b0_coeff.close(); } catch (IOException ioe) { System.out.println("IO procedure faulty..." + ioe); } try { PrintWriter ret_dists = new PrintWriter(new FileWriter("return_dists_" + dataFiles[0])); for (l = 1; l < trade_obs; l++) { ret_dists.println((ret_dist[l] / (double) total_obs) + " " + (pos_ret_dist[l] / (double) (pos_ret_dist[l] + neg_ret_dist[l])) + " " + (pos_ret_mean_time[l] / (double) pos_ret_dist[l]) + " " + (neg_ret_mean_time[l] / (double) neg_ret_dist[l])); if (pos_trades_started[l - 1] == 0 && neg_trades_started[l - 1] == 0) { System.out.println("0 0 0"); } else { System.out.println(formatter .format((pos_trades_started[l - 1] / (double) (pos_trades_started[l - 1] + neg_trades_started[l - 1]))) + " " + formatter.format(pos_trades_started_mean[l - 1] / (double) pos_trades_started[l - 1]) + " " + formatter .format(neg_trades_started_mean[l - 1] / (double) neg_trades_started[l - 1])); } } ret_dists.close(); } catch (IOException ioe) { System.out.println("IO procedure faulty..." + ioe); } for (i = 0; i < full_returns_array.size(); i++) { double[] array = full_returns_array.get(i); for (j = 0; j < array.length - 1; j++) { System.out.print(df4.format(array[j]) + " "); } System.out.println(df4.format(array[array.length - 1])); } if (morning_optimize) { System.out.println("Signal changed signs at " + optimizeTime + " " + changed_signs + " times"); } 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;/*ww w .ja v a 2s . 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>(); 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;/*from w w w .j av a2 s. c om*/ 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; }
From source file:ch.imetrica.mdfaTradingStrategies.MDFAStrategyEvolution.java
public boolean startStrategyDailyIntradayOneHour() { int j, i, l, N, file_count; double sum = 0; Double D;/*from w ww. j a v a 2 s . c o m*/ String ddelims = "[-]"; boolean computed = false; boolean print_filter = false; boolean made_trade = true; String date_stamp, strline; int daily_size; double profit, price_borrowed, price_sold, price_bought; double current_price, prev_price; double last_price, cur_pnl, stop_loss, lo_pnl, hi_pnl; double log_ret = 0; signal = new double[trade_obs]; xt = new double[trade_obs]; lag_signals = new double[trade_obs]; prix = new double[trade_obs]; lo_prix = new double[trade_obs]; hi_prix = new double[trade_obs]; total_succ = 0; total = 0; log_price = 0; N = n_obs; avg_vol = 0.0; b_avg = new double[L * n_rep]; count = 0; trade_succ_ratio = 0; double amount = 0; double prev_signal; reg_trading_hours = false; String[] intdates; //make sure arraylists empty ArrayList<String> perf_dates = new ArrayList<String>(); ArrayList<Double> perf_returns = new ArrayList<Double>(); double pnl; String[] dates; boolean inverse_hours = false; String time; ArrayList<String> account = new ArrayList<String>(); ArrayList<String> sunday = new ArrayList<String>(); ArrayList<String> latestDates = new ArrayList<String>(); last_trades = new ArrayList<Integer>(); final_trades = new ArrayList<Double>(); dailyoutret = new ArrayList<Double>(); maxIntValue = new ArrayList<Double>(); avg_volatility = new ArrayList<Double>(); close_series = new ArrayList<Double>(); highlow_series = new ArrayList<Double>(); exp_series_1 = new ArrayList<Double>(); exp_series_2 = new ArrayList<Double>(); price = new ArrayList<Double>(); lo_price = new ArrayList<Double>(); hi_price = new ArrayList<Double>(); mid = new ArrayList<Double>(); bid = new ArrayList<Double>(); ask = new ArrayList<Double>(); dates_series = new ArrayList<String>(); dailyReport = new ArrayList<String>(); b0_trend = new ArrayList<Double>(); vol_0 = new ArrayList<Double>(); vol_1 = new ArrayList<Double>(); sub_returns = new ArrayList<Double>(); trade_days = new ArrayList<String>(); returns = new ArrayList<Double>(); longreturns = new ArrayList<Double>(); shortreturns = new ArrayList<Double>(); dropdowns = new ArrayList<Double>(); success = new ArrayList<Double>(); dates_low_high = new ArrayList<String>(); crits = new ArrayList<String>(); svm = new ArrayList<String>(); filters = new ArrayList<Filter>(); date_returns = new ArrayList<String>(); live_series = new ArrayList<Double>(); //the data to be applied out of sample ib_data_hash = new ibHash(); fridayROI = 0; fridayROI_pos = 0; fridays = 0; int end_hour; lookback_returns = new ArrayList<Double>(); num_pos_returns = 0; deg_0 = new ArrayList<Double>(); deg_1 = new ArrayList<Double>(); crit_0 = new ArrayList<Double>(); crit_1 = new ArrayList<Double>(); full_returns_array = new ArrayList<double[]>(); morning_returns = new ArrayList<double[]>(); morning_buy = true; //enter transaction at morning open morning_optimize = false; //optimize in the morning trading hours num_full_positive_returns = 0; //--- Now get historical interp values ------ //uploadInterpParams("max_int.dat"); //------------------------------------------- forex24 = true; ret_dist = new double[trade_obs]; pos_ret_dist = new int[trade_obs]; neg_ret_dist = new int[trade_obs]; neg_trades_started = new int[trade_obs]; pos_trades_started = new int[trade_obs]; neg_trades_started_mean = new double[trade_obs]; pos_trades_started_mean = new double[trade_obs]; diff_account = new double[trade_obs]; pos_ret_mean_time = new double[trade_obs]; neg_ret_mean_time = new double[trade_obs]; mdfaTrades = new ArrayList<MDFATrade>(); fmt = DateTimeFormat.forPattern("y-MM-dd HH:mm:ss"); formatter = new DecimalFormat("#0.000000"); formatter3 = new DecimalFormat("#0.00000"); formatter2 = new DecimalFormat("#0.00"); histo_stat = new int[100]; interp_vals = new ArrayList<Double>(); max_ranks = new ArrayList<Double>(); profit_baby = 0; //setForecastDFAParameters(); bad_starts = 0; n_out_samp = 0; //take_profit = true; //take_profit_thresh = .0020; current_signal = 0; prev_price = 0; cur_pnl = 0; stop_loss = stop_loss_thresh; out_transaction = 0; in_transaction = 0; red_zone = false; global_stop_loss = stop_loss_thresh; profitable_stop = .0005; count = 0; short_sell = true; long_buy = true; day_count = 0; ArrayList<String> trade_times = new ArrayList<String>(); lo_pnl = 0; hi_pnl = 0; price_borrowed = 0; price_sold = 0; price_bought = 0; last_price = 0; binary_rule = true; signal_strength_rule = true; downtick_strategy = false; signal_profit = false; friday_closing = false; //asian_close = true; //----- Fill up with times here ------------------------- for (i = 0; i < 10; i++) { trade_times.add("0" + i + ":00:00"); } for (i = 10; i < 24; i++) { trade_times.add(i + ":00:00"); } // trade_times.add("00:00:00"); // trade_times.add("06:00:00"); // trade_times.add("12:00:00"); // trade_times.add("18:00:00"); String[] hourToks = startingTime.split("[:]+"); start_hour = (new Integer(hourToks[0])).intValue(); hourToks = endingTime.split("[:]+"); end_hour = (new Integer(hourToks[0])).intValue(); inverse_hours = false; if (start_hour > end_hour) //must switch hours here { int temphour = end_hour; inverse_hours = true; end_hour = start_hour; start_hour = temphour; } if (ib_data && ib_data_file != null) { try { fin = new FileInputStream(ib_data_file); din = new DataInputStream(fin); br = new BufferedReader(new InputStreamReader(din)); while ((strline = br.readLine()) != null) { String[] sp = strline.split("[,]+"); ib_data_hash.put(sp[0], new String( sp[1] + " " + sp[2] + " " + sp[3] + " " + sp[4] + " " + sp[5] + " " + sp[6])); //System.out.println(sp[1] + " " + sp[2] + " " + sp[3] + " " + sp[4] + " " + sp[5] + " " + sp[6]); } } catch (FileNotFoundException fe) { System.out.println("File not found..." + fe); } catch (IOException ioe) { System.out.println("IO procedure faulty..." + ioe); } } String[] myname = dataFiles[0].split("[.]+"); try { PrintWriter b0_coeff = new PrintWriter(new FileWriter("b0_coeff.dat")); PrintWriter perform = new PrintWriter(new FileWriter("intraday_performance_" + n_files + ".dat")); PrintWriter dailyout = new PrintWriter(new FileWriter("daily_nasdaq.dat")); PrintWriter out = new PrintWriter(new FileWriter("strategy_results_" + myname[0] + ".dat")); PrintWriter svmout = new PrintWriter(new FileWriter("neural.dat")); for (file_count = 0; file_count < 1; file_count++) { if (dataFiles[file_count].indexOf("JPY") != -1 || dataFiles[file_count].indexOf("Y") != -1) { //change_time_zone = true; System.out.println("Changed time zone to Tokyo"); jpy = true; stop_loss_thresh = stop_loss_thresh * 100; take_profit_thresh = take_profit_thresh * 100; global_stop_loss = global_stop_loss * 100; stop_loss = stop_loss_thresh; } else if (futures_data) { //change_time_zone = true; System.out.println("Changed time zone to Tokyo"); stop_loss_thresh = stop_loss_thresh * 10000; stop_loss = stop_loss_thresh; take_profit_thresh = take_profit_thresh * 10000; global_stop_loss = global_stop_loss * 10000; } else if (dataFiles[file_count].indexOf("MXN") != -1) { stop_loss_thresh = stop_loss_thresh * 10; take_profit_thresh = take_profit_thresh * 10; global_stop_loss = global_stop_loss * 10; stop_loss = stop_loss_thresh; } else if (dataFiles[file_count].indexOf("HKD") != -1) { stop_loss_thresh = stop_loss_thresh * 10; take_profit_thresh = take_profit_thresh * 10; global_stop_loss = global_stop_loss * 10; stop_loss = stop_loss_thresh; } else if (dataFiles[file_count].indexOf("XAU") != -1) { stop_loss_thresh = stop_loss_thresh * 1000; take_profit_thresh = take_profit_thresh * 1000; global_stop_loss = global_stop_loss * 1000; stop_loss = stop_loss_thresh; } else if (dataFiles[file_count].indexOf("GC") != -1) { stop_loss_thresh = stop_loss_thresh * 1000; take_profit_thresh = take_profit_thresh * 1000; global_stop_loss = global_stop_loss * 1000; stop_loss = stop_loss_thresh; } else if (dataFiles[file_count].indexOf("JNJ") != -1) { stop_loss_thresh = stop_loss_thresh * 100; take_profit_thresh = take_profit_thresh * 100; global_stop_loss = global_stop_loss * 100; stop_loss = stop_loss_thresh; } setTimeStandards(new File(dataFiles[file_count])); System.out.println("opening " + dataFiles[file_count]); fin = new FileInputStream(dataFiles[file_count]); din = new DataInputStream(fin); br = new BufferedReader(new InputStreamReader(din)); lookback_ready = false; spread = new PrintWriter(new FileWriter("spread_" + dataFiles[file_count] + ".dat")); //if(print_debug)System.out.println("Entering loop..."); trading_hours = false; computed = false; while ((strline = br.readLine()) != null) { //System.out.println(strline); tokens = strline.split(delims); n_toks = tokens.length; //System.out.println("Number of toks = "+n_toks); if (n_toks == 0) { System.out.println("End of file"); break; } if (n_toks >= 6) { bid_ask_data = true; } else { bid_ask_data = false; } date_stamp = tokens[0]; date_tokens = date_stamp.split(date_delims); intdates = date_tokens[0].split(ddelims); DateTime weekend = new DateTime((new Integer(intdates[0])).intValue(), (new Integer(intdates[1])).intValue(), (new Integer(intdates[2])).intValue(), 14, 0); time = date_tokens[1]; //insampStart is the time we collect daily data //if(date_stamp.indexOf(insampStart) != -1) if (trade_times.contains(time)) { //get bid/mid/ask data if (ib_data && ib_data_hash.containsKey(tokens[0])) { String[] hashed = ib_data_hash.get(tokens[0]).split("[ ]+"); for (i = 1; i < hashed.length; i++) { tokens[i] = hashed[i]; } // System.out.print(tokens[i] + " ");} } daily_price.add(new Double(tokens[1])); current_price = (new Double(tokens[1])).doubleValue(); if (daily_price.size() == 1) { daily_returns.add(new Double(0.0)); prev_price = current_price; } else { daily_returns.add(log(current_price) - log(prev_price)); prev_price = current_price; } daily_dates.add(date_stamp); } print_filter = false; latestDates.add(date_stamp); D = new Double(tokens[4]); close_series.add(D); if (ib_data && ib_data_hash.containsKey(tokens[0])) { String[] hashed = ib_data_hash.get(tokens[0]).split("[ ]+"); //System.out.println("Contains " + tokens[0] + ", lengths = " + hashed.length + ", " + tokens.length); for (i = 1; i < hashed.length; i++) { tokens[i] = hashed[i]; } // System.out.print(tokens[i] + " ");} bid.add(new Double(tokens[2])); ask.add(new Double(tokens[3])); mid.add(new Double(tokens[1])); } else { bid.add(new Double(tokens[2])); ask.add(new Double(tokens[3])); mid.add(new Double(tokens[1])); } D = new Double(tokens[1]); price.add(D); D = new Double(tokens[4]); if (ib_data && ib_data_hash.containsKey(tokens[0])) { live_series.add(log(mid.get(mid.size() - 1)) - log(mid.get(mid.size() - 2))); } else { live_series.add(D); } if (ib_data && ib_data_hash.containsKey(tokens[0])) //use as is { lo_price.add(new Double(tokens[7])); hi_price.add(new Double(tokens[8])); } else { lo_price.add((new Double(tokens[7]))); hi_price.add((new Double(tokens[8]))); } //---- start the account ------ if (account.size() == 0) { account.add(date_stamp + " " + 0); dailyoutret.add(0.0); } String[] hours = time.split("[:]+"); cur_hour = (new Integer(hours[0])).intValue(); (new Integer(hours[1])).intValue(); trading_closed = false; //if currently not in a transaction and between the hours of midnight and start-hour, then no new //positions will be opened if (asian_close) //only closed if most recent transaction was closed artificially through SL or TP after end hours { if ((in_transaction == 0 && out_transaction == 0) && cur_hour >= start_hour && cur_hour <= end_hour) { trading_closed = true; if (printall) System.out.println(cur_hour + " " + start_hour); } } else { if (cur_hour >= start_hour && cur_hour <= end_hour) { trading_closed = true; if (printall) { System.out.println(cur_hour + " " + start_hour); } } } // if(cur_hour == 14) {release_first = true;} // else {elease_first = false;} if (inverse_hours) { trading_closed = !trading_closed; } its_closing_time = (weekend.dayOfWeek().getAsText().equals("Friday") && date_stamp.indexOf("17:00:00") != -1); made_trade = false; if (daily_returns.size() >= n_obs && trade_times.contains(time)) //a new day begineth { computed = true; trading_hours = true; tseries = new double[n_rep * n_obs]; for (i = 0; i < n_obs; i++) { tseries[n_obs - 1 - i] = daily_returns.get(daily_returns.size() - 1 - i); tseries[n_obs + n_obs - 1 - i] = daily_returns.get(daily_returns.size() - 1 - i); if (n_rep > 2 && exp_series_1.size() > 0) { tseries[n_obs * 2 + n_obs - 1 - i] = exp_series_1.get(exp_series_1.size() - 1 - i); } if (n_rep > 3 && exp_series_2.size() > 0) { tseries[n_obs * 3 + n_obs - 1 - i] = exp_series_2.get(exp_series_2.size() - 1 - i); } } mdfa.set_tseries(tseries, n_obs, n_rep); //if(day_count == 0) //recompute filter coefficients if (day_count == 0 || weekend.dayOfWeek().getAsText().equals("Sunday") && date_stamp.indexOf("18:00:00") != -1) { if (printall) System.out.println("Recomputing filter..."); mdfa.computeFilterGeneral(true, print_filter); b_coeffs = new double[(n_rep - 1) * L]; //System.out.println(b_coeffs.length + " " + L + n_rep); for (l = 0; l < L; l++) { for (i = 0; i < n_rep - 1; i++) { b_coeffs[L * i + l] = mdfa.b[L * (i + 1) + l]; } // System.out.println(b_coeffs[l]);} //if(date_stamp.indexOf("2013-12-17") != -1) {System.out.println(b_coeffs[l]);} } if (printall) System.out.println(date_stamp + " b_coeffs = " + b_coeffs[0] + " " + b_coeffs[1] + " " + b_coeffs[2]); b_copy = new double[mdfa.b.length]; System.arraycopy(mdfa.b, 0, b_copy, 0, b_copy.length); b0_coeff.println(b_coeffs[0]); // + ", " + b_coeffs[L] + ", " + b_coeffs[2*L]); } sum = 0.0; for (j = 1; j < n_rep; j++) { for (l = 0; l < L; l++) { sum = sum + b_coeffs[L * (j - 1) + l] * tseries[N * j + n_obs - 1 - l]; } } prev_signal = current_signal; current_signal = sum; if (sig_inverse) { current_signal = -current_signal; } if (date_stamp.indexOf("16:00:00") != -1) { svmout.println( date_stamp + " " + formatter3.format(daily_price.get(daily_price.size() - 1))); } //----final signal --- daily_signal.add(current_signal); daily_size = daily_price.size(); dailyReport.add( "New day " + date_stamp + ", " + formatter3.format(daily_price.get(daily_size - 1)) + ", " + formatter.format(tseries[n_obs - 1]) + ", " + current_signal); //if(printall) System.out.println("New day "+date_stamp + ", " + formatter3.format(daily_price.get(daily_size-1)) + ", " + formatter.format(tseries[n_obs-1]) + ", " + current_signal); //--compute binary trading rule --- //if(printall) System.out.println("Current signal = " + current_signal + " Prev signal = " + prev_signal + ", trading_closed = " + trading_closed); if (friday_closing && its_closing_time) { if (printall) System.out.println("\nIt's Friday at 5pm, time to close shop for week"); if (current_signal > 0 && in_transaction == 1) //in a long transaction { price_sold = daily_price.get(daily_size - 1); profit = price_sold - price_bought; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1), 4); //account.add(date_stamp + " " + (amount + profit)); account.add(date_stamp + " " + daily_price.get(daily_price.size() - 1) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); amount = getAmount(account.get(account.size() - 1), 4); if (weekend.dayOfWeek().getAsText().equals("Sunday")) { sunday.add(date_stamp + " " + profit); } made_trade = true; dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); in_transaction = 0; if (printall) { if (profit > 0) System.out.println("Sold for a profit of " + profit); else if (profit < 0) System.out.println("Sold for a loss of " + profit); System.out.println("profit, price_bought, price_sold = " + profit + ", " + price_bought + ", " + price_sold); } } else if (current_signal < 0 && out_transaction == 1) //in a short transaction { price_sold = daily_price.get(daily_size - 1); profit = price_borrowed - price_sold; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1), 4); //account.add(date_stamp + " " + (amount + profit)); account.add(date_stamp + " " + daily_price.get(daily_price.size() - 1) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); amount = getAmount(account.get(account.size() - 1), 4); if (weekend.dayOfWeek().getAsText().equals("Sunday")) { sunday.add(date_stamp + " " + profit); } made_trade = true; dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); out_transaction = 0; if (printall) System.out.println("profit, price_borrowed, price_sold = " + profit + ", " + price_borrowed + ", " + price_sold); } } else { //if(binary_rule && !trading_closed) if (binary_rule) { made_trade = false; if (current_signal > 0 && prev_signal <= 0) //new point positive, we see momentum, buy { last_price = daily_price.get(daily_size - 1); if (short_sell && out_transaction == 1) //in a short-sell transaction, sell { price_sold = daily_price.get(daily_size - 1); profit = price_borrowed - price_sold; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1), 4); //account.add(date_stamp + " " + (amount + profit)); account.add(date_stamp + " " + formatter3.format(daily_price.get(daily_price.size() - 1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); amount = getAmount(account.get(account.size() - 1), 4); if (weekend.dayOfWeek().getAsText().equals("Sunday")) { sunday.add(date_stamp + " " + profit); } made_trade = true; dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); out_transaction = 0; if (printall) { if (profit > 0) System.out.println("Sold for a profit of " + profit); else if (profit < 0) System.out.println("Sold for a loss of " + profit); System.out.println("profit, price_borrowed, price_sold = " + profit + ", " + price_borrowed + ", " + price_sold); } } if ((long_buy && in_transaction == 0) && !trading_closed) { profit = 0; price_bought = daily_price.get(daily_size - 1); in_transaction = 1; account.add(date_stamp + " " + formatter3.format(daily_price.get(daily_price.size() - 1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); if (printall) System.out.println("Entered long transaction at " + price_bought); } } else if (current_signal < 0 && prev_signal >= 0) //if in transaction and signal goes below, sell { last_price = daily_price.get(daily_size - 1); if (long_buy && in_transaction == 1) { price_sold = daily_price.get(daily_size - 1); profit = price_sold - price_bought; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1), 4); //account.add(date_stamp + " " + (amount + profit)); account.add(date_stamp + " -" + formatter3.format(daily_price.get(daily_price.size() - 1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); amount = getAmount(account.get(account.size() - 1), 4); if (weekend.dayOfWeek().getAsText().equals("Sunday")) { sunday.add(date_stamp + " " + profit); } made_trade = true; dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); in_transaction = 0; if (printall) { if (profit > 0) System.out.println("Sold for a profit of " + profit); else if (profit < 0) System.out.println("Sold for a loss of " + profit); System.out.println("profit, price_bought, price_sold = " + profit + ", " + price_bought + ", " + price_sold); } } if ((short_sell && out_transaction == 0) && !trading_closed) { profit = 0; price_borrowed = daily_price.get(daily_size - 1); out_transaction = 1; account.add(date_stamp + " -" + formatter3.format(daily_price.get(daily_price.size() - 1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); if (printall) System.out.println("Entered short transaction at " + price_borrowed); } } } if (signal_strength_rule && ((in_transaction == 0 && out_transaction == 0) && !trading_closed)) { if (current_signal > 0) //new point positive, we see momentum, buy { last_price = daily_price.get(daily_size - 1); if (short_sell && out_transaction == 1) //in a short-sell transaction, sell { price_sold = daily_price.get(daily_size - 1); profit = price_borrowed - price_sold; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1), 4); //account.add(date_stamp + " " + (amount + profit)); account.add(date_stamp + " " + formatter3.format(daily_price.get(daily_price.size() - 1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); amount = getAmount(account.get(account.size() - 1), 4); if (weekend.dayOfWeek().getAsText().equals("Sunday")) { sunday.add(date_stamp + " " + profit); } made_trade = true; dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); out_transaction = 0; if (printall) System.out.println("profit, price_borrowed, price_sold = " + profit + ", " + price_borrowed + ", " + price_sold); } if (long_buy && in_transaction == 0) { profit = 0; price_bought = daily_price.get(daily_size - 1); in_transaction = 1; account.add(date_stamp + " " + formatter3.format(daily_price.get(daily_price.size() - 1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); if (printall) System.out.println("Entered long transaction at " + price_bought); } } else if (current_signal < 0) //if in transaction and signal goes below, sell { last_price = daily_price.get(daily_size - 1); if (long_buy && in_transaction == 1) { price_sold = daily_price.get(daily_size - 1); profit = price_sold - price_bought; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1), 4); //account.add(date_stamp + " " + (amount + profit)); account.add(date_stamp + " -" + formatter3.format(daily_price.get(daily_price.size() - 1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); amount = getAmount(account.get(account.size() - 1), 4); if (weekend.dayOfWeek().getAsText().equals("Sunday")) { sunday.add(date_stamp + " " + profit); } made_trade = true; dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); in_transaction = 0; if (printall) System.out.println("Bought for a profit of " + profit); if (printall) System.out.println("profit, price_bought, price_sold = " + profit + ", " + price_bought + ", " + price_sold); } if (short_sell && out_transaction == 0) { profit = 0; price_borrowed = daily_price.get(daily_size - 1); out_transaction = 1; account.add(date_stamp + " -" + formatter3.format(daily_price.get(daily_price.size() - 1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); if (printall) System.out.println("Entered short transaction at " + price_borrowed); } } } if (!made_trade) { profit = 0; //account.add(date_stamp + " " + formatter3.format(daily_price.get(daily_price.size()-1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); //dailyoutret.add(price.get(price.size()-1) - log_ret); //log_ret = price.get(price.size()-1); } day_count++; //if(recompute_day == day_count) {y_count=0;} if (printall) System.out.println( "" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1)) + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", " + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl)); allsignal.add(date_stamp + " " + current_signal); } } else if (trading_hours)// && !trading_closed)// && cur_min != 30) { if (in_transaction == 1) //in a long transaction { if (red_zone && price.get(price.size() - 1) > last_price) //check if new high price { last_price = price.get(price.size() - 1); } //cur_pnl = price.get(price.size()-1) - last_price; cur_pnl = bid.get(bid.size() - 1) - last_price; lo_pnl = lo_price.get(lo_price.size() - 1) - last_price; hi_pnl = hi_price.get(hi_price.size() - 1) - last_price; if (cur_pnl < -stop_loss) { if (printall) System.out.println( "Stop-loss Activated since lo_pnl = " + cur_pnl + " < -" + stop_loss); //System.out.println("Closing price at bar was " + price.get(price.size()-1) + " but lowest price in bar was " + lo_price.get(lo_price.size()-1)); //--------------sell---------- //price_sold = price.get(price.size()-1); //price_sold = price.get(price.size()-1); price_sold = bid.get(bid.size() - 1); profit = price_sold - price_bought; // price_sold = price_bought - stop_loss; // profit = -stop_loss; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; count = account.size() - 1; amount = getAmount(account.get(count), 4); //account.add(date_stamp + " " + (amount - stop_loss)); //account.add(date_stamp + " " + (amount + profit)); //account.add(date_stamp + " " + price.get(price.size()-1) + " " + profit + " " + (amount + profit)); account.add(date_stamp + " -" + formatter3.format(bid.get(bid.size() - 1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); amount = getAmount(account.get(account.size() - 1), 4); if (weekend.dayOfWeek().getAsText().equals("Sunday")) { sunday.add(date_stamp + " " + profit); } dailyoutret.add(bid.get(bid.size() - 1) - log_ret); log_ret = bid.get(bid.size() - 1); in_transaction = 0; //-- return stop loss to original setting -------- if (printall) System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit); stop_loss = global_stop_loss; red_zone = false; } else if (cur_pnl >= take_profit_thresh) { if (printall) System.out.println("Profit zone activated since cur_pnl = " + cur_pnl + " > " + take_profit_thresh); //price_sold = price.get(price.size()-1); price_sold = bid.get(bid.size() - 1); profit = price_sold - price_bought; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; count = account.size() - 1; amount = getAmount(account.get(count), 4); //account.add(date_stamp + " " + (amount + profit)); //account.add(date_stamp + " " + price.get(price.size()-1) + " " + profit + " " + (amount + profit)); account.add(date_stamp + " -" + formatter3.format(price_sold) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); amount = getAmount(account.get(account.size() - 1), 4); if (weekend.dayOfWeek().getAsText().equals("Sunday")) { sunday.add(date_stamp + " " + profit); } dailyoutret.add(price_sold - log_ret); log_ret = price_sold; in_transaction = 0; //-- return stop loss to original setting -------- if (printall) System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit); stop_loss = global_stop_loss; // stop_loss = profitable_stop; // last_price = price.get(price.size()-1); // red_zone = true; } } else if (out_transaction == 1) { if (red_zone && price.get(price.size() - 1) < last_price) //check if new high price { last_price = price.get(price.size() - 1); } //cur_pnl = last_price - price.get(price.size()-1); cur_pnl = last_price - ask.get(ask.size() - 1); lo_pnl = last_price - hi_price.get(hi_price.size() - 1); hi_pnl = last_price - lo_price.get(lo_price.size() - 1); if (cur_pnl < -stop_loss) { if (printall) System.out.println( "Stop-loss Activated since lo_pnl = " + cur_pnl + " < -" + stop_loss); //System.out.println("Closing price at bar was " + price.get(price.size()-1) + " but highest price in bar was " + hi_price.get(hi_price.size()-1)); //--------------sell---------- //price_sold = price.get(price.size()-1); price_sold = ask.get(ask.size() - 1); profit = price_borrowed - price_sold; // price_sold = price_borrowed + stop_loss; // profit = -stop_loss; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; count = account.size() - 1; amount = getAmount(account.get(count), 4); //account.add(date_stamp + " " + (amount - stop_loss)); //account.add(date_stamp + " " + (amount + profit)); //account.add(date_stamp + " " + price.get(price.size()-1) + " " + profit + " " + (amount + profit)); account.add(date_stamp + " " + formatter3.format(price_sold) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); amount = getAmount(account.get(account.size() - 1), 4); if (weekend.dayOfWeek().getAsText().equals("Sunday")) { sunday.add(date_stamp + " " + profit); } dailyoutret.add(price_sold - log_ret); log_ret = price_sold; out_transaction = 0; //-- return stop loss to original setting -------- stop_loss = global_stop_loss; red_zone = false; if (printall) System.out .println("Bought at " + date_stamp + " for a profit/loss of " + profit); } else if (cur_pnl >= take_profit_thresh) { if (printall) System.out.println("Profit zone activated since cur_pnl = " + cur_pnl + " > " + take_profit_thresh); // stop_loss = profitable_stop; // last_price = price.get(price.size()-1); // red_zone = true; //price_sold = price.get(price.size()-1); price_sold = ask.get(ask.size() - 1); profit = price_borrowed - price_sold; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; count = account.size() - 1; amount = getAmount(account.get(count), 4); //account.add(date_stamp + " " + (amount + profit)); account.add(date_stamp + " " + formatter3.format(price_sold) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); amount = getAmount(account.get(account.size() - 1), 4); if (weekend.dayOfWeek().getAsText().equals("Sunday")) { sunday.add(date_stamp + " " + profit); } dailyoutret.add(price_sold - log_ret); log_ret = price_sold; out_transaction = 0; //-- return stop loss to original setting -------- stop_loss = global_stop_loss; red_zone = false; if (printall) System.out .println("Bought at " + date_stamp + " for a profit/loss of " + profit); } } else if (downtick_strategy) { //strategy here is to buy/sell according to signal iff downtick has occurred if (current_signal > 0 && (price_sold > price.get(price.size() - 1))) { //let's buy some more if (printall) System.out.println("Buying at " + date_stamp + " since last price sold = " + price_sold + " > " + price.get(price.size() - 1)); price_bought = price.get(price.size() - 1); last_price = price.get(price.size() - 1); in_transaction = 1; } else if (current_signal < 0 && (price_sold < price.get(price.size() - 1))) { //let's short some more if (printall) System.out.println( "Shorting at " + date_stamp + " since last price bought back at = " + price_sold + " < " + price.get(price.size() - 1)); price_borrowed = price.get(price.size() - 1); last_price = price.get(price.size() - 1); out_transaction = 1; if (printall) System.out.println("Entered short transaction at " + price_borrowed); } cur_pnl = 0; lo_pnl = 0; hi_pnl = 0; } else { cur_pnl = 0; lo_pnl = 0; hi_pnl = 0; } if (weekend.dayOfWeek().getAsText().equals("Friday") && date_stamp.indexOf("17:00:00") != -1) { if (printall) System.out.println("End of week"); } dailyReport.add("" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1)) + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", " + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl)); if (printall) System.out .println("" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1)) + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", " + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl)); allsignal.add(date_stamp + " " + current_signal); } } } double mean_ntrades = 0; computed = true; dailyoutret.set(1, 0.0); double[] dreturns = new double[account.size()]; dreturns[0] = 0; double mean = 0; double sd = 0; n_neg_ret = 0; n_pos_ret = 0; neg_ret_mean = 0; pos_ret_mean = 0; pnl = 0; int n_trades = 0; ArrayList<Integer> n_trades_day = new ArrayList<Integer>(); for (i = 1; i < account.size(); i++) { out.println(account.get(i)); dailyout.println(account.get(i) + " " + dailyoutret.get(i)); //System.out.println(account.get(i)); dreturns[i] = getAmount(account.get(i), 4) - getAmount(account.get(i - 1), 4); dates = account.get(i).split("[ ]+"); if (!perf_dates.contains(dates[0])) //first date entry { if (perf_dates.size() != 0) { perf_returns.add(pnl); n_trades_day.add(n_trades); } perf_dates.add(dates[0]); pnl = dreturns[i]; if (dreturns[i] != 0) { n_trades = 1; } else { n_trades = 0; } } else //already contains the date, so add on pnl { pnl = pnl + dreturns[i]; //System.out.println(dreturns[i]); if (dreturns[i] != 0) { n_trades++; } // System.out.println(n_trades);} } if (dreturns[i] > 0) { n_pos_ret++; pos_ret_mean = pos_ret_mean + dreturns[i]; mean = mean + dreturns[i]; } else if (dreturns[i] < 0) { n_neg_ret++; neg_ret_mean = neg_ret_mean - dreturns[i]; mean = mean + dreturns[i]; } } perf_returns.add(pnl); n_trades_day.add(n_trades); for (i = 0; i < perf_dates.size(); i++) { if (printall) System.out.println(perf_dates.get(i) + " " + perf_returns.get(i) + " " + n_trades_day.get(i)); perform.println(perf_dates.get(i) + " " + perf_returns.get(i)); mean_ntrades = mean_ntrades + n_trades_day.get(i); } mean_ntrades = mean_ntrades / n_trades_day.size(); dates_price = new String[n_obs]; for (i = 0; i < n_obs; i++) { tokens = allsignal.get(allsignal.size() - n_obs + i).split("[ ]+"); //System.out.println(tokens[0] + " " + latestDates.get(latestDates.size() - n_obs + i - 1)); // if(tokens[0].equals(latestDates.get(latestDates.size() - n_obs + i - 1))) // { dates_price[i] = new String(latestDates.get(latestDates.size() - n_obs + i) + " " + (mid.get(mid.size() - n_obs + i) - mid.get(mid.size() - n_obs + i - 1)) + " " + mid.get(mid.size() - n_obs + i) + " " + tokens[2]); // } } double sunday_roi = 0; double sunday_trade = 0; double sunday_pos_mean = 0; double sunday_neg_mean = 0; int nsunday_pos_mean = 0; int nsunday_neg_mean = 0; System.out.println("\nSunday Performance"); for (i = 0; i < sunday.size(); i++) { //System.out.println(sunday.get(i)); sunday_trade = getAmount(sunday.get(i), 4); sunday_roi = sunday_roi + sunday_trade; if (sunday_trade > 0) { sunday_pos_mean += sunday_trade; nsunday_pos_mean++; } if (sunday_trade < 0) { sunday_neg_mean += sunday_trade; nsunday_neg_mean++; } } System.out.println("\nSunday Stats"); System.out.println("Sunday ROI = " + sunday_roi); sunday_roi = sunday_roi / (nsunday_pos_mean + nsunday_neg_mean); System.out.println("Sunday meantrade = " + sunday_roi); System.out.println("Sunday pos mean = " + (sunday_pos_mean / nsunday_pos_mean) + ", npos_trades = " + nsunday_pos_mean); System.out.println("Sunday neg mean = " + (sunday_neg_mean / nsunday_neg_mean) + ", nneg_trades = " + nsunday_neg_mean); mean = mean / (n_pos_ret + n_neg_ret); System.out.println("ROI = " + account.get(account.size() - 1)); //--- compute stats--------------- double risk = neg_ret_mean / (double) n_neg_ret; System.out.println("neg_ret_mean = " + (-neg_ret_mean) + ", " + n_neg_ret); double reward = pos_ret_mean / (double) n_pos_ret; System.out.println("pos_ret_mean = " + pos_ret_mean + ", " + n_pos_ret); double win_ratio = (double) (n_pos_ret) / (n_pos_ret + n_neg_ret); kellyPerc = win_ratio - (1.0 - win_ratio) * (risk / reward); ulcer_index = ulcerIndex(dreturns); System.out.println("win ratio = " + win_ratio + ", risk = " + risk + ", reward = " + reward); System.out.println("kelly and ulcer = " + kellyPerc + " " + ulcer_index); for (i = 0; i < dreturns.length; i++) { sd = sd + (dreturns[i] - mean) * (dreturns[i] - mean) / ((double) dreturns.length); } standard_deviation = Math.sqrt(sd); sharpeRatio = Math.sqrt(250) * mean / standard_deviation; maxdraw = computeDrawdown(dreturns); rank_coeff = segmentRankCorrelation(30, dreturns); System.out.println("MeanRet = " + mean + ", Sharpe = " + sharpeRatio + ", MaxDD = " + maxdraw + ", Rank = " + rank_coeff + ", avg_n_trades = " + mean_ntrades); out.close(); dailyout.close(); perform.close(); svmout.close(); b0_coeff.close(); } catch (FileNotFoundException fe) { System.out.println("File not found..." + fe); } catch (IOException ioe) { System.out.println("IO procedure faulty..." + ioe); } n_files++; return computed; }
From source file:ch.imetrica.mdfaTradingStrategies.MDFAStrategyEvolution.java
public boolean startStrategyDailyIntradayStocks() { int j, i, l, N, file_count; double sum = 0; Double D;/*from w w w.ja v a 2s. co m*/ String ddelims = "[-]"; boolean computed = false; boolean print_filter = false; boolean made_trade = true; String date_stamp, strline; int daily_size; double profit, price_borrowed, price_sold, price_bought; double current_price, prev_price; double last_price, cur_pnl, stop_loss, lo_pnl, hi_pnl; double log_ret = 0; signal = new double[trade_obs]; xt = new double[trade_obs]; lag_signals = new double[trade_obs]; prix = new double[trade_obs]; lo_prix = new double[trade_obs]; hi_prix = new double[trade_obs]; total_succ = 0; total = 0; log_price = 0; N = n_obs; avg_vol = 0.0; b_avg = new double[L * n_rep]; count = 0; trade_succ_ratio = 0; double amount = 0; double prev_signal; reg_trading_hours = false; String[] intdates; //make sure arraylists empty ArrayList<String> perf_dates = new ArrayList<String>(); ArrayList<Double> perf_returns = new ArrayList<Double>(); double pnl; String[] dates; boolean inverse_hours = false; String time; ArrayList<String> account = new ArrayList<String>(); ArrayList<String> sunday = new ArrayList<String>(); ArrayList<String> latestDates = new ArrayList<String>(); last_trades = new ArrayList<Integer>(); final_trades = new ArrayList<Double>(); dailyoutret = new ArrayList<Double>(); maxIntValue = new ArrayList<Double>(); avg_volatility = new ArrayList<Double>(); close_series = new ArrayList<Double>(); highlow_series = new ArrayList<Double>(); exp_series_1 = new ArrayList<Double>(); exp_series_2 = new ArrayList<Double>(); price = new ArrayList<Double>(); lo_price = new ArrayList<Double>(); hi_price = new ArrayList<Double>(); mid = new ArrayList<Double>(); bid = new ArrayList<Double>(); ask = new ArrayList<Double>(); dates_series = new ArrayList<String>(); dailyReport = new ArrayList<String>(); b0_trend = new ArrayList<Double>(); vol_0 = new ArrayList<Double>(); vol_1 = new ArrayList<Double>(); sub_returns = new ArrayList<Double>(); trade_days = new ArrayList<String>(); returns = new ArrayList<Double>(); longreturns = new ArrayList<Double>(); shortreturns = new ArrayList<Double>(); dropdowns = new ArrayList<Double>(); success = new ArrayList<Double>(); dates_low_high = new ArrayList<String>(); crits = new ArrayList<String>(); svm = new ArrayList<String>(); filters = new ArrayList<Filter>(); date_returns = new ArrayList<String>(); live_series = new ArrayList<Double>(); //the data to be applied out of sample ib_data_hash = new ibHash(); fridayROI = 0; fridayROI_pos = 0; fridays = 0; int end_hour; lookback_returns = new ArrayList<Double>(); num_pos_returns = 0; deg_0 = new ArrayList<Double>(); deg_1 = new ArrayList<Double>(); crit_0 = new ArrayList<Double>(); crit_1 = new ArrayList<Double>(); full_returns_array = new ArrayList<double[]>(); morning_returns = new ArrayList<double[]>(); morning_buy = true; //enter transaction at morning open morning_optimize = false; //optimize in the morning trading hours num_full_positive_returns = 0; //--- Now get historical interp values ------ //uploadInterpParams("max_int.dat"); //------------------------------------------- forex24 = true; ret_dist = new double[trade_obs]; pos_ret_dist = new int[trade_obs]; neg_ret_dist = new int[trade_obs]; neg_trades_started = new int[trade_obs]; pos_trades_started = new int[trade_obs]; neg_trades_started_mean = new double[trade_obs]; pos_trades_started_mean = new double[trade_obs]; diff_account = new double[trade_obs]; pos_ret_mean_time = new double[trade_obs]; neg_ret_mean_time = new double[trade_obs]; mdfaTrades = new ArrayList<MDFATrade>(); fmt = DateTimeFormat.forPattern("y-MM-dd HH:mm:ss"); formatter = new DecimalFormat("#0.000000"); formatter3 = new DecimalFormat("#0.00000"); formatter2 = new DecimalFormat("#0.00"); histo_stat = new int[100]; interp_vals = new ArrayList<Double>(); max_ranks = new ArrayList<Double>(); profit_baby = 0; //setForecastDFAParameters(); bad_starts = 0; n_out_samp = 0; //take_profit = true; //take_profit_thresh = .0020; current_signal = 0; prev_price = 0; cur_pnl = 0; stop_loss = stop_loss_thresh; out_transaction = 0; in_transaction = 0; red_zone = false; global_stop_loss = stop_loss_thresh; profitable_stop = .0005; count = 0; short_sell = true; long_buy = true; day_count = 0; ArrayList<String> trade_times = new ArrayList<String>(); lo_pnl = 0; hi_pnl = 0; price_borrowed = 0; price_sold = 0; price_bought = 0; last_price = 0; binary_rule = true; signal_strength_rule = true; downtick_strategy = false; signal_profit = false; friday_closing = true; //asian_close = true; //----- Fill up with times here ------------------------- // for(i=0;i<10;i++) // { // if(i%4 == 0) {trade_times.add("0"+i+":00:00");} // } // for(i=10;i<24;i++) // { // trade_times.add(i+":00:00");} // for(i=0;i<10;i++) {trade_times.add("0"+i+":00:00");} // for(i=10;i<24;i++) {trade_times.add(i+":00:00");} trade_times.add("10:00:00"); trade_times.add("11:00:00"); trade_times.add("12:00:00"); trade_times.add("13:00:00"); trade_times.add("14:00:00"); trade_times.add("15:00:00"); String[] hourToks = startingTime.split("[:]+"); start_hour = (new Integer(hourToks[0])).intValue(); hourToks = endingTime.split("[:]+"); end_hour = (new Integer(hourToks[0])).intValue(); inverse_hours = false; if (start_hour > end_hour) //must switch hours here { int temphour = end_hour; inverse_hours = true; end_hour = start_hour; start_hour = temphour; } if (ib_data && ib_data_file != null) { try { fin = new FileInputStream(ib_data_file); din = new DataInputStream(fin); br = new BufferedReader(new InputStreamReader(din)); while ((strline = br.readLine()) != null) { String[] sp = strline.split("[,]+"); ib_data_hash.put(sp[0], new String( sp[1] + " " + sp[2] + " " + sp[3] + " " + sp[4] + " " + sp[5] + " " + sp[6])); //System.out.println(sp[1] + " " + sp[2] + " " + sp[3] + " " + sp[4] + " " + sp[5] + " " + sp[6]); } } catch (FileNotFoundException fe) { System.out.println("File not found..." + fe); } catch (IOException ioe) { System.out.println("IO procedure faulty..." + ioe); } } String[] myname = dataFiles[0].split("[.]+"); try { PrintWriter b0_coeff = new PrintWriter(new FileWriter("b0_coeff.dat")); PrintWriter perform = new PrintWriter(new FileWriter("intraday_performance_" + n_files + ".dat")); PrintWriter dailyout = new PrintWriter(new FileWriter("daily_nasdaq.dat")); PrintWriter out = new PrintWriter(new FileWriter("strategy_results_" + myname[0] + ".dat")); for (file_count = 0; file_count < 1; file_count++) { stop_loss_thresh = stop_loss_thresh * 100; take_profit_thresh = take_profit_thresh * 100; global_stop_loss = global_stop_loss * 100; stop_loss = stop_loss_thresh; setTimeStandards(new File(dataFiles[file_count])); System.out.println("opening " + dataFiles[file_count]); fin = new FileInputStream(dataFiles[file_count]); din = new DataInputStream(fin); br = new BufferedReader(new InputStreamReader(din)); lookback_ready = false; spread = new PrintWriter(new FileWriter("spread_" + dataFiles[file_count] + ".dat")); //if(print_debug)System.out.println("Entering loop..."); trading_hours = false; computed = false; while ((strline = br.readLine()) != null) { //System.out.println(strline); tokens = strline.split(delims); n_toks = tokens.length; //System.out.println("Number of toks = "+n_toks); if (n_toks == 0) { System.out.println("End of file"); break; } if (n_toks >= 6) { bid_ask_data = true; } else { bid_ask_data = false; } date_stamp = tokens[0]; date_tokens = date_stamp.split(date_delims); intdates = date_tokens[0].split(ddelims); DateTime weekend = new DateTime((new Integer(intdates[0])).intValue(), (new Integer(intdates[1])).intValue(), (new Integer(intdates[2])).intValue(), 14, 0); time = date_tokens[1]; //insampStart is the time we collect daily data //if(date_stamp.indexOf(insampStart) != -1) if (trade_times.contains(time)) { //get bid/mid/ask data if (ib_data && ib_data_hash.containsKey(tokens[0])) { String[] hashed = ib_data_hash.get(tokens[0]).split("[ ]+"); for (i = 1; i < hashed.length; i++) { tokens[i] = hashed[i]; } // System.out.print(tokens[i] + " ");} } daily_price.add(new Double(tokens[1])); current_price = (new Double(tokens[1])).doubleValue(); if (daily_price.size() == 1) { daily_returns.add(new Double(0.0)); prev_price = current_price; } else { daily_returns.add(log(current_price) - log(prev_price)); prev_price = current_price; } daily_dates.add(date_stamp); } print_filter = false; latestDates.add(date_stamp); D = new Double(tokens[4]); close_series.add(D); if (ib_data && ib_data_hash.containsKey(tokens[0])) { String[] hashed = ib_data_hash.get(tokens[0]).split("[ ]+"); //System.out.println("Contains " + tokens[0] + ", lengths = " + hashed.length + ", " + tokens.length); for (i = 1; i < hashed.length; i++) { tokens[i] = hashed[i]; } // System.out.print(tokens[i] + " ");} bid.add(new Double(tokens[2])); ask.add(new Double(tokens[3])); mid.add(new Double(tokens[1])); } else { bid.add(new Double(tokens[2])); ask.add(new Double(tokens[3])); mid.add(new Double(tokens[1])); } D = new Double(tokens[1]); price.add(D); D = new Double(tokens[4]); if (ib_data && ib_data_hash.containsKey(tokens[0])) { live_series.add(log(mid.get(mid.size() - 1)) - log(mid.get(mid.size() - 2))); } else { live_series.add(D); } if (ib_data && ib_data_hash.containsKey(tokens[0])) //use as is { lo_price.add(new Double(tokens[7])); hi_price.add(new Double(tokens[8])); } else { lo_price.add((new Double(tokens[7]))); hi_price.add((new Double(tokens[8]))); } //---- start the account ------ if (account.size() == 0) { account.add(date_stamp + " " + 0); dailyoutret.add(0.0); } String[] hours = time.split("[:]+"); cur_hour = (new Integer(hours[0])).intValue(); (new Integer(hours[1])).intValue(); trading_closed = false; //if currently not in a transaction and between the hours of midnight and start-hour, then no new //positions will be opened if (asian_close) //only closed if most recent transaction was closed artificially through SL or TP after end hours { if ((in_transaction == 0 && out_transaction == 0) && cur_hour >= start_hour && cur_hour <= end_hour) { trading_closed = true; if (printall) System.out.println(cur_hour + " " + start_hour); } } else { if (cur_hour >= start_hour && cur_hour <= end_hour) { trading_closed = true; if (printall) { System.out.println(cur_hour + " " + start_hour); } } } // if(cur_hour == 14) {release_first = true;} // else {elease_first = false;} if (inverse_hours) { trading_closed = !trading_closed; } its_closing_time = false; //its_closing_time = (date_stamp.indexOf("16:00:00") != -1); made_trade = false; if (daily_returns.size() >= n_obs && trade_times.contains(time)) //a new day begineth { computed = true; trading_hours = true; tseries = new double[n_rep * n_obs]; for (i = 0; i < n_obs; i++) { tseries[n_obs - 1 - i] = daily_returns.get(daily_returns.size() - 1 - i); tseries[n_obs + n_obs - 1 - i] = daily_returns.get(daily_returns.size() - 1 - i); if (n_rep > 2 && exp_series_1.size() > 0) { tseries[n_obs * 2 + n_obs - 1 - i] = exp_series_1.get(exp_series_1.size() - 1 - i); } if (n_rep > 3 && exp_series_2.size() > 0) { tseries[n_obs * 3 + n_obs - 1 - i] = exp_series_2.get(exp_series_2.size() - 1 - i); } } mdfa.set_tseries(tseries, n_obs, n_rep); //if(day_count == 0) //recompute filter coefficients if (day_count == 0 || weekend.dayOfWeek().getAsText().equals("Sunday") && date_stamp.indexOf("18:00:00") != -1) { if (printall) System.out.println("Recomputing filter..."); mdfa.computeFilterGeneral(true, print_filter); b_coeffs = new double[(n_rep - 1) * L]; //System.out.println(b_coeffs.length + " " + L + n_rep); for (l = 0; l < L; l++) { for (i = 0; i < n_rep - 1; i++) { b_coeffs[L * i + l] = mdfa.b[L * (i + 1) + l]; } // System.out.println(b_coeffs[l]);} //if(date_stamp.indexOf("2013-12-17") != -1) {System.out.println(b_coeffs[l]);} } if (printall) System.out.println(date_stamp + " b_coeffs = " + b_coeffs[0] + " " + b_coeffs[1] + " " + b_coeffs[2]); b_copy = new double[mdfa.b.length]; System.arraycopy(mdfa.b, 0, b_copy, 0, b_copy.length); b0_coeff.println(b_coeffs[0]); // + ", " + b_coeffs[L] + ", " + b_coeffs[2*L]); } sum = 0.0; for (j = 1; j < n_rep; j++) { for (l = 0; l < L; l++) { sum = sum + b_coeffs[L * (j - 1) + l] * tseries[N * j + n_obs - 1 - l]; } } prev_signal = current_signal; current_signal = sum; if (sig_inverse) { current_signal = -current_signal; } //----final signal --- daily_signal.add(current_signal); daily_size = daily_price.size(); dailyReport.add( "New day " + date_stamp + ", " + formatter3.format(daily_price.get(daily_size - 1)) + ", " + formatter.format(tseries[n_obs - 1]) + ", " + current_signal); //if(printall) System.out.println("New day "+date_stamp + ", " + formatter3.format(daily_price.get(daily_size-1)) + ", " + formatter.format(tseries[n_obs-1]) + ", " + current_signal); //--compute binary trading rule --- //if(printall) System.out.println("Current signal = " + current_signal + " Prev signal = " + prev_signal + ", trading_closed = " + trading_closed); if (friday_closing && its_closing_time) { if (printall) System.out.println("\nIt's Friday at 5pm, time to close shop for week"); if (current_signal > 0 && in_transaction == 1) //in a long transaction { price_sold = daily_price.get(daily_size - 1); profit = price_sold - price_bought; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1), 4); //account.add(date_stamp + " " + (amount + profit)); account.add(date_stamp + " " + daily_price.get(daily_price.size() - 1) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); amount = getAmount(account.get(account.size() - 1), 4); if (weekend.dayOfWeek().getAsText().equals("Sunday")) { sunday.add(date_stamp + " " + profit); } made_trade = true; dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); in_transaction = 0; if (printall) { if (profit > 0) System.out.println("Sold for a profit of " + profit); else if (profit < 0) System.out.println("Sold for a loss of " + profit); System.out.println("profit, price_bought, price_sold = " + profit + ", " + price_bought + ", " + price_sold); } } else if (current_signal < 0 && out_transaction == 1) //in a short transaction { price_sold = daily_price.get(daily_size - 1); profit = price_borrowed - price_sold; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1), 4); //account.add(date_stamp + " " + (amount + profit)); account.add(date_stamp + " " + daily_price.get(daily_price.size() - 1) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); amount = getAmount(account.get(account.size() - 1), 4); if (weekend.dayOfWeek().getAsText().equals("Sunday")) { sunday.add(date_stamp + " " + profit); } made_trade = true; dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); out_transaction = 0; if (printall) System.out.println("profit, price_borrowed, price_sold = " + profit + ", " + price_borrowed + ", " + price_sold); } } else { //if(binary_rule && !trading_closed) if (binary_rule && (cur_hour <= 16 && cur_hour >= 10)) { // if(signal_profit) // { // //if(printall) System.out.println("Trading_closed = " + trading_closed); // if((current_signal > 0 && in_transaction == 1) && (daily_price.get(daily_size-1) > last_price)) // { // // // price_sold = daily_price.get(daily_size-1); // profit = price_sold - price_bought; // if(profit > 0) {succ_trades=succ_trades+1;} // total_trades=total_trades+1; // // amount = getAmount(account.get(account.size()-1)); // account.add(date_stamp + " " + (amount + profit)); // amount = getAmount(account.get(account.size()-1)); // // made_trade = true; // dailyoutret.add(daily_price.get(daily_size-1) - log_ret); // log_ret = daily_price.get(daily_size-1).doubleValue(); // // in_transaction = 0; // // if(printall){ // if(profit>0) System.out.println("Sold for a profit of " + profit); // else if(profit<0) System.out.println("Sold for a loss of " + profit); // System.out.println("profit, price_bought, price_sold = " + profit + ", " + price_bought + ", " + price_sold); // } // // if(!trading_closed) // {price_bought = daily_price.get(daily_size-1); // //in_transaction = 1; // out_transaction = 1; // last_price = daily_price.get(daily_size-1); // if(printall) System.out.println("Entered long transaction at " + price_bought); // } // } // else if((current_signal < 0 && out_transaction == 1) && (daily_price.get(daily_size-1) < last_price)) // { // // price_sold = daily_price.get(daily_size-1); // profit = price_borrowed - price_sold; // // if(profit > 0) {succ_trades=succ_trades+1;} // total_trades=total_trades+1; // // amount = getAmount(account.get(account.size()-1)); // account.add(date_stamp + " " + (amount + profit)); // amount = getAmount(account.get(account.size()-1)); // // made_trade = true; // dailyoutret.add(daily_price.get(daily_size-1) - log_ret); // log_ret = daily_price.get(daily_size-1).doubleValue(); // // // out_transaction = 0; // // if(printall) // { // if(profit>0) System.out.println("Sold for a profit of " + profit); // else if(profit<0) System.out.println("Sold for a loss of " + profit); // } // if(printall) System.out.println("profit, price_borrowed, price_sold = " + profit + ", " + price_borrowed + ", " + price_sold); // // if(!trading_closed) // { // price_borrowed = daily_price.get(daily_size-1); // //out_transaction = 1; // in_transaction = 1; // if(printall)System.out.println("Entered short transaction at " + price_borrowed); // } // } // // } made_trade = false; if (current_signal > 0 && prev_signal <= 0) //new point positive, we see momentum, buy { last_price = daily_price.get(daily_size - 1); if (short_sell && out_transaction == 1) //in a short-sell transaction, sell { price_sold = daily_price.get(daily_size - 1); profit = price_borrowed - price_sold; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1), 4); //account.add(date_stamp + " " + (amount + profit)); account.add(date_stamp + " " + formatter3.format(daily_price.get(daily_price.size() - 1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); amount = getAmount(account.get(account.size() - 1), 4); if (weekend.dayOfWeek().getAsText().equals("Sunday")) { sunday.add(date_stamp + " " + profit); } made_trade = true; dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); out_transaction = 0; if (printall) { if (profit > 0) System.out.println("Sold for a profit of " + profit); else if (profit < 0) System.out.println("Sold for a loss of " + profit); System.out.println("profit, price_borrowed, price_sold = " + profit + ", " + price_borrowed + ", " + price_sold); } } if ((long_buy && in_transaction == 0) && !trading_closed) { profit = 0; price_bought = daily_price.get(daily_size - 1); in_transaction = 1; account.add(date_stamp + " " + formatter3.format(daily_price.get(daily_price.size() - 1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); if (printall) System.out.println("Entered long transaction at " + price_bought); } } else if (current_signal < 0 && prev_signal >= 0) //if in transaction and signal goes below, sell { last_price = daily_price.get(daily_size - 1); if (long_buy && in_transaction == 1) { price_sold = daily_price.get(daily_size - 1); profit = price_sold - price_bought; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1), 4); //account.add(date_stamp + " " + (amount + profit)); account.add(date_stamp + " -" + formatter3.format(daily_price.get(daily_price.size() - 1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); amount = getAmount(account.get(account.size() - 1), 4); if (weekend.dayOfWeek().getAsText().equals("Sunday")) { sunday.add(date_stamp + " " + profit); } made_trade = true; dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); in_transaction = 0; if (printall) { if (profit > 0) System.out.println("Sold for a profit of " + profit); else if (profit < 0) System.out.println("Sold for a loss of " + profit); System.out.println("profit, price_bought, price_sold = " + profit + ", " + price_bought + ", " + price_sold); } } if ((short_sell && out_transaction == 0) && !trading_closed) { profit = 0; price_borrowed = daily_price.get(daily_size - 1); out_transaction = 1; account.add(date_stamp + " -" + formatter3.format(daily_price.get(daily_price.size() - 1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); if (printall) System.out.println("Entered short transaction at " + price_borrowed); } } } if (signal_strength_rule && ((in_transaction == 0 && out_transaction == 0) && !trading_closed && (cur_hour <= 16 && cur_hour >= 10))) { if (current_signal > 0) //new point positive, we see momentum, buy { last_price = daily_price.get(daily_size - 1); if (short_sell && out_transaction == 1) //in a short-sell transaction, sell { price_sold = daily_price.get(daily_size - 1); profit = price_borrowed - price_sold; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1), 4); //account.add(date_stamp + " " + (amount + profit)); account.add(date_stamp + " " + formatter3.format(daily_price.get(daily_price.size() - 1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); amount = getAmount(account.get(account.size() - 1), 4); if (weekend.dayOfWeek().getAsText().equals("Sunday")) { sunday.add(date_stamp + " " + profit); } made_trade = true; dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); out_transaction = 0; if (printall) System.out.println("profit, price_borrowed, price_sold = " + profit + ", " + price_borrowed + ", " + price_sold); } if (long_buy && in_transaction == 0) { profit = 0; price_bought = daily_price.get(daily_size - 1); in_transaction = 1; account.add(date_stamp + " " + formatter3.format(daily_price.get(daily_price.size() - 1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); if (printall) System.out.println("Entered long transaction at " + price_bought); } } else if (current_signal < 0) //if in transaction and signal goes below, sell { last_price = daily_price.get(daily_size - 1); if (long_buy && in_transaction == 1) { price_sold = daily_price.get(daily_size - 1); profit = price_sold - price_bought; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1), 4); //account.add(date_stamp + " " + (amount + profit)); account.add(date_stamp + " -" + formatter3.format(daily_price.get(daily_price.size() - 1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); amount = getAmount(account.get(account.size() - 1), 4); if (weekend.dayOfWeek().getAsText().equals("Sunday")) { sunday.add(date_stamp + " " + profit); } made_trade = true; dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); in_transaction = 0; if (printall) System.out.println("Bought for a profit of " + profit); if (printall) System.out.println("profit, price_bought, price_sold = " + profit + ", " + price_bought + ", " + price_sold); } if (short_sell && out_transaction == 0) { profit = 0; price_borrowed = daily_price.get(daily_size - 1); out_transaction = 1; account.add(date_stamp + " -" + formatter3.format(daily_price.get(daily_price.size() - 1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); if (printall) System.out.println("Entered short transaction at " + price_borrowed); } } } if (!made_trade) { profit = 0; //account.add(date_stamp + " " + formatter3.format(daily_price.get(daily_price.size()-1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); //dailyoutret.add(price.get(price.size()-1) - log_ret); //log_ret = price.get(price.size()-1); } day_count++; //if(recompute_day == day_count) {y_count=0;} if (printall) System.out.println( "" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1)) + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", " + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl)); allsignal.add(date_stamp + " " + current_signal); } } else if (trading_hours && (cur_hour <= 16 && cur_hour >= 10))// && !trading_closed)// && cur_min != 30) { if (in_transaction == 1) //in a long transaction { if (red_zone && price.get(price.size() - 1) > last_price) //check if new high price { last_price = price.get(price.size() - 1); } //cur_pnl = price.get(price.size()-1) - last_price; cur_pnl = bid.get(bid.size() - 1) - last_price; lo_pnl = lo_price.get(lo_price.size() - 1) - last_price; hi_pnl = hi_price.get(hi_price.size() - 1) - last_price; if (cur_pnl < -stop_loss) { if (printall) System.out.println( "Stop-loss Activated since lo_pnl = " + cur_pnl + " < -" + stop_loss); //System.out.println("Closing price at bar was " + price.get(price.size()-1) + " but lowest price in bar was " + lo_price.get(lo_price.size()-1)); //--------------sell---------- //price_sold = price.get(price.size()-1); //price_sold = price.get(price.size()-1); price_sold = bid.get(bid.size() - 1); profit = price_sold - price_bought; // price_sold = price_bought - stop_loss; // profit = -stop_loss; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; count = account.size() - 1; amount = getAmount(account.get(count), 4); //account.add(date_stamp + " " + (amount - stop_loss)); //account.add(date_stamp + " " + (amount + profit)); //account.add(date_stamp + " " + price.get(price.size()-1) + " " + profit + " " + (amount + profit)); account.add(date_stamp + " -" + formatter3.format(bid.get(bid.size() - 1)) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); amount = getAmount(account.get(account.size() - 1), 4); if (weekend.dayOfWeek().getAsText().equals("Sunday")) { sunday.add(date_stamp + " " + profit); } dailyoutret.add(bid.get(bid.size() - 1) - log_ret); log_ret = bid.get(bid.size() - 1); in_transaction = 0; //-- return stop loss to original setting -------- if (printall) System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit); stop_loss = global_stop_loss; red_zone = false; } else if (cur_pnl >= take_profit_thresh) { if (printall) System.out.println("Profit zone activated since cur_pnl = " + cur_pnl + " > " + take_profit_thresh); //price_sold = price.get(price.size()-1); price_sold = bid.get(bid.size() - 1); profit = price_sold - price_bought; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; count = account.size() - 1; amount = getAmount(account.get(count), 4); //account.add(date_stamp + " " + (amount + profit)); //account.add(date_stamp + " " + price.get(price.size()-1) + " " + profit + " " + (amount + profit)); account.add(date_stamp + " -" + formatter3.format(price_sold) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); amount = getAmount(account.get(account.size() - 1), 4); if (weekend.dayOfWeek().getAsText().equals("Sunday")) { sunday.add(date_stamp + " " + profit); } dailyoutret.add(price_sold - log_ret); log_ret = price_sold; in_transaction = 0; //-- return stop loss to original setting -------- if (printall) System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit); stop_loss = global_stop_loss; // stop_loss = profitable_stop; // last_price = price.get(price.size()-1); // red_zone = true; } } else if (out_transaction == 1) { if (red_zone && price.get(price.size() - 1) < last_price) //check if new high price { last_price = price.get(price.size() - 1); } //cur_pnl = last_price - price.get(price.size()-1); cur_pnl = last_price - ask.get(ask.size() - 1); lo_pnl = last_price - hi_price.get(hi_price.size() - 1); hi_pnl = last_price - lo_price.get(lo_price.size() - 1); if (cur_pnl < -stop_loss) { if (printall) System.out.println( "Stop-loss Activated since lo_pnl = " + cur_pnl + " < -" + stop_loss); //System.out.println("Closing price at bar was " + price.get(price.size()-1) + " but highest price in bar was " + hi_price.get(hi_price.size()-1)); //--------------sell---------- //price_sold = price.get(price.size()-1); price_sold = ask.get(ask.size() - 1); profit = price_borrowed - price_sold; // price_sold = price_borrowed + stop_loss; // profit = -stop_loss; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; count = account.size() - 1; amount = getAmount(account.get(count), 4); //account.add(date_stamp + " " + (amount - stop_loss)); //account.add(date_stamp + " " + (amount + profit)); //account.add(date_stamp + " " + price.get(price.size()-1) + " " + profit + " " + (amount + profit)); account.add(date_stamp + " " + formatter3.format(price_sold) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); amount = getAmount(account.get(account.size() - 1), 4); if (weekend.dayOfWeek().getAsText().equals("Sunday")) { sunday.add(date_stamp + " " + profit); } dailyoutret.add(price_sold - log_ret); log_ret = price_sold; out_transaction = 0; //-- return stop loss to original setting -------- stop_loss = global_stop_loss; red_zone = false; if (printall) System.out .println("Bought at " + date_stamp + " for a profit/loss of " + profit); } else if (cur_pnl >= take_profit_thresh) { if (printall) System.out.println("Profit zone activated since cur_pnl = " + cur_pnl + " > " + take_profit_thresh); // stop_loss = profitable_stop; // last_price = price.get(price.size()-1); // red_zone = true; //price_sold = price.get(price.size()-1); price_sold = ask.get(ask.size() - 1); profit = price_borrowed - price_sold; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; count = account.size() - 1; amount = getAmount(account.get(count), 4); //account.add(date_stamp + " " + (amount + profit)); account.add(date_stamp + " " + formatter3.format(price_sold) + " " + formatter3.format(profit) + " " + formatter3.format(amount + profit)); amount = getAmount(account.get(account.size() - 1), 4); if (weekend.dayOfWeek().getAsText().equals("Sunday")) { sunday.add(date_stamp + " " + profit); } dailyoutret.add(price_sold - log_ret); log_ret = price_sold; out_transaction = 0; //-- return stop loss to original setting -------- stop_loss = global_stop_loss; red_zone = false; if (printall) System.out .println("Bought at " + date_stamp + " for a profit/loss of " + profit); } } else if (downtick_strategy) { //strategy here is to buy/sell according to signal iff downtick has occurred if (current_signal > 0 && (price_sold > price.get(price.size() - 1))) { //let's buy some more if (printall) System.out.println("Buying at " + date_stamp + " since last price sold = " + price_sold + " > " + price.get(price.size() - 1)); price_bought = price.get(price.size() - 1); last_price = price.get(price.size() - 1); in_transaction = 1; } else if (current_signal < 0 && (price_sold < price.get(price.size() - 1))) { //let's short some more if (printall) System.out.println( "Shorting at " + date_stamp + " since last price bought back at = " + price_sold + " < " + price.get(price.size() - 1)); price_borrowed = price.get(price.size() - 1); last_price = price.get(price.size() - 1); out_transaction = 1; if (printall) System.out.println("Entered short transaction at " + price_borrowed); } cur_pnl = 0; lo_pnl = 0; hi_pnl = 0; } else { cur_pnl = 0; lo_pnl = 0; hi_pnl = 0; } if (weekend.dayOfWeek().getAsText().equals("Friday") && date_stamp.indexOf("17:00:00") != -1) { if (printall) System.out.println("End of week"); } dailyReport.add("" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1)) + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", " + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl)); if (printall) System.out .println("" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1)) + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", " + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl)); allsignal.add(date_stamp + " " + current_signal); } } } double mean_ntrades = 0; computed = true; dailyoutret.set(1, 0.0); double[] dreturns = new double[account.size()]; dreturns[0] = 0; double mean = 0; double sd = 0; n_neg_ret = 0; n_pos_ret = 0; neg_ret_mean = 0; pos_ret_mean = 0; pnl = 0; int n_trades = 0; ArrayList<Integer> n_trades_day = new ArrayList<Integer>(); for (i = 1; i < account.size(); i++) { out.println(account.get(i)); dailyout.println(account.get(i) + " " + dailyoutret.get(i)); //System.out.println(account.get(i)); dreturns[i] = getAmount(account.get(i), 4) - getAmount(account.get(i - 1), 4); dates = account.get(i).split("[ ]+"); if (!perf_dates.contains(dates[0])) //first date entry { if (perf_dates.size() != 0) { perf_returns.add(pnl); n_trades_day.add(n_trades); } perf_dates.add(dates[0]); pnl = dreturns[i]; if (dreturns[i] != 0) { n_trades = 1; } else { n_trades = 0; } } else //already contains the date, so add on pnl { pnl = pnl + dreturns[i]; //System.out.println(dreturns[i]); if (dreturns[i] != 0) { n_trades++; } // System.out.println(n_trades);} } if (dreturns[i] > 0) { n_pos_ret++; pos_ret_mean = pos_ret_mean + dreturns[i]; mean = mean + dreturns[i]; } else if (dreturns[i] < 0) { n_neg_ret++; neg_ret_mean = neg_ret_mean - dreturns[i]; mean = mean + dreturns[i]; } } perf_returns.add(pnl); n_trades_day.add(n_trades); for (i = 0; i < perf_dates.size(); i++) { if (printall) System.out.println(perf_dates.get(i) + " " + perf_returns.get(i) + " " + n_trades_day.get(i)); perform.println(perf_dates.get(i) + " " + perf_returns.get(i)); mean_ntrades = mean_ntrades + n_trades_day.get(i); } mean_ntrades = mean_ntrades / n_trades_day.size(); dates_price = new String[n_obs]; for (i = 0; i < n_obs; i++) { tokens = allsignal.get(allsignal.size() - n_obs + i).split("[ ]+"); //System.out.println(tokens[0] + " " + latestDates.get(latestDates.size() - n_obs + i - 1)); // if(tokens[0].equals(latestDates.get(latestDates.size() - n_obs + i - 1))) // { dates_price[i] = new String(latestDates.get(latestDates.size() - n_obs + i) + " " + (mid.get(mid.size() - n_obs + i) - mid.get(mid.size() - n_obs + i - 1)) + " " + mid.get(mid.size() - n_obs + i) + " " + tokens[2]); // } } double sunday_roi = 0; double sunday_trade = 0; double sunday_pos_mean = 0; double sunday_neg_mean = 0; int nsunday_pos_mean = 0; int nsunday_neg_mean = 0; System.out.println("\nSunday Performance"); for (i = 0; i < sunday.size(); i++) { //System.out.println(sunday.get(i)); sunday_trade = getAmount(sunday.get(i), 4); sunday_roi = sunday_roi + sunday_trade; if (sunday_trade > 0) { sunday_pos_mean += sunday_trade; nsunday_pos_mean++; } if (sunday_trade < 0) { sunday_neg_mean += sunday_trade; nsunday_neg_mean++; } } System.out.println("\nSunday Stats"); System.out.println("Sunday ROI = " + sunday_roi); sunday_roi = sunday_roi / (nsunday_pos_mean + nsunday_neg_mean); System.out.println("Sunday meantrade = " + sunday_roi); System.out.println("Sunday pos mean = " + (sunday_pos_mean / nsunday_pos_mean) + ", npos_trades = " + nsunday_pos_mean); System.out.println("Sunday neg mean = " + (sunday_neg_mean / nsunday_neg_mean) + ", nneg_trades = " + nsunday_neg_mean); mean = mean / (n_pos_ret + n_neg_ret); System.out.println("ROI = " + account.get(account.size() - 1)); //--- compute stats--------------- double risk = neg_ret_mean / (double) n_neg_ret; System.out.println("neg_ret_mean = " + (-neg_ret_mean) + ", " + n_neg_ret); double reward = pos_ret_mean / (double) n_pos_ret; System.out.println("pos_ret_mean = " + pos_ret_mean + ", " + n_pos_ret); double win_ratio = (double) (n_pos_ret) / (n_pos_ret + n_neg_ret); kellyPerc = win_ratio - (1.0 - win_ratio) * (risk / reward); ulcer_index = ulcerIndex(dreturns); System.out.println("win ratio = " + win_ratio + ", risk = " + risk + ", reward = " + reward); System.out.println("kelly and ulcer = " + kellyPerc + " " + ulcer_index); for (i = 0; i < dreturns.length; i++) { sd = sd + (dreturns[i] - mean) * (dreturns[i] - mean) / ((double) dreturns.length); } standard_deviation = Math.sqrt(sd); sharpeRatio = Math.sqrt(250) * mean / standard_deviation; maxdraw = computeDrawdown(dreturns); rank_coeff = segmentRankCorrelation(30, dreturns); System.out.println("MeanRet = " + mean + ", Sharpe = " + sharpeRatio + ", MaxDD = " + maxdraw + ", Rank = " + rank_coeff + ", avg_n_trades = " + mean_ntrades); out.close(); dailyout.close(); perform.close(); b0_coeff.close(); } catch (FileNotFoundException fe) { System.out.println("File not found..." + fe); } catch (IOException ioe) { System.out.println("IO procedure faulty..." + ioe); } n_files++; return computed; }
From source file:ch.imetrica.mdfaTradingStrategies.MDFAStrategyEvolution.java
public boolean startStrategyDailyIntradayOneHourBidAsk() { int j, i, l, N, file_count; double sum = 0; Double D;// www .j av a2s. c o m String ddelims = "[-]"; boolean computed = false; boolean print_filter = false; boolean made_trade = true; String date_stamp, strline; int daily_size; double profit, price_borrowed, price_sold, price_bought; double current_price, prev_price; double last_price, cur_pnl, stop_loss, lo_pnl, hi_pnl; double log_ret = 0; signal = new double[trade_obs]; xt = new double[trade_obs]; lag_signals = new double[trade_obs]; prix = new double[trade_obs]; lo_prix = new double[trade_obs]; hi_prix = new double[trade_obs]; total_succ = 0; total = 0; log_price = 0; N = n_obs; avg_vol = 0.0; b_avg = new double[L * n_rep]; count = 0; trade_succ_ratio = 0; double amount = 0; double prev_signal; reg_trading_hours = false; String[] intdates; //make sure arraylists empty ArrayList<String> perf_dates = new ArrayList<String>(); ArrayList<Double> perf_returns = new ArrayList<Double>(); double pnl; String[] dates; boolean inverse_hours = false; String time; ArrayList<String> account = new ArrayList<String>(); ArrayList<String> latestDates = new ArrayList<String>(); last_trades = new ArrayList<Integer>(); final_trades = new ArrayList<Double>(); dailyoutret = new ArrayList<Double>(); maxIntValue = new ArrayList<Double>(); avg_volatility = new ArrayList<Double>(); close_series = new ArrayList<Double>(); highlow_series = new ArrayList<Double>(); exp_series_1 = new ArrayList<Double>(); exp_series_2 = new ArrayList<Double>(); price = new ArrayList<Double>(); lo_price = new ArrayList<Double>(); hi_price = new ArrayList<Double>(); mid = new ArrayList<Double>(); bid = new ArrayList<Double>(); ask = new ArrayList<Double>(); dates_series = new ArrayList<String>(); dailyReport = new ArrayList<String>(); b0_trend = new ArrayList<Double>(); vol_0 = new ArrayList<Double>(); vol_1 = new ArrayList<Double>(); sub_returns = new ArrayList<Double>(); trade_days = new ArrayList<String>(); returns = new ArrayList<Double>(); longreturns = new ArrayList<Double>(); shortreturns = new ArrayList<Double>(); dropdowns = new ArrayList<Double>(); success = new ArrayList<Double>(); dates_low_high = new ArrayList<String>(); crits = new ArrayList<String>(); svm = new ArrayList<String>(); filters = new ArrayList<Filter>(); date_returns = new ArrayList<String>(); live_series = new ArrayList<Double>(); //the data to be applied out of sample ib_data_hash = new ibHash(); fridayROI = 0; fridayROI_pos = 0; fridays = 0; int end_hour; lookback_returns = new ArrayList<Double>(); num_pos_returns = 0; deg_0 = new ArrayList<Double>(); deg_1 = new ArrayList<Double>(); crit_0 = new ArrayList<Double>(); crit_1 = new ArrayList<Double>(); full_returns_array = new ArrayList<double[]>(); morning_returns = new ArrayList<double[]>(); morning_buy = true; //enter transaction at morning open morning_optimize = false; //optimize in the morning trading hours num_full_positive_returns = 0; //--- Now get historical interp values ------ //uploadInterpParams("max_int.dat"); //------------------------------------------- forex24 = true; ret_dist = new double[trade_obs]; pos_ret_dist = new int[trade_obs]; neg_ret_dist = new int[trade_obs]; neg_trades_started = new int[trade_obs]; pos_trades_started = new int[trade_obs]; neg_trades_started_mean = new double[trade_obs]; pos_trades_started_mean = new double[trade_obs]; diff_account = new double[trade_obs]; pos_ret_mean_time = new double[trade_obs]; neg_ret_mean_time = new double[trade_obs]; mdfaTrades = new ArrayList<MDFATrade>(); fmt = DateTimeFormat.forPattern("y-MM-dd HH:mm:ss"); formatter = new DecimalFormat("#0.000000"); formatter3 = new DecimalFormat("#0.00000"); formatter2 = new DecimalFormat("#0.00"); histo_stat = new int[100]; interp_vals = new ArrayList<Double>(); max_ranks = new ArrayList<Double>(); profit_baby = 0; //setForecastDFAParameters(); bad_starts = 0; n_out_samp = 0; //take_profit = true; //take_profit_thresh = .0020; current_signal = 0; prev_price = 0; cur_pnl = 0; stop_loss = stop_loss_thresh; out_transaction = 0; in_transaction = 0; red_zone = false; global_stop_loss = stop_loss_thresh; profitable_stop = .0005; count = 0; short_sell = true; long_buy = true; day_count = 0; ArrayList<String> trade_times = new ArrayList<String>(); ArrayList<String> fxcm_dates = new ArrayList<String>(); lo_pnl = 0; hi_pnl = 0; price_borrowed = 0; price_sold = 0; price_bought = 0; last_price = 0; binary_rule = true; signal_strength_rule = true; downtick_strategy = false; signal_profit = false; friday_closing = false; //asian_close = true; //----- Fill up with times here ------------------------- for (i = 0; i < 10; i++) { trade_times.add("0" + i + ":00:00"); } for (i = 10; i < 24; i++) { trade_times.add(i + ":00:00"); } // trade_times.add("00:00:00"); // trade_times.add("06:00:00"); // trade_times.add("12:00:00"); // trade_times.add("18:00:00"); String[] hourToks = startingTime.split("[:]+"); start_hour = (new Integer(hourToks[0])).intValue(); hourToks = endingTime.split("[:]+"); end_hour = (new Integer(hourToks[0])).intValue(); inverse_hours = false; if (start_hour > end_hour) //must switch hours here { int temphour = end_hour; inverse_hours = true; end_hour = start_hour; start_hour = temphour; } if (ib_data && ib_data_file != null) { try { fin = new FileInputStream(ib_data_file); din = new DataInputStream(fin); br = new BufferedReader(new InputStreamReader(din)); while ((strline = br.readLine()) != null) { String[] sp = strline.split("[,]+"); ib_data_hash.put(sp[0], new String( sp[1] + " " + sp[2] + " " + sp[3] + " " + sp[4] + " " + sp[5] + " " + sp[6])); //System.out.println(sp[1] + " " + sp[2] + " " + sp[3] + " " + sp[4] + " " + sp[5] + " " + sp[6]); } } catch (FileNotFoundException fe) { System.out.println("File not found..." + fe); } catch (IOException ioe) { System.out.println("IO procedure faulty..." + ioe); } } jpy = false; try { PrintWriter b0_coeff = new PrintWriter(new FileWriter("b0_coeff.dat")); PrintWriter perform = new PrintWriter(new FileWriter("intraday_performance_" + n_files + ".dat")); PrintWriter dailyout = new PrintWriter(new FileWriter("daily_nasdaq.dat")); PrintWriter out = new PrintWriter(new FileWriter("strategy_results.dat")); for (file_count = 0; file_count < 1; file_count++) { if (dataFiles[file_count].indexOf("JPY") != -1 && dataFiles[file_count].indexOf("NOKJPY") == -1) { //change_time_zone = true; System.out.println("Changed time zone to Tokyo"); jpy = true; stop_loss_thresh = stop_loss_thresh * 100; take_profit_thresh = take_profit_thresh * 100; global_stop_loss = global_stop_loss * 100; stop_loss = stop_loss_thresh; } else if (futures_data) { //change_time_zone = true; System.out.println("Changed time zone to Tokyo"); stop_loss_thresh = stop_loss_thresh * 10000; stop_loss = stop_loss_thresh; take_profit_thresh = take_profit_thresh * 10000; global_stop_loss = global_stop_loss * 10000; } ArrayList<String> dayData = new ArrayList<String>(); setTimeStandards(new File(dataFiles[file_count])); System.out.println("opening " + dataFiles[file_count]); fin = new FileInputStream(dataFiles[file_count]); din = new DataInputStream(fin); br = new BufferedReader(new InputStreamReader(din)); lookback_ready = false; spread = new PrintWriter(new FileWriter("spread_" + dataFiles[file_count] + ".dat")); //if(print_debug)System.out.println("Entering loop..."); trading_hours = false; computed = false; //---- add the latest FXCM data - while ((strline = br.readLine()) != null) { dayData.add(strline); //add the date tokens = strline.split("[,]+"); fxcm_dates.add(tokens[0]); } //----- now add the lates IB data ------------ System.out.println("opening " + dataFiles[file_count] + " IB data"); String[] fxpair = dataFiles[file_count].split("[.]+"); if ((new File(fxpair[0] + ".IB.dat")).exists()) { fin = new FileInputStream(fxpair[0] + ".IB.dat"); din = new DataInputStream(fin); br = new BufferedReader(new InputStreamReader(din)); String lastFXCMdate = fxcm_dates.get(fxcm_dates.size() - 1); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss"); Date date1 = sdf.parse(lastFXCMdate); while ((strline = br.readLine()) != null) { tokens = strline.split("[,]+"); Date date2 = sdf.parse(tokens[0]); if (!fxcm_dates.contains(tokens[0]) && date1.before(date2)) { System.out.println("New time and observation " + strline); dayData.add(strline); } } } for (int ts = 0; ts < dayData.size(); ts++) { strline = dayData.get(ts); //System.out.println(strline); tokens = strline.split(delims); n_toks = tokens.length; //System.out.println("Number of toks = "+n_toks); if (n_toks == 0) { System.out.println("End of file"); break; } if (n_toks >= 6) { bid_ask_data = true; } else { bid_ask_data = false; } date_stamp = tokens[0]; date_tokens = date_stamp.split(date_delims); intdates = date_tokens[0].split(ddelims); DateTime weekend = new DateTime((new Integer(intdates[0])).intValue(), (new Integer(intdates[1])).intValue(), (new Integer(intdates[2])).intValue(), 14, 0); time = date_tokens[1]; //insampStart is the time we collect daily data //if(date_stamp.indexOf(insampStart) != -1) if (trade_times.contains(time)) { //get bid/mid/ask data if (ib_data && ib_data_hash.containsKey(tokens[0])) { String[] hashed = ib_data_hash.get(tokens[0]).split("[ ]+"); for (i = 1; i < hashed.length; i++) { tokens[i] = hashed[i]; } // System.out.print(tokens[i] + " ");} } daily_price.add(new Double(tokens[1])); current_price = (new Double(tokens[1])).doubleValue(); if (daily_price.size() == 1) { daily_returns.add(new Double(0.0)); prev_price = current_price; } else { daily_returns.add(log(current_price) - log(prev_price)); prev_price = current_price; } daily_dates.add(date_stamp); } print_filter = false; latestDates.add(date_stamp); D = new Double(tokens[4]); close_series.add(D); if (ib_data && ib_data_hash.containsKey(tokens[0])) { String[] hashed = ib_data_hash.get(tokens[0]).split("[ ]+"); //System.out.println("Contains " + tokens[0] + ", lengths = " + hashed.length + ", " + tokens.length); for (i = 1; i < hashed.length; i++) { tokens[i] = hashed[i]; } // System.out.print(tokens[i] + " ");} bid.add(new Double(tokens[2])); ask.add(new Double(tokens[3])); mid.add(new Double(tokens[1])); } else { bid.add(new Double(tokens[2])); ask.add(new Double(tokens[3])); mid.add(new Double(tokens[1])); } D = new Double(tokens[1]); price.add(D); D = new Double(tokens[4]); if (ib_data && ib_data_hash.containsKey(tokens[0])) { live_series.add(log(mid.get(mid.size() - 1)) - log(mid.get(mid.size() - 2))); } else { live_series.add(D); } // if(ib_data && ib_data_hash.containsKey(tokens[0])) //use as is // {lo_price.add(new Double(tokens[7])); hi_price.add(new Double(tokens[8]));} // else // {lo_price.add((new Double(tokens[7]))); hi_price.add((new Double(tokens[8])));} lo_price.add((new Double(tokens[2]))); hi_price.add((new Double(tokens[3]))); //---- start the account ------ if (account.size() == 0) { account.add(date_stamp + " " + 0); dailyoutret.add(0.0); } String[] hours = time.split("[:]+"); cur_hour = (new Integer(hours[0])).intValue(); (new Integer(hours[1])).intValue(); trading_closed = false; //if currently not in a transaction and between the hours of midnight and start-hour, then no new //positions will be opened if (asian_close) //only closed if most recent transaction was closed artificially through SL or TP after end hours { if ((in_transaction == 0 && out_transaction == 0) && cur_hour >= start_hour && cur_hour <= end_hour) { trading_closed = true; } // if(printall) System.out.println(cur_hour + " " + start_hour);} } else { if (cur_hour >= start_hour && cur_hour <= end_hour) { trading_closed = true; } // if(printall) {System.out.println(cur_hour + " " + start_hour);}} } if (inverse_hours) { trading_closed = !trading_closed; } its_closing_time = (weekend.dayOfWeek().getAsText().equals("Friday") && date_stamp.indexOf("17:00:00") != -1); made_trade = false; if (daily_returns.size() >= n_obs && trade_times.contains(time)) //a new day begineth { computed = true; trading_hours = true; tseries = new double[n_rep * n_obs]; for (i = 0; i < n_obs; i++) { tseries[n_obs - 1 - i] = daily_returns.get(daily_returns.size() - 1 - i); tseries[n_obs + n_obs - 1 - i] = daily_returns.get(daily_returns.size() - 1 - i); if (n_rep > 2 && exp_series_1.size() > 0) { tseries[n_obs * 2 + n_obs - 1 - i] = exp_series_1.get(exp_series_1.size() - 1 - i); } if (n_rep > 3 && exp_series_2.size() > 0) { tseries[n_obs * 3 + n_obs - 1 - i] = exp_series_2.get(exp_series_2.size() - 1 - i); } } mdfa.set_tseries(tseries, n_obs, n_rep); //if(day_count == 0) //recompute filter coefficients if (day_count == 0 || weekend.dayOfWeek().getAsText().equals("Sunday") && date_stamp.indexOf("18:00:00") != -1) { if (printall) System.out.println("Recomputing filter..."); mdfa.computeFilterGeneral(true, print_filter); b_coeffs = new double[(n_rep - 1) * L]; //System.out.println(b_coeffs.length + " " + L + n_rep); for (l = 0; l < L; l++) { for (i = 0; i < n_rep - 1; i++) { b_coeffs[L * i + l] = mdfa.b[L * (i + 1) + l]; } // System.out.println(b_coeffs[l]);} //if(date_stamp.indexOf("2013-12-17") != -1) {System.out.println(b_coeffs[l]);} } if (printall) System.out.println(date_stamp + " b_coeffs = " + b_coeffs[0] + " " + b_coeffs[1] + " " + b_coeffs[2]); b_copy = new double[mdfa.b.length]; System.arraycopy(mdfa.b, 0, b_copy, 0, b_copy.length); b0_coeff.println(b_coeffs[0]); // + ", " + b_coeffs[L] + ", " + b_coeffs[2*L]); } sum = 0.0; for (j = 1; j < n_rep; j++) { for (l = 0; l < L; l++) { sum = sum + b_coeffs[L * (j - 1) + l] * tseries[N * j + n_obs - 1 - l]; } } prev_signal = current_signal; current_signal = sum; if (sig_inverse) { current_signal = -current_signal; } //----final signal --- daily_signal.add(current_signal); daily_size = daily_price.size(); dailyReport.add( "New day " + date_stamp + ", " + formatter3.format(daily_price.get(daily_size - 1)) + ", " + formatter.format(tseries[n_obs - 1]) + ", " + current_signal); //if(printall) System.out.println("New day "+date_stamp + ", " + formatter3.format(daily_price.get(daily_size-1)) + ", " + formatter.format(tseries[n_obs-1]) + ", " + current_signal); //--compute binary trading rule --- //if(printall) System.out.println("Current signal = " + current_signal + " Prev signal = " + prev_signal + ", trading_closed = " + trading_closed); if (friday_closing && its_closing_time) { if (printall) System.out.println("\nIt's Friday at 5pm, time to close shop for week"); if (current_signal > 0 && in_transaction == 1) //in a long transaction { price_sold = daily_price.get(daily_size - 1); profit = price_sold - price_bought; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); made_trade = true; dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); in_transaction = 0; if (printall) { if (profit > 0) System.out.println("Sold for a profit of " + profit); else if (profit < 0) System.out.println("Sold for a loss of " + profit); System.out.println("profit, price_bought, price_sold = " + profit + ", " + price_bought + ", " + price_sold); } } else if (current_signal < 0 && out_transaction == 1) //in a short transaction { price_sold = daily_price.get(daily_size - 1); profit = price_borrowed - price_sold; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); made_trade = true; dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); out_transaction = 0; if (printall) System.out.println("profit, price_borrowed, price_sold = " + profit + ", " + price_borrowed + ", " + price_sold); } } else { //if(binary_rule && !trading_closed) if (binary_rule) { // if(signal_profit) // { // //if(printall) System.out.println("Trading_closed = " + trading_closed); // if((current_signal > 0 && in_transaction == 1) && (daily_price.get(daily_size-1) > last_price)) // { // // // price_sold = daily_price.get(daily_size-1); // profit = price_sold - price_bought; // if(profit > 0) {succ_trades=succ_trades+1;} // total_trades=total_trades+1; // // amount = getAmount(account.get(account.size()-1)); // account.add(date_stamp + " " + (amount + profit)); // amount = getAmount(account.get(account.size()-1)); // // made_trade = true; // dailyoutret.add(daily_price.get(daily_size-1) - log_ret); // log_ret = daily_price.get(daily_size-1).doubleValue(); // // in_transaction = 0; // // if(printall){ // if(profit>0) System.out.println("Sold for a profit of " + profit); // else if(profit<0) System.out.println("Sold for a loss of " + profit); // System.out.println("profit, price_bought, price_sold = " + profit + ", " + price_bought + ", " + price_sold); // } // // if(!trading_closed) // {price_bought = daily_price.get(daily_size-1); // //in_transaction = 1; // out_transaction = 1; // last_price = daily_price.get(daily_size-1); // if(printall) System.out.println("Entered long transaction at " + price_bought); // } // } // else if((current_signal < 0 && out_transaction == 1) && (daily_price.get(daily_size-1) < last_price)) // { // // price_sold = daily_price.get(daily_size-1); // profit = price_borrowed - price_sold; // // if(profit > 0) {succ_trades=succ_trades+1;} // total_trades=total_trades+1; // // amount = getAmount(account.get(account.size()-1)); // account.add(date_stamp + " " + (amount + profit)); // amount = getAmount(account.get(account.size()-1)); // // made_trade = true; // dailyoutret.add(daily_price.get(daily_size-1) - log_ret); // log_ret = daily_price.get(daily_size-1).doubleValue(); // // // out_transaction = 0; // // if(printall) // { // if(profit>0) System.out.println("Sold for a profit of " + profit); // else if(profit<0) System.out.println("Sold for a loss of " + profit); // } // if(printall) System.out.println("profit, price_borrowed, price_sold = " + profit + ", " + price_borrowed + ", " + price_sold); // // if(!trading_closed) // { // price_borrowed = daily_price.get(daily_size-1); // //out_transaction = 1; // in_transaction = 1; // if(printall)System.out.println("Entered short transaction at " + price_borrowed); // } // } // // } made_trade = false; if (current_signal > 0 && prev_signal <= 0) //new point positive, we see momentum, buy { //last_price = daily_price.get(daily_size-1); last_price = ask.get(ask.size() - 1); if (short_sell && out_transaction == 1) //in a short-sell transaction, sell { //price_sold = daily_price.get(daily_size-1); price_sold = ask.get(ask.size() - 1); profit = price_borrowed - price_sold; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); made_trade = true; dailyoutret.add(ask.get(ask.size() - 1) - log_ret); log_ret = ask.get(ask.size() - 1).doubleValue(); out_transaction = 0; if (printall) { if (profit > 0) System.out.println("Sold for a profit of " + profit); else if (profit < 0) System.out.println("Sold for a loss of " + profit); System.out.println("profit, price_borrowed, price_sold = " + profit + ", " + price_borrowed + ", " + price_sold); } } if ((long_buy && in_transaction == 0) && !trading_closed) { price_bought = ask.get(ask.size() - 1); in_transaction = 1; if (printall) System.out.println("Entered long transaction at " + price_bought); } } else if (current_signal < 0 && prev_signal >= 0) //if in transaction and signal goes below, sell { //last_price = daily_price.get(daily_size-1); last_price = bid.get(bid.size() - 1); if (long_buy && in_transaction == 1) { price_sold = bid.get(bid.size() - 1); profit = price_sold - price_bought; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); made_trade = true; dailyoutret.add(bid.get(bid.size() - 1) - log_ret); log_ret = bid.get(bid.size() - 1).doubleValue(); in_transaction = 0; if (printall) { if (profit > 0) System.out.println("Sold for a profit of " + profit); else if (profit < 0) System.out.println("Sold for a loss of " + profit); System.out.println("profit, price_bought, price_sold = " + profit + ", " + price_bought + ", " + price_sold); } } if ((short_sell && out_transaction == 0) && !trading_closed) { price_borrowed = bid.get(bid.size() - 1); out_transaction = 1; if (printall) System.out.println("Entered short transaction at " + price_borrowed); } } } if (signal_strength_rule && ((in_transaction == 0 && out_transaction == 0) && !trading_closed)) { if (current_signal > 0) //new point positive, we see momentum, buy { //last_price = daily_price.get(daily_size-1); last_price = ask.get(ask.size() - 1); if (short_sell && out_transaction == 1) //in a short-sell transaction, sell { price_sold = ask.get(ask.size() - 1); profit = price_borrowed - price_sold; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); made_trade = true; dailyoutret.add(ask.get(ask.size() - 1) - log_ret); log_ret = ask.get(ask.size() - 1).doubleValue(); out_transaction = 0; if (printall) System.out.println("profit, price_borrowed, price_sold = " + profit + ", " + price_borrowed + ", " + price_sold); } if (long_buy && in_transaction == 0) { price_bought = ask.get(ask.size() - 1); in_transaction = 1; if (printall) System.out.println("Entered long transaction at " + price_bought); } } else if (current_signal < 0) //if in transaction and signal goes below, sell { last_price = bid.get(bid.size() - 1); if (long_buy && in_transaction == 1) { price_sold = bid.get(bid.size() - 1); profit = price_sold - price_bought; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); made_trade = true; dailyoutret.add(bid.get(bid.size() - 1) - log_ret); log_ret = bid.get(bid.size() - 1).doubleValue(); in_transaction = 0; if (printall) System.out.println("Bought for a profit of " + profit); if (printall) System.out.println("profit, price_bought, price_sold = " + profit + ", " + price_bought + ", " + price_sold); } if (short_sell && out_transaction == 0) { price_borrowed = bid.get(bid.size() - 1); out_transaction = 1; if (printall) System.out.println("Entered short transaction at " + price_borrowed); } } } if (!made_trade) { account.add(date_stamp + " " + amount); dailyoutret.add(price.get(price.size() - 1) - log_ret); log_ret = price.get(price.size() - 1); } day_count++; //if(recompute_day == day_count) {day_count=0;} //if(printall) System.out.println(""+date_stamp + ", " + formatter3.format(price.get(price.size()-1)) + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", " + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl)); allsignal.add(date_stamp + " " + current_signal); } } if (trading_hours)// && !trading_closed)// && cur_min != 30) { if (cur_pnl != 0 && date_stamp.indexOf("22:30:00") != -1) //close out position { if (in_transaction == 1) { //System.out.println("Ending trading for next startup"); price_sold = price.get(price.size() - 1); profit = price_sold - price_bought; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; count = account.size() - 1; amount = getAmount(account.get(count)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); dailyoutret.add(price.get(price.size() - 1) - log_ret); log_ret = price.get(price.size() - 1); in_transaction = 0; //-- return stop loss to original setting -------- //System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit); stop_loss = global_stop_loss; red_zone = false; } else if (out_transaction == 1) { //System.out.println("Ending trading for next startup"); price_sold = price.get(price.size() - 1); profit = price_borrowed - price_sold; // price_sold = price_borrowed + stop_loss; // profit = -stop_loss; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; count = account.size() - 1; amount = getAmount(account.get(count)); //account.add(date_stamp + " " + (amount - stop_loss)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); dailyoutret.add(price.get(price.size() - 1) - log_ret); log_ret = price.get(price.size() - 1); out_transaction = 0; //-- return stop loss to original setting -------- stop_loss = global_stop_loss; red_zone = false; } } if (in_transaction == 1) //in a long transaction { if (red_zone && bid.get(bid.size() - 1) > last_price) //check if new high price { last_price = bid.get(bid.size() - 1); } //cur_pnl = price.get(price.size()-1) - last_price; cur_pnl = bid.get(bid.size() - 1) - last_price; lo_pnl = lo_price.get(lo_price.size() - 1) - last_price; hi_pnl = hi_price.get(hi_price.size() - 1) - last_price; if (cur_pnl < -stop_loss) { if (printall) System.out.println( "Stop-loss Activated since lo_pnl = " + cur_pnl + " < -" + stop_loss); //System.out.println("Closing price at bar was " + price.get(price.size()-1) + " but lowest price in bar was " + lo_price.get(lo_price.size()-1)); //--------------sell---------- //price_sold = price.get(price.size()-1); //price_sold = price.get(price.size()-1); price_sold = bid.get(bid.size() - 1); profit = price_sold - price_bought; // price_sold = price_bought - stop_loss; // profit = -stop_loss; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; count = account.size() - 1; amount = getAmount(account.get(count)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); //if(weekend.dayOfWeek().getAsText().equals("Sunday")) {sunday.add(date_stamp + " " + profit);} dailyoutret.add(bid.get(bid.size() - 1) - log_ret); log_ret = bid.get(bid.size() - 1); in_transaction = 0; //-- return stop loss to original setting -------- if (printall) System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit); stop_loss = global_stop_loss; red_zone = false; } else if (cur_pnl >= take_profit_thresh) { if (printall) System.out.println("Profit zone activated since cur_pnl = " + cur_pnl + " > " + take_profit_thresh); //price_sold = price.get(price.size()-1); price_sold = bid.get(bid.size() - 1); profit = price_sold - price_bought; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; count = account.size() - 1; amount = getAmount(account.get(count)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); //if(weekend.dayOfWeek().getAsText().equals("Sunday")) {sunday.add(date_stamp + " " + profit);} dailyoutret.add(price_sold - log_ret); log_ret = price_sold; in_transaction = 0; //-- return stop loss to original setting -------- if (printall) System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit); stop_loss = global_stop_loss; // stop_loss = profitable_stop; // last_price = price.get(price.size()-1); // red_zone = true; } } else if (out_transaction == 1) { if (red_zone && ask.get(ask.size() - 1) < last_price) //check if new high price { last_price = ask.get(ask.size() - 1); } //cur_pnl = last_price - price.get(price.size()-1); cur_pnl = last_price - ask.get(ask.size() - 1); lo_pnl = last_price - hi_price.get(hi_price.size() - 1); hi_pnl = last_price - lo_price.get(lo_price.size() - 1); if (cur_pnl < -stop_loss) { if (printall) System.out.println( "Stop-loss Activated since lo_pnl = " + cur_pnl + " < -" + stop_loss); //System.out.println("Closing price at bar was " + price.get(price.size()-1) + " but highest price in bar was " + hi_price.get(hi_price.size()-1)); //--------------sell---------- //price_sold = price.get(price.size()-1); price_sold = ask.get(ask.size() - 1); profit = price_borrowed - price_sold; // price_sold = price_borrowed + stop_loss; // profit = -stop_loss; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; count = account.size() - 1; amount = getAmount(account.get(count)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); //if(weekend.dayOfWeek().getAsText().equals("Sunday")) {sunday.add(date_stamp + " " + profit);} dailyoutret.add(price_sold - log_ret); log_ret = price_sold; out_transaction = 0; //-- return stop loss to original setting -------- stop_loss = global_stop_loss; red_zone = false; if (printall) System.out .println("Bought at " + date_stamp + " for a profit/loss of " + profit); } else if (cur_pnl >= take_profit_thresh) { if (printall) System.out.println("Profit zone activated since cur_pnl = " + cur_pnl + " > " + take_profit_thresh); // stop_loss = profitable_stop; // last_price = price.get(price.size()-1); // red_zone = true; //price_sold = price.get(price.size()-1); price_sold = ask.get(ask.size() - 1); profit = price_borrowed - price_sold; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; count = account.size() - 1; amount = getAmount(account.get(count)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); //if(weekend.dayOfWeek().getAsText().equals("Sunday")) {sunday.add(date_stamp + " " + profit);} dailyoutret.add(price_sold - log_ret); log_ret = price_sold; out_transaction = 0; //-- return stop loss to original setting -------- stop_loss = global_stop_loss; red_zone = false; if (printall) System.out .println("Bought at " + date_stamp + " for a profit/loss of " + profit); } } else if (downtick_strategy) { //strategy here is to buy/sell according to signal iff downtick has occurred if (current_signal > 0 && (price_sold > price.get(price.size() - 1))) { //let's buy some more if (printall) System.out.println("Buying at " + date_stamp + " since last price sold = " + price_sold + " > " + price.get(price.size() - 1)); price_bought = price.get(price.size() - 1); last_price = price.get(price.size() - 1); in_transaction = 1; } else if (current_signal < 0 && (price_sold < price.get(price.size() - 1))) { //let's short some more if (printall) System.out.println( "Shorting at " + date_stamp + " since last price bought back at = " + price_sold + " < " + price.get(price.size() - 1)); price_borrowed = price.get(price.size() - 1); last_price = price.get(price.size() - 1); out_transaction = 1; if (printall) System.out.println("Entered short transaction at " + price_borrowed); } cur_pnl = 0; lo_pnl = 0; hi_pnl = 0; } else { cur_pnl = 0; lo_pnl = 0; hi_pnl = 0; } if (weekend.dayOfWeek().getAsText().equals("Friday") && date_stamp.indexOf("17:00:00") != -1) { if (printall) System.out.println("End of week"); } dailyReport.add("" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1)) + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", " + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl)); if (printall) { if (current_signal > 0) { System.out .println("" + date_stamp + ", " + formatter3.format(bid.get(bid.size() - 1)) + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", " + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl)); } else { System.out .println("" + date_stamp + ", " + formatter3.format(ask.get(ask.size() - 1)) + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", " + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl)); } } allsignal.add(date_stamp + " " + current_signal); } } } double mean_ntrades = 0; computed = true; dailyoutret.set(1, 0.0); double[] dreturns = new double[account.size()]; dreturns[0] = 0; double mean = 0; double sd = 0; n_neg_ret = 0; n_pos_ret = 0; neg_ret_mean = 0; pos_ret_mean = 0; pnl = 0; int n_trades = 0; ArrayList<Integer> n_trades_day = new ArrayList<Integer>(); double pret; ArrayList<Double> perf_rets = new ArrayList<Double>(); ArrayList<String> perf_datestimes = new ArrayList<String>(); for (i = 1; i < account.size(); i++) { out.println(account.get(i)); dailyout.println(account.get(i) + " " + dailyoutret.get(i)); //System.out.println(account.get(i)); dreturns[i] = getAmount(account.get(i)) - getAmount(account.get(i - 1)); if (jpy) { dreturns[i] = dreturns[i] * .01; } dates = account.get(i).split("[ ]+"); if (!perf_dates.contains(dates[0])) //first date entry { if (perf_dates.size() != 0) { perf_returns.add(pnl); n_trades_day.add(n_trades); } perf_dates.add(dates[0]); if (dreturns[i] != 0) { perf_datestimes.add(dates[0] + " " + dates[1]); perf_rets.add(dreturns[i]); } pnl = dreturns[i]; if (dreturns[i] != 0) { n_trades = 1; } else { n_trades = 0; } } else //already contains the date, so add on pnl { pnl = pnl + dreturns[i]; //System.out.println(dreturns[i]); if (dreturns[i] != 0) { n_trades++; perf_datestimes.add(dates[0] + " " + dates[1]); perf_rets.add(dreturns[i]); } // System.out.println(n_trades);} } if (dreturns[i] > 0) { n_pos_ret++; pos_ret_mean = pos_ret_mean + dreturns[i]; mean = mean + dreturns[i]; } else if (dreturns[i] < 0) { n_neg_ret++; neg_ret_mean = neg_ret_mean - dreturns[i]; mean = mean + dreturns[i]; } } perf_returns.add(pnl); n_trades_day.add(n_trades); for (i = 0; i < perf_dates.size(); i++) { if (printall) System.out.println(perf_dates.get(i) + " " + perf_returns.get(i) + " " + n_trades_day.get(i)); perform.println(perf_dates.get(i) + " " + perf_returns.get(i)); date_returns.add(perf_dates.get(i) + " " + perf_returns.get(i)); mean_ntrades = mean_ntrades + n_trades_day.get(i); } mean_ntrades = mean_ntrades / n_trades_day.size(); dates_price = new String[n_obs]; for (i = 1; i < n_obs; i++) { tokens = allsignal.get(allsignal.size() - n_obs + i).split("[ ]+"); if (perf_datestimes.contains(latestDates.get(latestDates.size() - n_obs + i))) { pret = perf_rets.get(perf_datestimes.indexOf(latestDates.get(latestDates.size() - n_obs + i))); System.out.println(latestDates.get(latestDates.size() - n_obs + i) + " " + pret); } else { pret = 0; } //System.out.println(tokens[0] + " " + latestDates.get(latestDates.size() - n_obs + i - 1)); // if(tokens[0].equals(latestDates.get(latestDates.size() - n_obs + i - 1))) // { dates_price[i] = new String(latestDates.get(latestDates.size() - n_obs + i) + " " + (mid.get(mid.size() - n_obs + i) - mid.get(mid.size() - n_obs + i - 1)) + " " + mid.get(mid.size() - n_obs + i) + " " + tokens[2] + " " + pret); // } } dates_price[0] = dates_price[1]; mean = mean / (n_pos_ret + n_neg_ret); System.out.println("ROI = " + account.get(account.size() - 1)); //--- compute stats--------------- double risk = neg_ret_mean / (double) n_neg_ret; System.out.println("neg_ret_mean = " + (-neg_ret_mean) + ", " + n_neg_ret); double reward = pos_ret_mean / (double) n_pos_ret; System.out.println("pos_ret_mean = " + pos_ret_mean + ", " + n_pos_ret); double win_ratio = (double) (n_pos_ret) / (n_pos_ret + n_neg_ret); kellyPerc = win_ratio - (1.0 - win_ratio) * (risk / reward); ulcer_index = ulcerIndex(dreturns); System.out.println("win ratio = " + win_ratio + ", risk = " + risk + ", reward = " + reward); System.out.println("kelly and ulcer = " + kellyPerc + " " + ulcer_index); for (i = 0; i < dreturns.length; i++) { sd = sd + (dreturns[i] - mean) * (dreturns[i] - mean) / ((double) dreturns.length); } standard_deviation = Math.sqrt(sd); sharpeRatio = Math.sqrt(250) * mean / standard_deviation; maxdraw = computeDrawdown(dreturns); rank_coeff = segmentRankCorrelation(30, dreturns); System.out.println("MeanRet = " + mean + ", Sharpe = " + sharpeRatio + ", MaxDD = " + maxdraw + ", Rank = " + rank_coeff + ", avg_n_trades = " + mean_ntrades); out.close(); dailyout.close(); perform.close(); b0_coeff.close(); } catch (ParseException fe) { System.out.println("ParseException"); } catch (NullPointerException fe) { System.out.println("Null pointer"); } catch (IllegalArgumentException fe) { System.out.println("IllegalArgument"); } catch (FileNotFoundException fe) { System.out.println("File not found..." + fe); } catch (IOException ioe) { System.out.println("IO procedure faulty..." + ioe); } n_files++; return computed; }
From source file:ch.imetrica.mdfaTradingStrategies.MDFAStrategyEvolution.java
public boolean startStrategyDailyIntradayOneHourBidAsk15Min(double sprd, boolean spreadon) { int j, i, l, N, file_count; double sum = 0; Double D;/*from www. j a va 2 s . com*/ String ddelims = "[-]"; boolean computed = false; boolean print_filter = false; boolean made_trade = true; String date_stamp, strline; int daily_size; double profit, price_borrowed, price_sold, price_bought; double current_price, prev_price; double last_price, cur_pnl, stop_loss, lo_pnl, hi_pnl; double log_ret = 0; signal = new double[trade_obs]; xt = new double[trade_obs]; lag_signals = new double[trade_obs]; prix = new double[trade_obs]; lo_prix = new double[trade_obs]; hi_prix = new double[trade_obs]; total_succ = 0; total = 0; log_price = 0; N = n_obs; avg_vol = 0.0; b_avg = new double[L * n_rep]; count = 0; trade_succ_ratio = 0; double amount = 0; double prev_signal; reg_trading_hours = false; String[] intdates; //make sure arraylists empty ArrayList<String> perf_dates = new ArrayList<String>(); ArrayList<Double> perf_returns = new ArrayList<Double>(); double pnl; String[] dates; boolean inverse_hours = false; String time; ArrayList<String> account = new ArrayList<String>(); ArrayList<String> latestDates = new ArrayList<String>(); last_trades = new ArrayList<Integer>(); final_trades = new ArrayList<Double>(); dailyoutret = new ArrayList<Double>(); maxIntValue = new ArrayList<Double>(); avg_volatility = new ArrayList<Double>(); close_series = new ArrayList<Double>(); highlow_series = new ArrayList<Double>(); exp_series_1 = new ArrayList<Double>(); exp_series_2 = new ArrayList<Double>(); price = new ArrayList<Double>(); lo_price = new ArrayList<Double>(); hi_price = new ArrayList<Double>(); mid = new ArrayList<Double>(); bid = new ArrayList<Double>(); ask = new ArrayList<Double>(); dates_series = new ArrayList<String>(); dailyReport = new ArrayList<String>(); b0_trend = new ArrayList<Double>(); vol_0 = new ArrayList<Double>(); vol_1 = new ArrayList<Double>(); sub_returns = new ArrayList<Double>(); trade_days = new ArrayList<String>(); returns = new ArrayList<Double>(); longreturns = new ArrayList<Double>(); shortreturns = new ArrayList<Double>(); dropdowns = new ArrayList<Double>(); success = new ArrayList<Double>(); dates_low_high = new ArrayList<String>(); crits = new ArrayList<String>(); svm = new ArrayList<String>(); filters = new ArrayList<Filter>(); date_returns = new ArrayList<String>(); live_series = new ArrayList<Double>(); //the data to be applied out of sample ib_data_hash = new ibHash(); fridayROI = 0; fridayROI_pos = 0; fridays = 0; int end_hour; lookback_returns = new ArrayList<Double>(); num_pos_returns = 0; deg_0 = new ArrayList<Double>(); deg_1 = new ArrayList<Double>(); crit_0 = new ArrayList<Double>(); crit_1 = new ArrayList<Double>(); full_returns_array = new ArrayList<double[]>(); morning_returns = new ArrayList<double[]>(); morning_buy = true; //enter transaction at morning open morning_optimize = false; //optimize in the morning trading hours num_full_positive_returns = 0; //--- Now get historical interp values ------ //uploadInterpParams("max_int.dat"); //------------------------------------------- forex24 = true; ret_dist = new double[trade_obs]; pos_ret_dist = new int[trade_obs]; neg_ret_dist = new int[trade_obs]; neg_trades_started = new int[trade_obs]; pos_trades_started = new int[trade_obs]; neg_trades_started_mean = new double[trade_obs]; pos_trades_started_mean = new double[trade_obs]; diff_account = new double[trade_obs]; pos_ret_mean_time = new double[trade_obs]; neg_ret_mean_time = new double[trade_obs]; mdfaTrades = new ArrayList<MDFATrade>(); fmt = DateTimeFormat.forPattern("y-MM-dd HH:mm:ss"); formatter = new DecimalFormat("#0.000000"); formatter3 = new DecimalFormat("#0.00000"); formatter2 = new DecimalFormat("#0.00"); histo_stat = new int[100]; interp_vals = new ArrayList<Double>(); max_ranks = new ArrayList<Double>(); profit_baby = 0; //setForecastDFAParameters(); bad_starts = 0; n_out_samp = 0; //take_profit = true; //take_profit_thresh = .0020; current_signal = 0; prev_price = 0; cur_pnl = 0; stop_loss = stop_loss_thresh; out_transaction = 0; in_transaction = 0; red_zone = false; global_stop_loss = stop_loss_thresh; profitable_stop = .0005; count = 0; short_sell = true; long_buy = true; day_count = 0; ArrayList<String> trade_times = new ArrayList<String>(); ArrayList<String> fxcm_dates = new ArrayList<String>(); lo_pnl = 0; hi_pnl = 0; price_borrowed = 0; price_sold = 0; price_bought = 0; last_price = 0; binary_rule = true; signal_strength_rule = true; downtick_strategy = false; signal_profit = false; friday_closing = false; //asian_close = true; boolean spread_on = spreadon; //----- Fill up with times here ------------------------- for (i = 0; i < 10; i++) { trade_times.add("0" + i + ":00:00"); } for (i = 10; i < 24; i++) { trade_times.add(i + ":00:00"); } for (i = 0; i < 10; i++) { trade_times.add("0" + i + ":15:00"); } for (i = 10; i < 24; i++) { trade_times.add(i + ":15:00"); } for (i = 0; i < 10; i++) { trade_times.add("0" + i + ":30:00"); } for (i = 10; i < 24; i++) { trade_times.add(i + ":30:00"); } for (i = 0; i < 10; i++) { trade_times.add("0" + i + ":45:00"); } for (i = 10; i < 24; i++) { trade_times.add(i + ":45:00"); } // trade_times.add("00:00:00"); // trade_times.add("06:00:00"); // trade_times.add("12:00:00"); // trade_times.add("18:00:00"); String[] hourToks = startingTime.split("[:]+"); start_hour = (new Integer(hourToks[0])).intValue(); hourToks = endingTime.split("[:]+"); end_hour = (new Integer(hourToks[0])).intValue(); inverse_hours = false; if (start_hour > end_hour) //must switch hours here { int temphour = end_hour; inverse_hours = true; end_hour = start_hour; start_hour = temphour; } if (ib_data && ib_data_file != null) { try { fin = new FileInputStream(ib_data_file); din = new DataInputStream(fin); br = new BufferedReader(new InputStreamReader(din)); while ((strline = br.readLine()) != null) { String[] sp = strline.split("[,]+"); ib_data_hash.put(sp[0], new String( sp[1] + " " + sp[2] + " " + sp[3] + " " + sp[4] + " " + sp[5] + " " + sp[6])); //System.out.println(sp[1] + " " + sp[2] + " " + sp[3] + " " + sp[4] + " " + sp[5] + " " + sp[6]); } } catch (FileNotFoundException fe) { System.out.println("File not found..." + fe); } catch (IOException ioe) { System.out.println("IO procedure faulty..." + ioe); } } jpy = false; try { PrintWriter b0_coeff = new PrintWriter(new FileWriter("b0_coeff.dat")); PrintWriter perform = new PrintWriter(new FileWriter("intraday_performance_" + n_files + ".dat")); PrintWriter dailyout = new PrintWriter(new FileWriter("daily_nasdaq.dat")); PrintWriter out = new PrintWriter(new FileWriter("strategy_results.dat")); for (file_count = 0; file_count < 1; file_count++) { if (dataFiles[file_count].indexOf("JPY") != -1 && dataFiles[file_count].indexOf("NOKJPY") == -1) { //change_time_zone = true; System.out.println("Changed time zone to Tokyo"); jpy = true; stop_loss_thresh = stop_loss_thresh * 100; take_profit_thresh = take_profit_thresh * 100; global_stop_loss = global_stop_loss * 100; stop_loss = stop_loss_thresh; } else if (futures_data) { //change_time_zone = true; System.out.println("Changed time zone to Tokyo"); stop_loss_thresh = stop_loss_thresh * 10000; stop_loss = stop_loss_thresh; take_profit_thresh = take_profit_thresh * 10000; global_stop_loss = global_stop_loss * 10000; } ArrayList<String> dayData = new ArrayList<String>(); setTimeStandards(new File(dataFiles[file_count])); System.out.println("opening " + dataFiles[file_count]); fin = new FileInputStream(dataFiles[file_count]); din = new DataInputStream(fin); br = new BufferedReader(new InputStreamReader(din)); lookback_ready = false; spread = new PrintWriter(new FileWriter("spread_" + dataFiles[file_count] + ".dat")); //if(print_debug)System.out.println("Entering loop..."); trading_hours = false; computed = false; //---- add the latest FXCM data - while ((strline = br.readLine()) != null) { dayData.add(strline); //add the date tokens = strline.split("[,]+"); fxcm_dates.add(tokens[0]); } //----- now add the lates IB data ------------ System.out.println("opening " + dataFiles[file_count] + " IB data"); String[] fxpair = dataFiles[file_count].split("[.]+"); if ((new File(fxpair[0] + ".IB.dat")).exists()) { fin = new FileInputStream(fxpair[0] + ".IB.dat"); din = new DataInputStream(fin); br = new BufferedReader(new InputStreamReader(din)); String lastFXCMdate = fxcm_dates.get(fxcm_dates.size() - 1); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss"); Date date1 = sdf.parse(lastFXCMdate); while ((strline = br.readLine()) != null) { tokens = strline.split("[,]+"); Date date2 = sdf.parse(tokens[0]); if (!fxcm_dates.contains(tokens[0]) && date1.before(date2)) { System.out.println("New time and observation " + strline); dayData.add(strline); } } } for (int ts = 0; ts < dayData.size(); ts++) { strline = dayData.get(ts); //System.out.println(strline); tokens = strline.split(delims); n_toks = tokens.length; //System.out.println("Number of toks = "+n_toks); if (n_toks == 0) { System.out.println("End of file"); break; } if (n_toks >= 6) { bid_ask_data = true; } else { bid_ask_data = false; } date_stamp = tokens[0]; date_tokens = date_stamp.split(date_delims); intdates = date_tokens[0].split(ddelims); DateTime weekend = new DateTime((new Integer(intdates[0])).intValue(), (new Integer(intdates[1])).intValue(), (new Integer(intdates[2])).intValue(), 14, 0); time = date_tokens[1]; //insampStart is the time we collect daily data //if(date_stamp.indexOf(insampStart) != -1) if (trade_times.contains(time)) { //get bid/mid/ask data if (ib_data && ib_data_hash.containsKey(tokens[0])) { String[] hashed = ib_data_hash.get(tokens[0]).split("[ ]+"); for (i = 1; i < hashed.length; i++) { tokens[i] = hashed[i]; } // System.out.print(tokens[i] + " ");} } daily_price.add(new Double(tokens[1])); current_price = (new Double(tokens[1])).doubleValue(); if (daily_price.size() == 1) { daily_returns.add(new Double(0.0)); prev_price = current_price; } else { daily_returns.add(log(current_price) - log(prev_price)); prev_price = current_price; } daily_dates.add(date_stamp); } print_filter = false; latestDates.add(date_stamp); D = new Double(tokens[4]); close_series.add(D); if (ib_data && ib_data_hash.containsKey(tokens[0])) { String[] hashed = ib_data_hash.get(tokens[0]).split("[ ]+"); //System.out.println("Contains " + tokens[0] + ", lengths = " + hashed.length + ", " + tokens.length); for (i = 1; i < hashed.length; i++) { tokens[i] = hashed[i]; } // System.out.print(tokens[i] + " ");} bid.add((new Double(tokens[2])) - sprd); ask.add((new Double(tokens[3])) + sprd); mid.add(new Double(tokens[1])); } else { bid.add((new Double(tokens[2])) - sprd); ask.add((new Double(tokens[3])) + sprd); mid.add(new Double(tokens[1])); } D = new Double(tokens[1]); //price.add(D); if (spread_on) { if (current_signal > 0) { price.add(ask.get(ask.size() - 1)); } else { price.add(bid.get(bid.size() - 1)); } // if(current_signal > 0) // {price.add(mid.get(mid.size()-1) - sprd );} // else // {price.add(mid.get(mid.size()-1) + sprd );} } else { price.add(mid.get(mid.size() - 1)); } D = new Double(tokens[4]); if (ib_data && ib_data_hash.containsKey(tokens[0])) { live_series.add(log(mid.get(mid.size() - 1)) - log(mid.get(mid.size() - 2))); } else { live_series.add(D); } // if(ib_data && ib_data_hash.containsKey(tokens[0])) //use as is // {lo_price.add(new Double(tokens[7])); hi_price.add(new Double(tokens[8]));} // else // {lo_price.add((new Double(tokens[7]))); hi_price.add((new Double(tokens[8])));} lo_price.add((new Double(tokens[2]))); hi_price.add((new Double(tokens[3]))); //---- start the account ------ if (account.size() == 0) { account.add(date_stamp + " " + 0); dailyoutret.add(0.0); } String[] hours = time.split("[:]+"); cur_hour = (new Integer(hours[0])).intValue(); (new Integer(hours[1])).intValue(); trading_closed = false; //if currently not in a transaction and between the hours of midnight and start-hour, then no new //positions will be opened if (asian_close) //only closed if most recent transaction was closed artificially through SL or TP after end hours { if ((in_transaction == 0 && out_transaction == 0) && cur_hour >= start_hour && cur_hour <= end_hour) { trading_closed = true; } // if(printall) System.out.println(cur_hour + " " + start_hour);} } else { if (cur_hour >= start_hour && cur_hour <= end_hour) { trading_closed = true; } // if(printall) {System.out.println(cur_hour + " " + start_hour);}} } if (inverse_hours) { trading_closed = !trading_closed; } its_closing_time = (weekend.dayOfWeek().getAsText().equals("Friday") && date_stamp.indexOf("17:00:00") != -1); made_trade = false; if (daily_returns.size() >= n_obs && trade_times.contains(time)) //a new day begineth { computed = true; trading_hours = true; tseries = new double[n_rep * n_obs]; for (i = 0; i < n_obs; i++) { tseries[n_obs - 1 - i] = daily_returns.get(daily_returns.size() - 1 - i); tseries[n_obs + n_obs - 1 - i] = daily_returns.get(daily_returns.size() - 1 - i); if (n_rep > 2 && exp_series_1.size() > 0) { tseries[n_obs * 2 + n_obs - 1 - i] = exp_series_1.get(exp_series_1.size() - 1 - i); } if (n_rep > 3 && exp_series_2.size() > 0) { tseries[n_obs * 3 + n_obs - 1 - i] = exp_series_2.get(exp_series_2.size() - 1 - i); } } mdfa.set_tseries(tseries, n_obs, n_rep); //if(day_count == 0) //recompute filter coefficients if (day_count == 0 || weekend.dayOfWeek().getAsText().equals("Sunday") && date_stamp.indexOf("18:00:00") != -1) { if (printall) System.out.println("Recomputing filter..."); mdfa.computeFilterGeneral(true, print_filter); b_coeffs = new double[(n_rep - 1) * L]; //System.out.println(b_coeffs.length + " " + L + n_rep); for (l = 0; l < L; l++) { for (i = 0; i < n_rep - 1; i++) { b_coeffs[L * i + l] = mdfa.b[L * (i + 1) + l]; } // System.out.println(b_coeffs[l]);} //if(date_stamp.indexOf("2013-12-17") != -1) {System.out.println(b_coeffs[l]);} } if (printall) System.out.println(date_stamp + " b_coeffs = " + b_coeffs[0] + " " + b_coeffs[1] + " " + b_coeffs[2]); b_copy = new double[mdfa.b.length]; System.arraycopy(mdfa.b, 0, b_copy, 0, b_copy.length); b0_coeff.println(b_coeffs[0]); // + ", " + b_coeffs[L] + ", " + b_coeffs[2*L]); } sum = 0.0; for (j = 1; j < n_rep; j++) { for (l = 0; l < L; l++) { sum = sum + b_coeffs[L * (j - 1) + l] * tseries[N * j + n_obs - 1 - l]; } } prev_signal = current_signal; current_signal = sum; if (sig_inverse) { current_signal = -current_signal; } //----final signal --- daily_signal.add(current_signal); daily_size = daily_price.size(); dailyReport.add( "New day " + date_stamp + ", " + formatter3.format(daily_price.get(daily_size - 1)) + ", " + formatter.format(tseries[n_obs - 1]) + ", " + current_signal); //if(printall) System.out.println("New day "+date_stamp + ", " + formatter3.format(daily_price.get(daily_size-1)) + ", " + formatter.format(tseries[n_obs-1]) + ", " + current_signal); //--compute binary trading rule --- //if(printall) System.out.println("Current signal = " + current_signal + " Prev signal = " + prev_signal + ", trading_closed = " + trading_closed); if (friday_closing && its_closing_time) { if (printall) System.out.println("\nIt's Friday at 5pm, time to close shop for week"); if (current_signal > 0 && in_transaction == 1) //in a long transaction { price_sold = daily_price.get(daily_size - 1); profit = price_sold - price_bought; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); made_trade = true; dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); in_transaction = 0; if (printall) { if (profit > 0) System.out.println("Sold for a profit of " + profit); else if (profit < 0) System.out.println("Sold for a loss of " + profit); System.out.println("profit, price_bought, price_sold = " + profit + ", " + price_bought + ", " + price_sold); } } else if (current_signal < 0 && out_transaction == 1) //in a short transaction { price_sold = daily_price.get(daily_size - 1); profit = price_borrowed - price_sold; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); made_trade = true; dailyoutret.add(daily_price.get(daily_size - 1) - log_ret); log_ret = daily_price.get(daily_size - 1).doubleValue(); out_transaction = 0; if (printall) System.out.println("profit, price_borrowed, price_sold = " + profit + ", " + price_borrowed + ", " + price_sold); } } else { //if(binary_rule && !trading_closed) if (binary_rule) { // if(signal_profit) // { // //if(printall) System.out.println("Trading_closed = " + trading_closed); // if((current_signal > 0 && in_transaction == 1) && (daily_price.get(daily_size-1) > last_price)) // { // // // price_sold = daily_price.get(daily_size-1); // profit = price_sold - price_bought; // if(profit > 0) {succ_trades=succ_trades+1;} // total_trades=total_trades+1; // // amount = getAmount(account.get(account.size()-1)); // account.add(date_stamp + " " + (amount + profit)); // amount = getAmount(account.get(account.size()-1)); // // made_trade = true; // dailyoutret.add(daily_price.get(daily_size-1) - log_ret); // log_ret = daily_price.get(daily_size-1).doubleValue(); // // in_transaction = 0; // // if(printall){ // if(profit>0) System.out.println("Sold for a profit of " + profit); // else if(profit<0) System.out.println("Sold for a loss of " + profit); // System.out.println("profit, price_bought, price_sold = " + profit + ", " + price_bought + ", " + price_sold); // } // // if(!trading_closed) // {price_bought = daily_price.get(daily_size-1); // //in_transaction = 1; // out_transaction = 1; // last_price = daily_price.get(daily_size-1); // if(printall) System.out.println("Entered long transaction at " + price_bought); // } // } // else if((current_signal < 0 && out_transaction == 1) && (daily_price.get(daily_size-1) < last_price)) // { // // price_sold = daily_price.get(daily_size-1); // profit = price_borrowed - price_sold; // // if(profit > 0) {succ_trades=succ_trades+1;} // total_trades=total_trades+1; // // amount = getAmount(account.get(account.size()-1)); // account.add(date_stamp + " " + (amount + profit)); // amount = getAmount(account.get(account.size()-1)); // // made_trade = true; // dailyoutret.add(daily_price.get(daily_size-1) - log_ret); // log_ret = daily_price.get(daily_size-1).doubleValue(); // // // out_transaction = 0; // // if(printall) // { // if(profit>0) System.out.println("Sold for a profit of " + profit); // else if(profit<0) System.out.println("Sold for a loss of " + profit); // } // if(printall) System.out.println("profit, price_borrowed, price_sold = " + profit + ", " + price_borrowed + ", " + price_sold); // // if(!trading_closed) // { // price_borrowed = daily_price.get(daily_size-1); // //out_transaction = 1; // in_transaction = 1; // if(printall)System.out.println("Entered short transaction at " + price_borrowed); // } // } // // } made_trade = false; if (current_signal > 0 && prev_signal <= 0) //new point positive, we see momentum, buy { //last_price = daily_price.get(daily_size-1); last_price = ask.get(ask.size() - 1); if (short_sell && out_transaction == 1) //in a short-sell transaction, buy { //price_sold = daily_price.get(daily_size-1); price_sold = ask.get(ask.size() - 1); profit = price_borrowed - price_sold; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); made_trade = true; dailyoutret.add(ask.get(ask.size() - 1) - log_ret); log_ret = ask.get(ask.size() - 1).doubleValue(); out_transaction = 0; if (printall) { if (profit > 0) System.out.println("Sold for a profit of " + profit); else if (profit < 0) System.out.println("Sold for a loss of " + profit); System.out.println("profit, price_borrowed, price_sold = " + profit + ", " + price_borrowed + ", " + price_sold); } } if ((long_buy && in_transaction == 0) && !trading_closed) { price_bought = ask.get(ask.size() - 1); in_transaction = 1; if (printall) System.out.println("Entered long transaction at " + price_bought); } } else if (current_signal < 0 && prev_signal >= 0) //if in transaction and signal goes below, sell { //last_price = daily_price.get(daily_size-1); last_price = bid.get(bid.size() - 1); if (long_buy && in_transaction == 1) { price_sold = bid.get(bid.size() - 1); profit = price_sold - price_bought; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); made_trade = true; dailyoutret.add(bid.get(bid.size() - 1) - log_ret); log_ret = bid.get(bid.size() - 1).doubleValue(); in_transaction = 0; if (printall) { if (profit > 0) System.out.println("Sold for a profit of " + profit); else if (profit < 0) System.out.println("Sold for a loss of " + profit); System.out.println("profit, price_bought, price_sold = " + profit + ", " + price_bought + ", " + price_sold); } } if ((short_sell && out_transaction == 0) && !trading_closed) { price_borrowed = bid.get(bid.size() - 1); out_transaction = 1; if (printall) System.out.println("Entered short transaction at " + price_borrowed); } } } if (signal_strength_rule && ((in_transaction == 0 && out_transaction == 0) && !trading_closed)) { if (current_signal > 0) //new point positive, we see momentum, buy { //last_price = daily_price.get(daily_size-1); last_price = ask.get(ask.size() - 1); if (short_sell && out_transaction == 1) //in a short-sell transaction, sell { price_sold = ask.get(ask.size() - 1); profit = price_borrowed - price_sold; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); made_trade = true; dailyoutret.add(ask.get(ask.size() - 1) - log_ret); log_ret = ask.get(ask.size() - 1).doubleValue(); out_transaction = 0; if (printall) System.out.println("profit, price_borrowed, price_sold = " + profit + ", " + price_borrowed + ", " + price_sold); } if (long_buy && in_transaction == 0) { price_bought = ask.get(ask.size() - 1); in_transaction = 1; if (printall) System.out.println("Entered long transaction at " + price_bought); } } else if (current_signal < 0) //if in transaction and signal goes below, sell { last_price = bid.get(bid.size() - 1); if (long_buy && in_transaction == 1) { price_sold = bid.get(bid.size() - 1); profit = price_sold - price_bought; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; amount = getAmount(account.get(account.size() - 1)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); made_trade = true; dailyoutret.add(bid.get(bid.size() - 1) - log_ret); log_ret = bid.get(bid.size() - 1).doubleValue(); in_transaction = 0; if (printall) System.out.println("Bought for a profit of " + profit); if (printall) System.out.println("profit, price_bought, price_sold = " + profit + ", " + price_bought + ", " + price_sold); } if (short_sell && out_transaction == 0) { price_borrowed = bid.get(bid.size() - 1); out_transaction = 1; if (printall) System.out.println("Entered short transaction at " + price_borrowed); } } } if (!made_trade) { account.add(date_stamp + " " + amount); dailyoutret.add(price.get(price.size() - 1) - log_ret); log_ret = price.get(price.size() - 1); } day_count++; //if(recompute_day == day_count) {day_count=0;} //if(printall) System.out.println(""+date_stamp + ", " + formatter3.format(price.get(price.size()-1)) + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", " + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl)); allsignal.add(date_stamp + " " + current_signal); } } if (trading_hours)// && !trading_closed)// && cur_min != 30) { // if(cur_pnl != 0 && date_stamp.indexOf("22:30:00") != -1) //close out position // { // if(in_transaction == 1) // { // // //System.out.println("Ending trading for next startup"); // // // price_sold = price.get(price.size()-1); // profit = price_sold - price_bought; // // if(profit > 0) {succ_trades=succ_trades+1;} // total_trades=total_trades+1; // // count = account.size()-1; // amount = getAmount(account.get(count)); // // account.add(date_stamp + " " + (amount + profit)); // amount = getAmount(account.get(account.size()-1)); // // // dailyoutret.add(price.get(price.size()-1) - log_ret); // log_ret = price.get(price.size()-1); // // in_transaction = 0; // // //-- return stop loss to original setting -------- // //System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit); // stop_loss = global_stop_loss; // red_zone = false; // // } // else if(out_transaction == 1) // { // // //System.out.println("Ending trading for next startup"); // // // price_sold = price.get(price.size()-1); // profit = price_borrowed - price_sold; // // // // price_sold = price_borrowed + stop_loss; // // profit = -stop_loss; // // if(profit > 0) {succ_trades=succ_trades+1;} // total_trades=total_trades+1; // // count = account.size()-1; // amount = getAmount(account.get(count)); // // //account.add(date_stamp + " " + (amount - stop_loss)); // account.add(date_stamp + " " + (amount + profit)); // amount = getAmount(account.get(account.size()-1)); // // dailyoutret.add(price.get(price.size()-1) - log_ret); // log_ret = price.get(price.size()-1); // // // out_transaction = 0; // // //-- return stop loss to original setting -------- // stop_loss = global_stop_loss; // red_zone = false; // // } // } if (in_transaction == 1) //in a long transaction { if (red_zone && bid.get(bid.size() - 1) > last_price) //check if new high price { last_price = bid.get(bid.size() - 1); } //cur_pnl = price.get(price.size()-1) - last_price; cur_pnl = bid.get(bid.size() - 1) - last_price; lo_pnl = lo_price.get(lo_price.size() - 1) - last_price; hi_pnl = hi_price.get(hi_price.size() - 1) - last_price; if (cur_pnl < -stop_loss) { if (printall) System.out.println( "Stop-loss Activated since lo_pnl = " + cur_pnl + " < -" + stop_loss); //System.out.println("Closing price at bar was " + price.get(price.size()-1) + " but lowest price in bar was " + lo_price.get(lo_price.size()-1)); //--------------sell---------- //price_sold = price.get(price.size()-1); //price_sold = price.get(price.size()-1); price_sold = bid.get(bid.size() - 1); profit = price_sold - price_bought; // price_sold = price_bought - stop_loss; // profit = -stop_loss; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; count = account.size() - 1; amount = getAmount(account.get(count)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); //if(weekend.dayOfWeek().getAsText().equals("Sunday")) {sunday.add(date_stamp + " " + profit);} dailyoutret.add(bid.get(bid.size() - 1) - log_ret); log_ret = bid.get(bid.size() - 1); in_transaction = 0; //-- return stop loss to original setting -------- if (printall) System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit); stop_loss = global_stop_loss; red_zone = false; } else if (cur_pnl >= take_profit_thresh) { if (printall) System.out.println("Profit zone activated since cur_pnl = " + cur_pnl + " > " + take_profit_thresh); //price_sold = price.get(price.size()-1); price_sold = bid.get(bid.size() - 1); profit = price_sold - price_bought; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; count = account.size() - 1; amount = getAmount(account.get(count)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); //if(weekend.dayOfWeek().getAsText().equals("Sunday")) {sunday.add(date_stamp + " " + profit);} dailyoutret.add(price_sold - log_ret); log_ret = price_sold; in_transaction = 0; //-- return stop loss to original setting -------- if (printall) System.out.println("Sold at " + date_stamp + " for a profit/loss of " + profit); stop_loss = global_stop_loss; // stop_loss = profitable_stop; // last_price = price.get(price.size()-1); // red_zone = true; } } else if (out_transaction == 1) { if (red_zone && ask.get(ask.size() - 1) < last_price) //check if new high price { last_price = ask.get(ask.size() - 1); } //cur_pnl = last_price - price.get(price.size()-1); cur_pnl = last_price - ask.get(ask.size() - 1); lo_pnl = last_price - hi_price.get(hi_price.size() - 1); hi_pnl = last_price - lo_price.get(lo_price.size() - 1); if (cur_pnl < -stop_loss) { if (printall) System.out.println( "Stop-loss Activated since lo_pnl = " + cur_pnl + " < -" + stop_loss); //System.out.println("Closing price at bar was " + price.get(price.size()-1) + " but highest price in bar was " + hi_price.get(hi_price.size()-1)); //--------------sell---------- //price_sold = price.get(price.size()-1); price_sold = ask.get(ask.size() - 1); profit = price_borrowed - price_sold; // price_sold = price_borrowed + stop_loss; // profit = -stop_loss; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; count = account.size() - 1; amount = getAmount(account.get(count)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); //if(weekend.dayOfWeek().getAsText().equals("Sunday")) {sunday.add(date_stamp + " " + profit);} dailyoutret.add(price_sold - log_ret); log_ret = price_sold; out_transaction = 0; //-- return stop loss to original setting -------- stop_loss = global_stop_loss; red_zone = false; if (printall) System.out .println("Bought at " + date_stamp + " for a profit/loss of " + profit); } else if (cur_pnl >= take_profit_thresh) { if (printall) System.out.println("Profit zone activated since cur_pnl = " + cur_pnl + " > " + take_profit_thresh); // stop_loss = profitable_stop; // last_price = price.get(price.size()-1); // red_zone = true; //price_sold = price.get(price.size()-1); price_sold = ask.get(ask.size() - 1); profit = price_borrowed - price_sold; if (profit > 0) { succ_trades = succ_trades + 1; } total_trades = total_trades + 1; count = account.size() - 1; amount = getAmount(account.get(count)); account.add(date_stamp + " " + (amount + profit)); amount = getAmount(account.get(account.size() - 1)); //if(weekend.dayOfWeek().getAsText().equals("Sunday")) {sunday.add(date_stamp + " " + profit);} dailyoutret.add(price_sold - log_ret); log_ret = price_sold; out_transaction = 0; //-- return stop loss to original setting -------- stop_loss = global_stop_loss; red_zone = false; if (printall) System.out .println("Bought at " + date_stamp + " for a profit/loss of " + profit); } } else if (downtick_strategy) { //strategy here is to buy/sell according to signal iff downtick has occurred if (current_signal > 0 && (price_sold > price.get(price.size() - 1))) { //let's buy some more if (printall) System.out.println("Buying at " + date_stamp + " since last price sold = " + price_sold + " > " + price.get(price.size() - 1)); price_bought = price.get(price.size() - 1); last_price = price.get(price.size() - 1); in_transaction = 1; } else if (current_signal < 0 && (price_sold < price.get(price.size() - 1))) { //let's short some more if (printall) System.out.println( "Shorting at " + date_stamp + " since last price bought back at = " + price_sold + " < " + price.get(price.size() - 1)); price_borrowed = price.get(price.size() - 1); last_price = price.get(price.size() - 1); out_transaction = 1; if (printall) System.out.println("Entered short transaction at " + price_borrowed); } cur_pnl = 0; lo_pnl = 0; hi_pnl = 0; } else { cur_pnl = 0; lo_pnl = 0; hi_pnl = 0; } if (weekend.dayOfWeek().getAsText().equals("Friday") && date_stamp.indexOf("17:00:00") != -1) { if (printall) System.out.println("End of week"); } dailyReport.add("" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1)) + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", " + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl)); if (printall) { if (current_signal > 0) { System.out.println( "" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1)) + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", " + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl)); } else { System.out.println( "" + date_stamp + ", " + formatter3.format(price.get(price.size() - 1)) + ", " + current_signal + ", " + formatter3.format(cur_pnl) + ", " + formatter3.format(lo_pnl) + ", " + formatter3.format(hi_pnl)); } } allsignal.add(date_stamp + " " + current_signal); } } } double mean_ntrades = 0; computed = true; dailyoutret.set(1, 0.0); double[] dreturns = new double[account.size()]; dreturns[0] = 0; double mean = 0; double sd = 0; n_neg_ret = 0; n_pos_ret = 0; neg_ret_mean = 0; pos_ret_mean = 0; pnl = 0; int n_trades = 0; ArrayList<Integer> n_trades_day = new ArrayList<Integer>(); double pret; ArrayList<Double> perf_rets = new ArrayList<Double>(); ArrayList<String> perf_datestimes = new ArrayList<String>(); for (i = 1; i < account.size(); i++) { out.println(account.get(i)); dailyout.println(account.get(i) + " " + dailyoutret.get(i)); //System.out.println(account.get(i)); dreturns[i] = getAmount(account.get(i)) - getAmount(account.get(i - 1)); if (jpy) { dreturns[i] = dreturns[i] * .01; } dates = account.get(i).split("[ ]+"); if (!perf_dates.contains(dates[0])) //first date entry { if (perf_dates.size() != 0) { perf_returns.add(pnl); n_trades_day.add(n_trades); } perf_dates.add(dates[0]); if (dreturns[i] != 0) { perf_datestimes.add(dates[0] + " " + dates[1]); perf_rets.add(dreturns[i]); } pnl = dreturns[i]; if (dreturns[i] != 0) { n_trades = 1; } else { n_trades = 0; } } else //already contains the date, so add on pnl { pnl = pnl + dreturns[i]; //System.out.println(dreturns[i]); if (dreturns[i] != 0) { n_trades++; perf_datestimes.add(dates[0] + " " + dates[1]); perf_rets.add(dreturns[i]); } // System.out.println(n_trades);} } if (dreturns[i] > 0) { n_pos_ret++; pos_ret_mean = pos_ret_mean + dreturns[i]; mean = mean + dreturns[i]; } else if (dreturns[i] < 0) { n_neg_ret++; neg_ret_mean = neg_ret_mean - dreturns[i]; mean = mean + dreturns[i]; } } perf_returns.add(pnl); n_trades_day.add(n_trades); for (i = 0; i < perf_dates.size(); i++) { if (printall) System.out.println(perf_dates.get(i) + " " + perf_returns.get(i) + " " + n_trades_day.get(i)); perform.println(perf_dates.get(i) + " " + perf_returns.get(i)); date_returns.add(perf_dates.get(i) + " " + perf_returns.get(i)); mean_ntrades = mean_ntrades + n_trades_day.get(i); } mean_ntrades = mean_ntrades / n_trades_day.size(); dates_price = new String[n_obs]; for (i = 1; i < n_obs; i++) { tokens = allsignal.get(allsignal.size() - n_obs + i).split("[ ]+"); if (perf_datestimes.contains(latestDates.get(latestDates.size() - n_obs + i))) { pret = perf_rets.get(perf_datestimes.indexOf(latestDates.get(latestDates.size() - n_obs + i))); System.out.println(latestDates.get(latestDates.size() - n_obs + i) + " " + pret); } else { pret = 0; } //System.out.println(tokens[0] + " " + latestDates.get(latestDates.size() - n_obs + i - 1)); // if(tokens[0].equals(latestDates.get(latestDates.size() - n_obs + i - 1))) // { dates_price[i] = new String(latestDates.get(latestDates.size() - n_obs + i) + " " + (mid.get(mid.size() - n_obs + i) - mid.get(mid.size() - n_obs + i - 1)) + " " + mid.get(mid.size() - n_obs + i) + " " + tokens[2] + " " + pret); // } } dates_price[0] = dates_price[1]; mean = mean / (n_pos_ret + n_neg_ret); System.out.println("ROI = " + account.get(account.size() - 1)); //--- compute stats--------------- double risk = neg_ret_mean / (double) n_neg_ret; System.out.println("neg_ret_mean = " + (-neg_ret_mean) + ", " + n_neg_ret); double reward = pos_ret_mean / (double) n_pos_ret; System.out.println("pos_ret_mean = " + pos_ret_mean + ", " + n_pos_ret); double win_ratio = (double) (n_pos_ret) / (n_pos_ret + n_neg_ret); kellyPerc = win_ratio - (1.0 - win_ratio) * (risk / reward); ulcer_index = ulcerIndex(dreturns); System.out.println("win ratio = " + win_ratio + ", risk = " + risk + ", reward = " + reward); System.out.println("kelly and ulcer = " + kellyPerc + " " + ulcer_index); for (i = 0; i < dreturns.length; i++) { sd = sd + (dreturns[i] - mean) * (dreturns[i] - mean) / ((double) dreturns.length); } standard_deviation = Math.sqrt(sd); sharpeRatio = Math.sqrt(250) * mean / standard_deviation; maxdraw = computeDrawdown(dreturns); rank_coeff = segmentRankCorrelation(30, dreturns); System.out.println("MeanRet = " + mean + ", Sharpe = " + sharpeRatio + ", MaxDD = " + maxdraw + ", Rank = " + rank_coeff + ", avg_n_trades = " + mean_ntrades); out.close(); dailyout.close(); perform.close(); b0_coeff.close(); } catch (ParseException fe) { System.out.println("ParseException"); } catch (NullPointerException fe) { System.out.println("Null pointer"); } catch (IllegalArgumentException fe) { System.out.println("IllegalArgument"); } catch (FileNotFoundException fe) { System.out.println("File not found..." + fe); } catch (IOException ioe) { System.out.println("IO procedure faulty..." + ioe); } n_files++; return computed; }