SimulationThresholdEvents.java

As run with Δ(-1,1) observing the exchange BTCe.

Results
BTCe simulation (-1,1) againts mean-ind

 

package trader.simulation;

import java.util.ArrayList;
import java.util.List;
import org.jfree.data.xy.XYDataItem;
import org.jfree.data.xy.XYSeries;
import trader.analytics.Timer;
import trader.charts.DataCharting;
import trader.charts.components.SeriesShiftAveNormalised;
import trader.charts.data.TradeInterval;
import trader.charts.data.TradeSet;
import trader.models.Exchange;


public class SimulationThresholdEvents {


  public static void main(String[] args) throws Exception {
    oscillatingThresholds2("btce", "mean_ind", -1, 1);      // <-- instantiating with "mean_ind", btce is being compared against an aggregate

  }

  public static void oscillatingThresholds2(String exch1, String exch2,
      double low, double high) throws Exception {
    AveOscillator oscil = new AveOscillator(exch1, exch2, "2015-01-01", "2015-09-26", low, high); // <-- oscillator class defined below
    List xy_osc_points = oscil.getXYThresholds();

    String exch_name = Exchange.findExchange(exch1).name;

    int count = 0;
    double usd = 0;
    double cum_gain = 1;

    // set as 1 or above if images preceeding
    int photo_counter = 0;

    for (double[] xy : xy_osc_points) {
      int time = (int) xy[0];
      int trade_time = (int) xy[2];
      double gain = 0;
      String gain_str = "                ";
      if (count % 2 == 0)
        usd = xy[3];
      else {
        gain = (xy[3] / usd) * 0.998;     // <-- Note! a fee 0.2% is deducted on each trade
        // gain = xy[3] / usd;
        cum_gain *= gain;
        gain_str = String.format("%7.3f  %7.3f", gain, cum_gain);
      }

      String title_url = "";
      String title_chart = "";
      if (count % 2 == 0) {
        title_url = String.format("%6.2f (bid)", xy[3]);
        title_chart = String.format("Buy at %6.2f or less (%s Exchange)        ", xy[3], exch_name);
      } else {
        title_url = String.format("%6.2f (ask)", xy[3]);
        title_chart = String.format("Sell at %6.2f or more (%s Exchange)       ", xy[3], exch_name);
      }

      // generate a chart surrounding the event every 19 events (an odd number
      // is chosen to alternate between buy/sell positions
      if (count % 19 == 0) {
        System.out.println(String.format("%s %8.4f   %8.2f (%s)          %s %s    %s",
            new Timer(time).dateToSecond(), xy[1],
            xy[3], new Timer(trade_time).dateToSecond().substring(11),
            title_url,
            "chart",
            gain_str));
        DataCharting chart = new DataCharting(time, time+2400);
        chart.timestep(exch1, 0);
        // chart.timestep("btce", 0);
        chart.setTitle(title_chart);
        chart.setFiletoken(exch1);
        chart.vol(120);
        chart.save();
      } else {
        System.out.println(String.format("%s %8.4f   %8.2f (%s)          %s          %s",
            new Timer(time).dateToSecond(), xy[1],
            xy[3], new Timer(trade_time).dateToSecond().substring(11), title_url, gain_str));
      }


      count++;
    }

  }


  public static class AveOscillator {

    // produce mov-ave series one at a time across day-intervals
    private int step = 86400;

    // mov-ave paramters
    private int bar = 10;
    private int dt = 1;

    private double high_thres;
    private double low_thres;

    private String exch1;
    private String exch2;
    private String start_str;
    private String end_str;


    public AveOscillator(String exch1, String exch2,
        String start_str, String end_str, double low_thres, double high_thres) {
      this.exch1 = exch1;
      this.exch2 = exch2;
      this.start_str = start_str;
      this.end_str = end_str;
      this.low_thres = low_thres;
      this.high_thres = high_thres;
    }


    public List getXYThresholds() {

      List xy_osc_points = new ArrayList();
      boolean high = false;

      for (Timer interval : new Timer(start_str, end_str, step)) {
        System.out.println("processing: "+interval.dayToMinute());

        SeriesShiftAveNormalised shift = new SeriesShiftAveNormalised();    // <-- The class encapsulating the expression of Δ(-1,1)
        shift.add(exch1);
        shift.add(exch2);
        shift.bar(bar);
        shift.dt(dt);
        shift.interval(interval.intLow(), interval.intHigh());
        XYSeries xy_series = shift.series();

        TradeSet trade_set = new TradeSet(exch1, interval.intLow(), interval.intHigh());

        List xy_items = xy_series.getItems();
        for (XYDataItem xy : xy_items) {
          if (high && xy.getYValue() > high_thres) {

            int trade_timestamp = 0;
            double trade_usd = 0;
            for (TradeInterval t : trade_set.dt(1)) {
              if (t.x0() > xy.getXValue() && t.usd() > 0) {
                trade_timestamp = t.x0true();
                trade_usd = t.usd();
                break;
              }
            }
            xy_osc_points.add(new double[]{xy.getXValue()+interval.intLow(),
                xy.getYValue(), trade_timestamp, trade_usd});

            high = false;
          } else if (!high && xy.getYValue() < low_thres) {
            int trade_timestamp = 0;
            double trade_usd = 0;

            for (TradeInterval t : trade_set.dt(1)) {
              if (t.x0() > xy.getXValue() && t.usd() > 0) {
                trade_timestamp = t.x0true();
                trade_usd = t.usd();
                break;
              }
            }

            xy_osc_points.add(new double[]{xy.getXValue()+interval.intLow(),
                xy.getYValue(), trade_timestamp, trade_usd});
            high = true;
          }
        }
      }
      return xy_osc_points;
    }

  }



}