Index: ta4j-core/src/test/java/eu/damir/AnotherMockTimeSeries.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- ta4j-core/src/test/java/eu/damir/AnotherMockTimeSeries.java (revision ) +++ ta4j-core/src/test/java/eu/damir/AnotherMockTimeSeries.java (revision ) @@ -0,0 +1,95 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2014-2017 Marc de Verdelhan & respective authors (see AUTHORS) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package eu.damir; + +import org.ta4j.core.BaseTimeSeries; +import org.ta4j.core.Tick; +import org.ta4j.core.mocks.MockTick; + +import java.time.ZonedDateTime; +import java.time.temporal.ChronoField; +import java.util.ArrayList; +import java.util.List; + +/** + * A time series with sample data. + */ +public class AnotherMockTimeSeries extends BaseTimeSeries { + + public AnotherMockTimeSeries(double... data) { + super(doublesToTicks(data)); + } + + public AnotherMockTimeSeries(List ticks) { + super(ticks); + } + + public AnotherMockTimeSeries(double[] data, ZonedDateTime[] times) { + super(doublesAndTimesToTicks(data, times)); + } + + public AnotherMockTimeSeries(ZonedDateTime... dates) { + super(timesToTicks(dates)); + } + + public AnotherMockTimeSeries() { + super(arbitraryTicks()); + } + + private static List doublesToTicks(double... data) { + ArrayList ticks = new ArrayList<>(); + for (int i = 0; i < data.length; i++) { + ticks.add(new MockTick(data[i])); + //ticks.add(new MockTick(ZonedDateTime.now().with(ChronoField.MILLI_OF_SECOND, i), data[i])); + } + return ticks; + } + + private static List doublesAndTimesToTicks(double[] data, ZonedDateTime[] times) { + if (data.length != times.length) { + throw new IllegalArgumentException(); + } + ArrayList ticks = new ArrayList<>(); + for (int i = 0; i < data.length; i++) { + ticks.add(new MockTick(times[i], data[i])); + } + return ticks; + } + + private static List timesToTicks(ZonedDateTime... dates) { + ArrayList ticks = new ArrayList<>(); + int i = 1; + for (ZonedDateTime date : dates) { + ticks.add(new MockTick(date, i++)); + } + return ticks; + } + + private static List arbitraryTicks() { + ArrayList ticks = new ArrayList<>(); + for (double i = 0d; i < 5000; i++) { + ticks.add(new MockTick(ZonedDateTime.now(), i, i + 1, i + 2, i + 3, i + 4, i + 5, (int) (i + 6))); + } + return ticks; + } +} Index: ta4j-core/src/test/java/eu/damir/SMAPerformanceTest.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- ta4j-core/src/test/java/eu/damir/SMAPerformanceTest.java (revision ) +++ ta4j-core/src/test/java/eu/damir/SMAPerformanceTest.java (revision ) @@ -0,0 +1,77 @@ +package eu.damir; + +import org.junit.Test; +import org.ta4j.core.Decimal; +import org.ta4j.core.TimeSeries; +import org.ta4j.core.indicators.SMAIndicator; +import org.ta4j.core.indicators.helpers.ClosePriceIndicator; + +public class SMAPerformanceTest { + + + //on mmy macbook pro mid 2014: + //average = 5 + //Time elapsed: 22072 + @Test + public void testIfQuickEnoughOneIteration() { + long start = System.currentTimeMillis(); + int initialCapacity = 54 * 5 * 24 * 60 * 3; + + double[] input = new double[initialCapacity]; + for (int i = 0; i < input.length; i++) { + input[i] = 5d; + } + + // just adding close price instead timestamp with close price: + // AnotherMockTimeSeries: ticks.add(new MockTick(data[i])); + //MockTimeSeries: ticks.add(new MockTick(ZonedDateTime.now().with(ChronoField.MILLI_OF_SECOND, i), data[i])); + + TimeSeries timeSeries = new AnotherMockTimeSeries(input); + + Decimal average = null; + for (int h = 2; h < 3; h++) { + SMAIndicator quoteSMA = new SMAIndicator(new ClosePriceIndicator(timeSeries), h); + for (int i = 0; i < timeSeries.getTickCount(); i++) { + average = quoteSMA.getValue(i); + } + } + long end = System.currentTimeMillis(); + System.out.println("average = " + average); + + System.out.println("Time elapsed: " + (end - start)); + } + + + //on mmy macbook pro mid 2014: + //average = ... + //Time elapsed: infinity... ~50 min + @Test + public void testIfQuickEnoughFullIteration() { + long start = System.currentTimeMillis(); + int initialCapacity = 54 * 5 * 24 * 60 * 3; + + double[] input = new double[initialCapacity]; + for (int i = 0; i < input.length; i++) { + input[i] = 5d; + } + + // just adding close price instead timestamp with close price: + // AnotherMockTimeSeries: ticks.add(new MockTick(data[i])); + //MockTimeSeries: ticks.add(new MockTick(ZonedDateTime.now().with(ChronoField.MILLI_OF_SECOND, i), data[i])); + + + TimeSeries timeSeries = new AnotherMockTimeSeries(input); + + Decimal average = null; + for (int h = 2; h < 201; h++) { + SMAIndicator quoteSMA = new SMAIndicator(new ClosePriceIndicator(timeSeries), h); + for (int i = 0; i < timeSeries.getTickCount(); i++) { + average = quoteSMA.getValue(i); + } + } + long end = System.currentTimeMillis(); + System.out.println("average = " + average); + + System.out.println("Time elapsed: " + (end - start)); + } +} Index: ta4j-core/pom.xml IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- ta4j-core/pom.xml (revision 121468ca1db0124017b1669c528709eea2a69234) +++ ta4j-core/pom.xml (revision ) @@ -18,6 +18,11 @@ slf4j-api 1.7.25 + + org.slf4j + slf4j-log4j12 + 1.7.25 + @@ -32,5 +37,6 @@ 3.6.1 test +