BarBuilder extends AnalyticBase { @SuppressWarnings(value = { "unchecked", "rawtypes" }) @Override public Span process(Span span) { this.inputSpan = (SpanImpl)span; DataSeriesImpl<DataPointImpl> outputSeries = (DataSeriesImpl)getOutputTimeSeries(BarBuilder.OUTPUT_KEY); DataSeriesImpl<DataPointImpl> inputSeries = (DataSeriesImpl)getInputTimeSeries(BarBuilder.INPUT_KEY); int inputStartIdx = inputSeries.indexOf(inputSpan.getDate(), false); int inputLastIdx = inputSeries.indexOf(inputSpan.getNextDate(), false); Period inputPeriod = inputSeries.getPeriod(); Period outputPeriod = outputSeries.getPeriod(); boolean barCompleted = false; if(inputPeriod.equals(outputPeriod)) { for(int i = inputStartIdx;i <= inputLastIdx;i++) { outputSeries.insertData(inputSeries.get(i)); } return new SpanImpl(inputSpan); }else{ if(inputPeriod.size() > 1) { throw new IllegalStateException( "Can't build bars from Type with an Interval that's not 1. Input=" + inputPeriod + ", output=" + outputPeriod); } if(subscription.getTimeFrame(0).getPeriod().equals(new Period(PeriodType.SECOND, 1))) { System.out.println("debug"); } boolean wasAddedDuringInit = false; if(currentMergeBar == null) { wasAddedDuringInit = true; currentMergeBar = new BarImpl((BarImpl)inputSeries.get(inputStartIdx)); workingTargetDate = subscription.getTradingWeek().getNextSessionDate(inputSpan.getDate(), outputPeriod); currentMergeBar.setDate(workingTargetDate); workingSpan = new SpanImpl(subscription.getTimeFrame(0).getPeriod(), inputSpan.getDate(), inputSpan.getNextDate()); this.workingSpan.setNextDate(workingTargetDate); System.out.println("IS INIT: ADDING NEW BAR " + currentMergeBar + " " + outputSeries.getPeriod() + " : span = " + this.workingSpan); outputSeries.add(currentMergeBar); }else{ workingSpan.setDate(workingTargetDate); } for(int i = wasAddedDuringInit ? inputStartIdx + 1 : inputStartIdx;i <= inputLastIdx;i++) { BarImpl currentIdxBar = (BarImpl)inputSeries.get(i); if(currentIdxBar.getDate().isAfter(workingTargetDate)) { workingTargetDate = subscription.getTradingWeek().getNextSessionDate(workingTargetDate, outputPeriod); currentMergeBar = new BarImpl(currentIdxBar); currentMergeBar.setDate(workingTargetDate); this.workingSpan.setNextDate(workingTargetDate); System.out.println("IS AFTER: ADDING NEW BAR " + currentMergeBar + " " + outputSeries.getPeriod() + " : span = " + this.workingSpan); outputSeries.add(currentMergeBar); barCompleted = true; }else{ currentMergeBar.merge(currentIdxBar, false); if(workingSpan.getPeriod().equals(new Period(PeriodType.SECOND, 1))) { System.out.println("\tNOT IS AFTER: MERGING NEW BAR " + currentMergeBar + " " + outputSeries.getPeriod() + " : span = " + this.workingSpan); } } } valueUpdated(); } return barCompleted ? workingSpan : null; } BarBuilder(SeriesSubscription s); SeriesSubscription getSubscription(); @SuppressWarnings(value = { "unchecked", "rawtypes" }) @Override Span process(Span span); static String[] getInputKeys(); static String[] getOutputKeys(); static final String NAME; static final String INPUT_KEY; static final String OUTPUT_KEY; }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testMinuteTo5Minute() { String symbol = "ESZ13"; Instrument instr = TestHarness.makeInstrument(symbol); DateTime dt2 = new DateTime(2013, 12, 10, 12, 0, 0); TimeFrameImpl tf2 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 5), dt2, null); SeriesSubscription sub2 = new SeriesSubscription( "ESZ13", instr, "IO", new TimeFrameImpl[] { tf2 }, TradingWeekImpl.DEFAULT); BarBuilder barBuilder = new BarBuilder(sub2); barBuilder.addInputTimeSeries(BarBuilder.INPUT_KEY, new DataSeriesImpl<BarImpl>(new Period(PeriodType.MINUTE, 1))); DataSeriesImpl<BarImpl> inputSeries = (DataSeriesImpl) barBuilder.getInputTimeSeries(BarBuilder.INPUT_KEY); assertNotNull(inputSeries); barBuilder.addOutputTimeSeries(BarBuilder.OUTPUT_KEY, new DataSeriesImpl<BarImpl>(new Period(PeriodType.MINUTE, 5))); DataSeriesImpl<BarImpl> outputSeries = (DataSeriesImpl) barBuilder.getOutputTimeSeries(BarBuilder.OUTPUT_KEY); assertNotNull(outputSeries); assertEquals(new Period(PeriodType.MINUTE, 5), outputSeries.getPeriod()); List<BarImpl> list = getBars(); SpanImpl span = new SpanImpl(new Period(PeriodType.MINUTE, 5), new DateTime(2013, 12, 10, 12, 0, 0), new DateTime(2013, 12, 10, 12, 30, 0)); for (int i = 0; i < list.size(); i++) { inputSeries.add(list.get(i)); } span.setNextDate(inputSeries.get(inputSeries.size() - 1).getDate()); barBuilder.process(span); assertEquals(6, outputSeries.size()); int mins = 5; for (int i = 0; i < outputSeries.size(); i++) { assertEquals(mins, outputSeries.get(i).getDate().getMinuteOfHour()); mins += 5; } list = getBars2(); for (BarImpl db : list) { inputSeries.add(db); span.setDate(db.getDate()); span.setNextDate(db.getDate()); barBuilder.process(span); } assertEquals(12, outputSeries.size()); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testSecondsToMinutes() { String symbol = "ESZ13"; Instrument instr = TestHarness.makeInstrument(symbol); DateTime dt2 = new DateTime(2013, 12, 10, 12, 0, 0); TimeFrameImpl tf2 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 1), dt2, null); SeriesSubscription sub2 = new SeriesSubscription( "ESZ13", instr, "IO", new TimeFrameImpl[] { tf2 }, TradingWeekImpl.DEFAULT); BarBuilder barBuilder = new BarBuilder(sub2); barBuilder.addInputTimeSeries(BarBuilder.INPUT_KEY, new DataSeriesImpl<BarImpl>(new Period(PeriodType.SECOND, 1))); DataSeriesImpl<BarImpl> inputSeries = (DataSeriesImpl) barBuilder.getInputTimeSeries(BarBuilder.INPUT_KEY); assertNotNull(inputSeries); barBuilder.addOutputTimeSeries(BarBuilder.OUTPUT_KEY, new DataSeriesImpl<BarImpl>(new Period(PeriodType.MINUTE, 1))); DataSeriesImpl<BarImpl> outputSeries = (DataSeriesImpl) barBuilder.getOutputTimeSeries(BarBuilder.OUTPUT_KEY); assertNotNull(outputSeries); assertEquals(new Period(PeriodType.MINUTE, 1), outputSeries.getPeriod()); List<BarImpl> list = getBars3(); SpanImpl span = new SpanImpl(new Period(PeriodType.SECOND, 1), new DateTime(2013, 12, 10, 12, 0, 0), new DateTime(2013, 12, 10, 12, 0, 0)); for (int i = 0; i < list.size(); i++) { inputSeries.add(list.get(i)); } span.setNextDate(inputSeries.get(inputSeries.size() - 1).getDate()); barBuilder.process(span); assertEquals(6, outputSeries.size()); list = getBars4(); for (BarImpl db : list) { inputSeries.add(db); span.setDate(db.getDate()); span.setNextDate(db.getDate()); barBuilder.process(span); } assertEquals(12, outputSeries.size()); System.out.println("output series size: " + outputSeries.size()); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testSecondsToHours() { String symbol = "ESZ13"; Instrument instr = TestHarness.makeInstrument(symbol); DateTime dt2 = new DateTime(2013, 12, 10, 0, 0, 0); TimeFrameImpl tf2 = new TimeFrameImpl(new Period(PeriodType.HOUR, 3), dt2, null); SeriesSubscription sub2 = new SeriesSubscription( "ESZ13", instr, "IO", new TimeFrameImpl[] { tf2 }, TradingWeekImpl.DEFAULT); BarBuilder barBuilder = new BarBuilder(sub2); barBuilder.addInputTimeSeries(BarBuilder.INPUT_KEY, new DataSeriesImpl<BarImpl>( new Period(PeriodType.SECOND, 1))); DataSeriesImpl<BarImpl> inputSeries = (DataSeriesImpl) barBuilder.getInputTimeSeries(BarBuilder.INPUT_KEY); assertNotNull(inputSeries); barBuilder.addOutputTimeSeries(BarBuilder.OUTPUT_KEY, new DataSeriesImpl<BarImpl>( new Period(PeriodType.HOUR, 3))); DataSeriesImpl<BarImpl> outputSeries = (DataSeriesImpl) barBuilder.getOutputTimeSeries(BarBuilder.OUTPUT_KEY); assertNotNull(outputSeries); assertEquals(new Period(PeriodType.HOUR, 3), outputSeries.getPeriod()); List<BarImpl> list = getBars5(); System.out.println("got " + list.size() + " bars"); SpanImpl span = new SpanImpl(new Period(PeriodType.SECOND, 1), new DateTime(2013, 12, 10, 0, 0, 0), new DateTime(2013, 12, 10, 0, 0, 0)); for (int i = 0; i < list.size(); i++) { inputSeries.add(list.get(i)); } System.out.println("added " + list.size() + " bars to inputSeries"); span.setNextDate(inputSeries.get(inputSeries.size() - 1).getDate()); System.out.println("setNextDate to: " + inputSeries.get(inputSeries.size() - 1).getDate() + " --> " + span); System.out.println("calling process..."); Span resultSpan = barBuilder.process(span); System.out.println("resultSpan = " + resultSpan); System.out.println("outputSeries size = " + outputSeries.size()); System.out.println("process finished..."); assertEquals(2, outputSeries.size()); list = getBars6(); for (BarImpl db : list) { inputSeries.add(db); span.setDate(db.getDate()); span.setNextDate(db.getDate()); barBuilder.process(span); } assertEquals(5, outputSeries.size()); for (int i = 0; i < outputSeries.size(); i++) { System.out.println(outputSeries.get(i)); } System.out.println("output series size: " + outputSeries.size()); }