Ejemplo n.º 1
0
  private void generateOutChart(
      String chartFileName,
      SortedMap<Date, double[]> calcOutput,
      Quotations quotations,
      SortedMap<Date, Double> buySerie,
      SortedMap<Date, Double> sellSerie)
      throws NotEnoughDataException, IOException, FileNotFoundException {

    ChartGenerator chartGenerator =
        new ChartGenerator("Premium Markets output V. Lag fixed SMA 100");

    // line series
    SortedMap<DataSetBarDescr, SortedMap<Date, Double>> lineSeries =
        new TreeMap<DataSetBarDescr, SortedMap<Date, Double>>();
    SortedMap<Date, Double> quotes =
        QuotationsFactories.getFactory()
            .buildSMapFromQuotationsClose(
                quotations, quotations.getFirstDateShiftedIdx(), quotations.getLastDateIdx());
    lineSeries.put(new DataSetBarDescr(2, "historical prices", Color.BLUE.darker()), quotes);

    SortedMap<Date, Double> fixedSma100;
    try {
      TalibSmaSmoother smaSmoother = new TalibSmaSmoother(100);
      fixedSma100 = smaSmoother.sSmooth(quotes, true);
      lineSeries.put(
          new DataSetBarDescr(1, "Artificial no lag sma 100", Color.ORANGE, 3f), fixedSma100);
    } catch (NegativeArraySizeException e) {
      throw new NotEnoughDataException(
          quotations.getStock(),
          "Generate chart : No enough data to calculate sma for " + quotations.getStock(),
          e);
    }

    SortedMap<Date, Double> sCalcOutput = sCalcOutput(calcOutput);
    lineSeries.put(
        new DataSetBarDescr(0, "Premium Markets trend line", new Color(46, 236, 236), 3f),
        sCalcOutput);

    // bar series
    SortedMap<DataSetBarDescr, SortedMap<Date, Double>> barSeries =
        new TreeMap<DataSetBarDescr, SortedMap<Date, Double>>();

    /// sma b&s
    SDiscretDerivator derivator = new SlopeDerivator(1, 0, true, true);
    SortedMap<Date, Double> sma100Drv = derivator.sDerivateDiscret(fixedSma100);

    SortedMap<Date, Double> fixedSmaBearish = new TreeMap<Date, Double>();
    SortedMap<Date, Double> fixedSmaBullish = new TreeMap<Date, Double>();
    for (Date date : sma100Drv.keySet()) {
      Double fixedSmaAtDate = sma100Drv.get(date);
      if (fixedSmaAtDate == 0) {
        fixedSmaBullish.put(date, quotes.get(date) * 1.25);
      } else if (fixedSmaAtDate == 1) {
        fixedSmaBearish.put(date, quotes.get(date) * 1.25);
      }
    }
    barSeries.put(
        new DataSetBarDescr(4, "Artificial no lag bearish sma", new Color(246, 173, 173)),
        fixedSmaBearish);
    barSeries.put(
        new DataSetBarDescr(3, "Artificial no lag bullish sma", new Color(189, 249, 189)),
        fixedSmaBullish);

    // Prediction period
    Date predStart = maxLastKey(fixedSmaBearish, fixedSmaBullish);
    Date predEnd = maxLastKey(sellSerie, buySerie);
    SortedMap<Date, Double> pred = new TreeMap<Date, Double>();
    SortedMap<Date, Double> predSubMap = quotes.subMap(predStart, predEnd);

    ApacheStats apacheStatsQ = new ApacheStats(new Max());
    double maxQ = apacheStatsQ.sEvaluate(quotes.values());
    ApacheStats apacheStatsO = new ApacheStats(new Max());
    double maxO = apacheStatsO.evaluate(calcOutput.values());
    double fact = Math.max(maxQ, maxO);
    for (Date date : predSubMap.keySet()) {
      pred.put(date, fact * 1.25);
    }
    barSeries.put(new DataSetBarDescr(0, "Prediction zone", new Color(128, 128, 128, 100)), pred);

    /// Output b&s
    barSeries.put(new DataSetBarDescr(1, "Bearish prediction", Color.RED), sellSerie);
    barSeries.put(new DataSetBarDescr(2, "Bullish prediction", Color.GREEN), buySerie);

    // chart
    chartGenerator.generateChartPNGFor(
        new FileOutputStream(
            new File(System.getProperty("installdir") + File.separator + chartFileName)),
        false,
        lineSeries,
        barSeries);
  }